Переглянути джерело

app: make exit app work on android - must stop the service

darkfi 2 тижнів тому
батько
коміт
d12fbc06b1

+ 18 - 0
bin/app/java/MainActivity.java

@@ -7,6 +7,8 @@ import android.view.inputmethod.EditorInfo;
 import android.text.InputType;
 import android.content.Intent;
 import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
 import android.util.Log;
 import java.io.File;
 import java.io.FileWriter;
@@ -179,6 +181,22 @@ public void openUrl(String url) {
     }
 }
 
+public void exitApp() {
+    Log.d("darkfi", "exitApp: stopping service and finishing");
+    Intent serviceIntent = new Intent(this, ForegroundService.class);
+    stopService(serviceIntent);
+    finishAndRemoveTask();
+    // Let the finish animation and window removal complete before the
+    // process dies, else WindowManager throws DeadObjectException
+    // dispatching visibility to the dead window.
+    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
+        @Override
+        public void run() {
+            System.exit(0);
+        }
+    }, 750);
+}
+
 //% END
 
 //% MAIN_ACTIVITY_ON_CREATE

+ 10 - 0
bin/app/src/android/mod.rs

@@ -106,3 +106,13 @@ pub fn open_url(url: &str) {
         delete_local_ref(env, jurl);
     }
 }
+
+/// Fully exit the app by calling the `exitApp` method on MainActivity:
+/// stops the ForegroundService, closes the activity, and kills the
+/// process. Android only.
+pub fn exit_app() {
+    unsafe {
+        let env = get_jni_env();
+        ndk_utils::call_void_method!(env, android::ACTIVITY, "exitApp", "()V");
+    }
+}

+ 3 - 0
bin/app/src/app/schema/mod.rs

@@ -571,6 +571,9 @@ pub async fn make(
     let listen_click = ex.spawn(async move {
         while recvr.recv().await.is_ok() {
             i!("clicked exit");
+            #[cfg(target_os = "android")]
+            crate::android::exit_app();
+            #[cfg(not(target_os = "android"))]
             miniquad::window::request_quit();
         }
     });