New uninstallation method

This commit is contained in:
topjohnwu
2018-06-27 05:58:56 +08:00
parent c8f65fc9a1
commit e86015badc
34 changed files with 97 additions and 101 deletions
@@ -3,6 +3,7 @@ package com.topjohnwu.magisk;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
@@ -17,6 +18,7 @@ import com.topjohnwu.magisk.asyncs.FlashZip;
import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.superuser.CallbackList;
import com.topjohnwu.superuser.Shell;
@@ -113,6 +115,9 @@ public class FlashActivity extends Activity {
case Const.Value.FLASH_ZIP:
new FlashZip(this, uri, console, logs).exec();
break;
case Const.Value.UNINSTALL:
new UninstallMagisk(this, uri, console, logs).exec();
break;
case Const.Value.FLASH_MAGISK:
new InstallMagisk(this, console, logs, uri, InstallMagisk.DIRECT_MODE).exec();
break;
@@ -130,4 +135,21 @@ public class FlashActivity extends Activity {
public void onBackPressed() {
// Prevent user accidentally press back button
}
private static class UninstallMagisk extends FlashZip {
private UninstallMagisk(Activity context, Uri uri, List<String> console, List<String> logs) {
super(context, uri, console, logs);
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
new Handler().postDelayed(() ->
RootUtils.uninstallPkg(getActivity().getPackageName()), 3000);
} else {
super.onPostExecute(result);
}
}
}
}
@@ -54,11 +54,14 @@ public class MagiskManager extends Application implements Shell.Container {
public int magiskVersionCode = -1;
public String remoteMagiskVersionString;
public int remoteMagiskVersionCode = -1;
public String magiskLink;
public String releaseNoteLink;
public String remoteManagerVersionString;
public int remoteManagerVersionCode = -1;
public String magiskLink;
public String releaseNoteLink;
public String managerLink;
public String uninstallerLink;
public boolean keepVerity = false;
public boolean keepEnc = false;
@@ -47,6 +47,8 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
mm.remoteManagerVersionString = manager.getString("version");
mm.remoteManagerVersionCode = manager.getInt("versionCode");
mm.managerLink = manager.getString("link");
JSONObject uninstaller = json.getJSONObject("uninstaller");
mm.uninstallerLink = uninstaller.getString("link");
} catch (JSONException ignored) {}
return null;
}
@@ -40,8 +40,7 @@ public class FlashZip extends ParallelTask<Void, Void, Integer> {
private boolean unzipAndCheck() throws Exception {
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android", true);
String s = ShellUtils.fastCmd("head -n 1 " + new File(mCachedFile.getParentFile(), "updater-script"));
return s != null && s.contains("#MAGISK");
return ShellUtils.fastCmdResult("grep -q '#MAGISK' " + new File(mCachedFile.getParentFile(), "updater-script"));
}
@Override
@@ -26,7 +26,6 @@ import org.kamranzafar.jtar.TarOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -73,8 +72,8 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
if (mode == FIX_ENV_MODE) {
dialog = ProgressDialog.show(getActivity(),
"Additional Setup", "Running environment setup...");
Activity a = getActivity();
dialog = ProgressDialog.show(a, a.getString(R.string.setup_title), a.getString(R.string.setup_msg));
console = new NOPList<>();
}
}
@@ -1,5 +1,7 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.app.ProgressDialog;
import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager;
@@ -8,22 +10,26 @@ import com.topjohnwu.superuser.ShellUtils;
public class RestoreImages extends ParallelTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public RestoreImages(Activity activity) {
super(activity);
}
@Override
protected void onPreExecute() {
Activity a = getActivity();
dialog = ProgressDialog.show(a, a.getString(R.string.restore_img_title), a.getString(R.string.restore_img_msg));
}
@Override
protected Boolean doInBackground(Void... voids) {
String sha1;
sha1 = ShellUtils.fastCmd("cat /.backup/.sha1");
if (sha1 == null) {
sha1 = ShellUtils.fastCmd("cat /init.magisk.rc | grep STOCKSHA1");
if (sha1 == null)
return false;
sha1 = sha1.substring(sha1.indexOf('=') + 1);
}
return ShellUtils.fastCmdResult("restore_imgs " + sha1);
return ShellUtils.fastCmdResult("restore_imgs");
}
@Override
protected void onPostExecute(Boolean result) {
dialog.cancel();
if (result) {
MagiskManager.toast(R.string.restore_done, Toast.LENGTH_SHORT);
} else {
@@ -15,7 +15,7 @@ public class ShellInitializer extends Shell.Initializer {
@Override
public boolean onRootShellInit(Context context, @NonNull Shell shell) throws Exception {
BusyBox.BB_PATH = new File(Const.BUSYBOX_PATH);
try (InputStream magiskUtils = context.getAssets().open(Const.UTIL_FUNCTIONS);
try (InputStream magiskUtils = context.getResources().openRawResource(R.raw.util_functions);
InputStream managerUtils = context.getResources().openRawResource(R.raw.utils)
) {
shell.loadInputStream(null, null, magiskUtils);
@@ -7,10 +7,10 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.Toast;
import com.topjohnwu.magisk.FlashActivity;
@@ -27,9 +27,6 @@ import com.topjohnwu.magisk.receivers.RebootReceiver;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@@ -249,40 +246,23 @@ public class ShowUI {
public static void uninstallDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity);
new AlertDialogBuilder(activity)
AlertDialog.Builder b = new AlertDialogBuilder(activity)
.setTitle(R.string.uninstall_magisk_title)
.setMessage(R.string.uninstall_magisk_msg)
.setPositiveButton(R.string.complete_uninstall, (d, i) -> {
ByteArrayOutputStream uninstaller = new ByteArrayOutputStream();
try (InputStream in = mm.getAssets().open(Const.UNINSTALLER)) {
ShellUtils.pump(in, uninstaller);
} catch (IOException e) {
e.printStackTrace();
return;
}
ByteArrayOutputStream utils = new ByteArrayOutputStream();
try (InputStream in = mm.getAssets().open(Const.UTIL_FUNCTIONS)) {
ShellUtils.pump(in, utils);
} catch (IOException e) {
e.printStackTrace();
return;
}
Shell.Sync.su(
Utils.fmt("echo '%s' > /cache/%s", uninstaller.toString().replace("'", "'\\''"), Const.UNINSTALLER),
Utils.fmt("echo '%s' > %s/%s", utils.toString().replace("'", "'\\''"),
mm.magiskVersionCode >= Const.MAGISK_VER.HIDDEN_PATH ? "/data/adb/magisk" : "/data/magisk", Const.UTIL_FUNCTIONS)
);
try {
uninstaller.close();
utils.close();
} catch (IOException ignored) {}
MagiskManager.toast(R.string.uninstall_toast, Toast.LENGTH_LONG);
new Handler().postDelayed(() -> RootUtils.uninstallPkg(mm.getPackageName()), 5000);
})
.setNeutralButton(R.string.restore_img, (d, i) -> new RestoreImages().exec())
.setNegativeButton(R.string.uninstall_app, (d, i) -> RootUtils.uninstallPkg(mm.getPackageName()))
.show();
.setNeutralButton(R.string.restore_img, (d, i) -> new RestoreImages(activity).exec());
if (!TextUtils.isEmpty(mm.uninstallerLink)) {
b.setPositiveButton(R.string.complete_uninstall, (d, i) ->
Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(context, FlashActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL);
context.startActivity(intent);
}
}, mm.uninstallerLink, "magisk-uninstaller.zip"));
}
b.show();
}
}