From 43bde91dab6f464f62e488bddfdd8b0e78b082d4 Mon Sep 17 00:00:00 2001 From: Oizaro <45825534+Oizaro@users.noreply.github.com> Date: Sun, 20 Sep 2020 00:11:03 +0200 Subject: [PATCH] Move battery saving notification to original method --- .../java/org/microg/gms/auth/AuthManager.java | 5 - .../gms/common/ForegroundServiceContext.java | 34 +++++-- .../microg/gms/common/StatusNotification.java | 92 ------------------- 3 files changed, 26 insertions(+), 105 deletions(-) delete mode 100644 play-services-core/src/main/java/org/microg/gms/common/StatusNotification.java diff --git a/play-services-core/src/main/java/org/microg/gms/auth/AuthManager.java b/play-services-core/src/main/java/org/microg/gms/auth/AuthManager.java index 0a67574b..ebb9725b 100644 --- a/play-services-core/src/main/java/org/microg/gms/auth/AuthManager.java +++ b/play-services-core/src/main/java/org/microg/gms/auth/AuthManager.java @@ -32,8 +32,6 @@ import java.io.IOException; import static android.content.pm.ApplicationInfo.FLAG_SYSTEM; import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; -import org.microg.gms.common.StatusNotification; - public class AuthManager { private static final String TAG = "GmsAuthManager"; @@ -52,9 +50,6 @@ public class AuthManager { private String accountType; public AuthManager(Context context, String accountName, String packageName, String service) { - - StatusNotification.Notify(context); - this.context = context; this.accountName = accountName; if (packageName.contains("youtube.music")) { diff --git a/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java b/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java index 12b208dd..aa09c74b 100644 --- a/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java +++ b/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java @@ -4,6 +4,7 @@ import android.app.ActivityManager; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.Service; import android.content.ComponentName; import android.content.Context; @@ -11,9 +12,13 @@ import android.content.ContextWrapper; import android.content.Intent; import android.os.Build; import android.os.PowerManager; +import android.provider.Settings; import android.util.Log; import androidx.annotation.RequiresApi; +import androidx.core.app.NotificationCompat; + +import com.mgoogle.android.gms.R; import java.util.List; @@ -65,15 +70,28 @@ public class ForegroundServiceContext extends ContextWrapper { @RequiresApi(api = Build.VERSION_CODES.O) private static Notification buildForegroundNotification(Context context) { - NotificationChannel channel = new NotificationChannel("foreground-service", "Foreground Service", NotificationManager.IMPORTANCE_NONE); - channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); - channel.setShowBadge(false); - channel.setVibrationPattern(new long[0]); - context.getSystemService(NotificationManager.class).createNotificationChannel(channel); - return new Notification.Builder(context, channel.getId()) + Intent notificationIntent = new Intent(); + notificationIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); + notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + PendingIntent notificationPendingIntent = PendingIntent.getActivity(context, + 0, + notificationIntent, + 0); + + NotificationChannel Channel = new NotificationChannel("foreground-service", + context.getResources().getString(R.string.notification_service_name), + NotificationManager.IMPORTANCE_LOW); + Channel.setShowBadge(false); + Channel.setLockscreenVisibility(0); + Channel.setVibrationPattern(new long[0]); + context.getSystemService(NotificationManager.class).createNotificationChannel(Channel); + return new NotificationCompat.Builder(context, "foreground-service") .setOngoing(true) - .setContentTitle("Running in background") - //.setSmallIcon(R.drawable.ic_cloud_bell) + .setContentIntent(notificationPendingIntent) + .setSmallIcon(R.drawable.ic_foreground_notification) + .setStyle(new NotificationCompat.BigTextStyle() + .setBigContentTitle(context.getResources().getString(R.string.notification_service_title)) + .bigText(context.getResources().getString(R.string.notification_service_content))) .build(); } } \ No newline at end of file diff --git a/play-services-core/src/main/java/org/microg/gms/common/StatusNotification.java b/play-services-core/src/main/java/org/microg/gms/common/StatusNotification.java deleted file mode 100644 index 1ce3f1e5..00000000 --- a/play-services-core/src/main/java/org/microg/gms/common/StatusNotification.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.microg.gms.common; - -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.os.Build; -import android.os.PowerManager; -import android.provider.Settings; - -import androidx.core.app.NotificationCompat; -import androidx.core.app.NotificationManagerCompat; - -import com.mgoogle.android.gms.R; - -public class StatusNotification { - - private static Notification Notification; - private static int notificationID = 1; - private static String notificationChannelID = "foreground-service"; - private static boolean notificationExists = false; - - public static void Notify(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - - boolean isChannelEnabled = true; - - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - NotificationChannel notificationChannel = notificationManager.getNotificationChannel(notificationChannelID); - if (notificationChannel != null - && notificationChannel.getImportance() == NotificationManager.IMPORTANCE_NONE) { - isChannelEnabled = false; - } - } - - if (NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled() - && isChannelEnabled) { - if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) { - if (!notificationExists) { - buildStatusNotification(context); - } else { - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.notify(notificationID, Notification); - } - } else { - if (notificationExists) { - ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID); - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).deleteNotificationChannel(notificationChannelID); - } - notificationExists = false; - } - } - } - } - } - - private static void buildStatusNotification(Context context) { - Intent notificationIntent = new Intent(); - notificationIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); - notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent notificationPendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); - - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel Channel = new NotificationChannel(notificationChannelID, - context.getResources().getString(R.string.notification_service_name), - NotificationManager.IMPORTANCE_LOW); - Channel.setShowBadge(false); - Channel.setLockscreenVisibility(0); - Channel.setVibrationPattern(new long[0]); - context.getSystemService(NotificationManager.class).createNotificationChannel(Channel); - } - Notification = new NotificationCompat.Builder(context, notificationChannelID) - .setOngoing(true) - .setContentIntent(notificationPendingIntent) - .setSmallIcon(R.drawable.ic_foreground_notification) - .setStyle(new NotificationCompat.BigTextStyle() - .setBigContentTitle(context.getResources().getString(R.string.notification_service_title)) - .bigText(context.getResources().getString(R.string.notification_service_content))) - .build(); - - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - - notificationManager.notify(notificationID, Notification); - - notificationExists = true; - } -}