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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.android.settings.core;
import static android.content.Intent.EXTRA_USER_ID;
import static com.android.settings.dashboard.DashboardFragment.CATEGORY;
import android.annotation.IntDef;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.SettingsSlicesContract;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.Utils;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settings.slices.SliceData;
import com.android.settings.slices.Sliceable;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexableRaw;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
/**
* Abstract class to consolidate utility between preference controllers and act as an interface
* for Slices. The abstract classes that inherit from this class will act as the direct interfaces
* for each type when plugging into Slices.
*/
public abstract class BasePreferenceController extends AbstractPreferenceController implements
Sliceable {
private static final String TAG = "SettingsPrefController";
/**
* Denotes the availability of the Setting.
* <p>
* Used both explicitly and by the convenience methods {@link #isAvailable()} and
* {@link #isSupported()}.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef({AVAILABLE, AVAILABLE_UNSEARCHABLE, UNSUPPORTED_ON_DEVICE, DISABLED_FOR_USER,
DISABLED_DEPENDENT_SETTING, CONDITIONALLY_UNAVAILABLE})
public @interface AvailabilityStatus {
}
/**
* The setting is available, and searchable to all search clients.
*/
public static final int AVAILABLE = 0;
/**
* The setting is available, but is not searchable to any search client.
*/
public static final int AVAILABLE_UNSEARCHABLE = 1;
/**
* A generic catch for settings which are currently unavailable, but may become available in
* the future. You should use {@link #DISABLED_FOR_USER} or {@link #DISABLED_DEPENDENT_SETTING}
* if they describe the condition more accurately.
*/
public static final int CONDITIONALLY_UNAVAILABLE = 2;
/**
* The setting is not, and will not supported by this device.
* <p>
* There is no guarantee that the setting page exists, and any links to the Setting should take
* you to the home page of Settings.
*/
public static final int UNSUPPORTED_ON_DEVICE = 3;
/**
* The setting cannot be changed by the current user.
* <p>
* Links to the Setting should take you to the page of the Setting, even if it cannot be
* changed.
*/
public static final int DISABLED_FOR_USER = 4;
/**
* The setting has a dependency in the Settings App which is currently blocking access.
* <p>
* It must be possible for the Setting to be enabled by changing the configuration of the device
* settings. That is, a setting that cannot be changed because of the state of another setting.
* This should not be used for a setting that would be hidden from the UI entirely.
* <p>
* Correct use: Intensity of night display should be {@link #DISABLED_DEPENDENT_SETTING} when
* night display is off.
* Incorrect use: Mobile Data is {@link #DISABLED_DEPENDENT_SETTING} when there is no
* data-enabled sim.
* <p>
* Links to the Setting should take you to the page of the Setting, even if it cannot be
* changed.
*/
public static final int DISABLED_DEPENDENT_SETTING = 5;
protected final String mPreferenceKey;
protected UiBlockListener mUiBlockListener;
private boolean mIsForWork;
@Nullable
private UserHandle mWorkProfileUser;
private int mMetricsCategory;
/**
* Instantiate a controller as specified controller type and user-defined key.
* <p/>
* This is done through reflection. Do not use this method unless you know what you are doing.
*/
public static BasePreferenceController createInstance(Context context,
String controllerName, String key) {
try {
final Class<?> clazz = Class.forName(controllerName);
final Constructor<?> preferenceConstructor =
clazz.getConstructor(Context.class, String.class);
final Object[] params = new Object[]{context, key};
return (BasePreferenceController) preferenceConstructor.newInstance(params);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException(
"Invalid preference controller: " + controllerName, e);
}
}
/**
* Instantiate a controller as specified controller type.
* <p/>
* This is done through reflection. Do not use this method unless you know what you are doing.
*/
public static BasePreferenceController createInstance(Context context, String controllerName) {
try {
final Class<?> clazz = Class.forName(controllerName);
final Constructor<?> preferenceConstructor = clazz.getConstructor(Context.class);
final Object[] params = new Object[]{context};
return (BasePreferenceController) preferenceConstructor.newInstance(params);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException(
"Invalid preference controller: " + controllerName, e);
}
}
/**
* Instantiate a controller as specified controller type and work profile
* <p/>
* This is done through reflection. Do not use this method unless you know what you are doing.
*
* @param context application context
* @param controllerName class name of the {@link BasePreferenceController}
* @param key attribute android:key of the {@link Preference}
* @param isWorkProfile is this controller only for work profile user?
*/
public static BasePreferenceController createInstance(Context context, String controllerName,
String key, boolean isWorkProfile) {
try {
final Class<?> clazz = Class.forName(controllerName);
final Constructor<?> preferenceConstructor =
clazz.getConstructor(Context.class, String.class);
final Object[] params = new Object[]{context, key};
final BasePreferenceController controller =
(BasePreferenceController) preferenceConstructor.newInstance(params);
controller.setForWork(isWorkProfile);
return controller;
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException
| IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException(
"Invalid preference controller: " + controllerName, e);
}
}
public BasePreferenceController(Context context, String preferenceKey) {
super(context);
mPreferenceKey = preferenceKey;
if (TextUtils.isEmpty(mPreferenceKey)) {
throw new IllegalArgumentException("Preference key must be set");
}
}
/**
* @return {@link AvailabilityStatus} for the Setting. This status is used to determine if the
* Setting should be shown or disabled in Settings. Further, it can be used to produce
* appropriate error / warning Slice in the case of unavailability.
* </p>
* The status is used for the convenience methods: {@link #isAvailable()},
* {@link #isSupported()}
* </p>
* The inherited class doesn't need to check work profile if
* android:forWork="true" is set in preference xml.
*/
@AvailabilityStatus
public abstract int getAvailabilityStatus();
@Override
public String getPreferenceKey() {
return mPreferenceKey;
}
@Override
public Uri getSliceUri() {
return new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
// Default to non-platform authority. Platform Slices will override authority
// accordingly.
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
// Default to action based slices. Intent based slices will override accordingly.
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(getPreferenceKey())
.build();
}
/**
* @return {@code true} when the controller can be changed on the device.
*
* <p>
* Will return true for {@link #AVAILABLE} and {@link #DISABLED_DEPENDENT_SETTING}.
* <p>
* When the availability status returned by {@link #getAvailabilityStatus()} is
* {@link #DISABLED_DEPENDENT_SETTING}, then the setting will be disabled by default in the
* DashboardFragment, and it is up to the {@link BasePreferenceController} to enable the
* preference at the right time.
* <p>
* This function also check if work profile is existed when android:forWork="true" is set for
* the controller in preference xml.
* TODO (mfritze) Build a dependency mechanism to allow a controller to easily define the
* dependent setting.
*/
@Override
public final boolean isAvailable() {
if (mIsForWork && mWorkProfileUser == null) {
return false;
}
final int availabilityStatus = getAvailabilityStatus();
return (availabilityStatus == AVAILABLE
|| availabilityStatus == AVAILABLE_UNSEARCHABLE
|| availabilityStatus == DISABLED_DEPENDENT_SETTING);
}
/**
* @return {@code false} if the setting is not applicable to the device. This covers both
* settings which were only introduced in future versions of android, or settings that have
* hardware dependencies.
* </p>
* Note that a return value of {@code true} does not mean that the setting is available.
*/
public final boolean isSupported() {
return getAvailabilityStatus() != UNSUPPORTED_ON_DEVICE;
}
/**
* Displays preference in this controller.
*/
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (getAvailabilityStatus() == DISABLED_DEPENDENT_SETTING) {
// Disable preference if it depends on another setting.
final Preference preference = screen.findPreference(getPreferenceKey());
if (preference != null) {
preference.setEnabled(false);
}
}
}
/**
* @return the UI type supported by the controller.
*/
@SliceData.SliceType
public int getSliceType() {
return SliceData.SliceType.INTENT;
}
/**
* Updates non-indexable keys for search provider.
*
* Called by SearchIndexProvider#getNonIndexableKeys
*/
public void updateNonIndexableKeys(List<String> keys) {
final boolean shouldSuppressFromSearch = !isAvailable()
|| getAvailabilityStatus() == AVAILABLE_UNSEARCHABLE;
if (shouldSuppressFromSearch) {
final String key = getPreferenceKey();
if (TextUtils.isEmpty(key)) {
Log.w(TAG, "Skipping updateNonIndexableKeys due to empty key " + toString());
return;
}
if (keys.contains(key)) {
Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list. " + toString());
return;
}
keys.add(key);
}
}
/**
* Indicates this controller is only for work profile user
*/
void setForWork(boolean forWork) {
mIsForWork = forWork;
if (mIsForWork) {
mWorkProfileUser = Utils.getManagedProfile(UserManager.get(mContext));
}
}
/**
* Launches the specified fragment for the work profile user if the associated
* {@link Preference} is clicked. Otherwise just forward it to the super class.
*
* @param preference the preference being clicked.
* @return {@code true} if handled.
*/
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
return super.handlePreferenceTreeClick(preference);
}
if (!mIsForWork || mWorkProfileUser == null) {
return super.handlePreferenceTreeClick(preference);
}
final Bundle extra = preference.getExtras();
extra.putInt(EXTRA_USER_ID, mWorkProfileUser.getIdentifier());
new SubSettingLauncher(preference.getContext())
.setDestination(preference.getFragment())
.setSourceMetricsCategory(preference.getExtras().getInt(CATEGORY,
SettingsEnums.PAGE_UNKNOWN))
.setArguments(preference.getExtras())
.setUserHandle(mWorkProfileUser)
.launch();
return true;
}
/**
* Updates raw data for search provider.
*
* Called by SearchIndexProvider#getRawDataToIndex
*/
public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
}
/**
* Updates dynamic raw data for search provider.
*
* Called by SearchIndexProvider#getDynamicRawDataToIndex
*/
public void updateDynamicRawDataToIndex(List<SearchIndexableRaw> rawData) {
}
/**
* Set {@link UiBlockListener}
*
* @param uiBlockListener listener to set
*/
public void setUiBlockListener(UiBlockListener uiBlockListener) {
mUiBlockListener = uiBlockListener;
}
/**
* Listener to invoke when background job is finished
*/
public interface UiBlockListener {
/**
* To notify client that UI related background work is finished.
* (i.e. Slice is fully loaded.)
*
* @param controller Controller that contains background work
*/
void onBlockerWorkFinished(BasePreferenceController controller);
}
/**
* Used for {@link BasePreferenceController} to decide whether it is ui blocker.
* If it is, entire UI will be invisible for a certain period until controller
* invokes {@link UiBlockListener}
*
* This won't block UI thread however has similar side effect. Please use it if you
* want to avoid janky animation(i.e. new preference is added in the middle of page).
*
* This must be used in {@link BasePreferenceController}
*/
public interface UiBlocker {
}
/**
* Set the metrics category of the parent fragment.
*
* Called by DashboardFragment#onAttach
*/
public void setMetricsCategory(int metricsCategory) {
mMetricsCategory = metricsCategory;
}
/**
* @return the metrics category of the parent fragment.
*/
protected int getMetricsCategory() {
return mMetricsCategory;
}
/**
* @return Non-{@code null} {@link UserHandle} when a work profile is enabled.
* Otherwise {@code null}.
*/
@Nullable
protected UserHandle getWorkProfileUser() {
return mWorkProfileUser;
}
}
|