| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 62 |
|
| 63 |
private CellBroadcastIntents() { |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 105 |
|
| 106 |
private static void putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId) { |
| 107 |
int subId = getSubIdForPhone(context, 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 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private static int getSubIdForPhone(Context context, int phoneId) { |
| 123 |
SubscriptionManager subMan = |
| 124 |
(SubscriptionManager) context.getSystemService( |
| 125 |
Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 126 |
int[] subIds = subMan.getSubscriptionIds(phoneId); |
| 127 |
if (subIds != null) { |
| 128 |
return subIds[0]; |
| 129 |
} else { |
| 130 |
return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 131 |
} |
| 132 |
} |
| 133 |
} |