mirror of
http://galexander.org/git/simplesshd.git
synced 2024-12-02 12:18:14 +00:00
Implement the hack from stackoverflow to detect if the task is really in
the background even though it's in Activity.onResume() (works around Android 9 bug).
This commit is contained in:
parent
01b3a65a05
commit
a2aa4a2d1c
@ -1,6 +1,7 @@
|
|||||||
package org.galexander.sshd;
|
package org.galexander.sshd;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.ActivityManager;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationChannel;
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
@ -16,6 +17,7 @@ import androidx.core.app.NotificationCompat;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class SimpleSSHDService extends Service {
|
public class SimpleSSHDService extends Service {
|
||||||
/* if restarting twice within 10 seconds, give up */
|
/* if restarting twice within 10 seconds, give up */
|
||||||
@ -222,15 +224,38 @@ public class SimpleSSHDService extends Service {
|
|||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
if (Prefs.get_foreground()) {
|
if (Prefs.get_foreground()) {
|
||||||
ctx.startForegroundService(i);
|
ctx.startForegroundService(i);
|
||||||
} else if (ctx instanceof Activity) {
|
} else if (!(ctx instanceof Activity)) {
|
||||||
ctx.startService(i);
|
|
||||||
} else {
|
|
||||||
Toast.makeText(ctx,
|
Toast.makeText(ctx,
|
||||||
"SimpleSSHD cannot start in background since Oreo (enable Settings -> Foreground Service).",
|
"SimpleSSHD cannot start in background since Oreo (enable Settings -> Foreground Service).",
|
||||||
Toast.LENGTH_LONG).show();
|
Toast.LENGTH_LONG).show();
|
||||||
|
} else if (is_foreground_app(ctx)) {
|
||||||
|
ctx.startService(i);
|
||||||
|
} else {
|
||||||
|
/* The OS put us in SimpleSSHD.onResume() even
|
||||||
|
* though it's not ready to put an app activity
|
||||||
|
* in the foreground yet! Just give up. Maybe
|
||||||
|
* the OS will try again? */
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ctx.startService(i);
|
ctx.startService(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* There's a bug in Android 9 Pie (SDK 28) where onResume() is called
|
||||||
|
* during wake-up when the OS isn't ready to put the app in the
|
||||||
|
* foreground, so startService() fails. This should detect that state.
|
||||||
|
* This code is copy-pasted from stackoverflow, and initially comes from
|
||||||
|
* a Google bug report on the issue? */
|
||||||
|
public static boolean is_foreground_app(Context ctx) {
|
||||||
|
ActivityManager am = (ActivityManager)ctx.getSystemService(
|
||||||
|
Context.ACTIVITY_SERVICE);
|
||||||
|
List<ActivityManager.RunningAppProcessInfo> procs =
|
||||||
|
am.getRunningAppProcesses();
|
||||||
|
if (procs == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// higher importance has lower number (?)
|
||||||
|
return (procs.get(0).importance <=
|
||||||
|
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user