fix bad perf on setup init

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/299/head
androidacy-user 1 year ago
parent dca6212925
commit cc13635f49

@ -110,20 +110,28 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
}
public static SharedPreferences getSharedPreferences(String name) {
// encryptedSharedPreferences is used
MasterKey mainKeyAlias;
try {
mainKeyAlias = new MasterKey.Builder(MainApplication.getINSTANCE().getApplicationContext()).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException(e);
// for debugging, log timing
long start = SystemClock.elapsedRealtime();
// log what's requesting the shared preferences
// get caller if debug build
if (BuildConfig.DEBUG) {
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
StackTraceElement e = stacktrace[3];
String methodName = e.getMethodName();
String className = e.getClassName();
Timber.d("Shared preferences %s requested by %s.%s", name, className, methodName);
}
SharedPreferences mSharedPreferences;
// encryptedSharedPreferences is used
Context context = getINSTANCE().getApplicationContext();
try {
mSharedPreferences = EncryptedSharedPreferences.create(MainApplication.getINSTANCE().getApplicationContext(), name, mainKeyAlias, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
MasterKey masterKey = new MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();
SharedPreferences mSharedPrefs = EncryptedSharedPreferences.create(context, name, masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
Timber.d("Encrypted shared preferences %s took %dms", name, SystemClock.elapsedRealtime() - start);
return mSharedPrefs;
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException(e);
Timber.e(e, "Failed to create encrypted shared preferences");
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
return mSharedPreferences;
}
// Is application wrapped, and therefore must reduce it's feature set.
@ -169,8 +177,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
}
public static boolean isDeveloper() {
if (BuildConfig.DEBUG)
return true;
if (BuildConfig.DEBUG) return true;
return getSharedPreferences("mmm").getBoolean("developer", false);
}
@ -220,8 +227,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
}
public Markwon getMarkwon() {
if (this.markwon != null)
return this.markwon;
if (this.markwon != null) return this.markwon;
FoxThemeWrapper contextThemeWrapper = this.markwonThemeContext;
if (contextThemeWrapper == null) {
contextThemeWrapper = this.markwonThemeContext = new FoxThemeWrapper(this, this.managerThemeResId);
@ -339,8 +345,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
supportedLocales.add("zh-rCN");
supportedLocales.add("zh-rTW");
supportedLocales.add("en");
if (INSTANCE == null)
INSTANCE = this;
if (INSTANCE == null) INSTANCE = this;
relPackageName = this.getPackageName();
Timber.d("Starting FoxMMM version " + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + "), commit " + BuildConfig.COMMIT_HASH);
super.onCreate();
@ -361,8 +366,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
@SuppressWarnings("SpellCheckingInspection") String[] officialSignatureHashArray = new String[]{"7bec7c4462f4aac616612d9f56a023ee3046e83afa956463b5fab547fd0a0be6", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"};
String ourSignatureHash = Hashing.sha256().hashBytes(signatures[0].toByteArray()).toString();
isOfficial = Arrays.asList(officialSignatureHashArray).contains(ourSignatureHash);
} catch (
PackageManager.NameNotFoundException ignored) {
} catch (PackageManager.NameNotFoundException ignored) {
}
SharedPreferences sharedPreferences = MainApplication.getSharedPreferences("mmm");
// We are only one process so it's ok to do this
@ -435,10 +439,8 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
File dataDir = this.getDataDir();
// for path with / somewhere in the middle, its a subdirectory
if (path != null) {
if (path.startsWith("/"))
path = path.substring(1);
if (path.endsWith("/"))
path = path.substring(0, path.length() - 1);
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
if (path.contains("/")) {
String[] dirs = path.split("/");
for (String dir : dirs) {
@ -456,8 +458,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
// create the directory if it doesn't exist
if (!dataDir.exists()) {
if (!dataDir.mkdirs()) {
if (BuildConfig.DEBUG)
Timber.w("Failed to create directory %s", dataDir);
if (BuildConfig.DEBUG) Timber.w("Failed to create directory %s", dataDir);
}
}
}

@ -39,6 +39,8 @@ import io.realm.RealmConfiguration;
import timber.log.Timber;
public class SetupActivity extends FoxActivity implements LanguageActivity {
private int cachedTheme;
@SuppressLint({"ApplySharedPref", "RestrictedApi"})
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -158,7 +160,6 @@ public class SetupActivity extends FoxActivity implements LanguageActivity {
// Set first launch to false
// get instance of editor
SharedPreferences.Editor editor = prefs.edit();
editor.putString("last_shown_setup", "v1");
// Set the Automatic update check pref
editor.putBoolean("pref_background_update_check", ((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_background_update_check))).isChecked());
// Set the crash reporting pref
@ -174,6 +175,7 @@ public class SetupActivity extends FoxActivity implements LanguageActivity {
// commit the changes
realm.commitTransaction();
realm.close();
editor.putString("last_shown_setup", "v1");
// Commit the changes
editor.commit();
// Sleep for 1 second to allow the user to see the changes
@ -186,6 +188,10 @@ public class SetupActivity extends FoxActivity implements LanguageActivity {
if (BuildConfig.DEBUG) {
Timber.d("Automatic update check: %s", prefs.getBoolean("pref_background_update_check", false));
Timber.i("Crash reporting: %s", prefs.getBoolean("pref_crash_reporting", false));
Timber.i("Androidacy repo: %s", androidacyRepo);
Timber.i("Magisk Alt repo: %s", magiskAltRepo);
// log last shown setup
Timber.i("Last shown setup: %s", prefs.getString("last_shown_setup", "v0"));
}
// Restart the activity
MainActivity.doSetupRestarting = true;
@ -213,15 +219,35 @@ public class SetupActivity extends FoxActivity implements LanguageActivity {
@Override
public Resources.Theme getTheme() {
Resources.Theme theme = super.getTheme();
// try cached value
if (cachedTheme != 0) {
theme.applyStyle(cachedTheme, true);
return theme;
}
// Set the theme
SharedPreferences prefs = MainApplication.getSharedPreferences("mmm");
switch (prefs.getString("pref_theme", "system")) {
case "light" -> theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet_Light, true);
case "dark" -> theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet_Dark, true);
case "system" -> theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet, true);
case "black" -> theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet_Black, true);
case "transparent_light" ->
theme.applyStyle(R.style.Theme_MagiskModuleManager_Transparent_Light, true);
String themePref = prefs.getString("pref_theme", "system");
switch (themePref) {
case "light" -> {
theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet_Light, true);
cachedTheme = R.style.Theme_MagiskModuleManager_Monet_Light;
}
case "dark" -> {
theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet_Dark, true);
cachedTheme = R.style.Theme_MagiskModuleManager_Monet_Dark;
}
case "system" -> {
theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet, true);
cachedTheme = R.style.Theme_MagiskModuleManager_Monet;
}
case "black" -> {
theme.applyStyle(R.style.Theme_MagiskModuleManager_Monet_Black, true);
cachedTheme = R.style.Theme_MagiskModuleManager_Monet_Black;
}
case "transparent_light" -> {
theme.applyStyle(R.style.Theme_MagiskModuleManager_Transparent_Light, true);
cachedTheme = R.style.Theme_MagiskModuleManager_Transparent_Light;
}
}
return theme;
}

Loading…
Cancel
Save