forked from TeamNewPipe/NewPipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsActivity.java
More file actions
395 lines (330 loc) · 13.9 KB
/
SettingsActivity.java
File metadata and controls
395 lines (330 loc) · 13.9 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package org.schabi.newpipe.settings;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import com.jakewharton.rxbinding4.widget.RxTextView;
import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.databinding.SettingsLayoutBinding;
import org.schabi.newpipe.settings.preferencesearch.PreferenceParser;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchConfiguration;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchItem;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultHighlighter;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultListener;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearcher;
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.KeyboardUtil;
import org.schabi.newpipe.util.ReleaseVersionUtil;
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.views.FocusOverlayView;
import java.util.concurrent.TimeUnit;
import dagger.hilt.android.AndroidEntryPoint;
import icepick.Icepick;
import icepick.State;
/*
* Created by Christian Schabesberger on 31.08.15.
*
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
* SettingsActivity.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
@AndroidEntryPoint
public class SettingsActivity extends AppCompatActivity implements
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
PreferenceSearchResultListener {
private static final String TAG = "SettingsActivity";
private static final boolean DEBUG = MainActivity.DEBUG;
@IdRes
private static final int FRAGMENT_HOLDER_ID = R.id.settings_fragment_holder;
private PreferenceSearchFragment searchFragment;
@Nullable
private MenuItem menuSearchItem;
private View searchContainer;
private EditText searchEditText;
// State
@State
String searchText;
@State
boolean wasSearchActive;
@Override
protected void onCreate(final Bundle savedInstanceBundle) {
setTheme(ThemeHelper.getSettingsThemeStyle(this));
assureCorrectAppLanguage(this);
super.onCreate(savedInstanceBundle);
Icepick.restoreInstanceState(this, savedInstanceBundle);
final boolean restored = savedInstanceBundle != null;
final SettingsLayoutBinding settingsLayoutBinding =
SettingsLayoutBinding.inflate(getLayoutInflater());
setContentView(settingsLayoutBinding.getRoot());
initSearch(settingsLayoutBinding, restored);
setSupportActionBar(settingsLayoutBinding.settingsToolbarLayout.toolbar);
if (restored) {
// Restore state
if (this.wasSearchActive) {
setSearchActive(true);
if (!TextUtils.isEmpty(this.searchText)) {
this.searchEditText.setText(this.searchText);
}
}
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.settings_fragment_holder, new MainSettingsFragment())
.commit();
}
if (DeviceUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this);
}
}
@Override
protected void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this, outState);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public void onBackPressed() {
if (isSearchActive()) {
setSearchActive(false);
return;
}
super.onBackPressed();
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
if (id == android.R.id.home) {
// Check if the search is active and if so: Close it
if (isSearchActive()) {
setSearchActive(false);
return true;
}
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
finish();
} else {
getSupportFragmentManager().popBackStack();
}
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPreferenceStartFragment(@NonNull final PreferenceFragmentCompat caller,
final Preference preference) {
showSettingsFragment(instantiateFragment(preference.getFragment()));
return true;
}
private Fragment instantiateFragment(@NonNull final String className) {
return getSupportFragmentManager()
.getFragmentFactory()
.instantiate(this.getClassLoader(), className);
}
private void showSettingsFragment(final Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out,
R.animator.custom_fade_in, R.animator.custom_fade_out)
.replace(FRAGMENT_HOLDER_ID, fragment)
.addToBackStack(null)
.commit();
}
@Override
protected void onDestroy() {
setMenuSearchItem(null);
searchFragment = null;
super.onDestroy();
}
/*//////////////////////////////////////////////////////////////////////////
// Search
//////////////////////////////////////////////////////////////////////////*/
//region Search
private void initSearch(
final SettingsLayoutBinding settingsLayoutBinding,
final boolean restored
) {
searchContainer =
settingsLayoutBinding.settingsToolbarLayout.toolbar
.findViewById(R.id.toolbar_search_container);
// Configure input field for search
searchEditText = searchContainer.findViewById(R.id.toolbar_search_edit_text);
RxTextView.textChanges(searchEditText)
// Wait some time after the last input before actually searching
.debounce(200, TimeUnit.MILLISECONDS)
.subscribe(v -> runOnUiThread(this::onSearchChanged));
// Configure clear button
searchContainer.findViewById(R.id.toolbar_search_clear)
.setOnClickListener(ev -> resetSearchText());
ensureSearchRepresentsApplicationState();
// Build search configuration using SettingsResourceRegistry
final PreferenceSearchConfiguration config = new PreferenceSearchConfiguration();
// Build search items
final Context searchContext = getApplicationContext();
assureCorrectAppLanguage(searchContext);
final PreferenceParser parser = new PreferenceParser(searchContext, config);
final PreferenceSearcher searcher = new PreferenceSearcher(config);
// Find all searchable SettingsResourceRegistry fragments
SettingsResourceRegistry.getInstance().getAllEntries().stream()
.filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable)
// Get the resId
.map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId)
// Parse
.map(parser::parse)
// Add it to the searcher
.forEach(searcher::add);
if (restored) {
searchFragment = (PreferenceSearchFragment) getSupportFragmentManager()
.findFragmentByTag(PreferenceSearchFragment.NAME);
if (searchFragment != null) {
// Hide/Remove the search fragment otherwise we get an exception
// when adding it (because it's already present)
hideSearchFragment();
}
}
if (searchFragment == null) {
searchFragment = new PreferenceSearchFragment();
}
searchFragment.setSearcher(searcher);
}
/**
* Ensures that the search shows the correct/available search results.
* <br/>
* Some features are e.g. only available for debug builds, these should not
* be found when searching inside a release.
*/
private void ensureSearchRepresentsApplicationState() {
// Check if the update settings are available
if (!ReleaseVersionUtil.INSTANCE.isReleaseApk()) {
SettingsResourceRegistry.getInstance()
.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) {
this.menuSearchItem = menuSearchItem;
// Ensure that the item is in the correct state when adding it. This is due to
// Android's lifecycle (the Activity is recreated before the Fragment that registers this)
if (menuSearchItem != null) {
menuSearchItem.setVisible(!isSearchActive());
}
}
public void setSearchActive(final boolean active) {
if (DEBUG) {
Log.d(TAG, "setSearchActive called active=" + active);
}
// Ignore if search is already in correct state
if (isSearchActive() == active) {
return;
}
wasSearchActive = active;
searchContainer.setVisibility(active ? View.VISIBLE : View.GONE);
if (menuSearchItem != null) {
menuSearchItem.setVisible(!active);
}
if (active) {
getSupportFragmentManager()
.beginTransaction()
.add(FRAGMENT_HOLDER_ID, searchFragment, PreferenceSearchFragment.NAME)
.addToBackStack(PreferenceSearchFragment.NAME)
.commit();
KeyboardUtil.showKeyboard(this, searchEditText);
} else if (searchFragment != null) {
hideSearchFragment();
getSupportFragmentManager()
.popBackStack(
PreferenceSearchFragment.NAME,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
KeyboardUtil.hideKeyboard(this, searchEditText);
}
resetSearchText();
}
private void hideSearchFragment() {
getSupportFragmentManager().beginTransaction().remove(searchFragment).commit();
}
private void resetSearchText() {
searchEditText.setText("");
}
private boolean isSearchActive() {
return searchContainer.getVisibility() == View.VISIBLE;
}
private void onSearchChanged() {
if (!isSearchActive()) {
return;
}
if (searchFragment != null) {
searchText = this.searchEditText.getText().toString();
searchFragment.updateSearchResults(searchText);
}
}
@Override
public void onSearchResultClicked(@NonNull final PreferenceSearchItem result) {
if (DEBUG) {
Log.d(TAG, "onSearchResultClicked called result=" + result);
}
// Hide the search
setSearchActive(false);
// -- Highlight the result --
// Find out which fragment class we need
final Class<? extends Fragment> targetedFragmentClass =
SettingsResourceRegistry.getInstance()
.getFragmentClass(result.getSearchIndexItemResId());
if (targetedFragmentClass == null) {
// This should never happen
Log.w(TAG, "Unable to locate fragment class for resId="
+ result.getSearchIndexItemResId());
return;
}
// Check if the currentFragment is the one which contains the result
Fragment currentFragment =
getSupportFragmentManager().findFragmentById(FRAGMENT_HOLDER_ID);
if (!targetedFragmentClass.equals(currentFragment.getClass())) {
// If it's not the correct one display the correct one
currentFragment = instantiateFragment(targetedFragmentClass.getName());
showSettingsFragment(currentFragment);
}
// Run the highlighting
if (currentFragment instanceof PreferenceFragmentCompat) {
PreferenceSearchResultHighlighter
.highlight(result, (PreferenceFragmentCompat) currentFragment);
}
}
//endregion
}