1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.development.qstile;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ServiceInfo;
24 import android.os.SystemProperties;
25 
26 import com.android.settings.R;
27 import com.android.settings.dashboard.DashboardFragment;
28 import com.android.settings.development.DeveloperOptionAwareMixin;
29 import com.android.settings.search.BaseSearchIndexProvider;
30 import com.android.settingslib.development.DevelopmentSettingsEnabler;
31 import com.android.settingslib.search.SearchIndexable;
32 import com.android.settingslib.search.SearchIndexableRaw;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37 
38 @SearchIndexable
39 public class DevelopmentTileConfigFragment extends DashboardFragment implements
40         DeveloperOptionAwareMixin {
41     private static final String TAG = "DevelopmentTileConfig";
42     private static final String QS_TILE_PERF = "develop_qs_tile";
43 
44     @Override
getLogTag()45     protected String getLogTag() {
46         return TAG;
47     }
48 
49     @Override
getPreferenceScreenResId()50     protected int getPreferenceScreenResId() {
51         return R.xml.development_tile_settings;
52     }
53 
54     @Override
getMetricsCategory()55     public int getMetricsCategory() {
56         return SettingsEnums.DEVELOPMENT_QS_TILE_CONFIG;
57     }
58 
59     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
60             new BaseSearchIndexProvider(R.xml.development_tile_settings) {
61 
62                 @Override
63                 protected boolean isPageSearchEnabled(Context context) {
64                     return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context);
65                 }
66 
67                 @Override
68                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
69                         boolean enabled) {
70                     List<SearchIndexableRaw> result = new ArrayList<>();
71                     // Save the query system property for getNonIndexableKeys to avoid
72                     // getTitleServiceList multiple times
73                     SharedPreferences sharedPref = context.getSharedPreferences(QS_TILE_PERF,
74                             Context.MODE_PRIVATE);
75 
76                     List<ServiceInfo> services =
77                             DevelopmentTilePreferenceController.getTileServiceList(context);
78                     PackageManager pm = context.getPackageManager();
79                     SharedPreferences.Editor editor = sharedPref.edit();
80                     for (ServiceInfo sInfo : services) {
81                         SearchIndexableRaw data = new SearchIndexableRaw(context);
82                         data.title = sInfo.loadLabel(pm).toString();
83                         data.key = sInfo.name;
84                         result.add(data);
85 
86                         if (sInfo.metaData == null) {
87                             continue;
88                         }
89                         String flag = sInfo.metaData.getString(
90                                 DevelopmentTiles.META_DATA_REQUIRES_SYSTEM_PROPERTY);
91                         if (flag == null) {
92                             continue;
93                         }
94                         editor.putString(sInfo.name, flag);
95                     }
96                     editor.apply();
97 
98                     return result;
99                 }
100 
101                 @Override
102                 public List<String> getNonIndexableKeys(Context context) {
103                     List<String> keys = super.getNonIndexableKeys(context);
104 
105                     SharedPreferences sharedPref = context.getSharedPreferences(QS_TILE_PERF,
106                             Context.MODE_PRIVATE);
107                     Map<String, ?> map = sharedPref.getAll();
108                     for (Map.Entry<String, ?> entry : map.entrySet()) {
109                         if (entry.getValue() == null) {
110                             continue;
111                         }
112                         String key = entry.getKey();
113                         String flag = entry.getValue().toString();
114 
115                         if (!SystemProperties.getBoolean(flag, false)) {
116                             keys.add(key);
117                         }
118                     }
119 
120                     return keys;
121                 }
122             };
123 }
124