From ba1357e30761991c15f0c4a278200754d429a3dd Mon Sep 17 00:00:00 2001 From: androidacy-user Date: Sun, 18 Dec 2022 11:56:43 -0500 Subject: [PATCH] Work on SetupWizard Signed-off-by: androidacy-user --- .../java/com/fox2code/mmm/MainActivity.java | 16 +++++--- .../com/fox2code/mmm/MainApplication.java | 37 +++++++++++++++++++ .../mmm/installer/InstallerActivity.java | 8 +++- .../java/com/fox2code/mmm/repo/RepoData.java | 2 +- .../mmm/settings/SettingsActivity.java | 30 ++++++++++++--- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/root_preferences.xml | 8 +++- 7 files changed, 88 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/fox2code/mmm/MainActivity.java b/app/src/main/java/com/fox2code/mmm/MainActivity.java index eb3d4fb..98b5977 100644 --- a/app/src/main/java/com/fox2code/mmm/MainActivity.java +++ b/app/src/main/java/com/fox2code/mmm/MainActivity.java @@ -721,12 +721,12 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe @SuppressLint({"InflateParams", "RestrictedApi", "UnspecifiedImmutableFlag", "ApplySharedPref"}) private void checkShowInitialSetup() { if (BuildConfig.DEBUG) - Log.d("NoodleDebug", "Do setup now"); + Log.d("SetupWizard", "Do setup now"); // Check if this is the first launch SharedPreferences prefs = MainApplication.getSharedPreferences(); boolean firstLaunch = prefs.getBoolean("first_launch", true); if (BuildConfig.DEBUG) - Log.d("NoodleDebug", "First launch: " + firstLaunch); + Log.d("SetupWizard", "First launch: " + firstLaunch); if (firstLaunch) { doSetupNowRunning = true; // Show setup box @@ -742,12 +742,18 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe builder.setPositiveButton(R.string.setup_button, (dialog, which) -> { // 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()).commit(); + // For debug builds, log the preferences if (BuildConfig.DEBUG) { - Log.d("NoodleDebug", String.format("Setup: 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("SetupWizard", "First launch: " + prefs.getBoolean("first_launch", true)); + Log.d("SetupWizard", "Background update check: " + prefs.getBoolean("pref_background_update_check", false)); + Log.d("SetupWizard", "Crash reporting: " + prefs.getBoolean("pref_crash_reporting", false)); + Log.d("SetupWizard", "Androidacy repo: " + prefs.getBoolean("pref_androidacy_repo_enabled", false)); + Log.d("SetupWizard", "Magisk alt repo: " + prefs.getBoolean("pref_magisk_alt_repo_enabled", false)); } + dialog.dismiss(); // Sleep for 100ms. Who knows, it might fix it? try { - Thread.sleep(500); + Thread.sleep(750); } catch (InterruptedException e) { e.printStackTrace(); } @@ -781,7 +787,7 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe */ private boolean waitInitialSetupFinished() { if (BuildConfig.DEBUG) - Log.d("NoodleDebug", "waitInitialSetupFinished"); + Log.d("SetupWizard", "waitInitialSetupFinished"); if (doSetupNowRunning) updateScreenInsets(); // Fix an edge case try { diff --git a/app/src/main/java/com/fox2code/mmm/MainApplication.java b/app/src/main/java/com/fox2code/mmm/MainApplication.java index 8ed3ccb..cde5881 100644 --- a/app/src/main/java/com/fox2code/mmm/MainApplication.java +++ b/app/src/main/java/com/fox2code/mmm/MainApplication.java @@ -13,6 +13,7 @@ import android.text.SpannableStringBuilder; import android.util.Log; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.annotation.StyleRes; import androidx.core.app.NotificationManagerCompat; import androidx.emoji2.text.DefaultEmojiCompatConfig; @@ -30,6 +31,7 @@ import com.fox2code.mmm.utils.Http; import com.fox2code.rosettax.LanguageSwitcher; import com.topjohnwu.superuser.Shell; +import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; @@ -344,6 +346,41 @@ public class MainApplication extends FoxApplication implements androidx.work.Con super.onConfigurationChanged(newConfig); } + @RequiresApi(api = Build.VERSION_CODES.N) + public void clearAppData() { + // Clear app data + try { + // Clearing app data + // We have to manually delete the files and directories + // because the cache directory is not cleared by the following method + File cacheDir = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + cacheDir = this.getDataDir(); + } + if (cacheDir != null && cacheDir.isDirectory()) { + String[] children = cacheDir.list(); + if (children != null) { + for (String s : children) { + if (BuildConfig.DEBUG) Log.w("MainApplication", "Deleting " + s); + if (!s.equals("lib")) { + new File(cacheDir, s).delete(); + } + } + } + } + if (BuildConfig.DEBUG) Log.w("MainApplication", "Deleting cache dir"); + this.deleteSharedPreferences("mmm_boot"); + this.deleteSharedPreferences("mmm"); + this.deleteSharedPreferences("sentry"); + this.deleteSharedPreferences("androidacy"); + if (BuildConfig.DEBUG) Log.w("MainApplication", "Deleting shared prefs"); + this.getPackageManager().clearPackagePreferredActivities(this.getPackageName()); + if (BuildConfig.DEBUG) Log.w("MainApplication", "Done clearing app data"); + } catch (Exception e) { + Log.e("MainApplication", "Failed to clear app data", e); + } + } + private class Prism4jSwitchTheme implements Prism4jTheme { private final Prism4jTheme light = new Prism4jThemeDefault(Color.TRANSPARENT); private final Prism4jTheme dark = new Prism4jThemeDarkula(Color.TRANSPARENT); diff --git a/app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java b/app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java index b2ea81b..40ac127 100644 --- a/app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java +++ b/app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java @@ -760,7 +760,7 @@ public class InstallerActivity extends FoxActivity { this.progressIndicator.setVisibility(View.GONE); // This should be improved ? - String reboot_cmd = "/system/bin/svc power reboot || /system/bin/reboot"; + String reboot_cmd = "/system/bin/svc power reboot || /system/bin/reboot || setprop sys.powerctl reboot"; this.rebootFloatingButton.setOnClickListener(_view -> { if (this.warnReboot || MainApplication.shouldPreventReboot()) { MaterialAlertDialogBuilder builder = @@ -768,9 +768,10 @@ public class InstallerActivity extends FoxActivity { builder .setTitle(R.string.install_terminal_reboot_now) + .setMessage(R.string.install_terminal_reboot_now_message) .setCancelable(false) .setIcon(R.drawable.ic_reboot_24) - .setPositiveButton(R.string.yes, (x, y) -> Shell.cmd(reboot_cmd).submit()) + .setPositiveButton(R.string.ok, (x, y) -> Shell.cmd(reboot_cmd).submit()) .setNegativeButton(R.string.no, (x, y) -> x.dismiss()).show(); } else { Shell.cmd(reboot_cmd).submit(); @@ -801,6 +802,9 @@ public class InstallerActivity extends FoxActivity { } catch (PackageManager.NameNotFoundException e) { Log.w(TAG, "Config package \"" + configPkg + "\" missing for installer view"); + this.installerTerminal.addLine(String.format( + this.getString(R.string.install_terminal_config_missing), + configPkg)); } } } diff --git a/app/src/main/java/com/fox2code/mmm/repo/RepoData.java b/app/src/main/java/com/fox2code/mmm/repo/RepoData.java index 36c0c0f..7820678 100644 --- a/app/src/main/java/com/fox2code/mmm/repo/RepoData.java +++ b/app/src/main/java/com/fox2code/mmm/repo/RepoData.java @@ -202,7 +202,7 @@ public class RepoData extends XRepo { @Override public boolean isEnabled() { - return MainApplication.getSharedPreferences().getBoolean("pref_" + this.getPreferenceId() + "_enabled", this.isEnabledByDefault()); + return this.enabled; } @Override 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 9babb13..a72f1af 100644 --- a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java +++ b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java @@ -28,6 +28,7 @@ import android.widget.Button; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; @@ -159,6 +160,7 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { } public static class SettingsFragment extends PreferenceFragmentCompat implements FoxActivity.OnBackPressedCallback { + @RequiresApi(api = Build.VERSION_CODES.N) @SuppressLint("UnspecifiedImmutableFlag") @Override @SuppressWarnings("ConstantConditions") @@ -397,12 +399,30 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { // Hide the pref_crash option if not in debug mode - stop users from purposely crashing the app Log.d(TAG, String.format("Sentry installed: %s, debug: %s, magisk path: %s", SentryMain.IS_SENTRY_INSTALLED, BuildConfig.DEBUG, InstallerInitializer.peekMagiskPath())); - Objects.requireNonNull((Preference) findPreference("pref_crash")).setVisible(false); + Objects.requireNonNull((Preference) findPreference("pref_test_crash")).setVisible(false); + // Find pref_clear_data and set it invisible + Objects.requireNonNull((Preference) findPreference("pref_clear_data")).setVisible(false); } else { - findPreference("pref_crash").setOnPreferenceClickListener(preference -> { - // Hard crash the app - throw new Error("This is a test crash"); - }); + if (findPreference("pref_test_crash") != null && findPreference("pref_clear_data") != null) { + findPreference("pref_test_crash").setOnPreferenceClickListener(preference -> { + // Hard crash the app + throw new Error("This is a test crash"); + }); + findPreference("pref_clear_data").setOnPreferenceClickListener(preference -> { + // Clear app data + new MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.clear_data_dialogue_title).setMessage(R.string.clear_data_dialogue_message).setPositiveButton(R.string.yes, (dialog, which) -> { + // Clear app data + MainApplication.getINSTANCE().clearAppData(); + // Restart app + ProcessHelper.restartApplicationProcess(requireContext()); + }).setNegativeButton(R.string.no, (dialog, which) -> { + }).show(); + return true; + }); + } else { + Log.e(TAG, String.format("Something is null: %s, %s", + findPreference("pref_test_crash"), findPreference("pref_clear_data"))); + } } if (InstallerInitializer.peekMagiskVersion() < Constants.MAGISK_VER_CODE_INSTALL_COMMAND || !MainApplication.isDeveloper()) { findPreference("pref_use_magisk_install_command").setVisible(false); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8d360d5..ee982d2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -236,5 +236,5 @@ Skip Enabling blur on lower-end device You are trying to enable blur on a device that may not perform well with it.\nYou may enable it, but this may lead to a poor user experience and we recommend you don\'t. - This repo has less restrictions and reviews, which may lead to lower quality modules. Pretty barebones but has a lot of modules. + This repo has less restrictions and reviews, which may lead to lower quality modules. Pretty barebones but has a lot of modules.You are about to reboot your device. If you\'ve saved your work, hit ok to continue. Otherwise, hit cancel.Package %s is missing for module config, so we cannot launch it.Clear app dataClear app data?You\'re about to clear the app data. Please confirm this action. diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index ec5e7e6..56ec038 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -126,9 +126,15 @@ + +