blob: 6b998012d2180fc221c3c6cb69823c15ce0f6fad [file] [log] [blame]
Jordan Liuc872fad2019-10-11 11:42:03 -07001/*
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
17package com.android.cellbroadcastservice;
18
Jack Yu87db6b72020-01-24 11:45:59 -080019import android.annotation.NonNull;
Jordan Liuc872fad2019-10-11 11:42:03 -070020import android.content.Context;
Jordan Liu15605d12019-10-18 12:06:35 -070021import android.os.Bundle;
Jordan Liuc872fad2019-10-11 11:42:03 -070022import android.telephony.CellBroadcastService;
23import android.telephony.SmsCbLocation;
24import android.telephony.SmsCbMessage;
25import android.telephony.SubscriptionManager;
26import android.telephony.TelephonyManager;
Jordan Liu15605d12019-10-18 12:06:35 -070027import android.telephony.cdma.CdmaSmsCbProgramData;
Jordan Liu75ebf632019-12-16 15:35:27 -080028import android.util.Log;
Jordan Liuc872fad2019-10-11 11:42:03 -070029
Jordan Liu35ef4b52019-11-07 11:34:49 -080030import com.android.internal.annotations.VisibleForTesting;
31
Jordan Liu15605d12019-10-18 12:06:35 -070032import java.util.ArrayList;
33import java.util.List;
34import java.util.function.Consumer;
35
Jordan Liuc872fad2019-10-11 11:42:03 -070036/**
37 * The default implementation of CellBroadcastService, which is used for handling GSM and CDMA
38 * cell broadcast messages.
39 */
40public class DefaultCellBroadcastService extends CellBroadcastService {
41 private GsmCellBroadcastHandler mGsmCellBroadcastHandler;
42 private CellBroadcastHandler mCdmaCellBroadcastHandler;
Jordan Liu15605d12019-10-18 12:06:35 -070043 private CdmaServiceCategoryProgramHandler mCdmaScpHandler;
Jordan Liuc872fad2019-10-11 11:42:03 -070044
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 Liu15605d12019-10-18 12:06:35 -070057 mCdmaScpHandler =
58 CdmaServiceCategoryProgramHandler.makeScpHandler(getApplicationContext());
Jordan Liuc872fad2019-10-11 11:42:03 -070059 }
60
61 @Override
Chen Xu47bb2062020-01-02 18:13:20 -080062 public void onDestroy() {
63 mGsmCellBroadcastHandler.cleanup();
64 mCdmaCellBroadcastHandler.cleanup();
65 super.onDestroy();
66 }
67
68 @Override
Jordan Liuc872fad2019-10-11 11:42:03 -070069 public void onGsmCellBroadcastSms(int slotIndex, byte[] message) {
Jordan Liu75ebf632019-12-16 15:35:27 -080070 Log.d(TAG, "onGsmCellBroadcastSms received message on slotId=" + slotIndex);
Jordan Liuc872fad2019-10-11 11:42:03 -070071 mGsmCellBroadcastHandler.onGsmCellBroadcastSms(slotIndex, message);
72 }
73
74 @Override
75 public void onCdmaCellBroadcastSms(int slotIndex, byte[] bearerData, int serviceCategory) {
Jordan Liu75ebf632019-12-16 15:35:27 -080076 Log.d(TAG, "onCdmaCellBroadcastSms received message on slotId=" + slotIndex);
Jordan Liuc872fad2019-10-11 11:42:03 -070077 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 Liu35ef4b52019-11-07 11:34:49 -080089 SmsCbMessage message = parseBroadcastSms(getApplicationContext(), slotIndex, plmn,
90 bearerData, serviceCategory);
Jordan Liuc872fad2019-10-11 11:42:03 -070091 if (message != null) {
92 mCdmaCellBroadcastHandler.onCdmaCellBroadcastSms(message);
93 }
94 }
95
Jordan Liu15605d12019-10-18 12:06:35 -070096 @Override
97 public void onCdmaScpMessage(int slotIndex, List<CdmaSmsCbProgramData> programData,
98 String originatingAddress, Consumer<Bundle> callback) {
Jordan Liu75ebf632019-12-16 15:35:27 -080099 Log.d(TAG, "onCdmaScpMessage received message on slotId=" + slotIndex);
Jordan Liu15605d12019-10-18 12:06:35 -0700100 mCdmaScpHandler.onCdmaScpMessage(slotIndex, new ArrayList<>(programData),
101 originatingAddress, callback);
102 }
103
Jack Yu87db6b72020-01-24 11:45:59 -0800104 @Override
105 public @NonNull String getCellBroadcastAreaInfo(int slotIndex) {
106 Log.d(TAG, "getCellBroadcastAreaInfo on slotId=" + slotIndex);
107 return mGsmCellBroadcastHandler.getCellBroadcastAreaInfo(slotIndex);
108 }
109
Jordan Liuc872fad2019-10-11 11:42:03 -0700110 /**
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 Liu35ef4b52019-11-07 11:34:49 -0800118 @VisibleForTesting
119 public static SmsCbMessage parseBroadcastSms(Context context, int slotIndex, String plmn,
120 byte[] bearerData,
Jordan Liuc872fad2019-10-11 11:42:03 -0700121 int serviceCategory) {
Jordan Liu35ef4b52019-11-07 11:34:49 -0800122 BearerData bData = BearerData.decode(context, bearerData, serviceCategory);
Jordan Liuc872fad2019-10-11 11:42:03 -0700123 if (bData == null) {
Jordan Liu75ebf632019-12-16 15:35:27 -0800124 Log.w(TAG, "BearerData.decode() returned null");
Jordan Liuc872fad2019-10-11 11:42:03 -0700125 return null;
126 }
Jordan Liu75ebf632019-12-16 15:35:27 -0800127 Log.d(TAG, "MT raw BearerData = " + toHexString(bearerData, 0, bearerData.length));
Chen Xu1b2c9f92019-12-05 23:52:05 -0800128 SmsCbLocation location = new SmsCbLocation(plmn, -1, -1);
Jordan Liuc872fad2019-10-11 11:42:03 -0700129
Jack Yu0bd7e432019-11-20 23:19:29 -0800130 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 Liuc872fad2019-10-11 11:42:03 -0700138 return new SmsCbMessage(SmsCbMessage.MESSAGE_FORMAT_3GPP2,
139 SmsCbMessage.GEOGRAPHICAL_SCOPE_PLMN_WIDE, bData.messageId, location,
Jack Yuda92bbc2020-01-24 13:27:32 -0800140 serviceCategory, bData.getLanguage(), 0, bData.userData.payloadStr,
Jordan Liucdfc9f32019-11-21 11:59:07 -0800141 bData.priority, null, bData.cmasWarningInfo, 0, null, System.currentTimeMillis(),
142 slotIndex, subId);
Jordan Liuc872fad2019-10-11 11:42:03 -0700143 }
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}