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