From 68a16af53bf46b0dd9aad43e0a35d7c56bd2e165 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 21 Dec 2020 20:41:09 +0100 Subject: [PATCH] Change background service notification Fixes #1324 --- .../gms/common/ForegroundServiceContext.java | 53 ++++++++++++++++--- .../gms/common/ForegroundServiceInfo.java | 18 +++++++ .../res/drawable/ic_background_notify.xml | 9 ++++ .../src/main/res/values-de/strings.xml | 11 ++++ .../src/main/res/values/strings.xml | 11 ++++ .../microg/gms/checkin/CheckinService.java | 3 ++ .../java/org/microg/gms/gcm/McsService.java | 3 ++ .../exposurenotification/AdvertiserService.kt | 2 + .../exposurenotification/CleanupService.kt | 2 + .../exposurenotification/NotifyService.kt | 2 + .../exposurenotification/ScannerService.kt | 2 + 11 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceInfo.java create mode 100644 play-services-base-core/src/main/res/drawable/ic_background_notify.xml create mode 100644 play-services-base-core/src/main/res/values-de/strings.xml create mode 100644 play-services-base-core/src/main/res/values/strings.xml diff --git a/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java b/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java index 1387fb1b..3e17ca29 100644 --- a/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java +++ b/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java @@ -14,6 +14,8 @@ import android.util.Log; import androidx.annotation.RequiresApi; +import org.microg.gms.base.core.R; + public class ForegroundServiceContext extends ContextWrapper { private static final String TAG = "ForegroundService"; public static final String EXTRA_FOREGROUND = "foreground"; @@ -38,25 +40,62 @@ public class ForegroundServiceContext extends ContextWrapper { return powerManager.isIgnoringBatteryOptimizations(getPackageName()); } + private static String getServiceName(Service service) { + String serviceName = null; + try { + ForegroundServiceInfo annotation = service.getClass().getAnnotation(ForegroundServiceInfo.class); + if (annotation != null) { + if (annotation.res() != 0) { + try { + serviceName = service.getString(annotation.res()); + } catch (Exception ignored) { + } + } + if (serviceName == null) { + serviceName = annotation.value(); + } + } + } catch (Exception ignored) { + } + if (serviceName == null) { + serviceName = service.getClass().getSimpleName(); + } + return serviceName; + } + 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) { - Log.d(tag, "Started in foreground mode."); - service.startForeground(tag.hashCode(), buildForegroundNotification(service)); + String serviceName = getServiceName(service); + Log.d(tag, "Started " + serviceName + " in foreground mode."); + try { + Notification notification = buildForegroundNotification(service, serviceName); + service.startForeground(serviceName.hashCode(), notification); + Log.d(tag, "Notification: " + notification.toString()); + } catch (Exception e) { + Log.w(tag, e); + } } } @RequiresApi(api = Build.VERSION_CODES.O) - private static Notification buildForegroundNotification(Context context) { + private static Notification buildForegroundNotification(Context context, String serviceName) { NotificationChannel channel = new NotificationChannel("foreground-service", "Foreground Service", NotificationManager.IMPORTANCE_NONE); channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); channel.setShowBadge(false); - channel.setVibrationPattern(new long[] {0}); + channel.setVibrationPattern(new long[]{0}); context.getSystemService(NotificationManager.class).createNotificationChannel(channel); + String appTitle = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString(); + String notifyTitle = context.getString(R.string.foreground_service_notification_title); + String firstLine = context.getString(R.string.foreground_service_notification_text, serviceName); + String secondLine = context.getString(R.string.foreground_service_notification_big_text, appTitle); + Log.d(TAG, notifyTitle + " // " + firstLine + " // " + secondLine); return new Notification.Builder(context, channel.getId()) .setOngoing(true) - .setSmallIcon(android.R.drawable.stat_notify_error) - .setContentTitle("Running in background") - .setContentText("microG " + context.getClass().getSimpleName() + " is running in background.") + .setSmallIcon(R.drawable.ic_background_notify) + .setContentTitle(notifyTitle) + .setContentText(firstLine) + .setStyle(new Notification.BigTextStyle().bigText(firstLine + "\n" + secondLine)) .build(); } + } diff --git a/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceInfo.java b/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceInfo.java new file mode 100644 index 00000000..93f0c6df --- /dev/null +++ b/play-services-base-core/src/main/java/org/microg/gms/common/ForegroundServiceInfo.java @@ -0,0 +1,18 @@ +/* + * SPDX-FileCopyrightText: 2020, microG Project Team + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.microg.gms.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface ForegroundServiceInfo { + String value(); + int res() default 0; +} diff --git a/play-services-base-core/src/main/res/drawable/ic_background_notify.xml b/play-services-base-core/src/main/res/drawable/ic_background_notify.xml new file mode 100644 index 00000000..23df1537 --- /dev/null +++ b/play-services-base-core/src/main/res/drawable/ic_background_notify.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/play-services-base-core/src/main/res/values-de/strings.xml b/play-services-base-core/src/main/res/values-de/strings.xml new file mode 100644 index 00000000..0d3ec644 --- /dev/null +++ b/play-services-base-core/src/main/res/values-de/strings.xml @@ -0,0 +1,11 @@ + + + + + Im Hintergrund aktiv + %1$s läuft im Hintergrund weiter. + Füge %1$s als Ausnahme zur Batterie-Optimierung hinzu oder verstecke diese Benachrichtigung in den Systemeinstelleungen. + diff --git a/play-services-base-core/src/main/res/values/strings.xml b/play-services-base-core/src/main/res/values/strings.xml new file mode 100644 index 00000000..5514f42c --- /dev/null +++ b/play-services-base-core/src/main/res/values/strings.xml @@ -0,0 +1,11 @@ + + + + + Active in background + %1$s is running in background. + Exclude %1$s from battery optimizations or change notification settings to hide this notification. + diff --git a/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java b/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java index adb2399e..06d98af2 100644 --- a/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java +++ b/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java @@ -32,13 +32,16 @@ import android.util.Log; import androidx.legacy.content.WakefulBroadcastReceiver; +import com.google.android.gms.R; import com.google.android.gms.checkin.internal.ICheckinService; import org.microg.gms.auth.AuthConstants; +import org.microg.gms.common.ForegroundServiceInfo; import org.microg.gms.common.ForegroundServiceContext; import org.microg.gms.gcm.McsService; import org.microg.gms.people.PeopleManager; +@ForegroundServiceInfo(value = "Google device registration", res = R.string.service_name_checkin) public class CheckinService extends IntentService { private static final String TAG = "GmsCheckinSvc"; public static final long MAX_VALID_CHECKIN_AGE = 24 * 60 * 60 * 1000; // 12 hours diff --git a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java index 98c202ea..e5c6bb03 100644 --- a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java +++ b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java @@ -40,10 +40,12 @@ import android.util.Log; import androidx.legacy.content.WakefulBroadcastReceiver; +import com.google.android.gms.R; import com.squareup.wire.Message; import org.microg.gms.checkin.LastCheckinInfo; import org.microg.gms.common.ForegroundServiceContext; +import org.microg.gms.common.ForegroundServiceInfo; import org.microg.gms.common.PackageUtils; import org.microg.gms.gcm.mcs.AppData; import org.microg.gms.gcm.mcs.Close; @@ -106,6 +108,7 @@ import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_ERROR; import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_READY; import static org.microg.gms.gcm.McsConstants.MSG_TEARDOWN; +@ForegroundServiceInfo(value = "Cloud messaging", res = R.string.service_name_mcs) public class McsService extends Service implements Handler.Callback { private static final String TAG = "GmsGcmMcsSvc"; diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt index 126a6d41..4961c312 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt @@ -27,12 +27,14 @@ import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.microg.gms.common.ForegroundServiceContext +import org.microg.gms.common.ForegroundServiceInfo import java.io.FileDescriptor import java.io.PrintWriter import java.nio.ByteBuffer import java.util.* @TargetApi(21) +@ForegroundServiceInfo("Exposure Notification") class AdvertiserService : LifecycleService() { private val version = VERSION_1_0 private var advertising = false diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/CleanupService.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/CleanupService.kt index f4c9014e..95804b97 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/CleanupService.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/CleanupService.kt @@ -17,7 +17,9 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.microg.gms.common.ForegroundServiceContext +import org.microg.gms.common.ForegroundServiceInfo +@ForegroundServiceInfo("Exposure Notification") class CleanupService : LifecycleService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { ForegroundServiceContext.completeForegroundService(this, intent, TAG) diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/NotifyService.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/NotifyService.kt index 687dbe48..48682281 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/NotifyService.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/NotifyService.kt @@ -23,8 +23,10 @@ import androidx.core.content.ContextCompat import androidx.core.location.LocationManagerCompat import androidx.lifecycle.LifecycleService import org.microg.gms.common.ForegroundServiceContext +import org.microg.gms.common.ForegroundServiceInfo import org.microg.gms.nearby.core.R +@ForegroundServiceInfo("Exposure Notification") class NotifyService : LifecycleService() { private val notificationId = NotifyService::class.java.name.hashCode() private val trigger = object : BroadcastReceiver() { diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt index eba02481..addc6c8c 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt @@ -20,11 +20,13 @@ import android.util.Log import androidx.lifecycle.LifecycleService import androidx.lifecycle.lifecycleScope import org.microg.gms.common.ForegroundServiceContext +import org.microg.gms.common.ForegroundServiceInfo import java.io.FileDescriptor import java.io.PrintWriter import java.util.* @TargetApi(21) +@ForegroundServiceInfo("Exposure Notification") class ScannerService : LifecycleService() { private var scanning = false private var lastStartTime = 0L