Move battery saving notification to original method

This commit is contained in:
Oizaro 2020-09-20 00:11:03 +02:00
parent ef752e19e9
commit 43bde91dab
3 changed files with 26 additions and 105 deletions

View File

@ -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")) {

View File

@ -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();
}
}

View File

@ -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;
}
}