Skip to content

Commit

Permalink
Add actions in foreground service notification
Browse files Browse the repository at this point in the history
1 - Disable battery optimization
2 - Open notification service to disable it
3 - Tap in notification open main settings

Some notification builder changes
  • Loading branch information
WSTxda committed Dec 21, 2024
1 parent f29333b commit bc7c1cf
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package org.microg.gms.common;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Notification.Action;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.RequiresApi;

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

import static android.os.Build.VERSION.SDK_INT;
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;

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

@RequiresApi(26)
private static Notification buildForegroundNotification(Context context, String serviceName) {
NotificationChannel channel = new NotificationChannel("foreground-service", "Foreground Service", NotificationManager.IMPORTANCE_NONE);
// Notification channel
String channelName = context.getString(R.string.foreground_service_notification_title);
NotificationChannel channel = new NotificationChannel("foreground-service", channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
channel.setShowBadge(false);
channel.setVibrationPattern(new long[]{0});
context.getSystemService(NotificationManager.class).createNotificationChannel(channel);

// Title and text
String appTitle = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
String notifyTitle = context.getString(R.string.foreground_service_notification_title);
String firstLine = context.getString(R.string.foreground_service_notification_text, serviceName);
String secondLine = context.getString(R.string.foreground_service_notification_big_text, appTitle);

// Open battery optimizations settings
@SuppressLint("BatteryLife") Intent batteryOptimizationIntent = new Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
.setData(Uri.parse("package:" + context.getPackageName()));

PendingIntent batteryPendingIntent = PendingIntent.getActivity(
context, 0, batteryOptimizationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// Open notification settings in foreground service category
Intent notificationCategoryIntent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, "foreground-service");

PendingIntent notificationCategoryPendingIntent = PendingIntent.getActivity(
context, 1, notificationCategoryIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// Open settings activity when notification is tapped
Intent mainSettingsIntent = new Intent();
mainSettingsIntent.setClassName("app.revanced.android.gms", "org.microg.gms.ui.SettingsActivity");
mainSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent mainSettingsPendingIntent = PendingIntent.getActivity(
context, 2, mainSettingsIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// Notification actions
Action batteryAction = new Action.Builder(R.drawable.ic_battery_action, context.getString(R.string.foreground_action_battery_optimization), batteryPendingIntent).build();
Action notificationAction = new Action.Builder(R.drawable.ic_notification_action, context.getString(R.string.foreground_action_notification_settings), notificationCategoryPendingIntent).build();

Log.d(TAG, notifyTitle + " // " + firstLine + " // " + secondLine);

return new Notification.Builder(context, channel.getId())
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(notifyTitle)
.setContentText(firstLine)
.setStyle(new Notification.BigTextStyle().bigText(firstLine + "\n" + secondLine))
.setPriority(Notification.PRIORITY_HIGH)
.setShowWhen(false)
.setFullScreenIntent(notificationCategoryPendingIntent, true)
.setContentIntent(mainSettingsPendingIntent)
.addAction(batteryAction)
.addAction(notificationAction)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
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" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:fillColor="@android:color/white"
android:fillType="evenOdd"
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" />
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
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" />
</vector>
2 changes: 2 additions & 0 deletions play-services-base/core/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="foreground_service_notification_title">نشط في الخلفية</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> يعمل في الخلفية</string>
<string name="foreground_service_notification_big_text">استبعد <xliff:g example="خدمات microG">%1$s</xliff:g> من تحسينات البطارية أو قم بتغيير إعدادات الإشعارات لإخفاء هذا الإشعار</string>
<string name="foreground_action_battery_optimization">تحسين البطارية</string>
<string name="foreground_action_notification_settings">الإعدادات</string>

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

Expand Down
2 changes: 2 additions & 0 deletions play-services-base/core/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="foreground_service_notification_title">Aktif di latar belakang</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> sedang berjalan di latar belakang</string>
<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>
<string name="foreground_action_battery_optimization">Optimasi baterai</string>
<string name="foreground_action_notification_settings">Pengaturan</string>

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

Expand Down
2 changes: 2 additions & 0 deletions play-services-base/core/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="foreground_service_notification_title">Attivo in secondo piano</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> è in fase di esecuzione in secondo piano</string>
<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>
<string name="foreground_action_battery_optimization">Ottimizzazione batteria</string>
<string name="foreground_action_notification_settings">Impostazioni</string>

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

Expand Down
2 changes: 2 additions & 0 deletions play-services-base/core/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="foreground_service_notification_title">Aktywna w tle</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> jest uruchomiona w tle</string>
<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>
<string name="foreground_action_battery_optimization">Optymalizacja baterii</string>
<string name="foreground_action_notification_settings">Ustawienia</string>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<string name="foreground_service_notification_title">Ativo em segundo plano</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> está rodando em segundo plano</string>
<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>
<string name="foreground_action_battery_optimization">Otimização de bateria</string>
<string name="foreground_action_notification_settings">Configurações</string>

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

Expand Down
2 changes: 2 additions & 0 deletions play-services-base/core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="foreground_service_notification_title">Active in background</string>
<string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> is running in background</string>
<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>
<string name="foreground_action_battery_optimization">Battery optimization</string>
<string name="foreground_action_notification_settings">Settings</string>

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

Expand Down

0 comments on commit bc7c1cf

Please sign in to comment.