Skip to content

Commit bc7c1cf

Browse files
committed
Add actions in foreground service notification
1 - Disable battery optimization 2 - Open notification service to disable it 3 - Tap in notification open main settings Some notification builder changes
1 parent f29333b commit bc7c1cf

File tree

10 files changed

+82
-3
lines changed

10 files changed

+82
-3
lines changed

Diff for: play-services-base/core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package org.microg.gms.common;
22

3+
import android.annotation.SuppressLint;
34
import android.app.Notification;
5+
import android.app.Notification.Action;
46
import android.app.NotificationChannel;
57
import android.app.NotificationManager;
8+
import android.app.PendingIntent;
69
import android.app.Service;
710
import android.content.ComponentName;
811
import android.content.Context;
912
import android.content.ContextWrapper;
1013
import android.content.Intent;
14+
import android.net.Uri;
1115
import android.os.PowerManager;
16+
import android.provider.Settings;
1217
import android.util.Log;
1318

1419
import androidx.annotation.RequiresApi;
1520

1621
import org.microg.gms.base.core.R;
1722

1823
import static android.os.Build.VERSION.SDK_INT;
24+
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
1925

2026
public class ForegroundServiceContext extends ContextWrapper {
2127
private static final String TAG = "ForegroundService";
@@ -84,22 +90,63 @@ public static void completeForegroundService(Service service, Intent intent, Str
8490

8591
@RequiresApi(26)
8692
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);
8896
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
8997
channel.setShowBadge(false);
90-
channel.setVibrationPattern(new long[]{0});
9198
context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
99+
100+
// Title and text
92101
String appTitle = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
93102
String notifyTitle = context.getString(R.string.foreground_service_notification_title);
94103
String firstLine = context.getString(R.string.foreground_service_notification_text, serviceName);
95104
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+
96136
Log.d(TAG, notifyTitle + " // " + firstLine + " // " + secondLine);
137+
97138
return new Notification.Builder(context, channel.getId())
98139
.setOngoing(true)
99140
.setSmallIcon(R.drawable.ic_notification)
100141
.setContentTitle(notifyTitle)
101142
.setContentText(firstLine)
102143
.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)
103150
.build();
104151
}
105152

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:tint="?attr/colorControlNormal"
5+
android:viewportWidth="960"
6+
android:viewportHeight="960">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M320,880Q303,880 291.5,868.5Q280,857 280,840L280,200Q280,183 291.5,171.5Q303,160 320,160L400,160L400,120Q400,103 411.5,91.5Q423,80 440,80L520,80Q537,80 548.5,91.5Q560,103 560,120L560,160L640,160Q657,160 668.5,171.5Q680,183 680,200L680,840Q680,857 668.5,868.5Q657,880 640,880L320,880Z" />
10+
</vector>

Diff for: play-services-base/core/src/main/res/drawable/ic_notification.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
android:viewportWidth="24"
55
android:viewportHeight="24">
66
<path
7-
android:fillColor="#FF000000"
7+
android:fillColor="@android:color/white"
88
android:fillType="evenOdd"
99
android:pathData="M3.16 14.8c-1.55-1.55-1.55-4.05 0-5.6L9.2 3.17c1.54-1.55 4.04-1.55 5.58 0l6.05 6.05c1.55 1.54 1.55 4.04 0 5.58l-3.6 3.6c-1.36-1.38-3.26-2.23-5.36-2.23-2.03 0-3.88 0.8-5.24 2.11L3.16 14.8Zm8.72 2.02c1.92 0 3.65 0.78 4.9 2.03l-1.99 2c-1.54 1.54-4.04 1.54-5.58 0l-2.1-2.11c1.23-1.19 2.92-1.92 4.77-1.92Zm2.97-4.94c0 1.64-1.33 2.97-2.97 2.97-1.63 0-2.96-1.33-2.96-2.97 0-1.63 1.33-2.96 2.96-2.96 1.64 0 2.97 1.33 2.97 2.96Z" />
1010
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:tint="?attr/colorControlNormal"
5+
android:viewportWidth="960"
6+
android:viewportHeight="960">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M200,760Q183,760 171.5,748.5Q160,737 160,720Q160,703 171.5,691.5Q183,680 200,680L240,680L240,400Q240,317 290,252.5Q340,188 420,168L420,140Q420,115 437.5,97.5Q455,80 480,80Q505,80 522.5,97.5Q540,115 540,140L540,168Q620,188 670,252.5Q720,317 720,400L720,680L760,680Q777,680 788.5,691.5Q800,703 800,720Q800,737 788.5,748.5Q777,760 760,760L200,760ZM480,880Q447,880 423.5,856.5Q400,833 400,800L560,800Q560,833 536.5,856.5Q513,880 480,880Z" />
10+
</vector>

Diff for: play-services-base/core/src/main/res/values-ar/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<string name="foreground_service_notification_title">نشط في الخلفية</string>
99
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> يعمل في الخلفية</string>
1010
<string name="foreground_service_notification_big_text">استبعد <xliff:g example="خدمات microG">%1$s</xliff:g> من تحسينات البطارية أو قم بتغيير إعدادات الإشعارات لإخفاء هذا الإشعار</string>
11+
<string name="foreground_action_battery_optimization">تحسين البطارية</string>
12+
<string name="foreground_action_notification_settings">الإعدادات</string>
1113

1214
<string name="menu_settings">الإعدادات</string>
1315

Diff for: play-services-base/core/src/main/res/values-in/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<string name="foreground_service_notification_title">Aktif di latar belakang</string>
99
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> sedang berjalan di latar belakang</string>
1010
<string name="foreground_service_notification_big_text">Kecualikan <xliff:g example="microG Services">%1$s</xliff:g> dari optimisasi baterai atau ubah pengaturan notifikasi untuk menyembunyikan notifikasi ini</string>
11+
<string name="foreground_action_battery_optimization">Optimasi baterai</string>
12+
<string name="foreground_action_notification_settings">Pengaturan</string>
1113

1214
<string name="menu_settings">Pengaturan</string>
1315

Diff for: play-services-base/core/src/main/res/values-it/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<string name="foreground_service_notification_title">Attivo in secondo piano</string>
99
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> è in fase di esecuzione in secondo piano</string>
1010
<string name="foreground_service_notification_big_text">Escludi <xliff:g example="microG Services">%1$s</xliff:g> dalla lista delle app con ottimizzazione della batteria attiva o nascondi questa notifica dalle impostazioni dell\'app</string>
11+
<string name="foreground_action_battery_optimization">Ottimizzazione batteria</string>
12+
<string name="foreground_action_notification_settings">Impostazioni</string>
1113

1214
<string name="menu_settings">Impostazioni</string>
1315

Diff for: play-services-base/core/src/main/res/values-pl/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<string name="foreground_service_notification_title">Aktywna w tle</string>
99
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> jest uruchomiona w tle</string>
1010
<string name="foreground_service_notification_big_text">Wyłącz optymalizację baterii dla <xliff:g example="microG Services">%1$s</xliff:g> lub zmień ustawienia powiadomień, aby ukryć to powiadomienie</string>
11+
<string name="foreground_action_battery_optimization">Optymalizacja baterii</string>
12+
<string name="foreground_action_notification_settings">Ustawienia</string>
1113

1214
<string name="menu_settings">Ustawienia</string>
1315

Diff for: play-services-base/core/src/main/res/values-pt-rBR/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<string name="foreground_service_notification_title">Ativo em segundo plano</string>
88
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> está rodando em segundo plano</string>
99
<string name="foreground_service_notification_big_text">Exclua o <xliff:g example="microG Services">%1$s</xliff:g> das otimizações de bateria ou altere as configurações de notificação para ocultar este aviso</string>
10+
<string name="foreground_action_battery_optimization">Otimização de bateria</string>
11+
<string name="foreground_action_notification_settings">Configurações</string>
1012

1113
<string name="menu_settings">Configurações</string>
1214

Diff for: play-services-base/core/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<string name="foreground_service_notification_title">Active in background</string>
99
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> is running in background</string>
1010
<string name="foreground_service_notification_big_text">Exclude <xliff:g example="microG Services">%1$s</xliff:g> from battery optimizations or change notification settings to hide this notification</string>
11+
<string name="foreground_action_battery_optimization">Battery optimization</string>
12+
<string name="foreground_action_notification_settings">Settings</string>
1113

1214
<string name="menu_settings">Settings</string>
1315

0 commit comments

Comments
 (0)