Temporary whitelist support for Doze (#732)

Fixes #338 

Co-Authored-By: @ccaapton <6211551+ccaapton@users.noreply.github.com>
Co-Authored-By: @ale5000-git <15793015+ale5000-git@users.noreply.github.com>
This commit is contained in:
voidstarstar 2019-04-21 10:39:18 -04:00 committed by Marvin W
parent e7f37e17a8
commit 60023887d1
2 changed files with 27 additions and 4 deletions

View File

@ -90,6 +90,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST" tools:ignore="ProtectedPermissions"/>
<uses-sdk tools:overrideLibrary="com.takisoft.fix.support.v7.preference,android.support.graphics.drawable.animated,android.arch.lifecycle,android.arch.lifecycle.livedata.core,android.arch.lifecycle.viewmodel,android.arch.core"/>

View File

@ -35,6 +35,7 @@ import android.os.Messenger;
import android.os.Parcelable;
import android.os.PowerManager;
import android.os.SystemClock;
import android.os.UserHandle;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
@ -52,6 +53,7 @@ import org.microg.gms.gcm.mcs.LoginResponse;
import org.microg.gms.gcm.mcs.Setting;
import java.io.Closeable;
import java.lang.reflect.Field;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
@ -452,12 +454,13 @@ public class McsService extends Service implements Handler.Callback {
}
private void handleAppMessage(DataMessageStanza msg) {
database.noteAppMessage(msg.category, msg.getSerializedSize());
GcmDatabase.App app = database.getApp(msg.category);
String packageName = msg.category;
database.noteAppMessage(packageName, msg.getSerializedSize());
GcmDatabase.App app = database.getApp(packageName);
Intent intent = new Intent();
intent.setAction(ACTION_C2DM_RECEIVE);
intent.setPackage(msg.category);
intent.setPackage(packageName);
intent.putExtra(EXTRA_FROM, msg.from);
intent.putExtra(EXTRA_MESSAGE_ID, msg.id);
if (app.wakeForDelivery) {
@ -472,7 +475,7 @@ public class McsService extends Service implements Handler.Callback {
String receiverPermission;
try {
String name = msg.category + ".permission.C2D_MESSAGE";
String name = packageName + ".permission.C2D_MESSAGE";
getPackageManager().getPermissionInfo(name, 0);
receiverPermission = name;
} catch (PackageManager.NameNotFoundException e) {
@ -486,6 +489,25 @@ public class McsService extends Service implements Handler.Callback {
for (ResolveInfo resolveInfo : infos) {
logd("Target: " + resolveInfo);
Intent targetIntent = new Intent(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && app.wakeForDelivery) {
try {
Field field = Context.class.getField("DEVICE_IDLE_CONTROLLER");
IBinder binder = (IBinder) Class.forName("android.os.ServiceManager")
.getMethod("getService", String.class).invoke(null, field.get(null));
if (binder != null) {
Object deviceIdleController = Class.forName("android.os.IDeviceIdleController$Stub")
.getMethod("asInterface", IBinder.class).invoke(null, binder);
int userId = (int) UserHandle.class.getMethod("getUserId", int.class)
.invoke(null, getPackageManager().getApplicationInfo(packageName, 0).uid);
logd("Adding app " + packageName + " for userId " + userId + " to the temp whitelist");
deviceIdleController.getClass()
.getMethod("addPowerSaveTempWhitelistApp", String.class, long.class, int.class, String.class)
.invoke(deviceIdleController, packageName, 10000, userId, "GCM Push");
}
} catch (Exception e) {
Log.w(TAG, e);
}
}
targetIntent.setComponent(new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name));
sendOrderedBroadcast(targetIntent, receiverPermission);
}