Micro optimizations

This commit is contained in:
topjohnwu 2018-10-28 00:54:56 -04:00
parent bf4a46d57c
commit 75ab1fa570
14 changed files with 99 additions and 83 deletions

View File

@ -34,10 +34,7 @@ public class Const {
// Versions
public static final int UPDATE_SERVICE_VER = 1;
public static int MIN_MODULE_VER() {
return Data.magiskVersionCode >= MAGISK_VER.REMOVE_LEGACY_LINK ? 1500 : 1400;
}
public static final int MIN_MODULE_VER = 1500;
/* A list of apps that should not be shown as hide-able */
public static final List<String> HIDE_BLACKLIST = Arrays.asList(

View File

@ -13,7 +13,6 @@ import com.topjohnwu.magisk.receivers.RebootReceiver;
import com.topjohnwu.magisk.receivers.ShortcutReceiver;
import com.topjohnwu.magisk.services.OnBootService;
import com.topjohnwu.magisk.services.UpdateCheckService;
import com.topjohnwu.magisk.utils.FingerprintHelper;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
@ -62,12 +61,9 @@ public class Data {
public static boolean isDarkTheme;
public static int suRequestTimeout;
public static int suLogTimeout = 14;
public static int suAccessState;
public static boolean suFingerprint;
public static int multiuserMode;
public static int multiuserState = -1;
public static int suResponseType;
public static int suNotificationType;
public static int suNamespaceMode;
public static int updateChannel;
public static int repoOrder;
@ -180,15 +176,6 @@ public class Data {
suRequestTimeout = Utils.getPrefsInt(mm.prefs, Const.Key.SU_REQUEST_TIMEOUT, Const.Value.timeoutList[2]);
suResponseType = Utils.getPrefsInt(mm.prefs, Const.Key.SU_AUTO_RESPONSE, Const.Value.SU_PROMPT);
suNotificationType = Utils.getPrefsInt(mm.prefs, Const.Key.SU_NOTIFICATION, Const.Value.NOTIFICATION_TOAST);
suAccessState = mm.mDB.getSettings(Const.Key.ROOT_ACCESS, Const.Value.ROOT_ACCESS_APPS_AND_ADB);
multiuserMode = mm.mDB.getSettings(Const.Key.SU_MULTIUSER_MODE, Const.Value.MULTIUSER_MODE_OWNER_ONLY);
suNamespaceMode = mm.mDB.getSettings(Const.Key.SU_MNT_NS, Const.Value.NAMESPACE_MODE_REQUESTER);
suFingerprint = mm.mDB.getSettings(Const.Key.SU_FINGERPRINT, 0) != 0;
if (suFingerprint && !FingerprintHelper.canUseFingerprint()) {
// User revoked the fingerprint
mm.mDB.setSettings(Const.Key.SU_FINGERPRINT, 0);
suFingerprint = false;
}
// config
isDarkTheme = mm.prefs.getBoolean(Const.Key.DARK_THEME, false);
@ -202,13 +189,9 @@ public class Data {
.putBoolean(Const.Key.MAGISKHIDE, magiskHide)
.putBoolean(Const.Key.HOSTS, Const.MAGISK_HOST_FILE.exists())
.putBoolean(Const.Key.COREONLY, Const.MAGISK_DISABLE_FILE.exists())
.putBoolean(Const.Key.SU_FINGERPRINT, suFingerprint)
.putString(Const.Key.SU_REQUEST_TIMEOUT, String.valueOf(suRequestTimeout))
.putString(Const.Key.SU_AUTO_RESPONSE, String.valueOf(suResponseType))
.putString(Const.Key.SU_NOTIFICATION, String.valueOf(suNotificationType))
.putString(Const.Key.ROOT_ACCESS, String.valueOf(suAccessState))
.putString(Const.Key.SU_MULTIUSER_MODE, String.valueOf(multiuserMode))
.putString(Const.Key.SU_MNT_NS, String.valueOf(suNamespaceMode))
.putString(Const.Key.UPDATE_CHANNEL, String.valueOf(updateChannel))
.putInt(Const.Key.UPDATE_SERVICE_VER, Const.UPDATE_SERVICE_VER)
.putInt(Const.Key.REPO_ORDER, repoOrder)

View File

@ -1,10 +1,8 @@
package com.topjohnwu.magisk;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.topjohnwu.magisk.database.MagiskDB;
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
@ -39,19 +37,7 @@ public class MagiskManager extends ContainerApp {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mDB = MagiskDB.getInstance();
String pkg = mDB.getStrings(Const.Key.SU_MANAGER, null);
if (pkg != null && getPackageName().equals(Const.ORIG_PKG_NAME)) {
mDB.setStrings(Const.Key.SU_MANAGER, null);
Shell.su("pm uninstall " + pkg).exec();
}
if (TextUtils.equals(pkg, getPackageName())) {
try {
// We are the manager, remove com.topjohnwu.magisk as it could be malware
getPackageManager().getApplicationInfo(Const.ORIG_PKG_NAME, 0);
RootUtils.uninstallPkg(Const.ORIG_PKG_NAME);
} catch (PackageManager.NameNotFoundException ignored) {}
}
repoDB = new RepoDatabaseHelper(this);
LocaleManager.setLocale(this);
Data.loadConfig();

View File

@ -18,6 +18,7 @@ import com.topjohnwu.magisk.fragments.SettingsFragment;
import com.topjohnwu.magisk.fragments.SuperuserFragment;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
import androidx.annotation.NonNull;
@ -124,8 +125,7 @@ public class MainActivity extends BaseActivity
menu.findItem(R.id.downloads).setVisible(Download.checkNetworkStatus(this)
&& Shell.rootAccess() && Data.magiskVersionCode >= 0);
menu.findItem(R.id.log).setVisible(Shell.rootAccess());
menu.findItem(R.id.superuser).setVisible(Shell.rootAccess() &&
!(Const.USER_ID > 0 && Data.multiuserMode == Const.Value.MULTIUSER_MODE_OWNER_MANAGED));
menu.findItem(R.id.superuser).setVisible(Utils.showSuperUser());
}
public void navigate(String item) {

View File

@ -3,16 +3,18 @@ package com.topjohnwu.magisk;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import com.topjohnwu.magisk.asyncs.CheckUpdates;
import com.topjohnwu.magisk.asyncs.UpdateRepos;
import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
import com.topjohnwu.magisk.receivers.ShortcutReceiver;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.LocaleManager;
import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
@ -22,6 +24,19 @@ public class SplashActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String pkg = mm.mDB.getStrings(Const.Key.SU_MANAGER, null);
if (pkg != null && getPackageName().equals(Const.ORIG_PKG_NAME)) {
mm.mDB.setStrings(Const.Key.SU_MANAGER, null);
Shell.su("pm uninstall " + pkg).exec();
}
if (TextUtils.equals(pkg, getPackageName())) {
try {
// We are the manager, remove com.topjohnwu.magisk as it could be malware
getPackageManager().getApplicationInfo(Const.ORIG_PKG_NAME, 0);
RootUtils.uninstallPkg(Const.ORIG_PKG_NAME);
} catch (PackageManager.NameNotFoundException ignored) {}
}
// Magisk working as expected
if (Shell.rootAccess() && Data.magiskVersionCode > 0) {
// Update check service
@ -30,7 +45,6 @@ public class SplashActivity extends BaseActivity {
Utils.loadModules();
}
mm.repoDB = new RepoDatabaseHelper(this);
Data.importPrefs();
// Dynamic detect all locales

View File

@ -181,9 +181,9 @@ public class SuRequestActivity extends BaseActivity {
}
};
boolean useFingerprint = Data.suFingerprint && FingerprintHelper.canUseFingerprint();
boolean useFP = FingerprintHelper.useFingerPrint();
if (useFingerprint) {
if (useFP) {
try {
fingerprintHelper = new FingerprintHelper() {
@Override
@ -209,11 +209,11 @@ public class SuRequestActivity extends BaseActivity {
fingerprintHelper.authenticate();
} catch (Exception e) {
e.printStackTrace();
useFingerprint = false;
useFP = false;
}
}
if (!useFingerprint) {
if (!useFP) {
grant_btn.setOnClickListener(v -> {
handleAction(Policy.ALLOW);
timer.cancel();
@ -221,8 +221,8 @@ public class SuRequestActivity extends BaseActivity {
grant_btn.requestFocus();
}
grant_btn.setVisibility(useFingerprint ? View.GONE : View.VISIBLE);
fingerprintImg.setVisibility(useFingerprint ? View.VISIBLE : View.GONE);
grant_btn.setVisibility(useFP ? View.GONE : View.VISIBLE);
fingerprintImg.setVisibility(useFP ? View.VISIBLE : View.GONE);
deny_btn.setOnClickListener(v -> {
handleAction(Policy.DENY);

View File

@ -10,7 +10,6 @@ import android.widget.Switch;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.CustomAlertDialog;
import com.topjohnwu.magisk.components.ExpandableView;
@ -86,7 +85,7 @@ public class PolicyAdapter extends RecyclerView.Adapter<PolicyAdapter.ViewHolder
dbHelper.updatePolicy(policy);
}
};
if (Data.suFingerprint) {
if (FingerprintHelper.useFingerPrint()) {
holder.masterSwitch.setChecked(!isChecked);
FingerprintHelper.showAuthDialog((Activity) v.getContext(), () -> {
holder.masterSwitch.setChecked(isChecked);

View File

@ -36,7 +36,7 @@ public class Repo extends BaseModule {
if (getVersionCode() < 0) {
throw new IllegalRepoException("Repo [" + getId() + "] does not contain versionCode");
}
if (getMinMagiskVersion() < Const.MIN_MODULE_VER()) {
if (getMinMagiskVersion() < Const.MIN_MODULE_VER) {
Logger.debug("Repo [" + getId() + "] is outdated");
}
}

View File

@ -29,8 +29,7 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
mDb = getWritableDatabase();
// Remove outdated repos
mDb.delete(TABLE_NAME, "minMagisk<?",
new String[] { String.valueOf(Const.MIN_MODULE_VER()) });
mDb.delete(TABLE_NAME, "minMagisk<?", new String[] { String.valueOf(Const.MIN_MODULE_VER) });
}
@Override
@ -107,7 +106,7 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
orderBy = "last_update DESC";
}
return mDb.query(TABLE_NAME, null, "minMagisk<=? AND minMagisk>=?",
new String[] { String.valueOf(Data.magiskVersionCode), String.valueOf(Const.MIN_MODULE_VER()) },
new String[] { String.valueOf(Data.magiskVersionCode), String.valueOf(Const.MIN_MODULE_VER) },
null, null, orderBy);
}

View File

@ -7,8 +7,6 @@ import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabLayout;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MainActivity;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.adapters.TabFragmentAdapter;
@ -33,9 +31,7 @@ public class LogFragment extends BaseFragment {
TabFragmentAdapter adapter = new TabFragmentAdapter(getChildFragmentManager());
if (!(Const.USER_ID > 0 && Data.multiuserMode == Const.Value.MULTIUSER_MODE_OWNER_MANAGED)) {
adapter.addTab(new SuLogFragment(), getString(R.string.superuser));
}
adapter.addTab(new MagiskLogFragment(), getString(R.string.magisk));
tab.setupWithViewPager(viewPager);
tab.setVisibility(View.VISIBLE);

View File

@ -46,20 +46,36 @@ public class SettingsFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener,
Topic.Subscriber, Topic.AutoSubscriber {
private PreferenceScreen prefScreen;
private ListPreference updateChannel, suAccess, autoRes, suNotification,
requestTimeout, multiuserMode, namespaceMode;
private MagiskManager mm;
private PreferenceCategory generalCatagory;
private ListPreference updateChannel, autoRes, suNotification,
requestTimeout, rootConfig, multiuserConfig, nsConfig;
private int rootState, namespaceState;
private boolean showSuperuser;
private void prefsSync() {
rootState = mm.mDB.getSettings(Const.Key.ROOT_ACCESS, Const.Value.ROOT_ACCESS_APPS_AND_ADB);
namespaceState = mm.mDB.getSettings(Const.Key.SU_MNT_NS, Const.Value.NAMESPACE_MODE_REQUESTER);
showSuperuser = Utils.showSuperUser();
mm.prefs.edit()
.putString(Const.Key.ROOT_ACCESS, String.valueOf(rootState))
.putString(Const.Key.SU_MNT_NS, String.valueOf(namespaceState))
.putString(Const.Key.SU_MULTIUSER_MODE, String.valueOf(Data.multiuserState))
.putBoolean(Const.Key.SU_FINGERPRINT, FingerprintHelper.useFingerPrint())
.apply();
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.app_settings, rootKey);
mm = Data.MM();
prefScreen = getPreferenceScreen();
prefsSync();
generalCatagory = (PreferenceCategory) findPreference("general");
setPreferencesFromResource(R.xml.app_settings, rootKey);
PreferenceScreen prefScreen = getPreferenceScreen();
PreferenceCategory generalCatagory = (PreferenceCategory) findPreference("general");
PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk");
PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser");
Preference hideManager = findPreference("hide");
@ -72,12 +88,12 @@ public class SettingsFragment extends PreferenceFragmentCompat
});
updateChannel = (ListPreference) findPreference(Const.Key.UPDATE_CHANNEL);
suAccess = (ListPreference) findPreference(Const.Key.ROOT_ACCESS);
rootConfig = (ListPreference) findPreference(Const.Key.ROOT_ACCESS);
autoRes = (ListPreference) findPreference(Const.Key.SU_AUTO_RESPONSE);
requestTimeout = (ListPreference) findPreference(Const.Key.SU_REQUEST_TIMEOUT);
suNotification = (ListPreference) findPreference(Const.Key.SU_NOTIFICATION);
multiuserMode = (ListPreference) findPreference(Const.Key.SU_MULTIUSER_MODE);
namespaceMode = (ListPreference) findPreference(Const.Key.SU_MNT_NS);
multiuserConfig = (ListPreference) findPreference(Const.Key.SU_MULTIUSER_MODE);
nsConfig = (ListPreference) findPreference(Const.Key.SU_MNT_NS);
SwitchPreference reauth = (SwitchPreference) findPreference(Const.Key.SU_REAUTH);
SwitchPreference fingerprint = (SwitchPreference) findPreference(Const.Key.SU_FINGERPRINT);
@ -107,7 +123,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
// Disable dangerous settings in secondary user
if (Const.USER_ID > 0) {
suCategory.removePreference(multiuserMode);
suCategory.removePreference(multiuserConfig);
}
// Disable re-authentication option on Android O, it will not work
@ -163,8 +179,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
generalCatagory.removePreference(hideManager);
}
if (!Shell.rootAccess() || (Const.USER_ID > 0 &&
Data.multiuserMode == Const.Value.MULTIUSER_MODE_OWNER_MANAGED)) {
if (!showSuperuser) {
prefScreen.removePreference(suCategory);
}
@ -213,9 +228,16 @@ public class SettingsFragment extends PreferenceFragmentCompat
mm.mDB.setSettings(key, Utils.getPrefsInt(prefs, key));
break;
}
Data.loadConfig();
setSummary();
switch (key) {
case Const.Key.ROOT_ACCESS:
rootState = Utils.getPrefsInt(prefs, key);
break;
case Const.Key.SU_MULTIUSER_MODE:
Data.multiuserState = Utils.getPrefsInt(prefs, key);
break;
case Const.Key.SU_MNT_NS:
namespaceState = Utils.getPrefsInt(prefs, key);
break;
case Const.Key.DARK_THEME:
requireActivity().recreate();
break;
@ -259,6 +281,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
Utils.setupUpdateCheck();
break;
}
Data.loadConfig();
setSummary();
}
@Override
@ -270,7 +294,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
((SwitchPreference) preference).setChecked(!checked);
FingerprintHelper.showAuthDialog(requireActivity(), () -> {
((SwitchPreference) preference).setChecked(checked);
Data.suFingerprint = checked;
mm.mDB.setSettings(key, checked ? 1 : 0);
});
break;
@ -281,8 +304,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
private void setSummary() {
updateChannel.setSummary(getResources()
.getStringArray(R.array.update_channel)[Data.updateChannel]);
suAccess.setSummary(getResources()
.getStringArray(R.array.su_access)[Data.suAccessState]);
rootConfig.setSummary(getResources()
.getStringArray(R.array.su_access)[rootState]);
autoRes.setSummary(getResources()
.getStringArray(R.array.auto_response)[Data.suResponseType]);
suNotification.setSummary(getResources()
@ -290,10 +313,10 @@ public class SettingsFragment extends PreferenceFragmentCompat
requestTimeout.setSummary(
getString(R.string.request_timeout_summary,
mm.prefs.getString(Const.Key.SU_REQUEST_TIMEOUT, "10")));
multiuserMode.setSummary(getResources()
.getStringArray(R.array.multiuser_summary)[Data.multiuserMode]);
namespaceMode.setSummary(getResources()
.getStringArray(R.array.namespace_summary)[Data.suNamespaceMode]);
multiuserConfig.setSummary(getResources()
.getStringArray(R.array.multiuser_summary)[Data.multiuserState]);
nsConfig.setSummary(getResources()
.getStringArray(R.array.namespace_summary)[namespaceState]);
}
@Override

View File

@ -13,6 +13,7 @@ import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
import java.util.ArrayList;
@ -33,8 +34,7 @@ public class ShortcutReceiver extends BroadcastReceiver {
private ArrayList<ShortcutInfo> getShortCuts(MagiskManager mm) {
ArrayList<ShortcutInfo> shortCuts = new ArrayList<>();
boolean root = Shell.rootAccess();
if (root && !(Const.USER_ID > 0 &&
Data.multiuserMode == Const.Value.MULTIUSER_MODE_OWNER_MANAGED)) {
if (Utils.showSuperUser()) {
shortCuts.add(new ShortcutInfo.Builder(mm, "superuser")
.setShortLabel(mm.getString(R.string.superuser))
.setIntent(new Intent(mm, Data.classMap.get(SplashActivity.class))

View File

@ -35,6 +35,16 @@ public abstract class FingerprintHelper {
private Cipher cipher;
private CancellationSignal cancel;
public static boolean useFingerPrint() {
MagiskManager mm = Data.MM();
boolean fp = mm.mDB.getSettings(Const.Key.SU_FINGERPRINT, 0) != 0;
if (fp && !canUseFingerprint()) {
mm.mDB.setSettings(Const.Key.SU_FINGERPRINT, 0);
fp = false;
}
return fp;
}
public static boolean canUseFingerprint() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return false;

View File

@ -22,6 +22,7 @@ import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.container.Module;
import com.topjohnwu.magisk.container.ValueSortedMap;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.io.SuFile;
import java.util.Locale;
@ -140,4 +141,12 @@ public class Utils {
} catch (Exception ignored) {}
return info.loadLabel(pm).toString();
}
public static boolean showSuperUser() {
if (Data.multiuserState < 0)
Data.multiuserState = Data.MM().mDB.getSettings(Const.Key.SU_MULTIUSER_MODE,
Const.Value.MULTIUSER_MODE_OWNER_ONLY);
return Shell.rootAccess() && (Const.USER_ID == 0 ||
Data.multiuserState != Const.Value.MULTIUSER_MODE_OWNER_MANAGED);
}
}