1 /* 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 17 package android.telephony; 18 19 import android.Manifest; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SdkConstant; 23 import android.annotation.SdkConstant.SdkConstantType; 24 import android.annotation.SystemApi; 25 import android.app.AppOpsManager; 26 import android.content.BroadcastReceiver; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.os.Handler; 30 import android.os.UserHandle; 31 import android.provider.Telephony; 32 33 /** 34 * A static helper class used to send Intents with prepopulated flags. 35 * <p> 36 * 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}. 38 * 39 * @hide 40 */ 41 @SystemApi 42 public class CellBroadcastIntents { 43 private static final String LOG_TAG = "CellBroadcastIntents"; 44 45 private static final String EXTRA_MESSAGE = "message"; 46 47 /** 48 * Broadcast intent action for notifying area information has been updated. broadcast is also 49 * sent when the user turns off area info alerts. The information 50 * can be retrieved by {@link CellBroadcastService#getCellBroadcastAreaInfo(int)}. The 51 * associated SIM slot index of updated area information can be retrieved through the extra 52 * {@link SubscriptionManager#EXTRA_SLOT_INDEX}. 53 * 54 * @see SubscriptionManager#EXTRA_SLOT_INDEX 55 */ 56 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 57 public static final String ACTION_AREA_INFO_UPDATED = 58 "android.telephony.action.AREA_INFO_UPDATED"; 59 60 /** 61 * @hide 62 */ CellBroadcastIntents()63 private CellBroadcastIntents() { 64 } 65 66 /** 67 * Broadcasts an SMS_CB_RECEIVED_ACTION intent which can be received by background 68 * BroadcastReceivers. This is only intended to be used by the CellBroadcastService and will 69 * do nothing if the caller does not have permission to broadcast 70 * {@link Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION}. 71 * 72 * @param context The context from which to send the broadcast 73 * @param user The user from which to send the broadcast 74 * @param smsCbMessage The SmsCbMessage to include with the intent 75 * @param resultReceiver Your own BroadcastReceiver to treat as the final receiver of the 76 * broadcast. 77 * @param scheduler A custom Handler with which to schedule the resultReceiver 78 * callback; if null it will be scheduled in the Context's main 79 * thread. 80 * @param initialCode An initial value for the result code. Often Activity.RESULT_OK. 81 * @param slotIndex The slot index to include in the intent 82 */ sendSmsCbReceivedBroadcast(@onNull Context context, @Nullable UserHandle user, @NonNull SmsCbMessage smsCbMessage, @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, int slotIndex)83 public static void sendSmsCbReceivedBroadcast(@NonNull Context context, 84 @Nullable UserHandle user, @NonNull SmsCbMessage smsCbMessage, 85 @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, 86 int initialCode, int slotIndex) { 87 Intent backgroundIntent = new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION); 88 backgroundIntent.putExtra(EXTRA_MESSAGE, smsCbMessage); 89 putPhoneIdAndSubIdExtra(context, backgroundIntent, slotIndex); 90 91 String receiverPermission = Manifest.permission.RECEIVE_SMS; 92 String receiverAppOp = AppOpsManager.OPSTR_RECEIVE_SMS; 93 if (user != null) { 94 context.createContextAsUser(user, 0).sendOrderedBroadcast(backgroundIntent, 95 receiverPermission, receiverAppOp, resultReceiver, scheduler, initialCode, 96 null, null); 97 } else { 98 context.sendOrderedBroadcast(backgroundIntent, receiverPermission, 99 receiverAppOp, resultReceiver, scheduler, initialCode, null, null); 100 } 101 } 102 103 /** 104 * Put the phone ID and sub ID into an intent as extras. 105 */ putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId)106 private static void putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId) { 107 int subId = SubscriptionManager.getSubscriptionId(phoneId); 108 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 109 intent.putExtra("subscription", subId); 110 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); 111 } 112 intent.putExtra("phone", phoneId); 113 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, phoneId); 114 } 115 } 116