blob: 08f0907b3ddef2b8e625615e91b1d063f5bd8df4 [file] [log] [blame]
SongFerngWang1bb5a6f2019-12-10 00:42:54 +08001/*
2 * Copyright (C) 2019 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
17package com.android.phone;
18
Rambo Wang979ccae2021-04-28 17:51:36 -070019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080020import static android.provider.Telephony.ServiceStateTable;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080021import static android.provider.Telephony.ServiceStateTable.CONTENT_URI;
Rambo Wangfbdb3482021-03-15 21:14:19 -070022import static android.provider.Telephony.ServiceStateTable.DATA_NETWORK_TYPE;
Rambo Wang191a6812021-03-15 11:41:49 -070023import static android.provider.Telephony.ServiceStateTable.DATA_REG_STATE;
Rambo Wang917ee2f2021-03-15 12:15:51 -070024import static android.provider.Telephony.ServiceStateTable.DUPLEX_MODE;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080025import static android.provider.Telephony.ServiceStateTable.IS_MANUAL_NETWORK_SELECTION;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080026import static android.provider.Telephony.ServiceStateTable.VOICE_REG_STATE;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080027import static android.provider.Telephony.ServiceStateTable.getUriForSubscriptionId;
28import static android.provider.Telephony.ServiceStateTable.getUriForSubscriptionIdAndField;
29
Rambo Wang979ccae2021-04-28 17:51:36 -070030import android.Manifest;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080031import android.content.ContentProvider;
32import android.content.ContentValues;
33import android.content.Context;
34import android.database.Cursor;
35import android.database.MatrixCursor;
36import android.database.MatrixCursor.RowBuilder;
37import android.net.Uri;
Rambo Wang979ccae2021-04-28 17:51:36 -070038import android.os.Binder;
39import android.os.Build;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080040import android.os.Parcel;
Rambo Wang979ccae2021-04-28 17:51:36 -070041import android.telephony.LocationAccessPolicy;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080042import android.telephony.ServiceState;
43import android.telephony.SubscriptionManager;
44import android.util.Log;
45
46import com.android.internal.annotations.VisibleForTesting;
Rambo Wang979ccae2021-04-28 17:51:36 -070047import com.android.internal.telephony.TelephonyPermissions;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080048
49import java.util.HashMap;
Grace Jia1ca70d02020-01-16 14:13:12 -080050import java.util.List;
51import java.util.Objects;
Rambo Wang979ccae2021-04-28 17:51:36 -070052import java.util.Set;
SongFerngWang1bb5a6f2019-12-10 00:42:54 +080053
54/**
55 * The class to provide base facility to access ServiceState related content,
56 * which is stored in a SQLite database.
57 */
58public class ServiceStateProvider extends ContentProvider {
59 private static final String TAG = "ServiceStateProvider";
60
61 public static final String AUTHORITY = ServiceStateTable.AUTHORITY;
62 public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
63
Grace Jiadd25f0e2020-01-21 12:35:28 -080064 /**
65 * The current service state.
66 *
67 * This is the entire {@link ServiceState} object in byte array.
68 *
69 * @hide
70 */
71 public static final String SERVICE_STATE = "service_state";
72
73 /**
Grace Jiadd25f0e2020-01-21 12:35:28 -080074 * An integer value indicating the current voice roaming type.
75 * <p>
76 * This is the same as {@link ServiceState#getVoiceRoamingType()}.
77 * @hide
78 */
79 public static final String VOICE_ROAMING_TYPE = "voice_roaming_type";
80
81 /**
82 * An integer value indicating the current data roaming type.
83 * <p>
84 * This is the same as {@link ServiceState#getDataRoamingType()}.
85 * @hide
86 */
87 public static final String DATA_ROAMING_TYPE = "data_roaming_type";
88
89 /**
90 * The current registered voice network operator name in long alphanumeric format.
91 * <p>
92 * This is the same as {@link ServiceState#getOperatorAlphaLong()}.
93 * @hide
94 */
95 public static final String VOICE_OPERATOR_ALPHA_LONG = "voice_operator_alpha_long";
96
97 /**
98 * The current registered operator name in short alphanumeric format.
99 * <p>
100 * In GSM/UMTS, short format can be up to 8 characters long. The current registered voice
101 * network operator name in long alphanumeric format.
102 * <p>
103 * This is the same as {@link ServiceState#getOperatorAlphaShort()}.
104 * @hide
105 */
106 public static final String VOICE_OPERATOR_ALPHA_SHORT = "voice_operator_alpha_short";
107
108 /**
109 * The current registered operator numeric id.
110 * <p>
111 * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
112 * network code.
113 * <p>
114 * This is the same as {@link ServiceState#getOperatorNumeric()}.
115 */
116 public static final String VOICE_OPERATOR_NUMERIC = "voice_operator_numeric";
117
118 /**
119 * The current registered data network operator name in long alphanumeric format.
120 * <p>
121 * This is the same as {@link ServiceState#getOperatorAlphaLong()}.
122 * @hide
123 */
124 public static final String DATA_OPERATOR_ALPHA_LONG = "data_operator_alpha_long";
125
126 /**
127 * The current registered data network operator name in short alphanumeric format.
128 * <p>
129 * This is the same as {@link ServiceState#getOperatorAlphaShort()}.
130 * @hide
131 */
132 public static final String DATA_OPERATOR_ALPHA_SHORT = "data_operator_alpha_short";
133
134 /**
135 * The current registered data network operator numeric id.
136 * <p>
137 * This is the same as {@link ServiceState#getOperatorNumeric()}.
138 * @hide
139 */
140 public static final String DATA_OPERATOR_NUMERIC = "data_operator_numeric";
141
142 /**
143 * This is the same as {@link ServiceState#getRilVoiceRadioTechnology()}.
144 * @hide
145 */
146 public static final String RIL_VOICE_RADIO_TECHNOLOGY = "ril_voice_radio_technology";
147
148 /**
149 * This is the same as {@link ServiceState#getRilDataRadioTechnology()}.
150 * @hide
151 */
152 public static final String RIL_DATA_RADIO_TECHNOLOGY = "ril_data_radio_technology";
153
154 /**
155 * This is the same as {@link ServiceState#getCssIndicator()}.
156 * @hide
157 */
158 public static final String CSS_INDICATOR = "css_indicator";
159
160 /**
161 * This is the same as {@link ServiceState#getCdmaNetworkId()}.
162 * @hide
163 */
164 public static final String NETWORK_ID = "network_id";
165
166 /**
167 * This is the same as {@link ServiceState#getCdmaSystemId()}.
168 * @hide
169 */
170 public static final String SYSTEM_ID = "system_id";
171
172 /**
173 * This is the same as {@link ServiceState#getCdmaRoamingIndicator()}.
174 * @hide
175 */
176 public static final String CDMA_ROAMING_INDICATOR = "cdma_roaming_indicator";
177
178 /**
179 * This is the same as {@link ServiceState#getCdmaDefaultRoamingIndicator()}.
180 * @hide
181 */
182 public static final String CDMA_DEFAULT_ROAMING_INDICATOR =
183 "cdma_default_roaming_indicator";
184
185 /**
186 * This is the same as {@link ServiceState#getCdmaEriIconIndex()}.
187 * @hide
188 */
189 public static final String CDMA_ERI_ICON_INDEX = "cdma_eri_icon_index";
190
191 /**
192 * This is the same as {@link ServiceState#getCdmaEriIconMode()}.
193 * @hide
194 */
195 public static final String CDMA_ERI_ICON_MODE = "cdma_eri_icon_mode";
196
197 /**
198 * This is the same as {@link ServiceState#isEmergencyOnly()}.
199 * @hide
200 */
201 public static final String IS_EMERGENCY_ONLY = "is_emergency_only";
202
203 /**
204 * This is the same as {@link ServiceState#getDataRoamingFromRegistration()}.
205 * @hide
206 */
207 public static final String IS_DATA_ROAMING_FROM_REGISTRATION =
208 "is_data_roaming_from_registration";
209
210 /**
211 * This is the same as {@link ServiceState#isUsingCarrierAggregation()}.
212 * @hide
213 */
214 public static final String IS_USING_CARRIER_AGGREGATION = "is_using_carrier_aggregation";
215
216 /**
217 * The current registered raw data network operator name in long alphanumeric format.
218 * <p>
219 * This is the same as {@link ServiceState#getOperatorAlphaLongRaw()}.
220 * @hide
221 */
222 public static final String OPERATOR_ALPHA_LONG_RAW = "operator_alpha_long_raw";
223
224 /**
225 * The current registered raw data network operator name in short alphanumeric format.
226 * <p>
227 * This is the same as {@link ServiceState#getOperatorAlphaShortRaw()}.
228 * @hide
229 */
230 public static final String OPERATOR_ALPHA_SHORT_RAW = "operator_alpha_short_raw";
231
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800232 private final HashMap<Integer, ServiceState> mServiceStates = new HashMap<>();
Rambo Wang979ccae2021-04-28 17:51:36 -0700233
234 @VisibleForTesting
235 /* package */ static final String[] ALL_COLUMNS = {
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800236 VOICE_REG_STATE,
237 DATA_REG_STATE,
238 VOICE_ROAMING_TYPE,
239 DATA_ROAMING_TYPE,
240 VOICE_OPERATOR_ALPHA_LONG,
241 VOICE_OPERATOR_ALPHA_SHORT,
242 VOICE_OPERATOR_NUMERIC,
243 DATA_OPERATOR_ALPHA_LONG,
244 DATA_OPERATOR_ALPHA_SHORT,
245 DATA_OPERATOR_NUMERIC,
246 IS_MANUAL_NETWORK_SELECTION,
247 RIL_VOICE_RADIO_TECHNOLOGY,
248 RIL_DATA_RADIO_TECHNOLOGY,
249 CSS_INDICATOR,
250 NETWORK_ID,
251 SYSTEM_ID,
252 CDMA_ROAMING_INDICATOR,
253 CDMA_DEFAULT_ROAMING_INDICATOR,
254 CDMA_ERI_ICON_INDEX,
255 CDMA_ERI_ICON_MODE,
256 IS_EMERGENCY_ONLY,
257 IS_USING_CARRIER_AGGREGATION,
258 OPERATOR_ALPHA_LONG_RAW,
259 OPERATOR_ALPHA_SHORT_RAW,
Rambo Wangfbdb3482021-03-15 21:14:19 -0700260 DATA_NETWORK_TYPE,
Rambo Wang917ee2f2021-03-15 12:15:51 -0700261 DUPLEX_MODE,
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800262 };
263
Rambo Wang979ccae2021-04-28 17:51:36 -0700264 /**
265 * Columns that are exposed to public surface.
266 * These are the columns accessible to apps target S+ and lack
267 * {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE} permission.
268 */
269 @VisibleForTesting
270 /* package */ static final String[] PUBLIC_COLUMNS = {
271 VOICE_REG_STATE,
272 DATA_REG_STATE,
273 VOICE_OPERATOR_NUMERIC,
274 IS_MANUAL_NETWORK_SELECTION,
275 DATA_NETWORK_TYPE,
276 DUPLEX_MODE
277 };
278
279 /**
280 * Columns protected by location permissions (either FINE or COARSE).
281 * SecurityException will throw if applications without location permissions try to put those
282 * columns explicitly into cursor (e.g. through {@code projection} parameter in
283 * {@link #query(Uri, String[], String, String[], String)} method).
284 * Default (scrub-out) value will return if applications try to put all columns into cursor by
285 * specifying null of {@code projection} parameter and get values through the returned cursor.
286 */
287 private static final Set<String> LOCATION_PROTECTED_COLUMNS_SET = Set.of(
288 NETWORK_ID,
289 SYSTEM_ID
290 );
291
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800292 @Override
293 public boolean onCreate() {
294 return true;
295 }
296
297 /**
298 * Returns the {@link ServiceState} information on specified subscription.
299 *
300 * @param subId whose subscriber id is returned
301 * @return the {@link ServiceState} information on specified subscription.
302 */
303 @VisibleForTesting
304 public ServiceState getServiceState(int subId) {
305 return mServiceStates.get(subId);
306 }
307
308 /**
309 * Returns the system's default subscription id.
310 *
311 * @return the "system" default subscription id.
312 */
313 @VisibleForTesting
314 public int getDefaultSubId() {
315 return SubscriptionManager.getDefaultSubscriptionId();
316 }
317
318 @Override
319 public Uri insert(Uri uri, ContentValues values) {
Grace Jia1ca70d02020-01-16 14:13:12 -0800320 if (isPathPrefixMatch(uri, CONTENT_URI)) {
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800321 // Parse the subId
322 int subId = 0;
323 try {
324 subId = Integer.parseInt(uri.getLastPathSegment());
325 } catch (NumberFormatException e) {
326 Log.e(TAG, "insert: no subId provided in uri");
327 throw e;
328 }
329 Log.d(TAG, "subId=" + subId);
330
331 // handle DEFAULT_SUBSCRIPTION_ID
332 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
333 subId = getDefaultSubId();
334 }
335
336 final Parcel p = Parcel.obtain();
337 final byte[] rawBytes = values.getAsByteArray(SERVICE_STATE);
338 p.unmarshall(rawBytes, 0, rawBytes.length);
339 p.setDataPosition(0);
340
341 // create the new service state
342 final ServiceState newSS = ServiceState.CREATOR.createFromParcel(p);
343
344 // notify listeners
345 // if ss is null (e.g. first service state update) we will notify for all fields
346 ServiceState ss = getServiceState(subId);
347 notifyChangeForSubIdAndField(getContext(), ss, newSS, subId);
348 notifyChangeForSubId(getContext(), ss, newSS, subId);
349
350 // store the new service state
351 mServiceStates.put(subId, newSS);
352 return uri;
353 }
354 return null;
355 }
356
357 @Override
358 public int delete(Uri uri, String selection, String[] selectionArgs) {
359 throw new RuntimeException("Not supported");
360 }
361
362 @Override
363 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
364 throw new RuntimeException("Not supported");
365 }
366
367 @Override
368 public String getType(Uri uri) {
369 throw new RuntimeException("Not supported");
370 }
371
372 @Override
373 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
374 String sortOrder) {
Grace Jia1ca70d02020-01-16 14:13:12 -0800375 if (!isPathPrefixMatch(uri, CONTENT_URI)) {
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800376 throw new IllegalArgumentException("Invalid URI: " + uri);
377 } else {
378 // Parse the subId
379 int subId = 0;
380 try {
381 subId = Integer.parseInt(uri.getLastPathSegment());
382 } catch (NumberFormatException e) {
383 Log.d(TAG, "query: no subId provided in uri, using default.");
384 subId = getDefaultSubId();
385 }
386 Log.d(TAG, "subId=" + subId);
387
388 // handle DEFAULT_SUBSCRIPTION_ID
389 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
390 subId = getDefaultSubId();
391 }
392
393 // Get the service state
Rambo Wang979ccae2021-04-28 17:51:36 -0700394 ServiceState unredactedServiceState = getServiceState(subId);
395 if (unredactedServiceState == null) {
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800396 Log.d(TAG, "returning null");
397 return null;
398 }
399
Rambo Wang979ccae2021-04-28 17:51:36 -0700400 final boolean targetingAtLeastS = TelephonyPermissions.getTargetSdk(getContext(),
401 getCallingPackage()) >= Build.VERSION_CODES.S;
402 final boolean canReadPrivilegedPhoneState = getContext().checkCallingOrSelfPermission(
403 Manifest.permission.READ_PRIVILEGED_PHONE_STATE) == PERMISSION_GRANTED;
404
405 final String[] availableColumns;
406 final ServiceState ss;
407 if (targetingAtLeastS && !canReadPrivilegedPhoneState) {
408 // targetSdkVersion S+ without read privileged phone state permission can only
409 // access public columns which have no location sensitive info.
410 availableColumns = PUBLIC_COLUMNS;
411 ss = unredactedServiceState;
412 } else {
413 availableColumns = ALL_COLUMNS;
414
415 final boolean hasLocationPermission =
416 hasFineLocationPermission() || hasCoarseLocationPermission();
417 if (hasLocationPermission) {
418 ss = unredactedServiceState;
419 } else {
420 // The caller has no location permission but explicitly requires for location
421 // protected columns. Throw SecurityException to fail loudly.
422 if (projection != null) {
423 for (String requiredColumn : projection) {
424 if (LOCATION_PROTECTED_COLUMNS_SET.contains(requiredColumn)) {
425 throw new SecurityException("Column " + requiredColumn
426 + "requires location permissions to access.");
427 }
428 }
429 }
430
431 // The caller has no location permission but only requires columns without
432 // location sensitive info or "all" columns, return result that scrub out all
433 // sensitive info. In later case, we will not know which columns will be fetched
434 // from the returned cursor until the result has been returned.
435 ss = unredactedServiceState.createLocationInfoSanitizedCopy(
436 true /*removeCoarseLocation*/);
437 // TODO(b/188061647): remove the additional redaction once it is fixed in SS
438 ss.setCdmaSystemAndNetworkId(ServiceState.UNKNOWN_ID,
439 ServiceState.UNKNOWN_ID);
440 }
441 }
442
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800443 // Build the result
SongFerngWangf379b8b2019-12-23 18:24:44 +0800444 final int voice_reg_state = ss.getState();
445 final int data_reg_state = ss.getDataRegistrationState();
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800446 final int voice_roaming_type = ss.getVoiceRoamingType();
447 final int data_roaming_type = ss.getDataRoamingType();
448 final String voice_operator_alpha_long = ss.getOperatorAlphaLong();
449 final String voice_operator_alpha_short = ss.getOperatorAlphaShort();
450 final String voice_operator_numeric = ss.getOperatorNumeric();
451 final String data_operator_alpha_long = ss.getOperatorAlphaLong();
452 final String data_operator_alpha_short = ss.getOperatorAlphaShort();
453 final String data_operator_numeric = ss.getOperatorNumeric();
454 final int is_manual_network_selection = (ss.getIsManualSelection()) ? 1 : 0;
455 final int ril_voice_radio_technology = ss.getRilVoiceRadioTechnology();
456 final int ril_data_radio_technology = ss.getRilDataRadioTechnology();
457 final int css_indicator = ss.getCssIndicator();
458 final int network_id = ss.getCdmaNetworkId();
459 final int system_id = ss.getCdmaSystemId();
460 final int cdma_roaming_indicator = ss.getCdmaRoamingIndicator();
461 final int cdma_default_roaming_indicator = ss.getCdmaDefaultRoamingIndicator();
462 final int cdma_eri_icon_index = ss.getCdmaEriIconIndex();
463 final int cdma_eri_icon_mode = ss.getCdmaEriIconMode();
464 final int is_emergency_only = (ss.isEmergencyOnly()) ? 1 : 0;
465 final int is_using_carrier_aggregation = (ss.isUsingCarrierAggregation()) ? 1 : 0;
466 final String operator_alpha_long_raw = ss.getOperatorAlphaLongRaw();
467 final String operator_alpha_short_raw = ss.getOperatorAlphaShortRaw();
Rambo Wangfbdb3482021-03-15 21:14:19 -0700468 final int data_network_type = ss.getDataNetworkType();
Rambo Wang917ee2f2021-03-15 12:15:51 -0700469 final int duplex_mode = ss.getDuplexMode();
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800470
Rambo Wang979ccae2021-04-28 17:51:36 -0700471 Object[] data = availableColumns == ALL_COLUMNS ? new Object[]{
472 // data for all columns
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800473 voice_reg_state,
474 data_reg_state,
475 voice_roaming_type,
476 data_roaming_type,
477 voice_operator_alpha_long,
478 voice_operator_alpha_short,
479 voice_operator_numeric,
480 data_operator_alpha_long,
481 data_operator_alpha_short,
482 data_operator_numeric,
483 is_manual_network_selection,
484 ril_voice_radio_technology,
485 ril_data_radio_technology,
486 css_indicator,
487 network_id,
488 system_id,
489 cdma_roaming_indicator,
490 cdma_default_roaming_indicator,
491 cdma_eri_icon_index,
492 cdma_eri_icon_mode,
493 is_emergency_only,
494 is_using_carrier_aggregation,
495 operator_alpha_long_raw,
496 operator_alpha_short_raw,
Rambo Wangfbdb3482021-03-15 21:14:19 -0700497 data_network_type,
Rambo Wang917ee2f2021-03-15 12:15:51 -0700498 duplex_mode,
Rambo Wang979ccae2021-04-28 17:51:36 -0700499 } : new Object[]{
500 // data for public columns only
501 voice_reg_state,
502 data_reg_state,
503 voice_operator_numeric,
504 is_manual_network_selection,
505 data_network_type,
506 duplex_mode,
507 };
508
509 return buildSingleRowResult(projection, availableColumns, data);
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800510 }
511 }
512
513 private static Cursor buildSingleRowResult(String[] projection, String[] availableColumns,
514 Object[] data) {
515 if (projection == null) {
516 projection = availableColumns;
517 }
518 final MatrixCursor c = new MatrixCursor(projection, 1);
519 final RowBuilder row = c.newRow();
520 for (int i = 0; i < c.getColumnCount(); i++) {
521 final String columnName = c.getColumnName(i);
522 boolean found = false;
523 for (int j = 0; j < availableColumns.length; j++) {
524 if (availableColumns[j].equals(columnName)) {
525 row.add(data[j]);
526 found = true;
527 break;
528 }
529 }
530 if (!found) {
531 throw new IllegalArgumentException("Invalid column " + projection[i]);
532 }
533 }
534 return c;
535 }
536
537 /**
538 * Notify interested apps that certain fields of the ServiceState have changed.
539 *
540 * Apps which want to wake when specific fields change can use
541 * JobScheduler's TriggerContentUri. This replaces the waking functionality of the implicit
542 * broadcast of ACTION_SERVICE_STATE_CHANGED for apps targeting version O.
543 *
544 * We will only notify for certain fields. This is an intentional change from the behavior of
545 * the broadcast. Listeners will be notified when the voice or data registration state or
546 * roaming type changes.
547 */
548 @VisibleForTesting
549 public static void notifyChangeForSubIdAndField(Context context, ServiceState oldSS,
550 ServiceState newSS, int subId) {
551 final boolean firstUpdate = (oldSS == null) ? true : false;
552
553 // for every field, if the field has changed values, notify via the provider
554 if (firstUpdate || voiceRegStateChanged(oldSS, newSS)) {
555 context.getContentResolver().notifyChange(
556 getUriForSubscriptionIdAndField(subId, VOICE_REG_STATE),
557 /* observer= */ null, /* syncToNetwork= */ false);
558 }
559 if (firstUpdate || dataRegStateChanged(oldSS, newSS)) {
560 context.getContentResolver().notifyChange(
561 getUriForSubscriptionIdAndField(subId, DATA_REG_STATE), null, false);
562 }
563 if (firstUpdate || voiceRoamingTypeChanged(oldSS, newSS)) {
564 context.getContentResolver().notifyChange(
565 getUriForSubscriptionIdAndField(subId, VOICE_ROAMING_TYPE), null, false);
566 }
567 if (firstUpdate || dataRoamingTypeChanged(oldSS, newSS)) {
568 context.getContentResolver().notifyChange(
569 getUriForSubscriptionIdAndField(subId, DATA_ROAMING_TYPE), null, false);
570 }
Rambo Wangfbdb3482021-03-15 21:14:19 -0700571 if (firstUpdate || dataNetworkTypeChanged(oldSS, newSS)) {
572 context.getContentResolver().notifyChange(
573 getUriForSubscriptionIdAndField(subId, DATA_NETWORK_TYPE), null, false);
574 }
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800575 }
576
577 private static boolean voiceRegStateChanged(ServiceState oldSS, ServiceState newSS) {
SongFerngWangf379b8b2019-12-23 18:24:44 +0800578 return oldSS.getState() != newSS.getState();
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800579 }
580
581 private static boolean dataRegStateChanged(ServiceState oldSS, ServiceState newSS) {
SongFerngWangf379b8b2019-12-23 18:24:44 +0800582 return oldSS.getDataRegistrationState() != newSS.getDataRegistrationState();
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800583 }
584
585 private static boolean voiceRoamingTypeChanged(ServiceState oldSS, ServiceState newSS) {
586 return oldSS.getVoiceRoamingType() != newSS.getVoiceRoamingType();
587 }
588
589 private static boolean dataRoamingTypeChanged(ServiceState oldSS, ServiceState newSS) {
590 return oldSS.getDataRoamingType() != newSS.getDataRoamingType();
591 }
592
Rambo Wangfbdb3482021-03-15 21:14:19 -0700593 private static boolean dataNetworkTypeChanged(ServiceState oldSS, ServiceState newSS) {
594 return oldSS.getDataNetworkType() != newSS.getDataNetworkType();
595 }
596
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800597 /**
598 * Notify interested apps that the ServiceState has changed.
599 *
600 * Apps which want to wake when any field in the ServiceState has changed can use
601 * JobScheduler's TriggerContentUri. This replaces the waking functionality of the implicit
602 * broadcast of ACTION_SERVICE_STATE_CHANGED for apps targeting version O.
603 *
604 * We will only notify for certain fields. This is an intentional change from the behavior of
605 * the broadcast. Listeners will only be notified when the voice/data registration state or
606 * roaming type changes.
607 */
608 @VisibleForTesting
609 public static void notifyChangeForSubId(Context context, ServiceState oldSS, ServiceState newSS,
610 int subId) {
611 // if the voice or data registration or roaming state field has changed values, notify via
612 // the provider.
613 // If oldSS is null and newSS is not (e.g. first update of service state) this will also
614 // notify
615 if (oldSS == null || voiceRegStateChanged(oldSS, newSS) || dataRegStateChanged(oldSS, newSS)
Rambo Wangfbdb3482021-03-15 21:14:19 -0700616 || voiceRoamingTypeChanged(oldSS, newSS) || dataRoamingTypeChanged(oldSS, newSS)
617 || dataNetworkTypeChanged(oldSS, newSS)) {
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800618 context.getContentResolver().notifyChange(getUriForSubscriptionId(subId), null, false);
619 }
620 }
Grace Jia1ca70d02020-01-16 14:13:12 -0800621
622 /**
623 * Test if this is a path prefix match against the given Uri. Verifies that
624 * scheme, authority, and atomic path segments match.
625 *
626 * Copied from frameworks/base/core/java/android/net/Uri.java
627 */
628 private boolean isPathPrefixMatch(Uri uriA, Uri uriB) {
629 if (!Objects.equals(uriA.getScheme(), uriB.getScheme())) return false;
630 if (!Objects.equals(uriA.getAuthority(), uriB.getAuthority())) return false;
631
632 List<String> segA = uriA.getPathSegments();
633 List<String> segB = uriB.getPathSegments();
634
635 final int size = segB.size();
636 if (segA.size() < size) return false;
637
638 for (int i = 0; i < size; i++) {
639 if (!Objects.equals(segA.get(i), segB.get(i))) {
640 return false;
641 }
642 }
643
644 return true;
645 }
Grace Jiadd25f0e2020-01-21 12:35:28 -0800646
647 /**
648 * Used to insert a ServiceState into the ServiceStateProvider as a ContentValues instance.
649 *
650 * @param state the ServiceState to convert into ContentValues
651 * @return the convertedContentValues instance
652 * @hide
653 */
654 public static ContentValues getContentValuesForServiceState(ServiceState state) {
655 ContentValues values = new ContentValues();
656 final Parcel p = Parcel.obtain();
657 state.writeToParcel(p, 0);
658 // Turn the parcel to byte array. Safe to do this because the content values were never
659 // written into a persistent storage. ServiceStateProvider keeps values in the memory.
660 values.put(SERVICE_STATE, p.marshall());
661 return values;
662 }
Rambo Wang979ccae2021-04-28 17:51:36 -0700663
664 private boolean hasFineLocationPermission() {
665 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
666 LocationAccessPolicy.checkLocationPermission(getContext(),
667 new LocationAccessPolicy.LocationPermissionQuery.Builder()
668 .setCallingPackage(getCallingPackage())
669 .setCallingFeatureId(getCallingAttributionTag())
670 .setCallingPid(Binder.getCallingPid())
671 .setCallingUid(Binder.getCallingUid())
672 .setMethod("ServiceStateProvider#query")
673 .setLogAsInfo(true)
674 .setMinSdkVersionForFine(Build.VERSION_CODES.S)
675 .setMinSdkVersionForCoarse(Build.VERSION_CODES.S)
676 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.S)
677 .build());
678 return fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
679 }
680
681 private boolean hasCoarseLocationPermission() {
682 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
683 LocationAccessPolicy.checkLocationPermission(getContext(),
684 new LocationAccessPolicy.LocationPermissionQuery.Builder()
685 .setCallingPackage(getCallingPackage())
686 .setCallingFeatureId(getCallingAttributionTag())
687 .setCallingPid(Binder.getCallingPid())
688 .setCallingUid(Binder.getCallingUid())
689 .setMethod("ServiceStateProvider#query")
690 .setLogAsInfo(true)
691 .setMinSdkVersionForCoarse(Build.VERSION_CODES.S)
692 .setMinSdkVersionForFine(Integer.MAX_VALUE)
693 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.S)
694 .build());
695 return coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
696 }
SongFerngWang1bb5a6f2019-12-10 00:42:54 +0800697}