From 6b23df06592ccbe52534d43448310125685c9eaa Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Wed, 29 Dec 2021 18:12:33 +0100 Subject: [PATCH] Made debug settings searchable (debug only) * Consolidated main-setttings into a single file * Debug settings are only enabled in the DEBUG build * Moved LeakCanary (debug) specific stuff into a small class that's only shipped with the debug build * Other minor fixes --- .../settings/DebugSettingsFragment.java | 50 ++++++++++++++++--- .../settings/MainSettingsFragment.java | 12 +++-- .../newpipe/settings/SettingsActivity.java | 7 +++ .../settings/SettingsResourceRegistry.java | 5 +- app/src/main/res/values/strings.xml | 1 + .../{debug => main}/res/xml/main_settings.xml | 0 app/src/release/res/xml/main_settings.xml | 49 ------------------ 7 files changed, 61 insertions(+), 63 deletions(-) rename app/src/{debug => main}/java/org/schabi/newpipe/settings/DebugSettingsFragment.java (54%) rename app/src/{debug => main}/res/xml/main_settings.xml (100%) delete mode 100644 app/src/release/res/xml/main_settings.xml diff --git a/app/src/debug/java/org/schabi/newpipe/settings/DebugSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/DebugSettingsFragment.java similarity index 54% rename from app/src/debug/java/org/schabi/newpipe/settings/DebugSettingsFragment.java rename to app/src/main/java/org/schabi/newpipe/settings/DebugSettingsFragment.java index f48be553f..710a440e1 100644 --- a/app/src/debug/java/org/schabi/newpipe/settings/DebugSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/DebugSettingsFragment.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.settings; +import android.content.Intent; import android.os.Bundle; import androidx.preference.Preference; @@ -10,13 +11,15 @@ import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.error.UserAction; import org.schabi.newpipe.util.PicassoHelper; -import leakcanary.LeakCanary; +import java.util.Optional; public class DebugSettingsFragment extends BasePreferenceFragment { @Override public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { - addPreferencesFromResource(R.xml.debug_settings); + addPreferencesFromResourceRegistry(); + final Preference allowHeapDumpingPreference + = findPreference(getString(R.string.allow_heap_dumping_key)); final Preference showMemoryLeaksPreference = findPreference(getString(R.string.show_memory_leaks_key)); final Preference showImageIndicatorsPreference @@ -28,16 +31,29 @@ public class DebugSettingsFragment extends BasePreferenceFragment { final Preference createErrorNotificationPreference = findPreference(getString(R.string.create_error_notification_key)); + assert allowHeapDumpingPreference != null; assert showMemoryLeaksPreference != null; assert showImageIndicatorsPreference != null; assert crashTheAppPreference != null; assert showErrorSnackbarPreference != null; assert createErrorNotificationPreference != null; - showMemoryLeaksPreference.setOnPreferenceClickListener(preference -> { - startActivity(LeakCanary.INSTANCE.newLeakDisplayActivityIntent()); - return true; - }); + final Optional optPDLeakCanary = getBVLeakCanary(); + + allowHeapDumpingPreference.setEnabled(optPDLeakCanary.isPresent()); + showMemoryLeaksPreference.setEnabled(optPDLeakCanary.isPresent()); + + if (optPDLeakCanary.isPresent()) { + final DebugSettingsBVLeakCanaryAPI pdLeakCanary = optPDLeakCanary.get(); + + showMemoryLeaksPreference.setOnPreferenceClickListener(preference -> { + startActivity(pdLeakCanary.getNewLeakDisplayActivityIntent()); + return true; + }); + } else { + allowHeapDumpingPreference.setSummary(R.string.leak_canary_not_available); + showMemoryLeaksPreference.setSummary(R.string.leak_canary_not_available); + } showImageIndicatorsPreference.setOnPreferenceChangeListener((preference, newValue) -> { PicassoHelper.setIndicatorsEnabled((Boolean) newValue); @@ -60,4 +76,26 @@ public class DebugSettingsFragment extends BasePreferenceFragment { return true; }); } + + private Optional getBVLeakCanary() { + try { + // Try to find the implementation of the LeakCanary API + return Optional.of((DebugSettingsBVLeakCanaryAPI) + Class.forName(DebugSettingsBVLeakCanaryAPI.IMPL_CLASS).newInstance()); + } catch (final ClassNotFoundException + | IllegalAccessException | java.lang.InstantiationException e) { + return Optional.empty(); + } + } + + /** + * Build variant dependent leak canary API for this fragment. + * Why is LeakCanary not used directly? Because it can't be assured + */ + public interface DebugSettingsBVLeakCanaryAPI { + String IMPL_CLASS = + "org.schabi.newpipe.settings.DebugSettingsBVLeakCanary"; + + Intent getNewLeakDisplayActivityIntent(); + } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java index 6cd165861..d7fb559d6 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java @@ -6,7 +6,6 @@ import android.view.MenuInflater; import android.view.MenuItem; import androidx.annotation.NonNull; -import androidx.preference.Preference; import org.schabi.newpipe.App; import org.schabi.newpipe.CheckForNewAppVersion; @@ -26,12 +25,17 @@ public class MainSettingsFragment extends BasePreferenceFragment { // Check if the app is updatable if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) { - final Preference update - = findPreference(getString(R.string.update_pref_screen_key)); - getPreferenceScreen().removePreference(update); + getPreferenceScreen().removePreference( + findPreference(getString(R.string.update_pref_screen_key))); defaultPreferences.edit().putBoolean(getString(R.string.update_app_key), false).apply(); } + + // Hide debug preferences in RELEASE build variant + if (!DEBUG) { + getPreferenceScreen().removePreference( + findPreference(getString(R.string.debug_pref_screen_key))); + } } @Override diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java index 92bc21c9d..25d22dc9f 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java @@ -217,6 +217,13 @@ public class SettingsActivity extends AppCompatActivity implements .getEntryByPreferencesResId(R.xml.update_settings) .setSearchable(false); } + + // Hide debug preferences in RELEASE build variant + if (DEBUG) { + SettingsResourceRegistry.getInstance() + .getEntryByPreferencesResId(R.xml.debug_settings) + .setSearchable(true); + } } public void setMenuSearchItem(final MenuItem menuSearchItem) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java index c4db9f93d..c4751abea 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.settings; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.annotation.XmlRes; import androidx.fragment.app.Fragment; @@ -34,6 +33,7 @@ public final class SettingsResourceRegistry { add(AppearanceSettingsFragment.class, R.xml.appearance_settings); add(ContentSettingsFragment.class, R.xml.content_settings); + add(DebugSettingsFragment.class, R.xml.debug_settings).setSearchable(false); add(DownloadSettingsFragment.class, R.xml.download_settings); add(HistorySettingsFragment.class, R.xml.history_settings); add(NotificationSettingsFragment.class, R.xml.notification_settings); @@ -51,7 +51,6 @@ public final class SettingsResourceRegistry { return entry; } - @Nullable public SettingRegistryEntry getEntryByFragmentClass( final Class fragmentClass ) { @@ -62,7 +61,6 @@ public final class SettingsResourceRegistry { .orElse(null); } - @Nullable public SettingRegistryEntry getEntryByPreferencesResId(@XmlRes final int preferencesResId) { return registeredEntries.stream() .filter(e -> Objects.equals(e.getPreferencesResId(), preferencesResId)) @@ -78,7 +76,6 @@ public final class SettingsResourceRegistry { return entry.getPreferencesResId(); } - @Nullable public Class getFragmentClass(@XmlRes final int preferencesResId) { final SettingRegistryEntry entry = getEntryByPreferencesResId(preferencesResId); if (entry == null) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f7ad41ba8..64ed6980b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -441,6 +441,7 @@ Captions Modify player caption text scale and background styles. Requires app restart to take effect + LeakCanary is not available Memory leak monitoring may cause the app to become unresponsive when heap dumping Show memory leaks Report out-of-lifecycle errors diff --git a/app/src/debug/res/xml/main_settings.xml b/app/src/main/res/xml/main_settings.xml similarity index 100% rename from app/src/debug/res/xml/main_settings.xml rename to app/src/main/res/xml/main_settings.xml diff --git a/app/src/release/res/xml/main_settings.xml b/app/src/release/res/xml/main_settings.xml deleted file mode 100644 index 1d5241102..000000000 --- a/app/src/release/res/xml/main_settings.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - -