2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
14 package com.android.settings.core;
16 import android.annotation.IntDef;
17 import android.app.slice.Slice;
18 import android.content.Context;
19 import android.support.v7.preference.Preference;
20 import android.text.TextUtils;
21 import android.util.Log;
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settings.search.ResultPayload;
26 import com.android.settings.search.SearchIndexableRaw;
27 import com.android.settingslib.core.AbstractPreferenceController;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.util.List;
34 * Abstract class to consolidate utility between preference controllers and act as an interface
35 * for Slices. The abstract classes that inherit from this class will act as the direct interfaces
36 * for each type when plugging into Slices.
38 public abstract class BasePreferenceController extends AbstractPreferenceController {
40 private static final String TAG = "SettingsPrefController";
42 @Retention(RetentionPolicy.SOURCE)
43 @IntDef({AVAILABLE, DISABLED_UNSUPPORTED, DISABLED_FOR_USER, DISABLED_DEPENDENT_SETTING,
45 public @interface AvailabilityStatus {
49 * The setting is available.
51 public static final int AVAILABLE = 0;
54 * The setting is not supported by the device.
56 public static final int DISABLED_UNSUPPORTED = 1;
59 * The setting cannot be changed by the current user.
61 public static final int DISABLED_FOR_USER = 2;
64 * The setting has a dependency in the Settings App which is currently blocking access.
66 public static final int DISABLED_DEPENDENT_SETTING = 3;
69 * A catch-all case for internal errors and inexplicable unavailability.
71 public static final int UNAVAILABLE_UNKNOWN = 4;
73 protected final String mPreferenceKey;
75 public BasePreferenceController(Context context, String preferenceKey) {
77 mPreferenceKey = preferenceKey;
81 * @return {@AvailabilityStatus} for the Setting. This status is used to determine if the
82 * Setting should be shown or disabled in Settings. Further, it can be used to produce
83 * appropriate error / warning Slice in the case of unavailability.
85 * The status is used for the convenience methods: {@link #isAvailable()},
86 * {@link #isSupported()}
89 public abstract int getAvailabilityStatus();
92 * @return A slice for the corresponding setting.
94 public abstract Slice getSettingSlice();
97 public String getPreferenceKey() {
98 return mPreferenceKey;
102 public final boolean isAvailable() {
103 return getAvailabilityStatus() == AVAILABLE;
107 * @return {@code false} if the setting is not applicable to the device. This covers both
108 * settings which were only introduced in future versions of android, or settings that have
109 * hardware dependencies.
111 * Note that a return value of {@code true} does not mean that the setting is available.
113 public final boolean isSupported() {
114 return getAvailabilityStatus() != DISABLED_UNSUPPORTED;
118 * Updates non-indexable keys for search provider.
120 * Called by SearchIndexProvider#getNonIndexableKeys
122 public void updateNonIndexableKeys(List<String> keys) {
123 if (this instanceof AbstractPreferenceController) {
124 if (!isAvailable()) {
125 final String key = getPreferenceKey();
126 if (TextUtils.isEmpty(key)) {
128 "Skipping updateNonIndexableKeys due to empty key " + this.toString());
137 * Updates raw data for search provider.
139 * Called by SearchIndexProvider#getRawDataToIndex
141 public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
145 * @return the {@link ResultPayload} corresponding to the search result type for the preference.
146 * TODO (b/69808376) Remove this method.
147 * Do not extend this method. It will not launch with P.
150 public ResultPayload getResultPayload() {
154 // TODO (b/69380366) Add Method to get preference UI
156 // TODO (b/69380464) Add method to get intent
158 // TODO (b/69380560) Add method to get broadcast intent