Verified Commit 68a16af5 authored by Marvin W.'s avatar Marvin W. 🐿
Browse files

Change background service notification

Fixes #1324
parent f20d966f
......@@ -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();
}
}
/*
* 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;
}
<!--
~ SPDX-FileCopyrightText: 2020, microG Project Team
~ SPDX-License-Identifier: Apache-2.0
-->
<vector android:height="24dp" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#000000" android:pathData="M19.51,3.08L3.08,19.51c0.09,0.34 0.27,0.65 0.51,0.9 0.25,0.24 0.56,0.42 0.9,0.51L20.93,4.49c-0.19,-0.69 -0.73,-1.23 -1.42,-1.41zM11.88,3L3,11.88v2.83L14.71,3h-2.83zM5,3c-1.1,0 -2,0.9 -2,2v2l4,-4L5,3zM19,21c0.55,0 1.05,-0.22 1.41,-0.59 0.37,-0.36 0.59,-0.86 0.59,-1.41v-2l-4,4h2zM9.29,21h2.83L21,12.12L21,9.29L9.29,21z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<!--
~ SPDX-FileCopyrightText: 2020, microG Project Team
~ SPDX-License-Identifier: Apache-2.0
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="foreground_service_notification_title">Im Hintergrund aktiv</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> läuft im Hintergrund weiter.</string>
<string name="foreground_service_notification_big_text">Füge <xliff:g example="microG Services Core">%1$s</xliff:g> als Ausnahme zur Batterie-Optimierung hinzu oder verstecke diese Benachrichtigung in den Systemeinstelleungen.</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<!--
~ SPDX-FileCopyrightText: 2020, microG Project Team
~ SPDX-License-Identifier: Apache-2.0
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="foreground_service_notification_title">Active in background</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> is running in background.</string>
<string name="foreground_service_notification_big_text">Exclude <xliff:g example="microG Services Core">%1$s</xliff:g> from battery optimizations or change notification settings to hide this notification.</string>
</resources>
......@@ -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
......
......@@ -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";
......
......@@ -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
......
......@@ -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)
......
......@@ -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() {
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment