-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06e6b30
commit 1f2de3a
Showing
10 changed files
with
150 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
sample/src/main/java/com/xdandroid/sample/misc/BootReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.xdandroid.sample.misc; | ||
|
||
import android.content.*; | ||
|
||
/** | ||
* uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" | ||
* action android:name="android.intent.action.BOOT_COMPLETED" | ||
*/ | ||
public class BootReceiver extends BroadcastReceiver { | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
context = context.getApplicationContext(); | ||
Context c = context; | ||
new Thread(() -> { | ||
Intent revokeIntent = new Intent(c, RevokeActivity.class); | ||
revokeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
c.startActivity(revokeIntent); | ||
try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } | ||
Intent serverIntent = new Intent(); | ||
serverIntent.setComponent(new ComponentName("com.xdandroid.server", "com.xdandroid.server.TargetActivity")); | ||
serverIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
c.startActivity(serverIntent); | ||
}).start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
sample/src/main/java/com/xdandroid/sample/misc/ShellUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.xdandroid.sample.misc; | ||
|
||
import java.io.*; | ||
|
||
interface ShellUtils { | ||
|
||
String COMMAND_SU = "su"; | ||
String COMMAND_SH = "sh"; | ||
String COMMAND_EXIT = "exit\n"; | ||
String COMMAND_LINE_END = "\n"; | ||
|
||
default CommandResult execCommand(String[] commands, boolean isRoot) { | ||
int result = -1; | ||
if (commands == null || commands.length == 0) return new CommandResult(result, null, null); | ||
Process process = null; | ||
BufferedReader successResult = null; | ||
BufferedReader errorResult = null; | ||
StringBuilder successMsg = null; | ||
StringBuilder errorMsg = null; | ||
DataOutputStream os = null; | ||
try { | ||
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); | ||
os = new DataOutputStream(process.getOutputStream()); | ||
for (String command : commands) { | ||
if (command == null) continue; | ||
os.write(command.getBytes()); | ||
os.writeBytes(COMMAND_LINE_END); | ||
os.flush(); | ||
} | ||
os.writeBytes(COMMAND_EXIT); | ||
os.flush(); | ||
result = process.waitFor(); | ||
successMsg = new StringBuilder(); | ||
errorMsg = new StringBuilder(); | ||
successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); | ||
String s; | ||
while ((s = successResult.readLine()) != null) successMsg.append(s); | ||
while ((s = errorResult.readLine()) != null) errorMsg.append(s); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (os != null) os.close(); | ||
if (successResult != null) successResult.close(); | ||
if (errorResult != null) errorResult.close(); | ||
} catch (IOException e) { e.printStackTrace(); } | ||
if (process != null) process.destroy(); | ||
} | ||
return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); | ||
} | ||
|
||
class CommandResult { | ||
|
||
int result; | ||
String successMsg; | ||
String errorMsg; | ||
|
||
CommandResult(int result, String successMsg, String errorMsg) { | ||
this.result = result; | ||
this.successMsg = successMsg; | ||
this.errorMsg = errorMsg; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
sample/src/main/java/com/xdandroid/sample/misc/server/TargetActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.xdandroid.sample.misc.server; | ||
|
||
import android.app.*; | ||
|
||
/** | ||
* coreApp="true" | ||
* android:sharedUserId="android.uid.system" | ||
* android:process="system" | ||
* android:theme="@android:style/Theme.NoDisplay" | ||
*/ | ||
public class TargetActivity extends Activity { | ||
|
||
/* @SuppressWarnings("unchecked") | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return; | ||
new Thread(() -> { | ||
try { | ||
IPackageManager pm = (IPackageManager) ServiceManager.getService("package"); | ||
Field packagesField = pm.getClass().getDeclaredField("mPackages"); | ||
packagesField.setAccessible(true); | ||
final Field[] appInfoField = {null}; | ||
((ArrayMap<String, ?>) packagesField.get(pm)) | ||
.values() | ||
.stream() | ||
.map(pkg -> { | ||
try { | ||
if (appInfoField[0] == null) { | ||
appInfoField[0] = pkg.getClass().getDeclaredField("applicationInfo"); | ||
appInfoField[0].setAccessible(true); | ||
} | ||
return (ApplicationInfo) appInfoField[0].get(pkg); | ||
} catch (Exception e) { return null; } | ||
}) | ||
.filter(Objects::nonNull) | ||
.forEach(appInfo -> { | ||
if (appInfo.targetSdkVersion >= Build.VERSION_CODES.M) appInfo.targetSdkVersion = Build.VERSION.SDK_INT; | ||
if (appInfo.targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) appInfo.targetSdkVersion = Build.VERSION_CODES.LOLLIPOP_MR1; | ||
}); | ||
} catch (Exception e) { e.printStackTrace(); } | ||
}).start(); | ||
finish(); | ||
}*/ | ||
} |