Skip to content

Commit 6b23df0

Browse files
committed
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
1 parent d593148 commit 6b23df0

7 files changed

Lines changed: 61 additions & 63 deletions

File tree

app/src/debug/java/org/schabi/newpipe/settings/DebugSettingsFragment.java renamed to app/src/main/java/org/schabi/newpipe/settings/DebugSettingsFragment.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.settings;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45

56
import androidx.preference.Preference;
@@ -10,13 +11,15 @@
1011
import org.schabi.newpipe.error.UserAction;
1112
import org.schabi.newpipe.util.PicassoHelper;
1213

13-
import leakcanary.LeakCanary;
14+
import java.util.Optional;
1415

1516
public class DebugSettingsFragment extends BasePreferenceFragment {
1617
@Override
1718
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
18-
addPreferencesFromResource(R.xml.debug_settings);
19+
addPreferencesFromResourceRegistry();
1920

21+
final Preference allowHeapDumpingPreference
22+
= findPreference(getString(R.string.allow_heap_dumping_key));
2023
final Preference showMemoryLeaksPreference
2124
= findPreference(getString(R.string.show_memory_leaks_key));
2225
final Preference showImageIndicatorsPreference
@@ -28,16 +31,29 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
2831
final Preference createErrorNotificationPreference
2932
= findPreference(getString(R.string.create_error_notification_key));
3033

34+
assert allowHeapDumpingPreference != null;
3135
assert showMemoryLeaksPreference != null;
3236
assert showImageIndicatorsPreference != null;
3337
assert crashTheAppPreference != null;
3438
assert showErrorSnackbarPreference != null;
3539
assert createErrorNotificationPreference != null;
3640

37-
showMemoryLeaksPreference.setOnPreferenceClickListener(preference -> {
38-
startActivity(LeakCanary.INSTANCE.newLeakDisplayActivityIntent());
39-
return true;
40-
});
41+
final Optional<DebugSettingsBVLeakCanaryAPI> optPDLeakCanary = getBVLeakCanary();
42+
43+
allowHeapDumpingPreference.setEnabled(optPDLeakCanary.isPresent());
44+
showMemoryLeaksPreference.setEnabled(optPDLeakCanary.isPresent());
45+
46+
if (optPDLeakCanary.isPresent()) {
47+
final DebugSettingsBVLeakCanaryAPI pdLeakCanary = optPDLeakCanary.get();
48+
49+
showMemoryLeaksPreference.setOnPreferenceClickListener(preference -> {
50+
startActivity(pdLeakCanary.getNewLeakDisplayActivityIntent());
51+
return true;
52+
});
53+
} else {
54+
allowHeapDumpingPreference.setSummary(R.string.leak_canary_not_available);
55+
showMemoryLeaksPreference.setSummary(R.string.leak_canary_not_available);
56+
}
4157

4258
showImageIndicatorsPreference.setOnPreferenceChangeListener((preference, newValue) -> {
4359
PicassoHelper.setIndicatorsEnabled((Boolean) newValue);
@@ -60,4 +76,26 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
6076
return true;
6177
});
6278
}
79+
80+
private Optional<DebugSettingsBVLeakCanaryAPI> getBVLeakCanary() {
81+
try {
82+
// Try to find the implementation of the LeakCanary API
83+
return Optional.of((DebugSettingsBVLeakCanaryAPI)
84+
Class.forName(DebugSettingsBVLeakCanaryAPI.IMPL_CLASS).newInstance());
85+
} catch (final ClassNotFoundException
86+
| IllegalAccessException | java.lang.InstantiationException e) {
87+
return Optional.empty();
88+
}
89+
}
90+
91+
/**
92+
* Build variant dependent leak canary API for this fragment.
93+
* Why is LeakCanary not used directly? Because it can't be assured
94+
*/
95+
public interface DebugSettingsBVLeakCanaryAPI {
96+
String IMPL_CLASS =
97+
"org.schabi.newpipe.settings.DebugSettingsBVLeakCanary";
98+
99+
Intent getNewLeakDisplayActivityIntent();
100+
}
63101
}

app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.view.MenuItem;
77

88
import androidx.annotation.NonNull;
9-
import androidx.preference.Preference;
109

1110
import org.schabi.newpipe.App;
1211
import org.schabi.newpipe.CheckForNewAppVersion;
@@ -26,12 +25,17 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
2625

2726
// Check if the app is updatable
2827
if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) {
29-
final Preference update
30-
= findPreference(getString(R.string.update_pref_screen_key));
31-
getPreferenceScreen().removePreference(update);
28+
getPreferenceScreen().removePreference(
29+
findPreference(getString(R.string.update_pref_screen_key)));
3230

3331
defaultPreferences.edit().putBoolean(getString(R.string.update_app_key), false).apply();
3432
}
33+
34+
// Hide debug preferences in RELEASE build variant
35+
if (!DEBUG) {
36+
getPreferenceScreen().removePreference(
37+
findPreference(getString(R.string.debug_pref_screen_key)));
38+
}
3539
}
3640

3741
@Override

app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ private void prepareSearchConfig() {
217217
.getEntryByPreferencesResId(R.xml.update_settings)
218218
.setSearchable(false);
219219
}
220+
221+
// Hide debug preferences in RELEASE build variant
222+
if (DEBUG) {
223+
SettingsResourceRegistry.getInstance()
224+
.getEntryByPreferencesResId(R.xml.debug_settings)
225+
.setSearchable(true);
226+
}
220227
}
221228

222229
public void setMenuSearchItem(final MenuItem menuSearchItem) {

app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.settings;
22

33
import androidx.annotation.NonNull;
4-
import androidx.annotation.Nullable;
54
import androidx.annotation.XmlRes;
65
import androidx.fragment.app.Fragment;
76

@@ -34,6 +33,7 @@ private SettingsResourceRegistry() {
3433

3534
add(AppearanceSettingsFragment.class, R.xml.appearance_settings);
3635
add(ContentSettingsFragment.class, R.xml.content_settings);
36+
add(DebugSettingsFragment.class, R.xml.debug_settings).setSearchable(false);
3737
add(DownloadSettingsFragment.class, R.xml.download_settings);
3838
add(HistorySettingsFragment.class, R.xml.history_settings);
3939
add(NotificationSettingsFragment.class, R.xml.notification_settings);
@@ -51,7 +51,6 @@ private SettingRegistryEntry add(
5151
return entry;
5252
}
5353

54-
@Nullable
5554
public SettingRegistryEntry getEntryByFragmentClass(
5655
final Class<? extends Fragment> fragmentClass
5756
) {
@@ -62,7 +61,6 @@ public SettingRegistryEntry getEntryByFragmentClass(
6261
.orElse(null);
6362
}
6463

65-
@Nullable
6664
public SettingRegistryEntry getEntryByPreferencesResId(@XmlRes final int preferencesResId) {
6765
return registeredEntries.stream()
6866
.filter(e -> Objects.equals(e.getPreferencesResId(), preferencesResId))
@@ -78,7 +76,6 @@ public int getPreferencesResId(@NonNull final Class<? extends Fragment> fragment
7876
return entry.getPreferencesResId();
7977
}
8078

81-
@Nullable
8279
public Class<? extends Fragment> getFragmentClass(@XmlRes final int preferencesResId) {
8380
final SettingRegistryEntry entry = getEntryByPreferencesResId(preferencesResId);
8481
if (entry == null) {

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@
441441
<string name="caption_setting_title">Captions</string>
442442
<string name="caption_setting_description">Modify player caption text scale and background styles. Requires app restart to take effect</string>
443443
<!-- Debug Settings -->
444+
<string name="leak_canary_not_available">LeakCanary is not available</string>
444445
<string name="enable_leak_canary_summary">Memory leak monitoring may cause the app to become unresponsive when heap dumping</string>
445446
<string name="show_memory_leaks">Show memory leaks</string>
446447
<string name="enable_disposed_exceptions_title">Report out-of-lifecycle errors</string>

app/src/release/res/xml/main_settings.xml

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)