android: update activity lib, optimize app exit

This commit is contained in:
ardocrat 2023-08-01 01:03:51 +03:00
parent a95038fc9d
commit 80a04596df
9 changed files with 51 additions and 74 deletions

5
Cargo.lock generated
View file

@ -188,9 +188,9 @@ dependencies = [
[[package]] [[package]]
name = "android-activity" name = "android-activity"
version = "0.4.2" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40bc1575e653f158cbdc6ebcd917b9564e66321c5325c232c3591269c257be69" checksum = "64529721f27c2314ced0890ce45e469574a73e5e6fdd6e9da1860eb29285f5e0"
dependencies = [ dependencies = [
"android-properties", "android-properties",
"bitflags 1.3.2", "bitflags 1.3.2",
@ -2221,6 +2221,7 @@ dependencies = [
name = "grim" name = "grim"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"android-activity",
"android_logger", "android_logger",
"built", "built",
"byteorder", "byteorder",

View file

@ -76,5 +76,6 @@ eframe = { version = "0.22.0", features = [ "wgpu" ] }
[target.'cfg(target_os = "android")'.dependencies] [target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13.1" android_logger = "0.13.1"
jni = "0.21.1" jni = "0.21.1"
android-activity = "0.4.3"
winit = { version = "0.28", features = [ "android-game-activity" ] } winit = { version = "0.28", features = [ "android-game-activity" ] }
eframe = { version = "0.22.0", features = [ "wgpu", "android-game-activity" ] } eframe = { version = "0.22.0", features = [ "wgpu", "android-game-activity" ] }

View file

@ -31,11 +31,13 @@ public class BackgroundService extends Service {
mNotificationBuilder.setContentText(getSyncStatusText()); mNotificationBuilder.setContentText(getSyncStatusText());
NotificationManager manager = getSystemService(NotificationManager.class); NotificationManager manager = getSystemService(NotificationManager.class);
manager.notify(SYNC_STATUS_NOTIFICATION_ID, mNotificationBuilder.build()); 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()) { if (exitAppAfterNodeStop()) {
sendBroadcast(new Intent(MainActivity.FINISH_ACTIVITY_ACTION)); sendBroadcast(new Intent(MainActivity.STOP_APP_ACTION));
mStopped = true; mStopped = true;
} }
// Repeat notification update if service is not stopped. // Repeat notification update if service is not stopped.
if (!mStopped) { if (!mStopped) {
mHandler.postDelayed(this, 500); mHandler.postDelayed(this, 500);
@ -45,10 +47,15 @@ public class BackgroundService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
if (mStopped) {
return;
}
// Prevent CPU to sleep at background. // Prevent CPU to sleep at background.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.acquire(); mWakeLock.acquire();
// Create channel to show notifications. // Create channel to show notifications.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel( NotificationChannel notificationChannel = new NotificationChannel(
@ -58,6 +65,7 @@ public class BackgroundService extends Service {
NotificationManager manager = getSystemService(NotificationManager.class); NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel); manager.createNotificationChannel(notificationChannel);
} }
// Show notification with sync status. // Show notification with sync status.
Intent i = getPackageManager().getLaunchIntentForPackage(this.getPackageName()); Intent i = getPackageManager().getLaunchIntentForPackage(this.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_IMMUTABLE); 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) .setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent); .setContentIntent(pendingIntent);
Notification notification = mNotificationBuilder.build(); Notification notification = mNotificationBuilder.build();
// Start service at foreground state to prevent killing by system. // Start service at foreground state to prevent killing by system.
startForeground(SYNC_STATUS_NOTIFICATION_ID, notification); startForeground(SYNC_STATUS_NOTIFICATION_ID, notification);
// Update sync status at notification. // Update sync status at notification.
mHandler.post(mUpdateSyncStatus); mHandler.post(mUpdateSyncStatus);
} }
@ -98,16 +108,20 @@ public class BackgroundService extends Service {
public void onStop() { public void onStop() {
mStopped = true; mStopped = true;
// Stop updating the notification. // Stop updating the notification.
mHandler.removeCallbacks(mUpdateSyncStatus); mHandler.removeCallbacks(mUpdateSyncStatus);
// Remove service from foreground state. // Remove service from foreground state.
stopForeground(Service.STOP_FOREGROUND_REMOVE); stopForeground(Service.STOP_FOREGROUND_REMOVE);
// Remove notification. // Remove notification.
NotificationManager notificationManager = getSystemService(NotificationManager.class); NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.deleteNotificationChannel(TAG); notificationManager.deleteNotificationChannel(TAG);
} }
notificationManager.cancel(SYNC_STATUS_NOTIFICATION_ID); notificationManager.cancel(SYNC_STATUS_NOTIFICATION_ID);
// Release wake lock to allow CPU to sleep at background. // Release wake lock to allow CPU to sleep at background.
if (mWakeLock.isHeld()) { if (mWakeLock.isHeld()) {
mWakeLock.release(); mWakeLock.release();
@ -149,6 +163,6 @@ public class BackgroundService extends Service {
private native String getSyncStatusText(); private native String getSyncStatusText();
// Get sync title text for notification from native code. // Get sync title text for notification from native code.
private native String getSyncTitle(); 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(); private native boolean exitAppAfterNodeStop();
} }

View file

@ -5,7 +5,6 @@ import android.os.Bundle;
import android.os.Process; import android.os.Process;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import androidx.core.graphics.Insets; import androidx.core.graphics.Insets;
@ -14,14 +13,11 @@ import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import com.google.androidgamesdk.GameActivity; 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_HTML;
import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN; import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
public class MainActivity extends GameActivity { public class MainActivity extends GameActivity {
public static String STOP_APP_ACTION = "STOP_APP";
public static String FINISH_ACTIVITY_ACTION = "MainActivity.finish";
static { static {
System.loadLibrary("grim"); System.loadLibrary("grim");
@ -30,9 +26,9 @@ public class MainActivity extends GameActivity {
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context ctx, Intent i) { public void onReceive(Context ctx, Intent i) {
if (i.getAction().equals(FINISH_ACTIVITY_ACTION)) { if (i.getAction().equals(STOP_APP_ACTION)) {
unregisterReceiver(this);
onExit(); onExit();
Process.killProcess(Process.myPid());
} }
} }
}; };
@ -48,7 +44,7 @@ public class MainActivity extends GameActivity {
super.onCreate(null); super.onCreate(null);
// Register receiver to finish activity from the BackgroundService. // 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. // Start notification service.
BackgroundService.start(this); BackgroundService.start(this);
@ -99,43 +95,33 @@ public class MainActivity extends GameActivity {
// Implemented into native code to handle key code BACK event. // Implemented into native code to handle key code BACK event.
public native void onBack(); public native void onBack();
private boolean mManualExit; // Actions on app exit.
private final AtomicBoolean mActivityDestroyed = new AtomicBoolean(false); private void onExit() {
unregisterReceiver(mBroadcastReceiver);
BackgroundService.stop(this);
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
if (!mManualExit) { onExit();
unregisterReceiver(mBroadcastReceiver);
onTermination();
}
// 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(() -> { new Thread(() -> {
try { try {
onTermination();
Thread.sleep(3000); Thread.sleep(3000);
if (!mActivityDestroyed.get()) {
Process.killProcess(Process.myPid()); Process.killProcess(Process.myPid());
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}).start(); }).start();
// Destroy an app and kill process.
super.onDestroy(); super.onDestroy();
mActivityDestroyed.set(true); Process.killProcess(Process.myPid());
} }
// Called from native code. // Notify native code to stop activity (e.g. node) if app was terminated from recent apps.
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.
public native void onTermination(); public native void onTermination();
// Called from native code to set text into clipboard. // Called from native code to set text into clipboard.

View file

@ -65,8 +65,11 @@ impl<Platform: PlatformCallbacks> eframe::App for PlatformApp<Platform> {
} }
fn on_close_event(&mut self) -> bool { fn on_close_event(&mut self) -> bool {
let exit = self.root.exit_allowed;
if !exit {
Root::show_exit_modal(); Root::show_exit_modal();
self.root.exit_allowed }
exit
} }
} }

View file

@ -74,15 +74,4 @@ impl PlatformCallbacks for Android {
}; };
paste_data 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();
}
} }

View file

@ -27,6 +27,4 @@ impl PlatformCallbacks for Desktop {
fn get_string_from_buffer(&self) -> String { fn get_string_from_buffer(&self) -> String {
"".to_string() "".to_string()
} }
fn exit(&self) {}
} }

View file

@ -26,5 +26,4 @@ pub trait PlatformCallbacks {
fn hide_keyboard(&self); fn hide_keyboard(&self);
fn copy_string_to_buffer(&self, data: String); fn copy_string_to_buffer(&self, data: String);
fn get_string_from_buffer(&self) -> String; fn get_string_from_buffer(&self) -> String;
fn exit(&self);
} }

View file

@ -78,7 +78,7 @@ impl Root {
pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) { pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
// Show opened exit confirmation modal content. // Show opened exit confirmation modal content.
if self.can_draw_modal() { 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); let (is_panel_open, panel_width) = Self::network_panel_state_width(frame);
@ -148,14 +148,11 @@ impl Root {
} }
/// Draw exit confirmation modal content. /// Draw exit confirmation modal content.
fn exit_modal_content(&mut self, fn exit_modal_content(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
cb: &dyn PlatformCallbacks) {
Modal::ui(ui, |ui, modal| { Modal::ui(ui, |ui, modal| {
if self.show_exit_progress { if self.show_exit_progress {
if !Node::is_running() { if !Node::is_running() {
self.exit(frame, cb); self.exit(frame);
modal.close(); modal.close();
} }
ui.add_space(16.0); ui.add_space(16.0);
@ -185,7 +182,7 @@ impl Root {
columns[0].vertical_centered_justified(|ui| { columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal_exit.exit"), Colors::WHITE, || { View::button(ui, t!("modal_exit.exit"), Colors::WHITE, || {
if !Node::is_running() { if !Node::is_running() {
self.exit(frame, cb); self.exit(frame);
modal.close(); modal.close();
} else { } else {
Node::stop(true); Node::stop(true);
@ -206,22 +203,11 @@ impl Root {
}); });
} }
/// Platform-specific exit from the application. /// Exit from the application.
fn exit(&mut self, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) { fn exit(&mut self, frame: &mut eframe::Frame) {
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; self.exit_allowed = true;
frame.close(); frame.close();
} }
OperatingSystem::Unknown => {}
}
}
/// Handle Back key event. /// Handle Back key event.
pub fn on_back(&mut self) { pub fn on_back(&mut self) {