@@ -94,7 +94,7 @@ protected void onCreate(Bundle savedInstanceState) {
94
94
Log .v ("Python" , "Device: " + android .os .Build .DEVICE );
95
95
Log .v ("Python" , "Model: " + android .os .Build .MODEL );
96
96
super .onCreate (savedInstanceState );
97
-
97
+
98
98
PythonActivity .initialize ();
99
99
100
100
// Load shared libraries
@@ -161,7 +161,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
161
161
PythonActivity .nativeSetEnv ("ANDROID_ENTRYPOINT" , "main.pyo" );
162
162
PythonActivity .nativeSetEnv ("PYTHONHOME" , mFilesDirectory );
163
163
PythonActivity .nativeSetEnv ("PYTHONPATH" , mFilesDirectory + ":" + mFilesDirectory + "/lib" );
164
-
164
+
165
165
try {
166
166
Log .v (TAG , "Access to our meta-data..." );
167
167
this .mMetaData = this .mActivity .getPackageManager ().getApplicationInfo (
@@ -173,16 +173,24 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
173
173
}
174
174
} catch (PackageManager .NameNotFoundException e ) {
175
175
}
176
-
176
+
177
177
final Thread pythonThread = new Thread (new PythonMain (), "PythonThread" );
178
178
PythonActivity .mPythonThread = pythonThread ;
179
179
pythonThread .start ();
180
180
181
181
final Thread wvThread = new Thread (new WebViewLoaderMain (), "WvThread" );
182
182
wvThread .start ();
183
+ }
183
184
185
+ @ Override
186
+ public void onDestroy () {
187
+ Log .i ("Destroy" , "end of app" );
188
+ super .onDestroy ();
189
+
190
+ // make sure all child threads (python_thread) are stopped
191
+ android .os .Process .killProcess (android .os .Process .myPid ());
184
192
}
185
-
193
+
186
194
public void loadLibraries () {
187
195
PythonUtil .loadLibraries (getFilesDir ());
188
196
}
@@ -276,10 +284,48 @@ public void unpackData(final String resource, File target) {
276
284
}
277
285
}
278
286
287
+ public static void loadUrl (String url ) {
288
+ class LoadUrl implements Runnable {
289
+ private String mUrl ;
290
+
291
+ public LoadUrl (String url ) {
292
+ mUrl = url ;
293
+ }
294
+
295
+ public void run () {
296
+ mWebView .loadUrl (mUrl );
297
+ }
298
+ }
299
+
300
+ Log .i (TAG , "Opening URL: " + url );
301
+ mActivity .runOnUiThread (new LoadUrl (url ));
302
+ }
303
+
279
304
public static ViewGroup getLayout () {
280
305
return mLayout ;
281
306
}
282
307
308
+ long lastBackClick = SystemClock .elapsedRealtime ();
309
+ @ Override
310
+ public boolean onKeyDown (int keyCode , KeyEvent event ) {
311
+ // Check if the key event was the Back button and if there's history
312
+ if ((keyCode == KeyEvent .KEYCODE_BACK ) && mWebView .canGoBack ()) {
313
+ mWebView .goBack ();
314
+ return true ;
315
+ }
316
+ // If it wasn't the Back key or there's no web page history, bubble up to the default
317
+ // system behavior (probably exit the activity)
318
+ if (SystemClock .elapsedRealtime () - lastBackClick > 2000 ){
319
+ lastBackClick = SystemClock .elapsedRealtime ();
320
+ Toast .makeText (this , "Click again to close the app" ,
321
+ Toast .LENGTH_LONG ).show ();
322
+ return true ;
323
+ }
324
+
325
+ lastBackClick = SystemClock .elapsedRealtime ();
326
+ return super .onKeyDown (keyCode , event );
327
+ }
328
+
283
329
284
330
//----------------------------------------------------------------------------
285
331
// Listener interface for onNewIntent
@@ -350,7 +396,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
350
396
}
351
397
}
352
398
353
- public static void start_service (String serviceTitle , String serviceDescription ,
399
+ public static void start_service (String serviceTitle , String serviceDescription ,
354
400
String pythonServiceArgument ) {
355
401
Intent serviceIntent = new Intent (PythonActivity .mActivity , PythonService .class );
356
402
String argument = PythonActivity .mActivity .getFilesDir ().getAbsolutePath ();
0 commit comments