blob: 32d330e1a47f013354dffd3fb0b2297b02dce9f1 [file] [log] [blame]
Jordan Liu45c0d542019-11-22 11:00:05 -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 android.telephony;
18
Jordan Liu2bc01a82020-01-16 11:01:48 -080019import android.Manifest;
Jordan Liu45c0d542019-11-22 11:00:05 -080020import android.annotation.NonNull;
21import android.annotation.Nullable;
Jack Yuafaa71d2020-01-23 23:01:40 -080022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Jordan Liu45c0d542019-11-22 11:00:05 -080024import android.annotation.SystemApi;
Jordan Liu2bc01a82020-01-16 11:01:48 -080025import android.app.AppOpsManager;
Jordan Liu45c0d542019-11-22 11:00:05 -080026import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
Jordan Liu45c0d542019-11-22 11:00:05 -080029import android.os.Handler;
30import android.os.UserHandle;
Jordan Liu2bc01a82020-01-16 11:01:48 -080031import android.provider.Telephony;
Jordan Liu45c0d542019-11-22 11:00:05 -080032
33/**
34 * A static helper class used to send Intents with prepopulated flags.
35 * <p>
Jordan Liu2bc01a82020-01-16 11:01:48 -080036 * This is intended to be used by the CellBroadcastService and does nothing if the caller does not
37 * have permission to broadcast {@link Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION}.
Jordan Liu45c0d542019-11-22 11:00:05 -080038 *
39 * @hide
40 */
41@SystemApi
42public class CellBroadcastIntents {
43 private static final String LOG_TAG = "CellBroadcastIntents";
44
Jordan Liu2bc01a82020-01-16 11:01:48 -080045 private static final String EXTRA_MESSAGE = "message";
46
Jordan Liu45c0d542019-11-22 11:00:05 -080047 /**
Jack Yuafaa71d2020-01-23 23:01:40 -080048 * Broadcast intent action for notifying area information has been updated. The information
49 * can be retrieved by {@link CellBroadcastService#getCellBroadcastAreaInfo(int)}
50 */
51 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
52 public static final String ACTION_AREA_INFO_UPDATED =
53 "android.telephony.action.AREA_INFO_UPDATED";
54
55 /**
Jordan Liu45c0d542019-11-22 11:00:05 -080056 * @hide
57 */
58 private CellBroadcastIntents() {
59 }
60
61 /**
Jordan Liu2bc01a82020-01-16 11:01:48 -080062 * Broadcasts an SMS_CB_RECEIVED_ACTION intent which can be received by background
63 * BroadcastReceivers. This is only intended to be used by the CellBroadcastService and will
64 * do nothing if the caller does not have permission to broadcast
65 * {@link Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION}.
Jordan Liu45c0d542019-11-22 11:00:05 -080066 *
67 * @param context The context from which to send the broadcast
68 * @param user The user from which to send the broadcast
Jordan Liu2bc01a82020-01-16 11:01:48 -080069 * @param smsCbMessage The SmsCbMessage to include with the intent
Jordan Liu45c0d542019-11-22 11:00:05 -080070 * @param resultReceiver Your own BroadcastReceiver to treat as the final receiver of the
71 * broadcast.
72 * @param scheduler A custom Handler with which to schedule the resultReceiver
73 * callback; if null it will be scheduled in the Context's main
74 * thread.
75 * @param initialCode An initial value for the result code. Often Activity.RESULT_OK.
Jordan Liu2bc01a82020-01-16 11:01:48 -080076 * @param slotIndex The slot index to include in the intent
Jordan Liu45c0d542019-11-22 11:00:05 -080077 */
Jordan Liu2bc01a82020-01-16 11:01:48 -080078 public static void sendSmsCbReceivedBroadcast(@NonNull Context context,
79 @Nullable UserHandle user, @NonNull SmsCbMessage smsCbMessage,
80 @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler,
81 int initialCode, int slotIndex) {
82 Intent backgroundIntent = new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION);
83 backgroundIntent.putExtra(EXTRA_MESSAGE, smsCbMessage);
Jordan Liue1b7ab12019-12-09 13:45:07 -080084 backgroundIntent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jordan Liu2bc01a82020-01-16 11:01:48 -080085 putPhoneIdAndSubIdExtra(context, backgroundIntent, slotIndex);
86
87 String receiverPermission = Manifest.permission.RECEIVE_SMS;
88 String receiverAppOp = AppOpsManager.OPSTR_RECEIVE_SMS;
Jordan Liu45c0d542019-11-22 11:00:05 -080089 if (user != null) {
Jordan Liue1b7ab12019-12-09 13:45:07 -080090 context.createContextAsUser(user, 0).sendOrderedBroadcast(backgroundIntent,
91 receiverPermission, receiverAppOp, resultReceiver, scheduler, initialCode,
Jordan Liu2bc01a82020-01-16 11:01:48 -080092 null, null);
Jordan Liu45c0d542019-11-22 11:00:05 -080093 } else {
Jordan Liue1b7ab12019-12-09 13:45:07 -080094 context.sendOrderedBroadcast(backgroundIntent, receiverPermission,
Jordan Liu2bc01a82020-01-16 11:01:48 -080095 receiverAppOp, resultReceiver, scheduler, initialCode, null, null);
96 }
97 }
98
99 /**
100 * Put the phone ID and sub ID into an intent as extras.
101 */
102 private static void putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId) {
103 int subId = getSubIdForPhone(context, phoneId);
104 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
105 intent.putExtra("subscription", subId);
106 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
107 }
108 intent.putExtra("phone", phoneId);
109 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, phoneId);
110 }
111
112 /**
113 * Get the subscription ID for a phone ID, or INVALID_SUBSCRIPTION_ID if the phone does not
114 * have an active sub
115 * @param phoneId the phoneId to use
116 * @return the associated sub id
117 */
118 private static int getSubIdForPhone(Context context, int phoneId) {
119 SubscriptionManager subMan =
120 (SubscriptionManager) context.getSystemService(
121 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
122 int[] subIds = subMan.getSubscriptionIds(phoneId);
123 if (subIds != null) {
124 return subIds[0];
125 } else {
126 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jordan Liu45c0d542019-11-22 11:00:05 -0800127 }
128 }
129}