Massive improvement of Magisk Manager repackaging

This commit is contained in:
topjohnwu 2017-12-19 20:59:59 +08:00
parent 91337218b3
commit 543f435b1e
8 changed files with 61 additions and 42 deletions

View File

@ -111,8 +111,8 @@ public class MagiskManager extends Application {
if (getPackageName().equals(Const.ORIG_PKG_NAME)) {
String pkg = suDB.getStrings(Const.Key.SU_REQUESTER, null);
if (pkg != null) {
suDB.setStrings(Const.Key.SU_REQUESTER, null);
Utils.uninstallPkg(pkg);
suDB.setStrings(Const.Key.SU_REQUESTER, null);
}
}

View File

@ -148,7 +148,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
hideManager.setOnPreferenceClickListener((pref) -> {
Utils.runWithPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE,
() -> new HideManager().exec());
() -> new HideManager(getActivity()).exec());
return true;
});
} else {

View File

@ -1,5 +1,7 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.app.ProgressDialog;
import android.widget.Toast;
import com.topjohnwu.crypto.JarMap;
@ -18,6 +20,12 @@ import java.util.jar.JarEntry;
public class HideManager extends ParallelTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public HideManager(Activity activity) {
super(activity);
}
private String genPackageName(String prefix, int length) {
StringBuilder builder = new StringBuilder(length);
builder.append(prefix);
@ -86,8 +94,9 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
MagiskManager.toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT);
MagiskManager.toast(R.string.hide_manager_toast2, Toast.LENGTH_LONG);
dialog = ProgressDialog.show(getActivity(),
getActivity().getString(R.string.hide_manager_toast),
getActivity().getString(R.string.hide_manager_toast2));
}
@Override
@ -136,6 +145,7 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
@Override
protected void onPostExecute(Boolean b) {
dialog.dismiss();
if (!b) {
MagiskManager.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
}

View File

@ -4,7 +4,6 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;

View File

@ -12,11 +12,11 @@ import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
public abstract class DownloadReceiver extends BroadcastReceiver {
public String mFilename;
long downloadID;
import java.io.File;
public DownloadReceiver() {}
public abstract class DownloadReceiver extends BroadcastReceiver {
protected File mFile;
private long downloadID;
@Override
public void onReceive(Context context, Intent intent) {
@ -45,12 +45,14 @@ public abstract class DownloadReceiver extends BroadcastReceiver {
Utils.isDownloading = false;
}
public void setDownloadID(long id) {
public DownloadReceiver setDownloadID(long id) {
downloadID = id;
return this;
}
public void setFilename(String filename) {
mFilename = filename;
public DownloadReceiver setFile(File file) {
mFile = file;
return this;
}
public abstract void onDownloadDone(Uri uri);

View File

@ -8,6 +8,7 @@ import android.os.Build;
import android.support.v4.content.FileProvider;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import java.io.File;
@ -20,7 +21,16 @@ public class ManagerUpdate extends BroadcastReceiver {
new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
if (Shell.rootAccess()) {
Shell.su(Utils.fmt("pm install -r %s", mFile));
if (!context.getPackageName().equals(Const.ORIG_PKG_NAME)) {
Utils.dumpPrefs();
Shell.su(Utils.fmt("rm -rf /data/user*/*/%s/*", Const.ORIG_PKG_NAME));
Intent intent = context.getPackageManager().getLaunchIntentForPackage(Const.ORIG_PKG_NAME);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
@ -36,9 +46,10 @@ public class ManagerUpdate extends BroadcastReceiver {
context.startActivity(install);
}
}
}
},
intent.getStringExtra(Const.Key.INTENT_SET_LINK),
Utils.getLegalFilename("MagiskManager-v" +
intent.getStringExtra(Const.Key.INTENT_SET_VERSION) + ".apk"));
Utils.fmt("MagiskManager-v%s.apk", intent.getStringExtra(Const.Key.INTENT_SET_VERSION))
);
}
}

View File

@ -98,7 +98,7 @@ public class ShowUI {
public static void magiskInstallDialog(Activity activity, boolean enc, boolean verity) {
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.getLegalFilename("Magisk-v" + mm.remoteMagiskVersionString + ".zip");
String filename = Utils.fmt("Magisk-v%s.zip", mm.remoteMagiskVersionString);
new AlertDialogBuilder(activity)
.setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.magisk)))
.setMessage(mm.getString(R.string.repo_install_msg, filename))
@ -236,8 +236,7 @@ public class ShowUI {
new AlertDialogBuilder(activity)
.setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.app_name)))
.setMessage(mm.getString(R.string.repo_install_msg,
Utils.getLegalFilename("MagiskManager-v" +
mm.remoteManagerVersionString + ".apk")))
Utils.fmt("MagiskManager-v%s.apk", mm.remoteManagerVersionString)))
.setCancelable(true)
.setPositiveButton(R.string.install, (d, i) -> {
Utils.dumpPrefs();

View File

@ -68,7 +68,7 @@ public class Utils {
}
public static void uninstallPkg(String pkg) {
Shell.su_raw(fmt("find /data/user*/*/%s -exec umount -l {} 2>/dev/null \\;; pm uninstall %s", pkg, pkg));
Shell.su(fmt("umount -l /data/user*/*/%s/*/*.db 2>/dev/null; pm uninstall %s", pkg, pkg));
}
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
@ -76,7 +76,7 @@ public class Utils {
return;
runWithPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> {
File file = new File(Const.EXTERNAL_PATH, filename);
File file = new File(Const.EXTERNAL_PATH, getLegalFilename(filename));
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
|| (file.exists() && !file.delete())) {
@ -87,14 +87,12 @@ public class Utils {
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
isDownloading = true;
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager
.Request(Uri.parse(link))
.setDestinationUri(Uri.fromFile(file));
if (link != null) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
request.setDestinationUri(Uri.fromFile(file));
receiver.setDownloadID(downloadManager.enqueue(request));
}
receiver.setFilename(filename);
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
receiver.setDownloadID(dm.enqueue(request)).setFile(file);
context.getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
});
}
@ -273,7 +271,6 @@ public class Utils {
String config = fmt("/data/user/%d/%s", Const.USER_ID, Const.MANAGER_CONFIGS);
List<String> ret = readFile(config);
if (isValidShellResponse(ret)) {
removeItem(config);
SharedPreferences.Editor editor = MagiskManager.get().prefs.edit();
String json = ret.get(0);
Gson gson = new Gson();
@ -285,13 +282,14 @@ public class Utils {
editor.putString(entry.getKey(), (String) value);
} else if (value instanceof Boolean) {
editor.putBoolean(entry.getKey(), (boolean) value);
} else if (value instanceof Integer) {
editor.putInt(entry.getKey(), (int) value);
} else if (value instanceof Number) {
editor.putInt(entry.getKey(), ((Number) value).intValue());
}
}
editor.remove(Const.Key.ETAG_KEY);
editor.apply();
MagiskManager.get().loadConfig();
removeItem(config);
}
}