-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathSettingsResourceRegistry.java
More file actions
153 lines (128 loc) · 5.07 KB
/
SettingsResourceRegistry.java
File metadata and controls
153 lines (128 loc) · 5.07 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package org.schabi.newpipe.settings;
import androidx.annotation.NonNull;
import androidx.annotation.XmlRes;
import androidx.fragment.app.Fragment;
import org.schabi.newpipe.R;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* A registry that contains information about SettingsFragments.
* <br/>
* includes:
* <ul>
* <li>Class of the SettingsFragment</li>
* <li>XML-Resource</li>
* <li>...</li>
* </ul>
*
* E.g. used by the preference search.
*/
public final class SettingsResourceRegistry {
private static final SettingsResourceRegistry INSTANCE = new SettingsResourceRegistry();
private final Set<SettingRegistryEntry> registeredEntries = new HashSet<>();
private SettingsResourceRegistry() {
add(MainSettingsFragment.class, R.xml.main_settings).setSearchable(false);
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.notifications_settings);
add(PlayerNotificationSettingsFragment.class, R.xml.player_notification_settings);
add(UpdateSettingsFragment.class, R.xml.update_settings);
add(VideoAudioSettingsFragment.class, R.xml.video_audio_settings);
add(ExoPlayerSettingsFragment.class, R.xml.exoplayer_settings);
add(BackupRestoreSettingsFragment.class, R.xml.backup_restore_settings);
add(ProxySettingsFragment.class, R.xml.proxy_settings);
}
private SettingRegistryEntry add(
@NonNull final Class<? extends Fragment> fragmentClass,
@XmlRes final int preferencesResId
) {
final SettingRegistryEntry entry =
new SettingRegistryEntry(fragmentClass, preferencesResId);
this.registeredEntries.add(entry);
return entry;
}
public SettingRegistryEntry getEntryByFragmentClass(
final Class<? extends Fragment> fragmentClass
) {
Objects.requireNonNull(fragmentClass);
return registeredEntries.stream()
.filter(e -> Objects.equals(e.getFragmentClass(), fragmentClass))
.findFirst()
.orElse(null);
}
public SettingRegistryEntry getEntryByPreferencesResId(@XmlRes final int preferencesResId) {
return registeredEntries.stream()
.filter(e -> Objects.equals(e.getPreferencesResId(), preferencesResId))
.findFirst()
.orElse(null);
}
public int getPreferencesResId(@NonNull final Class<? extends Fragment> fragmentClass) {
final SettingRegistryEntry entry = getEntryByFragmentClass(fragmentClass);
if (entry == null) {
return -1;
}
return entry.getPreferencesResId();
}
public Class<? extends Fragment> getFragmentClass(@XmlRes final int preferencesResId) {
final SettingRegistryEntry entry = getEntryByPreferencesResId(preferencesResId);
if (entry == null) {
return null;
}
return entry.getFragmentClass();
}
public Set<SettingRegistryEntry> getAllEntries() {
return new HashSet<>(registeredEntries);
}
public static SettingsResourceRegistry getInstance() {
return INSTANCE;
}
public static class SettingRegistryEntry {
@NonNull
private final Class<? extends Fragment> fragmentClass;
@XmlRes
private final int preferencesResId;
private boolean searchable = true;
public SettingRegistryEntry(
@NonNull final Class<? extends Fragment> fragmentClass,
@XmlRes final int preferencesResId
) {
this.fragmentClass = Objects.requireNonNull(fragmentClass);
this.preferencesResId = preferencesResId;
}
@SuppressWarnings("HiddenField")
public SettingRegistryEntry setSearchable(final boolean searchable) {
this.searchable = searchable;
return this;
}
@NonNull
public Class<? extends Fragment> getFragmentClass() {
return fragmentClass;
}
public int getPreferencesResId() {
return preferencesResId;
}
public boolean isSearchable() {
return searchable;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final SettingRegistryEntry that = (SettingRegistryEntry) o;
return getPreferencesResId() == that.getPreferencesResId()
&& getFragmentClass().equals(that.getFragmentClass());
}
@Override
public int hashCode() {
return Objects.hash(getFragmentClass(), getPreferencesResId());
}
}
}