1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2026-04-19 21:31:24 +00:00

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
This commit is contained in:
litetex
2021-12-29 18:12:33 +01:00
parent d59314801c
commit 6b23df0659
7 changed files with 61 additions and 63 deletions

View File

@@ -0,0 +1,101 @@
package org.schabi.newpipe.settings;
import android.content.Intent;
import android.os.Bundle;
import androidx.preference.Preference;
import org.schabi.newpipe.R;
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.util.PicassoHelper;
import java.util.Optional;
public class DebugSettingsFragment extends BasePreferenceFragment {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
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
= findPreference(getString(R.string.show_image_indicators_key));
final Preference crashTheAppPreference
= findPreference(getString(R.string.crash_the_app_key));
final Preference showErrorSnackbarPreference
= findPreference(getString(R.string.show_error_snackbar_key));
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;
final Optional<DebugSettingsBVLeakCanaryAPI> 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);
return true;
});
crashTheAppPreference.setOnPreferenceClickListener(preference -> {
throw new RuntimeException();
});
showErrorSnackbarPreference.setOnPreferenceClickListener(preference -> {
ErrorUtil.showUiErrorSnackbar(DebugSettingsFragment.this,
"Dummy", new RuntimeException("Dummy"));
return true;
});
createErrorNotificationPreference.setOnPreferenceClickListener(preference -> {
ErrorUtil.createNotification(requireContext(),
new ErrorInfo(new RuntimeException("Dummy"), UserAction.UI_ERROR, "Dummy"));
return true;
});
}
private Optional<DebugSettingsBVLeakCanaryAPI> 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();
}
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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<? extends Fragment> 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<? extends Fragment> getFragmentClass(@XmlRes final int preferencesResId) {
final SettingRegistryEntry entry = getEntryByPreferencesResId(preferencesResId);
if (entry == null) {