Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webview improvements #750

Merged
merged 2 commits into from
Jun 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void onCreate(Bundle savedInstanceState) {
Log.v("Python", "Device: " + android.os.Build.DEVICE);
Log.v("Python", "Model: " + android.os.Build.MODEL);
super.onCreate(savedInstanceState);

PythonActivity.initialize();

// Load shared libraries
Expand Down Expand Up @@ -161,7 +161,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
PythonActivity.nativeSetEnv("PYTHONHOME", mFilesDirectory);
PythonActivity.nativeSetEnv("PYTHONPATH", mFilesDirectory + ":" + mFilesDirectory + "/lib");

try {
Log.v(TAG, "Access to our meta-data...");
this.mMetaData = this.mActivity.getPackageManager().getApplicationInfo(
Expand All @@ -173,16 +173,24 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
} catch (PackageManager.NameNotFoundException e) {
}

final Thread pythonThread = new Thread(new PythonMain(), "PythonThread");
PythonActivity.mPythonThread = pythonThread;
pythonThread.start();

final Thread wvThread = new Thread(new WebViewLoaderMain(), "WvThread");
wvThread.start();
}

@Override
public void onDestroy() {
Log.i("Destroy", "end of app");
super.onDestroy();

// make sure all child threads (python_thread) are stopped
android.os.Process.killProcess(android.os.Process.myPid());
}

public void loadLibraries() {
PythonUtil.loadLibraries(getFilesDir());
}
Expand Down Expand Up @@ -276,10 +284,48 @@ public void unpackData(final String resource, File target) {
}
}

public static void loadUrl(String url) {
class LoadUrl implements Runnable {
private String mUrl;

public LoadUrl(String url) {
mUrl = url;
}

public void run() {
mWebView.loadUrl(mUrl);
}
}

Log.i(TAG, "Opening URL: " + url);
mActivity.runOnUiThread(new LoadUrl(url));
}

public static ViewGroup getLayout() {
return mLayout;
}

long lastBackClick = SystemClock.elapsedRealtime();
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
lastBackClick = SystemClock.elapsedRealtime();
Toast.makeText(this, "Click again to close the app",
Toast.LENGTH_LONG).show();
return true;
}

lastBackClick = SystemClock.elapsedRealtime();
return super.onKeyDown(keyCode, event);
}


//----------------------------------------------------------------------------
// Listener interface for onNewIntent
Expand Down Expand Up @@ -350,7 +396,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
}

public static void start_service(String serviceTitle, String serviceDescription,
public static void start_service(String serviceTitle, String serviceDescription,
String pythonServiceArgument) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@ public class WebViewLoader {
private static final String TAG = "WebViewLoader";

public static void testConnection() {

while (true) {
if (WebViewLoader.pingHost("localhost", {{ args.port }}, 100)) {
Log.v(TAG, "Successfully pinged localhost:{{ args.port }}");
Handler mainHandler = new Handler(PythonActivity.mActivity.getMainLooper());

Runnable myRunnable = new Runnable() {
@Override
public void run() {
PythonActivity.mActivity.mWebView.loadUrl("http://127.0.0.1:{{ args.port }}/");
PythonActivity.mActivity.loadUrl("http://127.0.0.1:{{ args.port }}/");
Log.v(TAG, "Loaded webserver in webview");
}
};
mainHandler.post(myRunnable);
break;

} else {
Log.v(TAG, "Could not ping localhost:{{ args.port }}");
try {
Expand All @@ -55,5 +54,3 @@ public static boolean pingHost(String host, int port, int timeout) {
}
}
}


10 changes: 9 additions & 1 deletion testapps/testapp_flask/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def vibrate():
vibrator.vibrate(float(args['time']) * 1000)
print('vibrated')

@app.route('/loadUrl')
def loadUrl():
args = request.args
if 'url' not in args:
print('ERROR: asked to open an url but without url argument')
print('asked to open url', args['url'])
activity.loadUrl(args['url'])

@app.route('/orientation')
def orientation():
args = request.args
Expand All @@ -69,7 +77,7 @@ def orientation():
direction = args['dir']
if direction not in ('horizontal', 'vertical'):
print('ERROR: asked to orient to neither horizontal nor vertical')

if direction == 'horizontal':
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Expand Down
14 changes: 14 additions & 0 deletions testapps/testapp_flask/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ <h2>Page one</h2>
}
</script>

<button onClick="loadUrl()">
open url
</button>
<input type="text" value="http://www.google.com" id="url_field"/>

<script>
function loadUrl() {
var request = new XMLHttpRequest();
var url = document.getElementById("url_field").value
request.open('GET', 'loadUrl?url=' + url, true);
request.send();
}
</script>

<button onClick="vertical()">
vertical orientation
</button>
Expand Down