Skip to content

Commit

Permalink
[BatchOps] Add kill process support
Browse files Browse the repository at this point in the history
  • Loading branch information
MuntashirAkon committed Jul 3, 2020
1 parent 873abb7 commit c2b6afa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,13 @@ protected void onCreate(Bundle savedInstanceState) {
case R.id.action_disable_background:
handleBatchOp(BatchOpsManager.OP_DISABLE_BACKGROUND, R.string.alert_failed_to_disable_background);
return true;
case R.id.action_kill_process:
handleBatchOp(BatchOpsManager.OP_KILL, R.string.alert_failed_to_kill);
return true;
case R.id.action_uninstall:
handleBatchOp(BatchOpsManager.OP_UNINSTALL, R.string.alert_failed_to_uninstall);
return true;
case R.id.action_backup_apk:
case R.id.action_kill_process:
case R.id.action_backup_data:
case R.id.action_export_blocking_data:
Toast.makeText(this, "This operation is not supported yet.", Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.github.muntashirakon.AppManager.appops.AppOpsManager;
import io.github.muntashirakon.AppManager.runner.Runner;
import io.github.muntashirakon.AppManager.storage.StorageManager;
Expand Down Expand Up @@ -42,13 +41,11 @@ public BatchOpsManager(Context context) {
}

private List<String> packageNames;
private @OpType int op;
private Result lastResult;

@NonNull
public Result performOp(@OpType int op, List<String> packageNames) {
this.runner.clear();
this.op = op;
this.packageNames = packageNames;
switch (op) {
case OP_BACKUP_APK: // TODO
Expand All @@ -58,16 +55,11 @@ public Result performOp(@OpType int op, List<String> packageNames) {
case OP_DISABLE: return opDisable();
case OP_DISABLE_BACKGROUND: return opDisableBackground();
case OP_EXPORT_RULES: // TODO
case OP_KILL: // TODO
break;
case OP_KILL: return opKill();
case OP_UNINSTALL: return opUninstall();
}
lastResult = new Result() {
@Override
public int getOp() {
return op;
}

@Override
public boolean isSuccessful() {
return false;
Expand All @@ -86,9 +78,9 @@ public Result getLastResult() {
}

@NonNull
private Result opUninstall() {
private Result opClearData() {
for(String packageName: packageNames) {
addCommand(packageName, String.format(Locale.ROOT, "pm uninstall --user 0 %s", packageName));
addCommand(packageName, String.format(Locale.ROOT, "pm clear %s", packageName));
}
return runOpAndFetchResults();
}
Expand Down Expand Up @@ -119,26 +111,41 @@ private Result opDisableBackground() {
}

@NonNull
private Result opClearData() {
private Result opKill() {
for (String packageName : packageNames) {
addCommand(packageName, String.format(Locale.ROOT, "pidof %s", packageName), false);
}
Result result = runOpAndFetchResults();
List<String> pidOrPackageNames = result.failedPackages();
runner.clear();
for (int i = 0; i<packageNames.size(); ++i) {
if (!pidOrPackageNames.get(i).equals(packageNames.get(i))) {
addCommand(packageNames.get(i), String.format(Locale.ROOT, "kill -9 %s", pidOrPackageNames.get(i)));
}
}
return runOpAndFetchResults();
}

@NonNull
private Result opUninstall() {
for(String packageName: packageNames) {
addCommand(packageName, String.format(Locale.ROOT, "pm clear %s", packageName));
addCommand(packageName, String.format(Locale.ROOT, "pm uninstall --user 0 %s", packageName));
}
return runOpAndFetchResults();
}

private void addCommand(String packageName, String command) {
runner.addCommand(String.format(Locale.ROOT, "%s > /dev/null 2>&1 || echo %s", command, packageName));
addCommand(packageName, command, true);
}

private void addCommand(String packageName, String command, boolean isDevNull) {
runner.addCommand(String.format(Locale.ROOT, "%s %s || echo %s", command, isDevNull ? "> /dev/null 2>&1" : "", packageName));
}

@NonNull
private Result runOpAndFetchResults() {
Runner.Result result = runner.run();
lastResult = new Result() {
@Override
public int getOp() {
return op;
}

@Override
public boolean isSuccessful() {
return TextUtils.isEmpty(result.getOutput());
Expand All @@ -153,7 +160,6 @@ public List<String> failedPackages() {
}

public interface Result {
@OpType int getOp();
boolean isSuccessful();
List<String> failedPackages();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,5 @@
<string name="alert_failed_to_uninstall">Failed to uninstall some packages</string>
<string name="alert_failed_to_disable_background">Failed to disable run in background for some packages</string>
<string name="the_operation_was_successful">The operation was successful</string>
<string name="alert_failed_to_kill">Failed to kill some processes</string>
</resources>
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Fri Jul 03 17:29:47 BDT 2020
#Fri Jul 03 20:25:18 BDT 2020
VERSION_NAME=2.5.6
VERSION_CODE=231
VERSION_CODE=232

0 comments on commit c2b6afa

Please sign in to comment.