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)) { if (getPackageName().equals(Const.ORIG_PKG_NAME)) {
String pkg = suDB.getStrings(Const.Key.SU_REQUESTER, null); String pkg = suDB.getStrings(Const.Key.SU_REQUESTER, null);
if (pkg != null) { if (pkg != null) {
suDB.setStrings(Const.Key.SU_REQUESTER, null);
Utils.uninstallPkg(pkg); 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) -> { hideManager.setOnPreferenceClickListener((pref) -> {
Utils.runWithPermission(getActivity(), Utils.runWithPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
() -> new HideManager().exec()); () -> new HideManager(getActivity()).exec());
return true; return true;
}); });
} else { } else {

View File

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

View File

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

View File

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

View File

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

View File

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