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