Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 68a16af5 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Change background service notification

Fixes #1324
parent f20d966f
Loading
Loading
Loading
Loading
+46 −7
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ import android.util.Log;

import androidx.annotation.RequiresApi;

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

public class ForegroundServiceContext extends ContextWrapper {
    private static final String TAG = "ForegroundService";
    public static final String EXTRA_FOREGROUND = "foreground";
@@ -38,25 +40,62 @@ public class ForegroundServiceContext extends ContextWrapper {
        return powerManager.isIgnoringBatteryOptimizations(getPackageName());
    }

    private static String getServiceName(Service service) {
        String serviceName = null;
        try {
            ForegroundServiceInfo annotation = service.getClass().getAnnotation(ForegroundServiceInfo.class);
            if (annotation != null) {
                if (annotation.res() != 0) {
                    try {
                        serviceName = service.getString(annotation.res());
                    } catch (Exception ignored) {
                    }
                }
                if (serviceName == null) {
                    serviceName = annotation.value();
                }
            }
        } catch (Exception ignored) {
        }
        if (serviceName == null) {
            serviceName = service.getClass().getSimpleName();
        }
        return serviceName;
    }

    public static void completeForegroundService(Service service, Intent intent, String tag) {
        if (intent != null && intent.getBooleanExtra(EXTRA_FOREGROUND, false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.d(tag, "Started in foreground mode.");
            service.startForeground(tag.hashCode(), buildForegroundNotification(service));
            String serviceName = getServiceName(service);
            Log.d(tag, "Started " + serviceName + " in foreground mode.");
            try {
                Notification notification = buildForegroundNotification(service, serviceName);
                service.startForeground(serviceName.hashCode(), notification);
                Log.d(tag, "Notification: " + notification.toString());
            } catch (Exception e) {
                Log.w(tag, e);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private static Notification buildForegroundNotification(Context context) {
    private static Notification buildForegroundNotification(Context context, String serviceName) {
        NotificationChannel channel = new NotificationChannel("foreground-service", "Foreground Service", NotificationManager.IMPORTANCE_NONE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        channel.setShowBadge(false);
        channel.setVibrationPattern(new long[]{0});
        context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
        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);
        Log.d(TAG, notifyTitle + " // " + firstLine + " // " + secondLine);
        return new Notification.Builder(context, channel.getId())
                .setOngoing(true)
                .setSmallIcon(android.R.drawable.stat_notify_error)
                .setContentTitle("Running in background")
                .setContentText("microG " + context.getClass().getSimpleName() + " is running in background.")
                .setSmallIcon(R.drawable.ic_background_notify)
                .setContentTitle(notifyTitle)
                .setContentText(firstLine)
                .setStyle(new Notification.BigTextStyle().bigText(firstLine + "\n" + secondLine))
                .build();
    }

}
+18 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2020, microG Project Team
 * SPDX-License-Identifier: Apache-2.0
 */

package org.microg.gms.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ForegroundServiceInfo {
    String value();
    int res() default 0;
}
+9 −0
Original line number Diff line number Diff line
<!--
  ~ SPDX-FileCopyrightText: 2020, microG Project Team
  ~ SPDX-License-Identifier: Apache-2.0
  -->

<vector android:height="24dp" android:viewportHeight="24"
    android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#000000" android:pathData="M19.51,3.08L3.08,19.51c0.09,0.34 0.27,0.65 0.51,0.9 0.25,0.24 0.56,0.42 0.9,0.51L20.93,4.49c-0.19,-0.69 -0.73,-1.23 -1.42,-1.41zM11.88,3L3,11.88v2.83L14.71,3h-2.83zM5,3c-1.1,0 -2,0.9 -2,2v2l4,-4L5,3zM19,21c0.55,0 1.05,-0.22 1.41,-0.59 0.37,-0.36 0.59,-0.86 0.59,-1.41v-2l-4,4h2zM9.29,21h2.83L21,12.12L21,9.29L9.29,21z"/>
</vector>
+11 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ SPDX-FileCopyrightText: 2020, microG Project Team
  ~ SPDX-License-Identifier: Apache-2.0
  -->

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="foreground_service_notification_title">Im Hintergrund aktiv</string>
    <string name="foreground_service_notification_text"><xliff:g example="Exposure Notification">%1$s</xliff:g> läuft im Hintergrund weiter.</string>
    <string name="foreground_service_notification_big_text">Füge <xliff:g example="microG Services Core">%1$s</xliff:g> als Ausnahme zur Batterie-Optimierung hinzu oder verstecke diese Benachrichtigung in den Systemeinstelleungen.</string>
</resources>
+11 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ SPDX-FileCopyrightText: 2020, microG Project Team
  ~ SPDX-License-Identifier: Apache-2.0
  -->

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <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 Core">%1$s</xliff:g> from battery optimizations or change notification settings to hide this notification.</string>
</resources>
Loading