• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.cellbroadcastreceiver;
18 
19 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
20 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_INTENT_ACTION;
21 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_INTENT_TARGET_CLASS;
22 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_INTENT_TARGET_PACKAGE;
23 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_KEY;
24 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_KEYWORDS;
25 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_SCREEN_TITLE;
26 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_RAW_TITLE;
27 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_CLASS_NAME;
28 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_ICON_RESID;
29 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_ACTION;
30 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS;
31 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE;
32 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RANK;
33 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RESID;
34 import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
35 import static android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS;
36 import static android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS;
37 
38 import android.content.Intent;
39 import android.content.res.Resources;
40 import android.database.Cursor;
41 import android.database.MatrixCursor;
42 import android.provider.SearchIndexableResource;
43 import android.provider.SearchIndexablesProvider;
44 import android.provider.Settings;
45 import android.text.TextUtils;
46 
47 import java.util.ArrayList;
48 import java.util.List;
49 
50 public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvider {
51 
52     // Additional keywords for settings search
53     private static final int[] INDEXABLE_KEYWORDS_RESOURCES = {
54             R.string.etws_earthquake_warning,
55             R.string.etws_tsunami_warning,
56             R.string.cmas_presidential_level_alert,
57             R.string.cmas_required_monthly_test,
58             R.string.emergency_alerts_title
59     };
60 
61     private static final SearchIndexableResource[] INDEXABLE_RES = new SearchIndexableResource[] {
62             new SearchIndexableResource(1, R.xml.preferences,
63                     CellBroadcastSettings.class.getName(),
64                     R.mipmap.ic_launcher_cell_broadcast),
65     };
66     @Override
onCreate()67     public boolean onCreate() {
68         return true;
69     }
70 
71     @Override
queryXmlResources(String[] projection)72     public Cursor queryXmlResources(String[] projection) {
73         MatrixCursor cursor = new MatrixCursor(INDEXABLES_XML_RES_COLUMNS);
74         final int count = INDEXABLE_RES.length;
75         for (int n = 0; n < count; n++) {
76             Object[] ref = new Object[7];
77             ref[COLUMN_INDEX_XML_RES_RANK] = INDEXABLE_RES[n].rank;
78             ref[COLUMN_INDEX_XML_RES_RESID] = INDEXABLE_RES[n].xmlResId;
79             ref[COLUMN_INDEX_XML_RES_CLASS_NAME] = null;
80             ref[COLUMN_INDEX_XML_RES_ICON_RESID] = INDEXABLE_RES[n].iconResId;
81             ref[COLUMN_INDEX_XML_RES_INTENT_ACTION] = Intent.ACTION_MAIN;
82             ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = getContext().getPackageName();
83             ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS] = INDEXABLE_RES[n].className;
84             cursor.addRow(ref);
85         }
86         return cursor;
87     }
88 
89     @Override
queryRawData(String[] projection)90     public Cursor queryRawData(String[] projection) {
91         MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS);
92         final Resources res =
93                 CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(getContext());
94 
95         Object[] raw = new Object[INDEXABLES_RAW_COLUMNS.length];
96         raw[COLUMN_INDEX_RAW_TITLE] = res.getString(R.string.sms_cb_settings);
97         List<String> keywordList = new ArrayList<>();
98         for (int keywordRes : INDEXABLE_KEYWORDS_RESOURCES) {
99             keywordList.add(res.getString(keywordRes));
100         }
101 
102         if (!CellBroadcastChannelManager.getCellBroadcastChannelRanges(
103                 this.getContext(),
104                 R.array.public_safety_messages_channels_range_strings).isEmpty()) {
105             keywordList.add(res.getString(R.string.public_safety_message));
106         }
107 
108         if (!CellBroadcastChannelManager.getCellBroadcastChannelRanges(
109                 this.getContext(),
110                 R.array.state_local_test_alert_range_strings).isEmpty()) {
111             keywordList.add(res.getString(R.string.state_local_test_alert));
112         }
113 
114         raw[COLUMN_INDEX_RAW_KEYWORDS] = TextUtils.join(",", keywordList);
115 
116         raw[COLUMN_INDEX_RAW_SCREEN_TITLE] = res.getString(R.string.sms_cb_settings);
117         raw[COLUMN_INDEX_RAW_KEY] = CellBroadcastSettings.class.getSimpleName();
118         raw[COLUMN_INDEX_RAW_INTENT_ACTION] = Intent.ACTION_MAIN;
119         raw[COLUMN_INDEX_RAW_INTENT_TARGET_PACKAGE] = getContext().getPackageName();
120         raw[COLUMN_INDEX_RAW_INTENT_TARGET_CLASS] = CellBroadcastSettings.class.getName();
121 
122         cursor.addRow(raw);
123         return cursor;
124     }
125 
126     @Override
queryNonIndexableKeys(String[] projection)127     public Cursor queryNonIndexableKeys(String[] projection) {
128         MatrixCursor cursor = new MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS);
129 
130         // Show extra settings when developer options is enabled in settings.
131         boolean enableDevSettings = Settings.Global.getInt(getContext().getContentResolver(),
132                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
133 
134         Resources res = CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(getContext());
135         Object[] ref;
136 
137         ref = new Object[1];
138         ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
139                 CellBroadcastSettings.KEY_CATEGORY_DEV_SETTINGS;
140         cursor.addRow(ref);
141 
142         // Show alert settings and ETWS categories for ETWS builds and developer mode.
143         if (!enableDevSettings) {
144             // Remove general emergency alert preference items (not shown for CMAS builds).
145             ref = new Object[1];
146             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
147                     CellBroadcastSettings.KEY_ENABLE_ALERTS_MASTER_TOGGLE;
148             cursor.addRow(ref);
149 
150             ref = new Object[1];
151             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
152                     CellBroadcastSettings.KEY_ENABLE_ALERT_SPEECH;
153             cursor.addRow(ref);
154         }
155 
156         if (!res.getBoolean(R.bool.show_cmas_settings)) {
157             // Remove CMAS preference items in emergency alert category.
158             ref = new Object[1];
159             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
160                     CellBroadcastSettings.KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS;
161             cursor.addRow(ref);
162 
163             ref = new Object[1];
164             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
165                     CellBroadcastSettings.KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS;
166             cursor.addRow(ref);
167 
168             ref = new Object[1];
169             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
170                     CellBroadcastSettings.KEY_ENABLE_CMAS_AMBER_ALERTS;
171             cursor.addRow(ref);
172         }
173 
174         if (!Resources.getSystem().getBoolean(
175                 com.android.internal.R.bool.config_showAreaUpdateInfoSettings)) {
176             ref = new Object[1];
177             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
178                     CellBroadcastSettings.KEY_ENABLE_AREA_UPDATE_INFO_ALERTS;
179             cursor.addRow(ref);
180         }
181 
182         if (!enableDevSettings) {
183             ref = new Object[1];
184             ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
185                     CellBroadcastSettings.KEY_CATEGORY_DEV_SETTINGS;
186             cursor.addRow(ref);
187         }
188 
189         return cursor;
190     }
191 }
192