Some cleanups

This commit is contained in:
topjohnwu 2018-12-02 15:28:18 -05:00
parent 56a76df28e
commit 80dad54119
13 changed files with 25 additions and 36 deletions

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.os.Environment;
import android.os.Process; import android.os.Process;
import java.io.File; import java.io.File;
@ -9,7 +10,6 @@ import java.util.List;
public class Const { public class Const {
public static final String DEBUG_TAG = "MagiskManager"; public static final String DEBUG_TAG = "MagiskManager";
public static final String ORIG_PKG_NAME = BuildConfig.APPLICATION_ID;
public static final String MAGISKHIDE_PROP = "persist.magisk.hide"; public static final String MAGISKHIDE_PROP = "persist.magisk.hide";
// APK content // APK content
@ -19,11 +19,13 @@ public class Const {
// Paths // Paths
public static final String MAGISK_PATH = "/sbin/.magisk/img"; public static final String MAGISK_PATH = "/sbin/.magisk/img";
public static final File EXTERNAL_PATH;
public static File MAGISK_DISABLE_FILE; public static File MAGISK_DISABLE_FILE;
static { static {
/* Prevent crashing on unrooted devices */
MAGISK_DISABLE_FILE = new File("xxx"); MAGISK_DISABLE_FILE = new File("xxx");
EXTERNAL_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
EXTERNAL_PATH.mkdirs();
} }
public static final String BUSYBOX_PATH = "/sbin/.magisk/busybox"; public static final String BUSYBOX_PATH = "/sbin/.magisk/busybox";

View File

@ -15,7 +15,6 @@ import android.widget.Toast;
import com.topjohnwu.magisk.asyncs.FlashZip; import com.topjohnwu.magisk.asyncs.FlashZip;
import com.topjohnwu.magisk.asyncs.InstallMagisk; import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.components.BaseActivity; import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.CallbackList; import com.topjohnwu.superuser.CallbackList;
@ -59,7 +58,7 @@ public class FlashActivity extends BaseActivity {
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND)); now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
File logFile = new File(Download.EXTERNAL_PATH, filename); File logFile = new File(Const.EXTERNAL_PATH, filename);
try (FileWriter writer = new FileWriter(logFile)) { try (FileWriter writer = new FileWriter(logFile)) {
for (String s : logs) { for (String s : logs) {
writer.write(s); writer.write(s);

View File

@ -23,15 +23,15 @@ public class SplashActivity extends BaseActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
String pkg = mm.mDB.getStrings(Const.Key.SU_MANAGER, null); String pkg = mm.mDB.getStrings(Const.Key.SU_MANAGER, null);
if (pkg != null && getPackageName().equals(Const.ORIG_PKG_NAME)) { if (pkg != null && getPackageName().equals(BuildConfig.APPLICATION_ID)) {
mm.mDB.setStrings(Const.Key.SU_MANAGER, null); mm.mDB.setStrings(Const.Key.SU_MANAGER, null);
Shell.su("pm uninstall " + pkg).exec(); Shell.su("pm uninstall " + pkg).exec();
} }
if (TextUtils.equals(pkg, getPackageName())) { if (TextUtils.equals(pkg, getPackageName())) {
try { try {
// We are the manager, remove com.topjohnwu.magisk as it could be malware // We are the manager, remove com.topjohnwu.magisk as it could be malware
getPackageManager().getApplicationInfo(Const.ORIG_PKG_NAME, 0); getPackageManager().getApplicationInfo(BuildConfig.APPLICATION_ID, 0);
RootUtils.uninstallPkg(Const.ORIG_PKG_NAME); RootUtils.uninstallPkg(BuildConfig.APPLICATION_ID);
} catch (PackageManager.NameNotFoundException ignored) {} } catch (PackageManager.NameNotFoundException ignored) {}
} }

View File

@ -135,7 +135,7 @@ public class SuRequestActivity extends BaseActivity {
} }
// Never allow com.topjohnwu.magisk (could be malware) // Never allow com.topjohnwu.magisk (could be malware)
if (TextUtils.equals(policy.packageName, Const.ORIG_PKG_NAME)) { if (TextUtils.equals(policy.packageName, BuildConfig.APPLICATION_ID)) {
finish(); finish();
return; return;
} }

View File

@ -14,7 +14,6 @@ import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.container.TarEntry;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.WebService;
import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.magisk.utils.ZipUtils;
@ -243,7 +242,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
switch (mode) { switch (mode) {
case PATCH_MODE: case PATCH_MODE:
String fmt = mm.prefs.getString(Const.Key.BOOT_FORMAT, ".img"); String fmt = mm.prefs.getString(Const.Key.BOOT_FORMAT, ".img");
File dest = new File(Download.EXTERNAL_PATH, "patched_boot" + fmt); File dest = new File(Const.EXTERNAL_PATH, "patched_boot" + fmt);
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();
OutputStream out; OutputStream out;
switch (fmt) { switch (fmt) {

View File

@ -5,6 +5,7 @@ import android.app.ProgressDialog;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
@ -82,11 +83,11 @@ public class PatchAPK {
// Generate a new app with random package name // Generate a new app with random package name
SuFile repack = new SuFile("/data/local/tmp/repack.apk"); SuFile repack = new SuFile("/data/local/tmp/repack.apk");
String pkg = genPackageName("com.", Const.ORIG_PKG_NAME.length()); String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length());
try { try {
JarMap apk = new JarMap(mm.getPackageCodePath()); JarMap apk = new JarMap(mm.getPackageCodePath());
if (!patchPackageID(apk, Const.ORIG_PKG_NAME, pkg)) if (!patchPackageID(apk, BuildConfig.APPLICATION_ID, pkg))
return false; return false;
SignAPK.sign(apk, new SuFileOutputStream(repack)); SignAPK.sign(apk, new SuFileOutputStream(repack));
} catch (Exception e) { } catch (Exception e) {
@ -101,7 +102,7 @@ public class PatchAPK {
mm.mDB.setStrings(Const.Key.SU_MANAGER, pkg); mm.mDB.setStrings(Const.Key.SU_MANAGER, pkg);
Data.exportPrefs(); Data.exportPrefs();
RootUtils.rmAndLaunch(Const.ORIG_PKG_NAME, pkg); RootUtils.rmAndLaunch(BuildConfig.APPLICATION_ID, pkg);
return true; return true;
} }

View File

@ -13,7 +13,6 @@ import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.BaseActivity; import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.container.Repo; import com.topjohnwu.magisk.container.Repo;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.WebService;
import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.magisk.utils.ZipUtils;
@ -48,7 +47,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
super(context); super(context);
mRepo = repo; mRepo = repo;
mInstall = install && Shell.rootAccess(); mInstall = install && Shell.rootAccess();
mFile = new File(Download.EXTERNAL_PATH, repo.getDownloadFilename()); mFile = new File(Const.EXTERNAL_PATH, repo.getDownloadFilename());
} }
private void removeTopFolder(File input, File output) throws IOException { private void removeTopFolder(File input, File output) throws IOException {

View File

@ -8,11 +8,11 @@ import android.os.Bundle;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.NoUIActivity; import com.topjohnwu.magisk.NoUIActivity;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.LocaleManager;
import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Topic;
@ -91,7 +91,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au
granted = false; granted = false;
} }
if (granted) { if (granted) {
Download.EXTERNAL_PATH.mkdirs(); Const.EXTERNAL_PATH.mkdirs();
callback.run(); callback.run();
} else { } else {
// Passed in context should be an activity if not granted, need to show dialog! // Passed in context should be an activity if not granted, need to show dialog!

View File

@ -13,7 +13,6 @@ import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import java.util.List; import java.util.List;
@ -70,7 +69,7 @@ class InstallMethodDialog extends AlertDialog.Builder {
Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode); Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode);
NotificationProgress progress = new NotificationProgress(filename); NotificationProgress progress = new NotificationProgress(filename);
AndroidNetworking AndroidNetworking
.download(Data.magiskLink, Download.EXTERNAL_PATH.getPath(), filename) .download(Data.magiskLink, Const.EXTERNAL_PATH.getPath(), filename)
.build() .build()
.setDownloadProgressListener(progress) .setDownloadProgressListener(progress)
.startDownload(new DownloadListener() { .startDownload(new DownloadListener() {

View File

@ -19,7 +19,6 @@ import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.BaseFragment; import com.topjohnwu.magisk.components.BaseFragment;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
@ -100,7 +99,7 @@ public class MagiskLogFragment extends BaseFragment {
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND)); now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
File logFile = new File(Download.EXTERNAL_PATH, filename); File logFile = new File(Const.EXTERNAL_PATH, filename);
try { try {
logFile.createNewFile(); logFile.createNewFile();
} catch (IOException e) { } catch (IOException e) {

View File

@ -10,6 +10,7 @@ import android.view.ViewGroup;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
@ -151,7 +152,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
} }
if (Shell.rootAccess() && Const.USER_ID == 0) { if (Shell.rootAccess() && Const.USER_ID == 0) {
if (mm.getPackageName().equals(Const.ORIG_PKG_NAME)) { if (mm.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
generalCatagory.removePreference(restoreManager); generalCatagory.removePreference(restoreManager);
} else { } else {
if (!Download.checkNetworkStatus(mm)) if (!Download.checkNetworkStatus(mm))

View File

@ -5,7 +5,7 @@ import android.os.AsyncTask;
import com.androidnetworking.AndroidNetworking; import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError; import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.DownloadListener; import com.androidnetworking.interfaces.DownloadListener;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
@ -70,7 +70,7 @@ public class DlInstallManager {
public void onDownloadComplete(File apk, NotificationProgress progress) { public void onDownloadComplete(File apk, NotificationProgress progress) {
File patched = apk; File patched = apk;
MagiskManager mm = Data.MM(); MagiskManager mm = Data.MM();
if (!mm.getPackageName().equals(Const.ORIG_PKG_NAME)) { if (!mm.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
progress.getNotification() progress.getNotification()
.setProgress(0, 0, true) .setProgress(0, 0, true)
.setContentTitle(mm.getString(R.string.hide_manager_title)) .setContentTitle(mm.getString(R.string.hide_manager_title))
@ -79,7 +79,7 @@ public class DlInstallManager {
patched = new File(apk.getParent(), "patched.apk"); patched = new File(apk.getParent(), "patched.apk");
try { try {
JarMap jarMap = new JarMap(apk); JarMap jarMap = new JarMap(apk);
PatchAPK.patchPackageID(jarMap, Const.ORIG_PKG_NAME, mm.getPackageName()); PatchAPK.patchPackageID(jarMap, BuildConfig.APPLICATION_ID, mm.getPackageName());
SignAPK.sign(jarMap, new BufferedOutputStream(new FileOutputStream(patched))); SignAPK.sign(jarMap, new BufferedOutputStream(new FileOutputStream(patched)));
} catch (Exception e) { } catch (Exception e) {
return; return;
@ -97,7 +97,7 @@ public class DlInstallManager {
progress.dismiss(); progress.dismiss();
Data.exportPrefs(); Data.exportPrefs();
if (ShellUtils.fastCmdResult("pm install " + apk)) if (ShellUtils.fastCmdResult("pm install " + apk))
RootUtils.rmAndLaunch(Data.MM().getPackageName(), Const.ORIG_PKG_NAME); RootUtils.rmAndLaunch(Data.MM().getPackageName(), BuildConfig.APPLICATION_ID);
} }
} }
} }

View File

@ -3,19 +3,9 @@ package com.topjohnwu.magisk.utils;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.Environment;
import java.io.File;
public class Download { public class Download {
public static final File EXTERNAL_PATH =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
static {
EXTERNAL_PATH.mkdirs();
}
public static String getLegalFilename(CharSequence filename) { public static String getLegalFilename(CharSequence filename) {
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "") return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
.replace("$", "").replace("`", "").replace("*", "").replace("/", "_") .replace("$", "").replace("`", "").replace("*", "").replace("/", "_")