mirror of
https://github.com/jwinterm/grim.git
synced 2026-10-19 14:58:33 +02:00
android: update activity lib, optimize app exit
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user