Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

New stuff #184

Merged
merged 2 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.1'
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath "com.squareup.wire:wire-gradle-plugin:3.2.2"
}
Expand Down
8 changes: 4 additions & 4 deletions play-services-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ dependencies {

implementation "org.microg.gms:conscrypt-gmscore:2.5.1"
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'androidx.lifecycle:lifecycle-service:2.4.0'
implementation 'androidx.lifecycle:lifecycle-service:2.4.1'

// Navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'

implementation 'androidx.lifecycle:lifecycle-service:2.4.0'
implementation 'androidx.lifecycle:lifecycle-service:2.4.1'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10'
implementation 'com.android.volley:volley:1.2.1'

Expand Down
61 changes: 45 additions & 16 deletions play-services-core/src/main/java/org/microg/gms/gcm/McsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_READY;
import static org.microg.gms.gcm.McsConstants.MSG_TEARDOWN;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
Expand All @@ -77,6 +78,8 @@
import android.os.UserHandle;
import android.util.Log;

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

import com.mgoogle.android.gms.R;
Expand Down Expand Up @@ -159,9 +162,18 @@ public class McsService extends Service implements Handler.Callback {

private static final 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 {

Expand Down Expand Up @@ -190,6 +202,7 @@ private static void logd(Context context, String msg) {
}

@Override
@SuppressLint("PrivateApi")
public void onCreate() {
super.onCreate();
TriggerReceiver.register(this);
Expand All @@ -199,20 +212,27 @@ public void onCreate() {
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 {
String deviceIdleControllerName = "deviceidle";
try {
Field field = Context.class.getField("DEVICE_IDLE_CONTROLLER");
deviceIdleControllerName = (String) field.get(null);
} catch (Exception ignored) {
}
IBinder binder = (IBinder) Class.forName("android.os.ServiceManager")
.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);
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");
deviceIdleControllerName = (String) field.get(null);
} catch (Exception ignored) {
}
IBinder binder = (IBinder) Class.forName("android.os.ServiceManager")
.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) {
Log.w(TAG, e);
Expand Down Expand Up @@ -609,7 +629,16 @@ private void handleAppMessage(DataMessageStanza msg) {
}

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);
Expand Down