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) { public static SharedPreferences getSharedPreferences(String name) {
// encryptedSharedPreferences is used // for debugging, log timing
MasterKey mainKeyAlias; long start = SystemClock.elapsedRealtime();
try { // log what's requesting the shared preferences
mainKeyAlias = new MasterKey.Builder(MainApplication.getINSTANCE().getApplicationContext()).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(); // get caller if debug build
} catch (GeneralSecurityException | IOException e) { if (BuildConfig.DEBUG) {
throw new RuntimeException(e); 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 { 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) { } 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. // 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() { public static boolean isDeveloper() {
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG) return true;
return true;
return getSharedPreferences("mmm").getBoolean("developer", false); return getSharedPreferences("mmm").getBoolean("developer", false);
} }
@ -220,8 +227,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
} }
public Markwon getMarkwon() { public Markwon getMarkwon() {
if (this.markwon != null) if (this.markwon != null) return this.markwon;
return this.markwon;
FoxThemeWrapper contextThemeWrapper = this.markwonThemeContext; FoxThemeWrapper contextThemeWrapper = this.markwonThemeContext;
if (contextThemeWrapper == null) { if (contextThemeWrapper == null) {
contextThemeWrapper = this.markwonThemeContext = new FoxThemeWrapper(this, this.managerThemeResId); 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-rCN");
supportedLocales.add("zh-rTW"); supportedLocales.add("zh-rTW");
supportedLocales.add("en"); supportedLocales.add("en");
if (INSTANCE == null) if (INSTANCE == null) INSTANCE = this;
INSTANCE = this;
relPackageName = this.getPackageName(); relPackageName = this.getPackageName();
Timber.d("Starting FoxMMM version " + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + "), commit " + BuildConfig.COMMIT_HASH); Timber.d("Starting FoxMMM version " + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + "), commit " + BuildConfig.COMMIT_HASH);
super.onCreate(); super.onCreate();
@ -361,8 +366,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
@SuppressWarnings("SpellCheckingInspection") String[] officialSignatureHashArray = new String[]{"7bec7c4462f4aac616612d9f56a023ee3046e83afa956463b5fab547fd0a0be6", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}; @SuppressWarnings("SpellCheckingInspection") String[] officialSignatureHashArray = new String[]{"7bec7c4462f4aac616612d9f56a023ee3046e83afa956463b5fab547fd0a0be6", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"};
String ourSignatureHash = Hashing.sha256().hashBytes(signatures[0].toByteArray()).toString(); String ourSignatureHash = Hashing.sha256().hashBytes(signatures[0].toByteArray()).toString();
isOfficial = Arrays.asList(officialSignatureHashArray).contains(ourSignatureHash); isOfficial = Arrays.asList(officialSignatureHashArray).contains(ourSignatureHash);
} catch ( } catch (PackageManager.NameNotFoundException ignored) {
PackageManager.NameNotFoundException ignored) {
} }
SharedPreferences sharedPreferences = MainApplication.getSharedPreferences("mmm"); SharedPreferences sharedPreferences = MainApplication.getSharedPreferences("mmm");
// We are only one process so it's ok to do this // 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(); File dataDir = this.getDataDir();
// for path with / somewhere in the middle, its a subdirectory // for path with / somewhere in the middle, its a subdirectory
if (path != null) { if (path != null) {
if (path.startsWith("/")) if (path.startsWith("/")) path = path.substring(1);
path = path.substring(1); if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
if (path.endsWith("/"))
path = path.substring(0, path.length() - 1);
if (path.contains("/")) { if (path.contains("/")) {
String[] dirs = path.split("/"); String[] dirs = path.split("/");
for (String dir : dirs) { 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 // create the directory if it doesn't exist
if (!dataDir.exists()) { if (!dataDir.exists()) {
if (!dataDir.mkdirs()) { if (!dataDir.mkdirs()) {
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG) Timber.w("Failed to create directory %s", dataDir);
Timber.w("Failed to create directory %s", dataDir);
} }
} }
} }

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

Loading…
Cancel
Save