From 80a04596dfa1da893a78e19fabfafd9f475e0be7 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Tue, 1 Aug 2023 01:03:51 +0300 Subject: [PATCH] android: update activity lib, optimize app exit --- Cargo.lock | 5 +- Cargo.toml | 1 + .../mw/gri/android/BackgroundService.java | 20 ++++++-- .../java/mw/gri/android/MainActivity.java | 48 +++++++------------ src/gui/app.rs | 7 ++- src/gui/platform/android/mod.rs | 11 ----- src/gui/platform/desktop/mod.rs | 2 - src/gui/platform/mod.rs | 1 - src/gui/views/root.rs | 30 ++++-------- 9 files changed, 51 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ace9415..162ffd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,9 +188,9 @@ dependencies = [ [[package]] name = "android-activity" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40bc1575e653f158cbdc6ebcd917b9564e66321c5325c232c3591269c257be69" +checksum = "64529721f27c2314ced0890ce45e469574a73e5e6fdd6e9da1860eb29285f5e0" dependencies = [ "android-properties", "bitflags 1.3.2", @@ -2221,6 +2221,7 @@ dependencies = [ name = "grim" version = "0.1.0" dependencies = [ + "android-activity", "android_logger", "built", "byteorder", diff --git a/Cargo.toml b/Cargo.toml index bbf1e06..5688613 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,5 +76,6 @@ eframe = { version = "0.22.0", features = [ "wgpu" ] } [target.'cfg(target_os = "android")'.dependencies] android_logger = "0.13.1" jni = "0.21.1" +android-activity = "0.4.3" winit = { version = "0.28", features = [ "android-game-activity" ] } eframe = { version = "0.22.0", features = [ "wgpu", "android-game-activity" ] } \ No newline at end of file diff --git a/app/src/main/java/mw/gri/android/BackgroundService.java b/app/src/main/java/mw/gri/android/BackgroundService.java index ddcafbc..99f3892 100644 --- a/app/src/main/java/mw/gri/android/BackgroundService.java +++ b/app/src/main/java/mw/gri/android/BackgroundService.java @@ -31,11 +31,13 @@ public class BackgroundService extends Service { mNotificationBuilder.setContentText(getSyncStatusText()); NotificationManager manager = getSystemService(NotificationManager.class); manager.notify(SYNC_STATUS_NOTIFICATION_ID, mNotificationBuilder.build()); - // Send broadcast to MainActivity if app exit is needed after node stop. + + // Send broadcast to MainActivity if exit from the app is needed after node stop. if (exitAppAfterNodeStop()) { - sendBroadcast(new Intent(MainActivity.FINISH_ACTIVITY_ACTION)); + sendBroadcast(new Intent(MainActivity.STOP_APP_ACTION)); mStopped = true; } + // Repeat notification update if service is not stopped. if (!mStopped) { mHandler.postDelayed(this, 500); @@ -45,10 +47,15 @@ public class BackgroundService extends Service { @Override public void onCreate() { + if (mStopped) { + return; + } + // Prevent CPU to sleep at background. PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); mWakeLock.acquire(); + // Create channel to show notifications. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel( @@ -58,6 +65,7 @@ public class BackgroundService extends Service { NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(notificationChannel); } + // Show notification with sync status. Intent i = getPackageManager().getLaunchIntentForPackage(this.getPackageName()); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_IMMUTABLE); @@ -67,8 +75,10 @@ public class BackgroundService extends Service { .setSmallIcon(R.drawable.ic_stat_name) .setContentIntent(pendingIntent); Notification notification = mNotificationBuilder.build(); + // Start service at foreground state to prevent killing by system. startForeground(SYNC_STATUS_NOTIFICATION_ID, notification); + // Update sync status at notification. mHandler.post(mUpdateSyncStatus); } @@ -98,16 +108,20 @@ public class BackgroundService extends Service { public void onStop() { mStopped = true; + // Stop updating the notification. mHandler.removeCallbacks(mUpdateSyncStatus); + // Remove service from foreground state. stopForeground(Service.STOP_FOREGROUND_REMOVE); + // Remove notification. NotificationManager notificationManager = getSystemService(NotificationManager.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationManager.deleteNotificationChannel(TAG); } notificationManager.cancel(SYNC_STATUS_NOTIFICATION_ID); + // Release wake lock to allow CPU to sleep at background. if (mWakeLock.isHeld()) { mWakeLock.release(); @@ -149,6 +163,6 @@ public class BackgroundService extends Service { private native String getSyncStatusText(); // Get sync title text for notification from native code. private native String getSyncTitle(); - // Check if exit app is needed after node stop from native code. + // Check if app from the app is needed after node stop from native code. private native boolean exitAppAfterNodeStop(); } diff --git a/app/src/main/java/mw/gri/android/MainActivity.java b/app/src/main/java/mw/gri/android/MainActivity.java index 4723ee5..56b67b3 100644 --- a/app/src/main/java/mw/gri/android/MainActivity.java +++ b/app/src/main/java/mw/gri/android/MainActivity.java @@ -5,7 +5,6 @@ import android.os.Bundle; import android.os.Process; import android.system.ErrnoException; import android.system.Os; -import android.util.Log; import android.view.KeyEvent; import android.view.View; import androidx.core.graphics.Insets; @@ -14,14 +13,11 @@ import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.google.androidgamesdk.GameActivity; -import java.util.concurrent.atomic.AtomicBoolean; - import static android.content.ClipDescription.MIMETYPE_TEXT_HTML; import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN; public class MainActivity extends GameActivity { - - public static String FINISH_ACTIVITY_ACTION = "MainActivity.finish"; + public static String STOP_APP_ACTION = "STOP_APP"; static { System.loadLibrary("grim"); @@ -30,9 +26,9 @@ public class MainActivity extends GameActivity { private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context ctx, Intent i) { - if (i.getAction().equals(FINISH_ACTIVITY_ACTION)) { - unregisterReceiver(this); + if (i.getAction().equals(STOP_APP_ACTION)) { onExit(); + Process.killProcess(Process.myPid()); } } }; @@ -48,7 +44,7 @@ public class MainActivity extends GameActivity { super.onCreate(null); // Register receiver to finish activity from the BackgroundService. - registerReceiver(mBroadcastReceiver, new IntentFilter(FINISH_ACTIVITY_ACTION)); + registerReceiver(mBroadcastReceiver, new IntentFilter(STOP_APP_ACTION)); // Start notification service. BackgroundService.start(this); @@ -99,43 +95,33 @@ public class MainActivity extends GameActivity { // Implemented into native code to handle key code BACK event. public native void onBack(); - private boolean mManualExit; - private final AtomicBoolean mActivityDestroyed = new AtomicBoolean(false); + // Actions on app exit. + private void onExit() { + unregisterReceiver(mBroadcastReceiver); + BackgroundService.stop(this); + } @Override protected void onDestroy() { - if (!mManualExit) { - unregisterReceiver(mBroadcastReceiver); - onTermination(); - } + onExit(); - // Temp fix: kill process after 3 seconds to prevent app hanging at next launch. + // Kill process after 3 seconds if app was terminated from recent apps to prevent app hanging. new Thread(() -> { try { + onTermination(); Thread.sleep(3000); - if (!mActivityDestroyed.get()) { - Process.killProcess(Process.myPid()); - } + Process.killProcess(Process.myPid()); } catch (InterruptedException e) { throw new RuntimeException(e); } }).start(); + + // Destroy an app and kill process. super.onDestroy(); - mActivityDestroyed.set(true); + Process.killProcess(Process.myPid()); } - // Called from native code. - public void onExit() { - // Return if exit was already requested. - if (mManualExit) { - return; - } - mManualExit = true; - BackgroundService.stop(this); - finish(); - } - - // Notify native code to stop activity (e.g. node) on app destroy. + // Notify native code to stop activity (e.g. node) if app was terminated from recent apps. public native void onTermination(); // Called from native code to set text into clipboard. diff --git a/src/gui/app.rs b/src/gui/app.rs index 77157fe..36f759c 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -65,8 +65,11 @@ impl eframe::App for PlatformApp { } fn on_close_event(&mut self) -> bool { - Root::show_exit_modal(); - self.root.exit_allowed + let exit = self.root.exit_allowed; + if !exit { + Root::show_exit_modal(); + } + exit } } diff --git a/src/gui/platform/android/mod.rs b/src/gui/platform/android/mod.rs index da1b479..0a8eaae 100644 --- a/src/gui/platform/android/mod.rs +++ b/src/gui/platform/android/mod.rs @@ -74,15 +74,4 @@ impl PlatformCallbacks for Android { }; paste_data } - - fn exit(&self) { - use jni::objects::{JObject}; - - let vm = unsafe { jni::JavaVM::from_raw(self.android_app.vm_as_ptr() as _) }.unwrap(); - let mut env = vm.attach_current_thread().unwrap(); - let activity = unsafe { - JObject::from_raw(self.android_app.activity_as_ptr() as jni::sys::jobject) - }; - env.call_method(activity, "onExit", "()V", &[]).unwrap(); - } } \ No newline at end of file diff --git a/src/gui/platform/desktop/mod.rs b/src/gui/platform/desktop/mod.rs index 468ac93..553255f 100644 --- a/src/gui/platform/desktop/mod.rs +++ b/src/gui/platform/desktop/mod.rs @@ -27,6 +27,4 @@ impl PlatformCallbacks for Desktop { fn get_string_from_buffer(&self) -> String { "".to_string() } - - fn exit(&self) {} } diff --git a/src/gui/platform/mod.rs b/src/gui/platform/mod.rs index f5d22df..54b39f0 100644 --- a/src/gui/platform/mod.rs +++ b/src/gui/platform/mod.rs @@ -26,5 +26,4 @@ pub trait PlatformCallbacks { fn hide_keyboard(&self); fn copy_string_to_buffer(&self, data: String); fn get_string_from_buffer(&self) -> String; - fn exit(&self); } \ No newline at end of file diff --git a/src/gui/views/root.rs b/src/gui/views/root.rs index c0d722c..9bcdbd1 100644 --- a/src/gui/views/root.rs +++ b/src/gui/views/root.rs @@ -78,7 +78,7 @@ impl Root { pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) { // Show opened exit confirmation modal content. if self.can_draw_modal() { - self.exit_modal_content(ui, frame, cb); + self.exit_modal_content(ui, frame); } let (is_panel_open, panel_width) = Self::network_panel_state_width(frame); @@ -148,14 +148,11 @@ impl Root { } /// Draw exit confirmation modal content. - fn exit_modal_content(&mut self, - ui: &mut egui::Ui, - frame: &mut eframe::Frame, - cb: &dyn PlatformCallbacks) { + fn exit_modal_content(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { Modal::ui(ui, |ui, modal| { if self.show_exit_progress { if !Node::is_running() { - self.exit(frame, cb); + self.exit(frame); modal.close(); } ui.add_space(16.0); @@ -185,7 +182,7 @@ impl Root { columns[0].vertical_centered_justified(|ui| { View::button(ui, t!("modal_exit.exit"), Colors::WHITE, || { if !Node::is_running() { - self.exit(frame, cb); + self.exit(frame); modal.close(); } else { Node::stop(true); @@ -206,21 +203,10 @@ impl Root { }); } - /// Platform-specific exit from the application. - fn exit(&mut self, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) { - match OperatingSystem::from_target_os() { - OperatingSystem::Android => { - cb.exit(); - } - OperatingSystem::IOS => { - //TODO: exit on iOS. - } - OperatingSystem::Nix | OperatingSystem::Mac | OperatingSystem::Windows => { - self.exit_allowed = true; - frame.close(); - } - OperatingSystem::Unknown => {} - } + /// Exit from the application. + fn exit(&mut self, frame: &mut eframe::Frame) { + self.exit_allowed = true; + frame.close(); } /// Handle Back key event.