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. The information 49 * can be retrieved by {@link CellBroadcastService#getCellBroadcastAreaInfo(int)}. The 50 * associated SIM slot index of updated area information can be retrieved through the extra 51 * {@link SubscriptionManager#EXTRA_SLOT_INDEX}. 52 * 53 * @see SubscriptionManager#EXTRA_SLOT_INDEX 54 */ 55 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 56 public static final String ACTION_AREA_INFO_UPDATED = 57 "android.telephony.action.AREA_INFO_UPDATED"; 58 59 /** 60 * @hide 61 */ CellBroadcastIntents()62 private CellBroadcastIntents() { 63 } 64 65 /** 66 * Broadcasts an SMS_CB_RECEIVED_ACTION intent which can be received by background 67 * BroadcastReceivers. This is only intended to be used by the CellBroadcastService and will 68 * do nothing if the caller does not have permission to broadcast 69 * {@link Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION}. 70 * 71 * @param context The context from which to send the broadcast 72 * @param user The user from which to send the broadcast 73 * @param smsCbMessage The SmsCbMessage to include with the intent 74 * @param resultReceiver Your own BroadcastReceiver to treat as the final receiver of the 75 * broadcast. 76 * @param scheduler A custom Handler with which to schedule the resultReceiver 77 * callback; if null it will be scheduled in the Context's main 78 * thread. 79 * @param initialCode An initial value for the result code. Often Activity.RESULT_OK. 80 * @param slotIndex The slot index to include in the intent 81 */ sendSmsCbReceivedBroadcast(@onNull Context context, @Nullable UserHandle user, @NonNull SmsCbMessage smsCbMessage, @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, int slotIndex)82 public static void sendSmsCbReceivedBroadcast(@NonNull Context context, 83 @Nullable UserHandle user, @NonNull SmsCbMessage smsCbMessage, 84 @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, 85 int initialCode, int slotIndex) { 86 Intent backgroundIntent = new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION); 87 backgroundIntent.putExtra(EXTRA_MESSAGE, smsCbMessage); 88 putPhoneIdAndSubIdExtra(context, backgroundIntent, slotIndex); 89 90 String receiverPermission = Manifest.permission.RECEIVE_SMS; 91 String receiverAppOp = AppOpsManager.OPSTR_RECEIVE_SMS; 92 if (user != null) { 93 context.createContextAsUser(user, 0).sendOrderedBroadcast(backgroundIntent, 94 receiverPermission, receiverAppOp, resultReceiver, scheduler, initialCode, 95 null, null); 96 } else { 97 context.sendOrderedBroadcast(backgroundIntent, receiverPermission, 98 receiverAppOp, resultReceiver, scheduler, initialCode, null, null); 99 } 100 } 101 102 /** 103 * Put the phone ID and sub ID into an intent as extras. 104 */ putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId)105 private static void putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId) { 106 int subId = getSubIdForPhone(context, phoneId); 107 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 108 intent.putExtra("subscription", subId); 109 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); 110 } 111 intent.putExtra("phone", phoneId); 112 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, phoneId); 113 } 114 115 /** 116 * Get the subscription ID for a phone ID, or INVALID_SUBSCRIPTION_ID if the phone does not 117 * have an active sub 118 * @param phoneId the phoneId to use 119 * @return the associated sub id 120 */ getSubIdForPhone(Context context, int phoneId)121 private static int getSubIdForPhone(Context context, int phoneId) { 122 SubscriptionManager subMan = 123 (SubscriptionManager) context.getSystemService( 124 Context.TELEPHONY_SUBSCRIPTION_SERVICE); 125 int[] subIds = subMan.getSubscriptionIds(phoneId); 126 if (subIds != null) { 127 return subIds[0]; 128 } else { 129 return SubscriptionManager.INVALID_SUBSCRIPTION_ID; 130 } 131 } 132 } 133