Also show service notification when app is in foreground.

Useful when embedding microG
This commit is contained in:
Marvin W 2020-12-01 18:26:10 +01:00
parent d4e20883d7
commit 00a8e83540
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A

View File

@ -1,6 +1,5 @@
package org.microg.gms.common; package org.microg.gms.common;
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;
@ -15,8 +14,6 @@ import android.util.Log;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import java.util.List;
public class ForegroundServiceContext extends ContextWrapper { public class ForegroundServiceContext extends ContextWrapper {
private static final String TAG = "ForegroundService"; private static final String TAG = "ForegroundService";
public static final String EXTRA_FOREGROUND = "foreground"; public static final String EXTRA_FOREGROUND = "foreground";
@ -27,7 +24,7 @@ public class ForegroundServiceContext extends ContextWrapper {
@Override @Override
public ComponentName startService(Intent service) { public ComponentName startService(Intent service) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isIgnoringBatteryOptimizations() && !isAppOnForeground()) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isIgnoringBatteryOptimizations()) {
Log.d(TAG, "Starting in foreground mode."); Log.d(TAG, "Starting in foreground mode.");
service.putExtra(EXTRA_FOREGROUND, true); service.putExtra(EXTRA_FOREGROUND, true);
return super.startForegroundService(service); return super.startForegroundService(service);
@ -41,21 +38,6 @@ public class ForegroundServiceContext extends ContextWrapper {
return powerManager.isIgnoringBatteryOptimizations(getPackageName()); return powerManager.isIgnoringBatteryOptimizations(getPackageName());
} }
private boolean isAppOnForeground() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
public static void completeForegroundService(Service service, Intent intent, String tag) { public static void completeForegroundService(Service service, Intent intent, String tag) {
if (intent != null && intent.getBooleanExtra(EXTRA_FOREGROUND, false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (intent != null && intent.getBooleanExtra(EXTRA_FOREGROUND, false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d(tag, "Started in foreground mode."); Log.d(tag, "Started in foreground mode.");
@ -73,7 +55,7 @@ public class ForegroundServiceContext extends ContextWrapper {
return new Notification.Builder(context, channel.getId()) return new Notification.Builder(context, channel.getId())
.setOngoing(true) .setOngoing(true)
.setContentTitle("Running in background") .setContentTitle("Running in background")
//.setSmallIcon(R.drawable.ic_cloud_bell) .setContentText("microG " + context.getClass().getSimpleName() + " is running in background.")
.build(); .build();
} }
} }