From ce03a0b36ace562be2a47f90a1ae80d99b667ab1 Mon Sep 17 00:00:00 2001 From: Fox2Code Date: Sat, 17 Dec 2022 19:04:55 +0100 Subject: [PATCH] My brain is melting right now. --- app/build.gradle | 13 +-- .../java/com/fox2code/mmm/MainActivity.java | 80 ++++++++++++------- .../com/fox2code/mmm/MainApplication.java | 6 +- .../mmm/settings/SettingsActivity.java | 10 +-- .../com/fox2code/mmm/utils/ProcessHelper.java | 22 +++++ .../com/fox2code/mmm/sentry/SentryMain.java | 6 ++ .../com/fox2code/mmm/sentry/SentryMain.java | 2 + 7 files changed, 95 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/com/fox2code/mmm/utils/ProcessHelper.java diff --git a/app/build.gradle b/app/build.gradle index 86e04a8..3f188f2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,11 +15,14 @@ android { Properties properties = new Properties() if (project.rootProject.file('local.properties').exists()) { properties.load(project.rootProject.file('local.properties').newDataInputStream()) - // FFS DO NOT CHANGE THIS, IT WILL BREAK THE BUILD - storeFile file(properties.getProperty('keystore.file')) - storePassword properties.getProperty('keystore.password') - keyAlias 'key0' - keyPassword properties.getProperty('keystore.password') + // java.lang.IllegalArgumentException: path may not be null or empty string. path='null' + if (properties.getProperty('keystore.file') != null) { + // FFS DO NOT CHANGE THIS, IT WILL BREAK THE BUILD + storeFile file(properties.getProperty('keystore.file')) + storePassword properties.getProperty('keystore.password') + keyAlias 'key0' + keyPassword properties.getProperty('keystore.password') + } } } } diff --git a/app/src/main/java/com/fox2code/mmm/MainActivity.java b/app/src/main/java/com/fox2code/mmm/MainActivity.java index 4425a1e..d9d8b6b 100644 --- a/app/src/main/java/com/fox2code/mmm/MainActivity.java +++ b/app/src/main/java/com/fox2code/mmm/MainActivity.java @@ -47,11 +47,13 @@ import com.fox2code.mmm.manager.ModuleManager; import com.fox2code.mmm.module.ModuleViewAdapter; import com.fox2code.mmm.module.ModuleViewListBuilder; import com.fox2code.mmm.repo.RepoManager; +import com.fox2code.mmm.sentry.SentryMain; import com.fox2code.mmm.settings.SettingsActivity; import com.fox2code.mmm.utils.BlurUtils; import com.fox2code.mmm.utils.ExternalHelper; import com.fox2code.mmm.utils.Http; import com.fox2code.mmm.utils.IntentHelper; +import com.fox2code.mmm.utils.ProcessHelper; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.materialswitch.MaterialSwitch; import com.google.android.material.progressindicator.LinearProgressIndicator; @@ -88,6 +90,7 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe private SearchView searchView; private boolean initMode; private boolean doSetupNowRunning; + private boolean doSetupRestarting; private boolean urlFactoryInstalled = false; public MainActivity() { @@ -180,6 +183,7 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe this.cardIconifyUpdate(); this.updateScreenInsets(this.getResources().getConfiguration()); + checkShowInitialSetup(); InstallerInitializer.tryGetMagiskPathAsync(new InstallerInitializer.Callback() { @Override public void onPathReceived(String path) { @@ -201,23 +205,15 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe } public void commonNext() { - doSetupNowRunning = true; - doSetupNow(); - - // Wait for doSetupNow to finish - while (doSetupNowRunning) { - try { - //noinspection BusyWait - Thread.sleep(100); - } catch (InterruptedException ignored) { - } + updateScreenInsets(); // Fix an edge case + if (waitInitialSetupFinished()) { + return; } /*if (BuildConfig.DEBUG) { SharedPreferences prefs = MainApplication.getSharedPreferences(); if (BuildConfig.DEBUG) Log.d("MainActivity", String.format("Background update check: %s, Crash reporting: %s, Androidacy repo: %s, Magisk alt repo: %s", prefs.getBoolean("pref_background_update_check", false), prefs.getBoolean("pref_crash_reporting", false), prefs.getBoolean("pref_androidacy_repo_enabled", false), prefs.getBoolean("pref_magisk_alt_repo_enabled", false))); }*/ swipeRefreshBlocker = System.currentTimeMillis() + 5_000L; - updateScreenInsets(); // Fix an edge case if (MainApplication.isShowcaseMode()) moduleViewListBuilder.addNotification(NotificationType.SHOWCASE_MODE); if (!Http.hasWebView()) // Check Http for WebView availability @@ -518,7 +514,10 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe @Override public void onRefresh() { - if (this.swipeRefreshBlocker > System.currentTimeMillis() || this.initMode || this.progressIndicator == null || this.progressIndicator.getVisibility() == View.VISIBLE) { + if (this.swipeRefreshBlocker > System.currentTimeMillis() || + this.initMode || this.progressIndicator == null || + this.progressIndicator.getVisibility() == View.VISIBLE || + this.doSetupNowRunning) { this.swipeRefreshLayout.setRefreshing(false); return; // Do not double scan } @@ -702,13 +701,14 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe // Method to show a setup box on first launch @SuppressLint({"InflateParams", "RestrictedApi", "UnspecifiedImmutableFlag", "ApplySharedPref"}) - private void doSetupNow() { + private void checkShowInitialSetup() { if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Do setup now"); // Check if this is the first launch SharedPreferences prefs = MainApplication.getSharedPreferences(); - boolean firstLaunch = MainApplication.getSharedPreferences().getBoolean("first_launch", true); + boolean firstLaunch = prefs.getBoolean("first_launch", true); if (BuildConfig.DEBUG) Log.d("Noodle", "First launch: " + firstLaunch); if (firstLaunch) { + doSetupNowRunning = true; // Show setup box runOnUiThread(() -> { MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this); @@ -720,26 +720,30 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe // For sdk >= 31, use MaterialSwitch instead of MaterialSwitch // For now, we'll just have the positive button save the preferences and dismiss the dialog builder.setPositiveButton(R.string.setup_button, (dialog, which) -> { - // Set the preferences - prefs.edit().putBoolean("pref_background_update_check", - ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_background_update_check))).isChecked()).commit(); - prefs.edit().putBoolean("pref_crash_reporting", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_crash_reporting))).isChecked()).commit(); - prefs.edit().putBoolean("pref_androidacy_repo_enabled", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_androidacy_repo))).isChecked()).commit(); - prefs.edit().putBoolean("pref_magisk_alt_repo_enabled", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_magisk_alt_repo))).isChecked()).commit(); + // Set the preferences and pref_first_launch to false + prefs.edit().putBoolean("first_launch", false) + .putBoolean("pref_background_update_check", ((MaterialSwitch) + Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_background_update_check))).isChecked()) + .putBoolean("pref_crash_reporting", + ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_crash_reporting))).isChecked()) + .putBoolean("pref_androidacy_repo_enabled", ((MaterialSwitch) + Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_androidacy_repo))).isChecked()) + .putBoolean("pref_magisk_alt_repo_enabled", ((MaterialSwitch) + Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_magisk_alt_repo))).isChecked()).apply(); if (BuildConfig.DEBUG) { - Log.d("MainActivity", String.format("Background update check: %s, Crash reporting: %s, Androidacy repo: %s, Magisk alt repo: %s", prefs.getBoolean("pref_background_update_check", false), prefs.getBoolean("pref_crash_reporting", false), prefs.getBoolean("pref_androidacy_repo_enabled", false), prefs.getBoolean("pref_magisk_alt_repo_enabled", false))); + Log.d("MainActivity", String.format("Background update check: %s, Crash reporting: %s, Androidacy repo: %s, Magisk alt repo: %s", + prefs.getBoolean("pref_background_update_check", false), prefs.getBoolean("pref_crash_reporting", false), + prefs.getBoolean("pref_androidacy_repo_enabled", false), prefs.getBoolean("pref_magisk_alt_repo_enabled", false))); + } + // Only for sentry switching we need to restart I think? + if (SentryMain.isSentryEnabled() != MainApplication.isCrashReportingEnabled()) { + doSetupRestarting = true; + ProcessHelper.restartApplicationProcess(this); } - // Set pref_first_launch to false - MainApplication.getSharedPreferences().edit().putBoolean("first_launch", false).commit(); - // Restart the app - Intent intent = new Intent(this, MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - finish(); - startActivity(intent); ensurePermissions(); }); builder.setNegativeButton(R.string.setup_button_skip, (dialog, which) -> { - MainApplication.getSharedPreferences().edit().putBoolean("first_launch", false).commit(); + MainApplication.getSharedPreferences().edit().putBoolean("first_launch", false).apply(); dialog.dismiss(); ensurePermissions(); }); @@ -755,4 +759,22 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe ensurePermissions(); } } + + /** + * @return true if the load workflow must be stopped. + */ + private boolean waitInitialSetupFinished() { + if (BuildConfig.DEBUG) Log.d("NoodleDebug", "waitInitialSetupFinished"); + if (doSetupNowRunning) updateScreenInsets(); // Fix an edge case + try { + // Wait for doSetupNow to finish + while (doSetupNowRunning) { + //noinspection BusyWait + Thread.sleep(50); + } + } catch (InterruptedException e) { + return true; + } + return doSetupRestarting; + } } \ No newline at end of file diff --git a/app/src/main/java/com/fox2code/mmm/MainApplication.java b/app/src/main/java/com/fox2code/mmm/MainApplication.java index f4e1fb9..8ed3ccb 100644 --- a/app/src/main/java/com/fox2code/mmm/MainApplication.java +++ b/app/src/main/java/com/fox2code/mmm/MainApplication.java @@ -61,6 +61,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con @SuppressLint("StaticFieldLeak") private static MainApplication INSTANCE; private static boolean firstBoot; + private static boolean loadSentryInitialized; static { Shell.setDefaultBuilder(shellBuilder = Shell.Builder.create().setFlags(Shell.FLAG_REDIRECT_STDERR).setTimeout(10).setInitializers(InstallerInitializer.class)); @@ -168,7 +169,9 @@ public class MainApplication extends FoxApplication implements androidx.work.Con } public static boolean isCrashReportingEnabled() { - return getSharedPreferences().getBoolean("pref_crash_reporting", BuildConfig.DEFAULT_ENABLE_CRASH_REPORTING); + return SentryMain.IS_SENTRY_INSTALLED && + getSharedPreferences().getBoolean("pref_crash_reporting", + BuildConfig.DEFAULT_ENABLE_CRASH_REPORTING); } public static SharedPreferences getBootSharedPreferences() { @@ -306,7 +309,6 @@ public class MainApplication extends FoxApplication implements androidx.work.Con Log.d("MainApplication", "Emoji compat loaded!"); }, "Emoji compat init.").start(); } - SentryMain.initialize(this); if (Objects.equals(BuildConfig.ANDROIDACY_CLIENT_ID, "")) { Log.w("MainApplication", "Androidacy client id is empty! Please set it in androidacy" + ".properties. Will not enable Androidacy."); diff --git a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java index 9a10a45..9babb13 100644 --- a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java +++ b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java @@ -62,6 +62,7 @@ import com.fox2code.mmm.sentry.SentryMain; import com.fox2code.mmm.utils.ExternalHelper; import com.fox2code.mmm.utils.Http; import com.fox2code.mmm.utils.IntentHelper; +import com.fox2code.mmm.utils.ProcessHelper; import com.fox2code.rosettax.LanguageActivity; import com.fox2code.rosettax.LanguageSwitcher; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -148,14 +149,7 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { @Override @SuppressLint("InlinedApi") public void refreshRosettaX() { - Intent mStartActivity = new Intent(this, MainActivity.class); - mStartActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); - int mPendingIntentId = 123456; - PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, - mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE); - AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); - mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); - System.exit(0); // Exit app process + ProcessHelper.restartApplicationProcess(this); } @Override diff --git a/app/src/main/java/com/fox2code/mmm/utils/ProcessHelper.java b/app/src/main/java/com/fox2code/mmm/utils/ProcessHelper.java new file mode 100644 index 0000000..69e7f6f --- /dev/null +++ b/app/src/main/java/com/fox2code/mmm/utils/ProcessHelper.java @@ -0,0 +1,22 @@ +package com.fox2code.mmm.utils; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; + +import com.fox2code.mmm.MainActivity; + +public class ProcessHelper { + private static final int sPendingIntentId = 123456; + + public static void restartApplicationProcess(Context context) { + Intent mStartActivity = new Intent(context, MainActivity.class); + mStartActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); + PendingIntent mPendingIntent = PendingIntent.getActivity(context, sPendingIntentId, + mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE); + AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); + System.exit(0); // Exit app process + } +} diff --git a/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java b/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java index ceeae18..30c390f 100644 --- a/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java +++ b/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java @@ -17,6 +17,7 @@ import io.sentry.android.fragment.FragmentLifecycleIntegration; public class SentryMain { public static final boolean IS_SENTRY_INSTALLED = true; private static final String TAG = "SentryMain"; + private static boolean sentryEnabled = false; /** * Initialize Sentry @@ -39,6 +40,7 @@ public class SentryMain { if (!MainApplication.isCrashReportingEnabled()) { options.setDsn(""); } else { + sentryEnabled = true; // Set sentry state to enabled options.addIntegration(new FragmentLifecycleIntegration(mainApplication, true, true)); options.setCollectAdditionalContext(true); options.setAttachThreads(true); @@ -82,4 +84,8 @@ public class SentryMain { Sentry.addBreadcrumb(sentryBreadcrumb.breadcrumb); } } + + public static boolean isSentryEnabled() { + return sentryEnabled; + } } diff --git a/app/src/sentryless/java/com/fox2code/mmm/sentry/SentryMain.java b/app/src/sentryless/java/com/fox2code/mmm/sentry/SentryMain.java index 7d3e354..d8e9138 100644 --- a/app/src/sentryless/java/com/fox2code/mmm/sentry/SentryMain.java +++ b/app/src/sentryless/java/com/fox2code/mmm/sentry/SentryMain.java @@ -8,4 +8,6 @@ public class SentryMain { public static void initialize(MainApplication mainApplication) {} public static void addSentryBreadcrumb(SentryBreadcrumb sentryBreadcrumb) {} + + public static boolean isSentryEnabled() { return false; } }