Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 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 com.android.cellbroadcastservice; |
| 18 | |
| 19 | import android.content.Context; |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 21 | import android.telephony.CellBroadcastService; |
| 22 | import android.telephony.SmsCbLocation; |
| 23 | import android.telephony.SmsCbMessage; |
| 24 | import android.telephony.SubscriptionManager; |
| 25 | import android.telephony.TelephonyManager; |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 26 | import android.telephony.cdma.CdmaSmsCbProgramData; |
Jordan Liu | e532d23 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 28 | |
Jordan Liu | c8e1239 | 2019-11-07 11:34:49 -0800 | [diff] [blame] | 29 | import com.android.internal.annotations.VisibleForTesting; |
| 30 | |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | import java.util.function.Consumer; |
| 34 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 35 | /** |
| 36 | * The default implementation of CellBroadcastService, which is used for handling GSM and CDMA |
| 37 | * cell broadcast messages. |
| 38 | */ |
| 39 | public class DefaultCellBroadcastService extends CellBroadcastService { |
| 40 | private GsmCellBroadcastHandler mGsmCellBroadcastHandler; |
| 41 | private CellBroadcastHandler mCdmaCellBroadcastHandler; |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 42 | private CdmaServiceCategoryProgramHandler mCdmaScpHandler; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String TAG = "DefaultCellBroadcastService"; |
| 45 | |
| 46 | private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', |
| 47 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 48 | |
| 49 | @Override |
| 50 | public void onCreate() { |
| 51 | super.onCreate(); |
| 52 | mGsmCellBroadcastHandler = |
| 53 | GsmCellBroadcastHandler.makeGsmCellBroadcastHandler(getApplicationContext()); |
| 54 | mCdmaCellBroadcastHandler = |
| 55 | CellBroadcastHandler.makeCellBroadcastHandler(getApplicationContext()); |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 56 | mCdmaScpHandler = |
| 57 | CdmaServiceCategoryProgramHandler.makeScpHandler(getApplicationContext()); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
Chen Xu | 74b654e | 2020-01-02 18:13:20 -0800 | [diff] [blame^] | 61 | public void onDestroy() { |
| 62 | mGsmCellBroadcastHandler.cleanup(); |
| 63 | mCdmaCellBroadcastHandler.cleanup(); |
| 64 | super.onDestroy(); |
| 65 | } |
| 66 | |
| 67 | @Override |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 68 | public void onGsmCellBroadcastSms(int slotIndex, byte[] message) { |
Jordan Liu | e532d23 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 69 | Log.d(TAG, "onGsmCellBroadcastSms received message on slotId=" + slotIndex); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 70 | mGsmCellBroadcastHandler.onGsmCellBroadcastSms(slotIndex, message); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onCdmaCellBroadcastSms(int slotIndex, byte[] bearerData, int serviceCategory) { |
Jordan Liu | e532d23 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 75 | Log.d(TAG, "onCdmaCellBroadcastSms received message on slotId=" + slotIndex); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 76 | int[] subIds = |
| 77 | ((SubscriptionManager) getSystemService( |
| 78 | Context.TELEPHONY_SUBSCRIPTION_SERVICE)).getSubscriptionIds(slotIndex); |
| 79 | String plmn; |
| 80 | if (subIds != null && subIds.length > 0) { |
| 81 | int subId = subIds[0]; |
| 82 | plmn = ((TelephonyManager) getSystemService( |
| 83 | Context.TELEPHONY_SERVICE)).createForSubscriptionId( |
| 84 | subId).getNetworkOperator(); |
| 85 | } else { |
| 86 | plmn = ""; |
| 87 | } |
Jordan Liu | c8e1239 | 2019-11-07 11:34:49 -0800 | [diff] [blame] | 88 | SmsCbMessage message = parseBroadcastSms(getApplicationContext(), slotIndex, plmn, |
| 89 | bearerData, serviceCategory); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 90 | if (message != null) { |
| 91 | mCdmaCellBroadcastHandler.onCdmaCellBroadcastSms(message); |
| 92 | } |
| 93 | } |
| 94 | |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void onCdmaScpMessage(int slotIndex, List<CdmaSmsCbProgramData> programData, |
| 97 | String originatingAddress, Consumer<Bundle> callback) { |
Jordan Liu | e532d23 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 98 | Log.d(TAG, "onCdmaScpMessage received message on slotId=" + slotIndex); |
Jordan Liu | 8866f91 | 2019-10-18 12:06:35 -0700 | [diff] [blame] | 99 | mCdmaScpHandler.onCdmaScpMessage(slotIndex, new ArrayList<>(programData), |
| 100 | originatingAddress, callback); |
| 101 | } |
| 102 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Parses a CDMA broadcast SMS |
| 105 | * |
| 106 | * @param slotIndex the slotIndex the SMS was received on |
| 107 | * @param plmn the PLMN for a broadcast SMS or "" if unknown |
| 108 | * @param bearerData the bearerData of the SMS |
| 109 | * @param serviceCategory the service category of the broadcast |
| 110 | */ |
Jordan Liu | c8e1239 | 2019-11-07 11:34:49 -0800 | [diff] [blame] | 111 | @VisibleForTesting |
| 112 | public static SmsCbMessage parseBroadcastSms(Context context, int slotIndex, String plmn, |
| 113 | byte[] bearerData, |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 114 | int serviceCategory) { |
Jordan Liu | c8e1239 | 2019-11-07 11:34:49 -0800 | [diff] [blame] | 115 | BearerData bData = BearerData.decode(context, bearerData, serviceCategory); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 116 | if (bData == null) { |
Jordan Liu | e532d23 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 117 | Log.w(TAG, "BearerData.decode() returned null"); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 118 | return null; |
| 119 | } |
Jordan Liu | e532d23 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 120 | Log.d(TAG, "MT raw BearerData = " + toHexString(bearerData, 0, bearerData.length)); |
Chen Xu | 7feb7b0 | 2019-12-05 23:52:05 -0800 | [diff] [blame] | 121 | SmsCbLocation location = new SmsCbLocation(plmn, -1, -1); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 122 | |
Jack Yu | 515f34c | 2019-11-20 23:19:29 -0800 | [diff] [blame] | 123 | SubscriptionManager sm = (SubscriptionManager) context.getSystemService( |
| 124 | Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 125 | int subId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; |
| 126 | int[] subIds = sm.getSubscriptionIds(slotIndex); |
| 127 | if (subIds != null && subIds.length > 0) { |
| 128 | subId = subIds[0]; |
| 129 | } |
| 130 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 131 | return new SmsCbMessage(SmsCbMessage.MESSAGE_FORMAT_3GPP2, |
| 132 | SmsCbMessage.GEOGRAPHICAL_SCOPE_PLMN_WIDE, bData.messageId, location, |
| 133 | serviceCategory, bData.getLanguage(), bData.userData.payloadStr, |
Jordan Liu | ac12bae | 2019-11-21 11:59:07 -0800 | [diff] [blame] | 134 | bData.priority, null, bData.cmasWarningInfo, 0, null, System.currentTimeMillis(), |
| 135 | slotIndex, subId); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private static String toHexString(byte[] array, int offset, int length) { |
| 139 | char[] buf = new char[length * 2]; |
| 140 | int bufIndex = 0; |
| 141 | for (int i = offset; i < offset + length; i++) { |
| 142 | byte b = array[i]; |
| 143 | buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F]; |
| 144 | buf[bufIndex++] = HEX_DIGITS[b & 0x0F]; |
| 145 | } |
| 146 | return new String(buf); |
| 147 | } |
| 148 | } |