OSDN Git Service

b3d9878608ec65338aed15b86bc5d6f7fdb08846
[android-x86/packages-apps-Settings.git] / src / com / android / settings / core / BasePreferenceController.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
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
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
13  */
14 package com.android.settings.core;
15
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;
22
23
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;
28
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.util.List;
32
33 /**
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.
37  */
38 public abstract class BasePreferenceController extends AbstractPreferenceController {
39
40     private static final String TAG = "SettingsPrefController";
41
42     @Retention(RetentionPolicy.SOURCE)
43     @IntDef({AVAILABLE, DISABLED_UNSUPPORTED, DISABLED_FOR_USER, DISABLED_DEPENDENT_SETTING,
44             UNAVAILABLE_UNKNOWN})
45     public @interface AvailabilityStatus {
46     }
47
48     /**
49      * The setting is available.
50      */
51     public static final int AVAILABLE = 0;
52
53     /**
54      * The setting is not supported by the device.
55      */
56     public static final int DISABLED_UNSUPPORTED = 1;
57
58     /**
59      * The setting cannot be changed by the current user.
60      */
61     public static final int DISABLED_FOR_USER = 2;
62
63     /**
64      * The setting has a dependency in the Settings App which is currently blocking access.
65      */
66     public static final int DISABLED_DEPENDENT_SETTING = 3;
67
68     /**
69      * A catch-all case for internal errors and inexplicable unavailability.
70      */
71     public static final int UNAVAILABLE_UNKNOWN = 4;
72
73     protected final String mPreferenceKey;
74
75     public BasePreferenceController(Context context, String preferenceKey) {
76         super(context);
77         mPreferenceKey = preferenceKey;
78     }
79
80     /**
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.
84      * </p>
85      * The status is used for the convenience methods: {@link #isAvailable()},
86      * {@link #isSupported()}
87      */
88     @AvailabilityStatus
89     public abstract int getAvailabilityStatus();
90
91     /**
92      * @return A slice for the corresponding setting.
93      */
94     public abstract Slice getSettingSlice();
95
96     @Override
97     public String getPreferenceKey() {
98         return mPreferenceKey;
99     }
100
101     @Override
102     public final boolean isAvailable() {
103         return getAvailabilityStatus() == AVAILABLE;
104     }
105
106     /**
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.
110      * </p>
111      * Note that a return value of {@code true} does not mean that the setting is available.
112      */
113     public final boolean isSupported() {
114         return getAvailabilityStatus() != DISABLED_UNSUPPORTED;
115     }
116
117     /**
118      * Updates non-indexable keys for search provider.
119      *
120      * Called by SearchIndexProvider#getNonIndexableKeys
121      */
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)) {
127                     Log.w(TAG,
128                             "Skipping updateNonIndexableKeys due to empty key " + this.toString());
129                     return;
130                 }
131                 keys.add(key);
132             }
133         }
134     }
135
136     /**
137      * Updates raw data for search provider.
138      *
139      * Called by SearchIndexProvider#getRawDataToIndex
140      */
141     public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
142     }
143
144     /**
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.
148      */
149     @Deprecated
150     public ResultPayload getResultPayload() {
151         return null;
152     }
153
154     // TODO (b/69380366) Add Method to get preference UI
155
156     // TODO (b/69380464) Add method to get intent
157
158     // TODO (b/69380560) Add method to get broadcast intent
159 }