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

Commit 3e733135 authored by Torsten Grote's avatar Torsten Grote Committed by Marvin W.
Browse files

Use new method for lifting idle restrictions temporarily when receiving PUSH messages

The new method only gets used on Android 12 and above.

https: //cs.android.com/android/_/android/platform/frameworks/base/+/1aa89de1cfec5b7c1197d2f6c2b33dffda8bd3f5
Change-Id: Iaa501a862de061ecf78aef25d66e35ed94104406
parent 32199c81
Loading
Loading
Loading
Loading
+45 −16
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package org.microg.gms.gcm;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
@@ -39,6 +40,8 @@ import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.legacy.content.WakefulBroadcastReceiver;

import com.google.android.gms.R;
@@ -155,9 +158,18 @@ public class McsService extends Service implements Handler.Callback {

    private static int maxTtl = 24 * 60 * 60;

    private Object deviceIdleController;
    @Nullable
    private Method getUserIdMethod;
    @Nullable
    private Object deviceIdleController;
    @Nullable
    private Method addPowerSaveTempWhitelistAppMethod;
    @Nullable
    @RequiresApi(Build.VERSION_CODES.S)
    private Object powerExemptionManager;
    @Nullable
    @RequiresApi(Build.VERSION_CODES.S)
    private Method addToTemporaryAllowListMethod;

    private class HandlerThread extends Thread {

@@ -186,6 +198,7 @@ public class McsService extends Service implements Handler.Callback {
    }

    @Override
    @SuppressLint("PrivateApi")
    public void onCreate() {
        super.onCreate();
        TriggerReceiver.register(this);
@@ -195,6 +208,12 @@ public class McsService extends Service implements Handler.Callback {
        powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission("android.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST") == PackageManager.PERMISSION_GRANTED) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    Class<?> powerExemptionManagerClass = Class.forName("android.os.PowerExemptionManager");
                    powerExemptionManager = getSystemService(powerExemptionManagerClass);
                    addToTemporaryAllowListMethod =
                            powerExemptionManagerClass.getMethod("addToTemporaryAllowList", String.class, int.class, String.class, long.class);
                } else {
                    String deviceIdleControllerName = "deviceidle";
                    try {
                        Field field = Context.class.getField("DEVICE_IDLE_CONTROLLER");
@@ -210,6 +229,7 @@ public class McsService extends Service implements Handler.Callback {
                        addPowerSaveTempWhitelistAppMethod = deviceIdleController.getClass()
                                .getMethod("addPowerSaveTempWhitelistApp", String.class, long.class, int.class, String.class);
                    }
                }
            } catch (Exception e) {
                Log.w(TAG, e);
            }
@@ -594,7 +614,16 @@ public class McsService extends Service implements Handler.Callback {
    }

    private void addPowerSaveTempWhitelistApp(String packageName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            try {
                if (addToTemporaryAllowListMethod != null && powerExemptionManager != null) {
                    logd(this, "Adding app " + packageName + " to the temp allowlist");
                    addToTemporaryAllowListMethod.invoke(powerExemptionManager, packageName, 0, "GCM Push", 10000);
                }
            } catch (Exception e) {
                Log.e(TAG, "Error adding app" + packageName + " to the temp allowlist.", e);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                if (getUserIdMethod != null && addPowerSaveTempWhitelistAppMethod != null && deviceIdleController != null) {
                    int userId = (int) getUserIdMethod.invoke(null, getPackageManager().getApplicationInfo(packageName, 0).uid);