Cleanup contexts

This commit is contained in:
topjohnwu
2017-10-16 00:54:48 +08:00
parent c036f6d529
commit 896ca2ef6b
25 changed files with 99 additions and 141 deletions
@@ -1,6 +1,5 @@
package com.topjohnwu.magisk.utils;
import android.content.Context;
import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager;
@@ -10,7 +9,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -21,8 +19,6 @@ import java.util.List;
public class Shell {
private static WeakReference<MagiskManager> weakMm;
// -1 = no shell; 0 = non root shell; 1 = root shell
public static int status;
@@ -47,12 +43,8 @@ public class Shell {
STDOUT = process.getInputStream();
}
public static void registerShell(Context context) {
weakMm = new WeakReference<>(Utils.getMagiskManager(context));
}
public static Shell getShell() {
MagiskManager mm = weakMm.get();
MagiskManager mm = MagiskManager.get();
boolean needNewShell = mm.shell == null;
if (!needNewShell) {
@@ -34,7 +34,8 @@ public class ShowUI {
private static final int MAGISK_UPDATE_NOTIFICATION_ID = 1;
private static final int APK_UPDATE_NOTIFICATION_ID = 2;
public static void showMagiskUpdateNotification(MagiskManager mm) {
public static void showMagiskUpdateNotification() {
MagiskManager mm = MagiskManager.get();
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, MagiskManager.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk)
.setContentTitle(mm.getString(R.string.magisk_update_title))
@@ -54,7 +55,8 @@ public class ShowUI {
notificationManager.notify(MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
}
public static void showManagerUpdateNotification(MagiskManager mm) {
public static void showManagerUpdateNotification() {
MagiskManager mm = MagiskManager.get();
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, MagiskManager.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk)
.setContentTitle(mm.getString(R.string.manager_update_title))
@@ -73,7 +75,7 @@ public class ShowUI {
}
public static void showMagiskInstallDialog(MagiskFragment fragment, boolean enc, boolean verity) {
MagiskManager mm = Utils.getMagiskManager(fragment.getActivity());
MagiskManager mm = Utils.getMagiskManager(fragment.getContext());
String filename = Utils.getLegalFilename("Magisk-v" + mm.remoteMagiskVersionString + ".zip");
new AlertDialogBuilder(fragment.getActivity())
.setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.magisk)))
@@ -101,10 +103,10 @@ public class ShowUI {
switch (idx) {
case 1:
if (mm.remoteMagiskVersionCode < 1400) {
mm.toast(R.string.no_boot_file_patch_support, Toast.LENGTH_LONG);
MagiskManager.toast(R.string.no_boot_file_patch_support, Toast.LENGTH_LONG);
return;
}
mm.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG);
MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
fragment.startActivityForResult(intent, SELECT_BOOT_IMG,
@@ -256,7 +258,7 @@ public class ShowUI {
"cat " + uninstaller + " > /cache/" + UNINSTALLER,
"cat " + utils + " > /data/magisk/" + Utils.UTIL_FUNCTIONS
);
mm.toast(R.string.uninstall_toast, Toast.LENGTH_LONG);
MagiskManager.toast(R.string.uninstall_toast, Toast.LENGTH_LONG);
Shell.su_raw(
"sleep 5",
"pm uninstall " + mm.getApplicationInfo().packageName
@@ -268,7 +270,7 @@ public class ShowUI {
.setNeutralButton(R.string.restore_stock_boot, (d, i) -> {
String boot = fragment.getSelectedBootImage();
if (boot == null) return;
new RestoreStockBoot(mm, boot).exec();
new RestoreStockBoot(boot).exec();
})
.setNegativeButton(R.string.no_thanks, null)
.show();
@@ -143,41 +143,43 @@ public class Utils {
.setAction(R.string.ok, (v)->{}).show();
}
public static boolean checkNetworkStatus(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
public static boolean checkNetworkStatus() {
ConnectivityManager manager = (ConnectivityManager)
MagiskManager.get().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
public static String getLocaleString(Context context, Locale locale, @StringRes int id) {
public static String getLocaleString(Locale locale, @StringRes int id) {
Context context = MagiskManager.get();
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
Context localizedContext = context.createConfigurationContext(config);
return localizedContext.getString(id);
}
public static List<Locale> getAvailableLocale(Context context) {
public static List<Locale> getAvailableLocale() {
List<Locale> locales = new ArrayList<>();
HashSet<String> set = new HashSet<>();
Locale locale;
int compareId = R.string.download_file_error;
@StringRes int compareId = R.string.download_file_error;
// Add default locale
locales.add(Locale.ENGLISH);
set.add(getLocaleString(context, Locale.ENGLISH, compareId));
set.add(getLocaleString(Locale.ENGLISH, compareId));
// Add some special locales
locales.add(Locale.TAIWAN);
set.add(getLocaleString(context, Locale.TAIWAN, compareId));
set.add(getLocaleString(Locale.TAIWAN, compareId));
locale = new Locale("pt", "BR");
locales.add(locale);
set.add(getLocaleString(context, locale, compareId));
set.add(getLocaleString(locale, compareId));
// Other locales
for (String s : context.getAssets().getLocales()) {
for (String s : MagiskManager.get().getAssets().getLocales()) {
locale = Locale.forLanguageTag(s);
if (set.add(getLocaleString(context, locale, compareId))) {
if (set.add(getLocaleString(locale, compareId))) {
locales.add(locale);
}
}
@@ -206,7 +208,8 @@ public class Utils {
== DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER;
}
public static Context getEncContext(Context context) {
public static Context getEncContext() {
Context context = MagiskManager.get();
if (useFDE(context))
return context.createDeviceProtectedStorageContext();
else