VancedMicroG/play-services-core/src/main/java/org/microg/gms/common/StatusNotification.java

78 lines
3.1 KiB
Java
Raw Normal View History

2020-09-09 14:47:23 +00:00
package org.microg.gms.common;
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;
2020-09-17 10:48:42 +00:00
import androidx.core.app.NotificationManagerCompat;
2020-09-09 14:47:23 +00:00
import com.mgoogle.android.gms.R;
public class StatusNotification {
private static int notificationID;
2020-09-10 02:01:39 +00:00
private static boolean notificationExists = false;
2020-09-09 14:47:23 +00:00
public static boolean Notify(Context context) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
2020-09-17 10:48:42 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && NotificationManagerCompat.from(context).areNotificationsEnabled()) {
2020-09-09 14:47:23 +00:00
if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) {
2020-09-10 02:01:39 +00:00
if (notificationExists) {
return false;
2020-09-09 14:47:23 +00:00
}
buildStatusNotification(context);
2020-09-10 02:01:39 +00:00
} else {
if (notificationExists) {
((NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
notificationExists = false;
}
2020-09-09 14:47:23 +00:00
}
}
2020-09-10 02:01:39 +00:00
return false;
2020-09-09 14:47:23 +00:00
}
private static void buildStatusNotification(Context context) {
2020-09-10 02:01:39 +00:00
notificationID = 1;
2020-09-09 14:47:23 +00:00
Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
String notificationChannelID = "foreground-service";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2020-09-10 02:01:39 +00:00
NotificationChannel channel = new NotificationChannel(notificationChannelID,
context.getResources().getString(R.string.notification_service_name),
NotificationManager.IMPORTANCE_LOW);
2020-09-09 14:47:23 +00:00
channel.setShowBadge(true);
channel.setLockscreenVisibility(0);
channel.setVibrationPattern(new long[0]);
context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, notificationChannelID)
2020-09-10 02:01:39 +00:00
.setOngoing(true)
2020-09-09 14:47:23 +00:00
.setContentIntent(pendingIntent)
2020-09-10 02:01:39 +00:00
.setSmallIcon(R.drawable.ic_foreground_notification)
.setSubText(context.getResources().getString(R.string.notification_service_title))
.setContentTitle(context.getResources().getString(R.string.notification_service_content))
.setContentText(context.getResources().getString(R.string.notification_service_subcontent));
2020-09-09 14:47:23 +00:00
NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationID, notification.build());
2020-09-10 02:01:39 +00:00
notificationExists = true;
2020-09-09 14:47:23 +00:00
}
}