forked from TeamNewPipe/NewPipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugSettingsFragment.java
More file actions
96 lines (78 loc) · 3.82 KB
/
DebugSettingsFragment.java
File metadata and controls
96 lines (78 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* SPDX-FileCopyrightText: 2017-2025 NewPipe contributors <https://newpipe.net>
* SPDX-FileCopyrightText: 2025-2026 NewPipe e.V. <https://newpipe-ev.de>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package org.schabi.newpipe.settings;
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.local.feed.notifications.NotificationWorker;
import java.util.Optional;
public class DebugSettingsFragment extends BasePreferenceFragment {
private static final String DUMMY = "Dummy";
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResourceRegistry();
final Preference allowHeapDumpingPreference =
requirePreference(R.string.allow_heap_dumping_key);
final Preference showMemoryLeaksPreference =
requirePreference(R.string.show_memory_leaks_key);
final Preference checkNewStreamsPreference =
requirePreference(R.string.check_new_streams_key);
final Preference crashTheAppPreference =
requirePreference(R.string.crash_the_app_key);
final Preference showErrorSnackbarPreference =
requirePreference(R.string.show_error_snackbar_key);
final Preference createErrorNotificationPreference =
requirePreference(R.string.create_error_notification_key);
final Optional<DebugSettingsBVDLeakCanaryAPI> optBVLeakCanary = getBVDLeakCanary();
allowHeapDumpingPreference.setEnabled(optBVLeakCanary.isPresent());
showMemoryLeaksPreference.setEnabled(optBVLeakCanary.isPresent());
if (optBVLeakCanary.isPresent()) {
final DebugSettingsBVDLeakCanaryAPI pdLeakCanary = optBVLeakCanary.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);
}
checkNewStreamsPreference.setOnPreferenceClickListener(preference -> {
NotificationWorker.runNow(preference.getContext());
return true;
});
crashTheAppPreference.setOnPreferenceClickListener(preference -> {
throw new RuntimeException(DUMMY);
});
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;
});
}
/**
* Tries to find the {@link DebugSettingsBVDLeakCanaryAPI#IMPL_CLASS} and loads it if available.
* @return An {@link Optional} which is empty if the implementation class couldn't be loaded.
*/
private Optional<DebugSettingsBVDLeakCanaryAPI> getBVDLeakCanary() {
try {
// Try to find the implementation of the LeakCanary API
return Optional.of((DebugSettingsBVDLeakCanaryAPI)
Class.forName(DebugSettingsBVDLeakCanaryAPI.IMPL_CLASS)
.getDeclaredConstructor()
.newInstance());
} catch (final Exception e) {
return Optional.empty();
}
}
}