Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 19 | import android.content.ContentResolver; |
| 20 | import android.content.ContentUris; |
| 21 | import android.content.Context; |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.net.Uri; |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 25 | import android.os.Looper; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 26 | import android.os.Message; |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 27 | import android.os.SystemClock; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 28 | import android.provider.Telephony.CellBroadcasts; |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 29 | import android.telephony.CbGeoUtils.Geometry; |
| 30 | import android.telephony.CellIdentity; |
| 31 | import android.telephony.CellIdentityGsm; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 32 | import android.telephony.CellInfo; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 33 | import android.telephony.SmsCbLocation; |
| 34 | import android.telephony.SmsCbMessage; |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 35 | import android.telephony.SubscriptionManager; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 36 | import android.telephony.TelephonyManager; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 37 | import android.text.format.DateUtils; |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 38 | import android.util.Pair; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 39 | |
| 40 | import com.android.cellbroadcastservice.GsmSmsCbMessage.GeoFencingTriggerMessage; |
| 41 | import com.android.cellbroadcastservice.GsmSmsCbMessage.GeoFencingTriggerMessage.CellBroadcastIdentity; |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 43 | |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 44 | import java.text.DateFormat; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.HashMap; |
| 47 | import java.util.Iterator; |
| 48 | import java.util.List; |
| 49 | |
| 50 | /** |
| 51 | * Handler for 3GPP format Cell Broadcasts. Parent class can also handle CDMA Cell Broadcasts. |
| 52 | */ |
| 53 | public class GsmCellBroadcastHandler extends CellBroadcastHandler { |
| 54 | private static final boolean VDBG = false; // log CB PDU data |
| 55 | |
| 56 | /** Indicates that a message is not being broadcasted. */ |
| 57 | private static final String MESSAGE_NOT_BROADCASTED = "0"; |
| 58 | |
| 59 | /** This map holds incomplete concatenated messages waiting for assembly. */ |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 60 | private final HashMap<SmsCbConcatInfo, byte[][]> mSmsCbPageMap = |
| 61 | new HashMap<>(4); |
| 62 | |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 63 | @VisibleForTesting |
| 64 | protected GsmCellBroadcastHandler(Context context, Looper looper) { |
| 65 | super("GsmCellBroadcastHandler", context, looper); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void onQuitting() { |
| 70 | super.onQuitting(); // release wakelock |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Handle a GSM cell broadcast message passed from the telephony framework. |
| 75 | * @param message |
| 76 | */ |
| 77 | public void onGsmCellBroadcastSms(int slotIndex, byte[] message) { |
| 78 | sendMessage(EVENT_NEW_SMS_MESSAGE, slotIndex, -1, message); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Create a new CellBroadcastHandler. |
| 83 | * @param context the context to use for dispatching Intents |
| 84 | * @return the new handler |
| 85 | */ |
| 86 | public static GsmCellBroadcastHandler makeGsmCellBroadcastHandler(Context context) { |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 87 | GsmCellBroadcastHandler handler = new GsmCellBroadcastHandler(context, Looper.myLooper()); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 88 | handler.start(); |
| 89 | return handler; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Find the cell broadcast messages specify by the geo-fencing trigger message and perform a |
| 94 | * geo-fencing check for these messages. |
| 95 | * @param geoFencingTriggerMessage the trigger message |
| 96 | * |
| 97 | * @return {@code True} if geo-fencing is need for some cell broadcast message. |
| 98 | */ |
| 99 | private boolean handleGeoFencingTriggerMessage( |
| 100 | GeoFencingTriggerMessage geoFencingTriggerMessage, int slotIndex) { |
| 101 | final List<SmsCbMessage> cbMessages = new ArrayList<>(); |
| 102 | final List<Uri> cbMessageUris = new ArrayList<>(); |
| 103 | |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 104 | SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService( |
| 105 | Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 106 | int[] subIds = subMgr.getSubscriptionIds(slotIndex); |
| 107 | Resources res; |
| 108 | if (subIds != null) { |
| 109 | res = getResources(subIds[0]); |
| 110 | } else { |
| 111 | res = getResources(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID); |
| 112 | } |
| 113 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 114 | // Only consider the cell broadcast received within 24 hours. |
| 115 | long lastReceivedTime = System.currentTimeMillis() - DateUtils.DAY_IN_MILLIS; |
| 116 | |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 117 | // Some carriers require reset duplication detection after airplane mode or reboot. |
| 118 | if (res.getBoolean(R.bool.reset_on_power_cycle_or_airplane_mode)) { |
| 119 | lastReceivedTime = Long.max(lastReceivedTime, mLastAirplaneModeTime); |
| 120 | lastReceivedTime = Long.max(lastReceivedTime, |
| 121 | System.currentTimeMillis() - SystemClock.elapsedRealtime()); |
| 122 | } |
| 123 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 124 | // Find the cell broadcast message identify by the message identifier and serial number |
| 125 | // and is not broadcasted. |
| 126 | String where = CellBroadcasts.SERVICE_CATEGORY + "=? AND " |
| 127 | + CellBroadcasts.SERIAL_NUMBER + "=? AND " |
| 128 | + CellBroadcasts.MESSAGE_BROADCASTED + "=? AND " |
| 129 | + CellBroadcasts.RECEIVED_TIME + ">?"; |
| 130 | |
| 131 | ContentResolver resolver = mContext.getContentResolver(); |
| 132 | for (CellBroadcastIdentity identity : geoFencingTriggerMessage.cbIdentifiers) { |
Chen Xu | 34c7167 | 2019-10-21 22:56:37 -0700 | [diff] [blame] | 133 | try (Cursor cursor = resolver.query(CellBroadcasts.CONTENT_URI, |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 134 | CellBroadcasts.QUERY_COLUMNS_FWK, |
| 135 | where, |
| 136 | new String[] { Integer.toString(identity.messageIdentifier), |
| 137 | Integer.toString(identity.serialNumber), MESSAGE_NOT_BROADCASTED, |
| 138 | Long.toString(lastReceivedTime) }, |
| 139 | null /* sortOrder */)) { |
| 140 | if (cursor != null) { |
| 141 | while (cursor.moveToNext()) { |
| 142 | cbMessages.add(SmsCbMessage.createFromCursor(cursor)); |
Chen Xu | 34c7167 | 2019-10-21 22:56:37 -0700 | [diff] [blame] | 143 | cbMessageUris.add(ContentUris.withAppendedId(CellBroadcasts.CONTENT_URI, |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 144 | cursor.getInt(cursor.getColumnIndex(CellBroadcasts._ID)))); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 150 | log("Found " + cbMessages.size() + " not broadcasted messages since " |
| 151 | + DateFormat.getDateTimeInstance().format(lastReceivedTime)); |
| 152 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 153 | List<Geometry> commonBroadcastArea = new ArrayList<>(); |
| 154 | if (geoFencingTriggerMessage.shouldShareBroadcastArea()) { |
| 155 | for (SmsCbMessage msg : cbMessages) { |
| 156 | if (msg.getGeometries() != null) { |
| 157 | commonBroadcastArea.addAll(msg.getGeometries()); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // ATIS doesn't specify the geo fencing maximum wait time for the cell broadcasts specified |
| 163 | // in geo fencing trigger message. We will pick the largest maximum wait time among these |
| 164 | // cell broadcasts. |
| 165 | int maximumWaitTimeSec = 0; |
| 166 | for (SmsCbMessage msg : cbMessages) { |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 167 | maximumWaitTimeSec = Math.max(maximumWaitTimeSec, msg.getMaximumWaitingDuration()); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (DBG) { |
| 171 | logd("Geo-fencing trigger message = " + geoFencingTriggerMessage); |
| 172 | for (SmsCbMessage msg : cbMessages) { |
| 173 | logd(msg.toString()); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (cbMessages.isEmpty()) { |
| 178 | if (DBG) logd("No CellBroadcast message need to be broadcasted"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | requestLocationUpdate(location -> { |
| 183 | if (location == null) { |
| 184 | // If the location is not available, broadcast the messages directly. |
Jack Yu | 8cb5d15 | 2019-10-23 11:35:32 -0700 | [diff] [blame] | 185 | for (int i = 0; i < cbMessages.size(); i++) { |
| 186 | broadcastMessage(cbMessages.get(i), cbMessageUris.get(i), slotIndex); |
| 187 | } |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 188 | } else { |
| 189 | for (int i = 0; i < cbMessages.size(); i++) { |
| 190 | List<Geometry> broadcastArea = !commonBroadcastArea.isEmpty() |
| 191 | ? commonBroadcastArea : cbMessages.get(i).getGeometries(); |
| 192 | if (broadcastArea == null || broadcastArea.isEmpty()) { |
| 193 | broadcastMessage(cbMessages.get(i), cbMessageUris.get(i), slotIndex); |
| 194 | } else { |
| 195 | performGeoFencing(cbMessages.get(i), cbMessageUris.get(i), broadcastArea, |
| 196 | location, slotIndex); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | }, maximumWaitTimeSec); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Handle 3GPP-format Cell Broadcast messages sent from radio. |
| 206 | * |
| 207 | * @param message the message to process |
| 208 | * @return true if need to wait for geo-fencing or an ordered broadcast was sent. |
| 209 | */ |
| 210 | @Override |
| 211 | protected boolean handleSmsMessage(Message message) { |
| 212 | // For GSM, message.obj should be a byte[] |
| 213 | int slotIndex = message.arg1; |
| 214 | if (message.obj instanceof byte[]) { |
| 215 | byte[] pdu = (byte[]) message.obj; |
| 216 | SmsCbHeader header = createSmsCbHeader(pdu); |
| 217 | if (header == null) return false; |
| 218 | |
Jack Yu | 7afa1c0 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 219 | log("header=" + header); |
Jordan Liu | 5e17455 | 2019-11-07 11:54:10 -0800 | [diff] [blame^] | 220 | if (header.getServiceCategory() == SmsCbConstants.MESSAGE_ID_CMAS_GEO_FENCING_TRIGGER) { |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 221 | GeoFencingTriggerMessage triggerMessage = |
| 222 | GsmSmsCbMessage.createGeoFencingTriggerMessage(pdu); |
| 223 | if (triggerMessage != null) { |
| 224 | return handleGeoFencingTriggerMessage(triggerMessage, slotIndex); |
| 225 | } |
| 226 | } else { |
| 227 | SmsCbMessage cbMessage = handleGsmBroadcastSms(header, pdu, slotIndex); |
| 228 | if (cbMessage != null) { |
Jack Yu | 2390cad | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 229 | if (isDuplicate(cbMessage)) { |
| 230 | return false; |
| 231 | } |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 232 | handleBroadcastSms(cbMessage); |
| 233 | return true; |
| 234 | } |
| 235 | if (VDBG) log("Not handled GSM broadcasts."); |
| 236 | } |
| 237 | } |
| 238 | return super.handleSmsMessage(message); |
| 239 | } |
| 240 | |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 241 | // return the GSM cell location from the first GSM cell info |
| 242 | private Pair<Integer, Integer> getGsmLacAndCid() { |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 243 | TelephonyManager tm = |
| 244 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 245 | List<CellInfo> infos = tm.getAllCellInfo(); |
| 246 | for (CellInfo info : infos) { |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 247 | CellIdentity ci = info.getCellIdentity(); |
| 248 | if (ci instanceof CellIdentityGsm) { |
| 249 | CellIdentityGsm ciGsm = (CellIdentityGsm) ci; |
| 250 | int lac = ciGsm.getLac() != CellInfo.UNAVAILABLE ? ciGsm.getLac() : -1; |
| 251 | int cid = ciGsm.getCid() != CellInfo.UNAVAILABLE ? ciGsm.getCid() : -1; |
| 252 | return Pair.create(lac, cid); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 255 | return null; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * Handle 3GPP format SMS-CB message. |
| 261 | * @param header the cellbroadcast header. |
| 262 | * @param receivedPdu the received PDUs as a byte[] |
| 263 | */ |
| 264 | private SmsCbMessage handleGsmBroadcastSms(SmsCbHeader header, byte[] receivedPdu, |
| 265 | int slotIndex) { |
| 266 | try { |
| 267 | if (VDBG) { |
| 268 | int pduLength = receivedPdu.length; |
| 269 | for (int i = 0; i < pduLength; i += 8) { |
| 270 | StringBuilder sb = new StringBuilder("SMS CB pdu data: "); |
| 271 | for (int j = i; j < i + 8 && j < pduLength; j++) { |
| 272 | int b = receivedPdu[j] & 0xff; |
| 273 | if (b < 0x10) { |
| 274 | sb.append('0'); |
| 275 | } |
| 276 | sb.append(Integer.toHexString(b)).append(' '); |
| 277 | } |
| 278 | log(sb.toString()); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (VDBG) log("header=" + header); |
| 283 | TelephonyManager tm = |
| 284 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 285 | tm.createForSubscriptionId(getSubIdForPhone(slotIndex)); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 286 | // TODO make a systemAPI for getNetworkOperatorForSlotIndex |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 287 | String plmn = tm.getSimOperator(); |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 288 | int lac = -1; |
| 289 | int cid = -1; |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 290 | Pair<Integer, Integer> lacAndCid = getGsmLacAndCid(); |
| 291 | // Check if GSM lac and cid are available. This is required to support |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 292 | // dual-mode devices such as CDMA/LTE devices that require support for |
| 293 | // both 3GPP and 3GPP2 format messages |
Jordan Liu | 8d7d0a1 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 294 | if (lacAndCid != null) { |
| 295 | lac = lacAndCid.first; |
| 296 | cid = lacAndCid.second; |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | SmsCbLocation location; |
| 300 | switch (header.getGeographicalScope()) { |
| 301 | case SmsCbMessage.GEOGRAPHICAL_SCOPE_LOCATION_AREA_WIDE: |
| 302 | location = new SmsCbLocation(plmn, lac, -1); |
| 303 | break; |
| 304 | |
| 305 | case SmsCbMessage.GEOGRAPHICAL_SCOPE_CELL_WIDE: |
| 306 | case SmsCbMessage.GEOGRAPHICAL_SCOPE_CELL_WIDE_IMMEDIATE: |
| 307 | location = new SmsCbLocation(plmn, lac, cid); |
| 308 | break; |
| 309 | |
| 310 | case SmsCbMessage.GEOGRAPHICAL_SCOPE_PLMN_WIDE: |
| 311 | default: |
| 312 | location = new SmsCbLocation(plmn); |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | byte[][] pdus; |
| 317 | int pageCount = header.getNumberOfPages(); |
| 318 | if (pageCount > 1) { |
| 319 | // Multi-page message |
| 320 | SmsCbConcatInfo concatInfo = new SmsCbConcatInfo(header, location); |
| 321 | |
| 322 | // Try to find other pages of the same message |
| 323 | pdus = mSmsCbPageMap.get(concatInfo); |
| 324 | |
| 325 | if (pdus == null) { |
| 326 | // This is the first page of this message, make room for all |
| 327 | // pages and keep until complete |
| 328 | pdus = new byte[pageCount][]; |
| 329 | |
| 330 | mSmsCbPageMap.put(concatInfo, pdus); |
| 331 | } |
| 332 | |
| 333 | if (VDBG) log("pdus size=" + pdus.length); |
| 334 | // Page parameter is one-based |
| 335 | pdus[header.getPageIndex() - 1] = receivedPdu; |
| 336 | |
| 337 | for (byte[] pdu : pdus) { |
| 338 | if (pdu == null) { |
| 339 | // Still missing pages, exit |
| 340 | log("still missing pdu"); |
| 341 | return null; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Message complete, remove and dispatch |
| 346 | mSmsCbPageMap.remove(concatInfo); |
| 347 | } else { |
| 348 | // Single page message |
| 349 | pdus = new byte[1][]; |
| 350 | pdus[0] = receivedPdu; |
| 351 | } |
| 352 | |
| 353 | // Remove messages that are out of scope to prevent the map from |
| 354 | // growing indefinitely, containing incomplete messages that were |
| 355 | // never assembled |
| 356 | Iterator<SmsCbConcatInfo> iter = mSmsCbPageMap.keySet().iterator(); |
| 357 | |
| 358 | while (iter.hasNext()) { |
| 359 | SmsCbConcatInfo info = iter.next(); |
| 360 | |
| 361 | if (!info.matchesLocation(plmn, lac, cid)) { |
| 362 | iter.remove(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return GsmSmsCbMessage.createSmsCbMessage(mContext, header, location, pdus, slotIndex); |
| 367 | |
| 368 | } catch (RuntimeException e) { |
| 369 | loge("Error in decoding SMS CB pdu", e); |
| 370 | return null; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | private SmsCbHeader createSmsCbHeader(byte[] bytes) { |
| 375 | try { |
| 376 | return new SmsCbHeader(bytes); |
| 377 | } catch (Exception ex) { |
| 378 | loge("Can't create SmsCbHeader, ex = " + ex.toString()); |
| 379 | return null; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Holds all info about a message page needed to assemble a complete concatenated message. |
| 385 | */ |
| 386 | private static final class SmsCbConcatInfo { |
| 387 | |
| 388 | private final SmsCbHeader mHeader; |
| 389 | private final SmsCbLocation mLocation; |
| 390 | |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 391 | SmsCbConcatInfo(SmsCbHeader header, SmsCbLocation location) { |
| 392 | mHeader = header; |
| 393 | mLocation = location; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public int hashCode() { |
| 398 | return (mHeader.getSerialNumber() * 31) + mLocation.hashCode(); |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public boolean equals(Object obj) { |
| 403 | if (obj instanceof SmsCbConcatInfo) { |
| 404 | SmsCbConcatInfo other = (SmsCbConcatInfo) obj; |
| 405 | |
| 406 | // Two pages match if they have the same serial number (which includes the |
| 407 | // geographical scope and update number), and both pages belong to the same |
| 408 | // location (PLMN, plus LAC and CID if these are part of the geographical scope). |
| 409 | return mHeader.getSerialNumber() == other.mHeader.getSerialNumber() |
| 410 | && mLocation.equals(other.mLocation); |
| 411 | } |
| 412 | |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Compare the location code for this message to the current location code. The match is |
| 418 | * relative to the geographical scope of the message, which determines whether the LAC |
| 419 | * and Cell ID are saved in mLocation or set to -1 to match all values. |
| 420 | * |
| 421 | * @param plmn the current PLMN |
| 422 | * @param lac the current Location Area (GSM) or Service Area (UMTS) |
| 423 | * @param cid the current Cell ID |
| 424 | * @return true if this message is valid for the current location; false otherwise |
| 425 | */ |
Jordan Liu | dfcbfaf | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 426 | public boolean matchesLocation(String plmn, int lac, int cid) { |
| 427 | return mLocation.isInLocationArea(plmn, lac, cid); |
| 428 | } |
| 429 | } |
| 430 | } |