diff options
author | Trumeet <liangyuteng12345@gmail.com> | 2018-11-26 16:09:42 -0800 |
---|---|---|
committer | Trumeet <liangyuteng12345@gmail.com> | 2018-11-26 16:09:42 -0800 |
commit | c94ac86ad18e8d678cd4d549c0f4db209c989bb0 (patch) | |
tree | 16c50f3217229c12eed5c37e3186a9ec4fc28dff | |
parent | a8f7cffa1329dd9d200dea2e70028b54507c58c3 (diff) | |
download | SysUIController-c94ac86ad18e8d678cd4d549c0f4db209c989bb0.tar SysUIController-c94ac86ad18e8d678cd4d549c0f4db209c989bb0.tar.gz SysUIController-c94ac86ad18e8d678cd4d549c0f4db209c989bb0.tar.bz2 SysUIController-c94ac86ad18e8d678cd4d549c0f4db209c989bb0.zip |
feat(server): use sticky broadcast to prevent starting again when UI starts
-rw-r--r-- | app/src/main/java/eu/chainfire/librootjava/IPCBroadcastHelper.java | 42 | ||||
-rw-r--r-- | app/src/main/java/moe/yuuta/sysuicontroller/core/ControllerService.java | 15 |
2 files changed, 54 insertions, 3 deletions
diff --git a/app/src/main/java/eu/chainfire/librootjava/IPCBroadcastHelper.java b/app/src/main/java/eu/chainfire/librootjava/IPCBroadcastHelper.java new file mode 100644 index 0000000..0b67ef0 --- /dev/null +++ b/app/src/main/java/eu/chainfire/librootjava/IPCBroadcastHelper.java @@ -0,0 +1,42 @@ +package eu.chainfire.librootjava; + +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import androidx.annotation.NonNull; + +/** + * A helper to create custom broadcasts + * Should be synced with {@link eu.chainfire.librootjava.RootIPC}. + */ +public class IPCBroadcastHelper { + public static Intent buildStickyBroadcastIntent (@NonNull RootIPC rootIPC) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // RootIPC.java part + Field fIBinder = rootIPC.getClass().getDeclaredField("binder"); + Field fCode = rootIPC.getClass().getDeclaredField("code"); + Field fPackageName = rootIPC.getClass().getDeclaredField("packageName"); + fIBinder.setAccessible(true); + fCode.setAccessible(true); + fPackageName.setAccessible(true); + Intent intent = new Intent(); + intent.setPackage((String) fPackageName.get(rootIPC)); + intent.setAction(RootIPCReceiver.BROADCAST_ACTION); + + Bundle bundle = new Bundle(); + bundle.putBinder(RootIPCReceiver.BROADCAST_BINDER, (IBinder) fIBinder.get(rootIPC)); + bundle.putInt(RootIPCReceiver.BROADCAST_CODE, fCode.getInt(rootIPC)); + intent.putExtra(RootIPCReceiver.BROADCAST_EXTRA, bundle); + + // Reflection.java part + Method mGetFlagReceiverFromShell = Reflection.class.getDeclaredMethod("getFlagReceiverFromShell"); + mGetFlagReceiverFromShell.setAccessible(true); + intent.setFlags((int) mGetFlagReceiverFromShell.invoke(null)); + + return intent; + } +} diff --git a/app/src/main/java/moe/yuuta/sysuicontroller/core/ControllerService.java b/app/src/main/java/moe/yuuta/sysuicontroller/core/ControllerService.java index 0574fb4..994492c 100644 --- a/app/src/main/java/moe/yuuta/sysuicontroller/core/ControllerService.java +++ b/app/src/main/java/moe/yuuta/sysuicontroller/core/ControllerService.java @@ -3,6 +3,7 @@ package moe.yuuta.sysuicontroller.core; import android.annotation.SuppressLint; import android.app.StatusBarManager; import android.content.Context; +import android.content.Intent; import android.os.Binder; import android.os.Looper; import android.os.RemoteException; @@ -15,6 +16,7 @@ import java.util.Collections; import java.util.Date; import java.util.List; +import eu.chainfire.librootjava.IPCBroadcastHelper; import eu.chainfire.librootjava.RootIPC; import eu.chainfire.librootjava.RootJava; import eu.chainfire.librootjavadaemon.RootDaemon; @@ -32,12 +34,13 @@ public class ControllerService extends IStatusController.Stub { public static final int CODE_SERVICE = 0; private StatusBarManager mManager; private Context mContext; + private Intent mKeepWakeUpIntent; public static void main (String... args) throws Throwable { new ControllerService().run(args); } - @SuppressLint("WrongConstant") + @SuppressLint({"WrongConstant", "MissingPermission"}) private void run (String... args) throws Throwable { Looper.prepare(); mContext = RootJava.getSystemContext(); @@ -46,15 +49,19 @@ public class ControllerService extends IStatusController.Stub { RootJava.restoreOriginalLdLibraryPath(); RootDaemon.register(BuildConfig.APPLICATION_ID, this, CODE_SERVICE); Log.i(TAG, "Started at " + new Date().toString()); + RootIPC rootIPC; try { - new RootIPC(BuildConfig.APPLICATION_ID, this, CODE_SERVICE, + rootIPC = new RootIPC(BuildConfig.APPLICATION_ID, this, CODE_SERVICE, 10 * 1000, true); } catch (RootIPC.TimeoutException e) { Log.e(TAG, "Unable to establish a connection, exiting", e); throw e; } + mKeepWakeUpIntent = IPCBroadcastHelper.buildStickyBroadcastIntent(rootIPC); + mContext.sendStickyBroadcast(mKeepWakeUpIntent); RootDaemon.run(); + // Will be removed from exit() binder call, because this part may not be called. Log.i(TAG, "Stopped at " + new Date().toString()); } @@ -63,9 +70,11 @@ public class ControllerService extends IStatusController.Stub { Binder.getCallingPid(), Binder.getCallingUid(), "Permission denial"); } - @Override + @SuppressLint("MissingPermission") + @Override // Binder call public void exit () throws RemoteException { enforcePermission(); + mContext.removeStickyBroadcast(mKeepWakeUpIntent); RootDaemon.exit(); } |