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]]
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",

View file

@ -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" ] }

View file

@ -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();
}

View file

@ -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.

View file

@ -65,8 +65,11 @@ impl<Platform: PlatformCallbacks> eframe::App for PlatformApp<Platform> {
}
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
}
}

View file

@ -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();
}
}

View file

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

View file

@ -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);
}

View file

@ -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.