2020-09-09 15:26:42 +00:00
|
|
|
package org.microg.gms.common;
|
|
|
|
|
|
|
|
import android.app.ActivityManager;
|
2020-09-19 19:28:42 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2020-09-19 22:11:03 +00:00
|
|
|
import android.app.PendingIntent;
|
2020-09-09 15:26:42 +00:00
|
|
|
import android.app.Service;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.ContextWrapper;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.PowerManager;
|
2020-09-19 22:11:03 +00:00
|
|
|
import android.provider.Settings;
|
2020-09-09 15:26:42 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import androidx.annotation.RequiresApi;
|
2020-09-19 22:11:03 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
|
|
|
|
import com.mgoogle.android.gms.R;
|
2020-09-09 15:26:42 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class ForegroundServiceContext extends ContextWrapper {
|
|
|
|
private static final String TAG = "ForegroundService";
|
|
|
|
public static final String EXTRA_FOREGROUND = "foreground";
|
|
|
|
|
|
|
|
public ForegroundServiceContext(Context base) {
|
|
|
|
super(base);
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:59:46 +00:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
2020-09-09 15:26:42 +00:00
|
|
|
@Override
|
|
|
|
public ComponentName startService(Intent service) {
|
2020-09-19 22:59:46 +00:00
|
|
|
if (!isIgnoringBatteryOptimizations() && !isAppOnForeground()) {
|
2020-09-09 15:26:42 +00:00
|
|
|
Log.d(TAG, "Starting in foreground mode.");
|
|
|
|
service.putExtra(EXTRA_FOREGROUND, true);
|
2020-09-19 22:59:46 +00:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
return super.startForegroundService(service);
|
2020-09-19 23:21:47 +00:00
|
|
|
} else {
|
|
|
|
return super.startService(service);
|
2020-09-19 22:59:46 +00:00
|
|
|
}
|
2020-09-09 15:26:42 +00:00
|
|
|
}
|
|
|
|
return super.startService(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
|
|
private boolean isIgnoringBatteryOptimizations() {
|
|
|
|
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
|
|
|
|
return powerManager.isIgnoringBatteryOptimizations(getPackageName());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isAppOnForeground() {
|
|
|
|
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
|
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
|
|
|
|
if (appProcesses == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final String packageName = getPackageName();
|
|
|
|
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
|
|
|
|
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void completeForegroundService(Service service, Intent intent, String tag) {
|
2020-09-19 22:59:46 +00:00
|
|
|
if (intent != null && intent.getBooleanExtra(EXTRA_FOREGROUND, false)) {
|
2020-09-09 15:26:42 +00:00
|
|
|
Log.d(tag, "Started in foreground mode.");
|
2020-09-19 19:28:42 +00:00
|
|
|
service.startForeground(tag.hashCode(), buildForegroundNotification(service));
|
2020-09-09 15:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 19:28:42 +00:00
|
|
|
|
|
|
|
private static Notification buildForegroundNotification(Context context) {
|
2020-09-19 22:11:03 +00:00
|
|
|
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);
|
2020-09-19 22:59:46 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
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);
|
|
|
|
}
|
2020-09-19 22:11:03 +00:00
|
|
|
return new NotificationCompat.Builder(context, "foreground-service")
|
2020-09-19 19:28:42 +00:00
|
|
|
.setOngoing(true)
|
2020-09-19 22:11:03 +00:00
|
|
|
.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)))
|
2020-09-19 19:28:42 +00:00
|
|
|
.build();
|
|
|
|
}
|
2020-09-19 23:10:05 +00:00
|
|
|
}
|