mirror of
https://github.com/TeamVanced/VancedMicroG
synced 2024-09-05 14:06:18 +02:00
Use new method for lifting idle restrictions temporarily when receiving PUSH messages
This commit is contained in:
parent
6ca53537ee
commit
61582b63d0
@ -54,6 +54,7 @@ import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_ERROR;
|
|||||||
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_READY;
|
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_READY;
|
||||||
import static org.microg.gms.gcm.McsConstants.MSG_TEARDOWN;
|
import static org.microg.gms.gcm.McsConstants.MSG_TEARDOWN;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.app.AlarmManager;
|
import android.app.AlarmManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
@ -77,6 +78,8 @@ import android.os.SystemClock;
|
|||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.legacy.content.WakefulBroadcastReceiver;
|
import androidx.legacy.content.WakefulBroadcastReceiver;
|
||||||
|
|
||||||
import com.mgoogle.android.gms.R;
|
import com.mgoogle.android.gms.R;
|
||||||
@ -159,9 +162,18 @@ public class McsService extends Service implements Handler.Callback {
|
|||||||
|
|
||||||
private static final int maxTtl = 24 * 60 * 60;
|
private static final int maxTtl = 24 * 60 * 60;
|
||||||
|
|
||||||
private Object deviceIdleController;
|
@Nullable
|
||||||
private Method getUserIdMethod;
|
private Method getUserIdMethod;
|
||||||
|
@Nullable
|
||||||
|
private Object deviceIdleController;
|
||||||
|
@Nullable
|
||||||
private Method addPowerSaveTempWhitelistAppMethod;
|
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 {
|
private class HandlerThread extends Thread {
|
||||||
|
|
||||||
@ -190,6 +202,7 @@ public class McsService extends Service implements Handler.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressLint("PrivateApi")
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
TriggerReceiver.register(this);
|
TriggerReceiver.register(this);
|
||||||
@ -199,20 +212,27 @@ public class McsService extends Service implements Handler.Callback {
|
|||||||
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission("android.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST") == PackageManager.PERMISSION_GRANTED) {
|
||||||
try {
|
try {
|
||||||
String deviceIdleControllerName = "deviceidle";
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
try {
|
Class<?> powerExemptionManagerClass = Class.forName("android.os.PowerExemptionManager");
|
||||||
Field field = Context.class.getField("DEVICE_IDLE_CONTROLLER");
|
powerExemptionManager = getSystemService(powerExemptionManagerClass);
|
||||||
deviceIdleControllerName = (String) field.get(null);
|
addToTemporaryAllowListMethod =
|
||||||
} catch (Exception ignored) {
|
powerExemptionManagerClass.getMethod("addToTemporaryAllowList", String.class, int.class, String.class, long.class);
|
||||||
}
|
} else {
|
||||||
IBinder binder = (IBinder) Class.forName("android.os.ServiceManager")
|
String deviceIdleControllerName = "deviceidle";
|
||||||
.getMethod("getService", String.class).invoke(null, deviceIdleControllerName);
|
try {
|
||||||
if (binder != null) {
|
Field field = Context.class.getField("DEVICE_IDLE_CONTROLLER");
|
||||||
deviceIdleController = Class.forName("android.os.IDeviceIdleController$Stub")
|
deviceIdleControllerName = (String) field.get(null);
|
||||||
.getMethod("asInterface", IBinder.class).invoke(null, binder);
|
} catch (Exception ignored) {
|
||||||
getUserIdMethod = UserHandle.class.getMethod("getUserId", int.class);
|
}
|
||||||
addPowerSaveTempWhitelistAppMethod = deviceIdleController.getClass()
|
IBinder binder = (IBinder) Class.forName("android.os.ServiceManager")
|
||||||
.getMethod("addPowerSaveTempWhitelistApp", String.class, long.class, int.class, String.class);
|
.getMethod("getService", String.class).invoke(null, deviceIdleControllerName);
|
||||||
|
if (binder != null) {
|
||||||
|
deviceIdleController = Class.forName("android.os.IDeviceIdleController$Stub")
|
||||||
|
.getMethod("asInterface", IBinder.class).invoke(null, binder);
|
||||||
|
getUserIdMethod = UserHandle.class.getMethod("getUserId", int.class);
|
||||||
|
addPowerSaveTempWhitelistAppMethod = deviceIdleController.getClass()
|
||||||
|
.getMethod("addPowerSaveTempWhitelistApp", String.class, long.class, int.class, String.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.w(TAG, e);
|
Log.w(TAG, e);
|
||||||
@ -609,7 +629,16 @@ public class McsService extends Service implements Handler.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addPowerSaveTempWhitelistApp(String packageName) {
|
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 {
|
try {
|
||||||
if (getUserIdMethod != null && addPowerSaveTempWhitelistAppMethod != null && deviceIdleController != null) {
|
if (getUserIdMethod != null && addPowerSaveTempWhitelistAppMethod != null && deviceIdleController != null) {
|
||||||
int userId = (int) getUserIdMethod.invoke(null, getPackageManager().getApplicationInfo(packageName, 0).uid);
|
int userId = (int) getUserIdMethod.invoke(null, getPackageManager().getApplicationInfo(packageName, 0).uid);
|
||||||
|
Loading…
Reference in New Issue
Block a user