|
1 | 1 | package org.microg.gms.common;
|
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.app.Notification;
|
| 5 | +import android.app.Notification.Action; |
4 | 6 | import android.app.NotificationChannel;
|
5 | 7 | import android.app.NotificationManager;
|
| 8 | +import android.app.PendingIntent; |
6 | 9 | import android.app.Service;
|
7 | 10 | import android.content.ComponentName;
|
8 | 11 | import android.content.Context;
|
9 | 12 | import android.content.ContextWrapper;
|
10 | 13 | import android.content.Intent;
|
| 14 | +import android.net.Uri; |
11 | 15 | import android.os.PowerManager;
|
| 16 | +import android.provider.Settings; |
12 | 17 | import android.util.Log;
|
13 | 18 |
|
14 | 19 | import androidx.annotation.RequiresApi;
|
15 | 20 |
|
16 | 21 | import org.microg.gms.base.core.R;
|
17 | 22 |
|
18 | 23 | import static android.os.Build.VERSION.SDK_INT;
|
| 24 | +import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS; |
19 | 25 |
|
20 | 26 | public class ForegroundServiceContext extends ContextWrapper {
|
21 | 27 | private static final String TAG = "ForegroundService";
|
@@ -84,22 +90,63 @@ public static void completeForegroundService(Service service, Intent intent, Str
|
84 | 90 |
|
85 | 91 | @RequiresApi(26)
|
86 | 92 | private static Notification buildForegroundNotification(Context context, String serviceName) {
|
87 |
| - NotificationChannel channel = new NotificationChannel("foreground-service", "Foreground Service", NotificationManager.IMPORTANCE_NONE); |
| 93 | + // Notification channel |
| 94 | + String channelName = context.getString(R.string.foreground_service_notification_title); |
| 95 | + NotificationChannel channel = new NotificationChannel("foreground-service", channelName, NotificationManager.IMPORTANCE_HIGH); |
88 | 96 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
|
89 | 97 | channel.setShowBadge(false);
|
90 |
| - channel.setVibrationPattern(new long[]{0}); |
91 | 98 | context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
|
| 99 | + |
| 100 | + // Title and text |
92 | 101 | String appTitle = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
|
93 | 102 | String notifyTitle = context.getString(R.string.foreground_service_notification_title);
|
94 | 103 | String firstLine = context.getString(R.string.foreground_service_notification_text, serviceName);
|
95 | 104 | String secondLine = context.getString(R.string.foreground_service_notification_big_text, appTitle);
|
| 105 | + |
| 106 | + // Open battery optimizations settings |
| 107 | + @SuppressLint("BatteryLife") Intent batteryOptimizationIntent = new Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) |
| 108 | + .setData(Uri.parse("package:" + context.getPackageName())); |
| 109 | + |
| 110 | + PendingIntent batteryPendingIntent = PendingIntent.getActivity( |
| 111 | + context, 0, batteryOptimizationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE |
| 112 | + ); |
| 113 | + |
| 114 | + // Open notification settings in foreground service category |
| 115 | + Intent notificationCategoryIntent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) |
| 116 | + .putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()) |
| 117 | + .putExtra(Settings.EXTRA_CHANNEL_ID, "foreground-service"); |
| 118 | + |
| 119 | + PendingIntent notificationCategoryPendingIntent = PendingIntent.getActivity( |
| 120 | + context, 1, notificationCategoryIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE |
| 121 | + ); |
| 122 | + |
| 123 | + // Open settings activity when notification is tapped |
| 124 | + Intent mainSettingsIntent = new Intent(); |
| 125 | + mainSettingsIntent.setClassName("app.revanced.android.gms", "org.microg.gms.ui.SettingsActivity"); |
| 126 | + mainSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 127 | + |
| 128 | + PendingIntent mainSettingsPendingIntent = PendingIntent.getActivity( |
| 129 | + context, 2, mainSettingsIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE |
| 130 | + ); |
| 131 | + |
| 132 | + // Notification actions |
| 133 | + Action batteryAction = new Action.Builder(R.drawable.ic_battery_action, context.getString(R.string.foreground_action_battery_optimization), batteryPendingIntent).build(); |
| 134 | + Action notificationAction = new Action.Builder(R.drawable.ic_notification_action, context.getString(R.string.foreground_action_notification_settings), notificationCategoryPendingIntent).build(); |
| 135 | + |
96 | 136 | Log.d(TAG, notifyTitle + " // " + firstLine + " // " + secondLine);
|
| 137 | + |
97 | 138 | return new Notification.Builder(context, channel.getId())
|
98 | 139 | .setOngoing(true)
|
99 | 140 | .setSmallIcon(R.drawable.ic_notification)
|
100 | 141 | .setContentTitle(notifyTitle)
|
101 | 142 | .setContentText(firstLine)
|
102 | 143 | .setStyle(new Notification.BigTextStyle().bigText(firstLine + "\n" + secondLine))
|
| 144 | + .setPriority(Notification.PRIORITY_HIGH) |
| 145 | + .setShowWhen(false) |
| 146 | + .setFullScreenIntent(notificationCategoryPendingIntent, true) |
| 147 | + .setContentIntent(mainSettingsPendingIntent) |
| 148 | + .addAction(batteryAction) |
| 149 | + .addAction(notificationAction) |
103 | 150 | .build();
|
104 | 151 | }
|
105 | 152 |
|
|
0 commit comments