Jordan Liu | c872fad | 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 | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.app.Activity; |
| 23 | import android.app.AppOpsManager; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 28 | import android.content.IntentFilter; |
Philip P. Moltmann | 376f9fa | 2019-10-30 15:25:24 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 30 | import android.content.res.Resources; |
| 31 | import android.database.Cursor; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 32 | import android.location.Location; |
| 33 | import android.location.LocationListener; |
| 34 | import android.location.LocationManager; |
| 35 | import android.net.Uri; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
| 37 | import android.os.Handler; |
| 38 | import android.os.Looper; |
| 39 | import android.os.Message; |
Philip P. Moltmann | 376f9fa | 2019-10-30 15:25:24 -0700 | [diff] [blame] | 40 | import android.os.Process; |
Jack Yu | e7591d3 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 41 | import android.os.SystemClock; |
Jordan Liu | 26e820c | 2019-10-22 14:42:07 -0700 | [diff] [blame] | 42 | import android.os.SystemProperties; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 44 | import android.provider.Telephony; |
| 45 | import android.provider.Telephony.CellBroadcasts; |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 46 | import android.telephony.CbGeoUtils.Geometry; |
| 47 | import android.telephony.CbGeoUtils.LatLng; |
Jordan Liu | 5b80be2 | 2019-11-22 11:30:20 -0800 | [diff] [blame] | 48 | import android.telephony.CellBroadcastIntents; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 49 | import android.telephony.SmsCbMessage; |
| 50 | import android.telephony.SubscriptionManager; |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 51 | import android.telephony.cdma.CdmaSmsCbProgramData; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 52 | import android.text.TextUtils; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 53 | import android.text.format.DateUtils; |
| 54 | import android.util.LocalLog; |
Jordan Liu | 75ebf63 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 55 | import android.util.Log; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 56 | |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 57 | import com.android.internal.annotations.VisibleForTesting; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 58 | |
| 59 | import java.io.FileDescriptor; |
| 60 | import java.io.PrintWriter; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 61 | import java.text.DateFormat; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 62 | import java.util.ArrayList; |
| 63 | import java.util.Arrays; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 64 | import java.util.HashMap; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 65 | import java.util.List; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 66 | import java.util.Map; |
Jack Yu | 6f9a26e | 2019-11-07 23:10:02 -0800 | [diff] [blame] | 67 | import java.util.Objects; |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 68 | import java.util.stream.Collectors; |
| 69 | import java.util.stream.Stream; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Dispatch new Cell Broadcasts to receivers. Acquires a private wakelock until the broadcast |
| 73 | * completes and our result receiver is called. |
| 74 | */ |
| 75 | public class CellBroadcastHandler extends WakeLockStateMachine { |
| 76 | private static final String EXTRA_MESSAGE = "message"; |
| 77 | |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 78 | /** |
| 79 | * To disable cell broadcast duplicate detection for debugging purposes |
| 80 | * <code>adb shell am broadcast -a com.android.cellbroadcastservice.action.DUPLICATE_DETECTION |
| 81 | * --ez enable false</code> |
| 82 | * |
| 83 | * To enable cell broadcast duplicate detection for debugging purposes |
| 84 | * <code>adb shell am broadcast -a com.android.cellbroadcastservice.action.DUPLICATE_DETECTION |
| 85 | * --ez enable true</code> |
| 86 | */ |
| 87 | private static final String ACTION_DUPLICATE_DETECTION = |
| 88 | "com.android.cellbroadcastservice.action.DUPLICATE_DETECTION"; |
| 89 | |
| 90 | /** |
| 91 | * The extra for cell broadcast duplicate detection enable/disable |
| 92 | */ |
| 93 | private static final String EXTRA_ENABLE = "enable"; |
| 94 | |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 95 | private final LocalLog mLocalLog = new LocalLog(100); |
| 96 | |
Jordan Liu | 26e820c | 2019-10-22 14:42:07 -0700 | [diff] [blame] | 97 | private static final boolean IS_DEBUGGABLE = SystemProperties.getInt("ro.debuggable", 0) == 1; |
| 98 | |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 99 | /** Uses to request the location update. */ |
| 100 | public final LocationRequester mLocationRequester; |
| 101 | |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 102 | /** Timestamp of last airplane mode on */ |
Jack Yu | e7591d3 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 103 | protected long mLastAirplaneModeTime = 0; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 104 | |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 105 | /** Resource cache */ |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 106 | private final Map<Integer, Resources> mResourcesCache = new HashMap<>(); |
| 107 | |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 108 | /** Whether performing duplicate detection or not. Note this is for debugging purposes only. */ |
| 109 | private boolean mEnableDuplicateDetection = true; |
| 110 | |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Service category equivalent map. The key is the GSM service category, the value is the CDMA |
| 113 | * service category. |
| 114 | */ |
| 115 | private final Map<Integer, Integer> mServiceCategoryCrossRATMap; |
| 116 | |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 117 | private CellBroadcastHandler(Context context) { |
Jack Yu | e7591d3 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 118 | this("CellBroadcastHandler", context, Looper.myLooper()); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jordan Liu | f24590f | 2019-11-14 14:23:49 -0800 | [diff] [blame] | 121 | @VisibleForTesting |
| 122 | public CellBroadcastHandler(String debugTag, Context context, Looper looper) { |
Jack Yu | e7591d3 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 123 | super(debugTag, context, looper); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 124 | mLocationRequester = new LocationRequester( |
| 125 | context, |
| 126 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE), |
| 127 | getHandler().getLooper()); |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 128 | |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 129 | // Adding GSM / CDMA service category mapping. |
| 130 | mServiceCategoryCrossRATMap = Stream.of(new Integer[][] { |
| 131 | // Presidential alert |
| 132 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL, |
| 133 | CdmaSmsCbProgramData.CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT}, |
| 134 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL_LANGUAGE, |
| 135 | CdmaSmsCbProgramData.CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT}, |
| 136 | |
| 137 | // Extreme alert |
| 138 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED, |
| 139 | CdmaSmsCbProgramData.CATEGORY_CMAS_EXTREME_THREAT}, |
| 140 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED_LANGUAGE, |
| 141 | CdmaSmsCbProgramData.CATEGORY_CMAS_EXTREME_THREAT}, |
| 142 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY, |
| 143 | CdmaSmsCbProgramData.CATEGORY_CMAS_EXTREME_THREAT}, |
| 144 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY_LANGUAGE, |
| 145 | CdmaSmsCbProgramData.CATEGORY_CMAS_EXTREME_THREAT}, |
| 146 | |
| 147 | // Severe alert |
| 148 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED, |
| 149 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 150 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED_LANGUAGE, |
| 151 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 152 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_LIKELY, |
| 153 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 154 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_LIKELY_LANGUAGE, |
| 155 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 156 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_IMMEDIATE_OBSERVED, |
| 157 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 158 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_IMMEDIATE_OBSERVED_LANGUAGE, |
| 159 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 160 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_IMMEDIATE_LIKELY, |
| 161 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 162 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_IMMEDIATE_LIKELY_LANGUAGE, |
| 163 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 164 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_OBSERVED, |
| 165 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 166 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_OBSERVED_LANGUAGE, |
| 167 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 168 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY, |
| 169 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 170 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY_LANGUAGE, |
| 171 | CdmaSmsCbProgramData.CATEGORY_CMAS_SEVERE_THREAT}, |
| 172 | |
| 173 | // Amber alert |
| 174 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY, |
| 175 | CdmaSmsCbProgramData.CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY}, |
| 176 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY_LANGUAGE, |
| 177 | CdmaSmsCbProgramData.CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY}, |
| 178 | |
| 179 | // Monthly test alert |
| 180 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_REQUIRED_MONTHLY_TEST, |
| 181 | CdmaSmsCbProgramData.CATEGORY_CMAS_TEST_MESSAGE}, |
| 182 | { SmsCbConstants.MESSAGE_ID_CMAS_ALERT_REQUIRED_MONTHLY_TEST_LANGUAGE, |
| 183 | CdmaSmsCbProgramData.CATEGORY_CMAS_TEST_MESSAGE}, |
| 184 | }).collect(Collectors.toMap(data -> data[0], data -> data[1])); |
| 185 | |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 186 | IntentFilter intentFilter = new IntentFilter(); |
| 187 | intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 188 | if (IS_DEBUGGABLE) { |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 189 | intentFilter.addAction(ACTION_DUPLICATE_DETECTION); |
| 190 | } |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 191 | mContext.registerReceiver( |
| 192 | new BroadcastReceiver() { |
| 193 | @Override |
| 194 | public void onReceive(Context context, Intent intent) { |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 195 | switch (intent.getAction()) { |
| 196 | case Intent.ACTION_AIRPLANE_MODE_CHANGED: |
| 197 | boolean airplaneModeOn = intent.getBooleanExtra("state", false); |
| 198 | if (airplaneModeOn) { |
| 199 | mLastAirplaneModeTime = System.currentTimeMillis(); |
Jack Yu | 446d174 | 2019-11-12 17:30:27 -0800 | [diff] [blame] | 200 | log("Airplane mode on."); |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 201 | } |
| 202 | break; |
| 203 | case ACTION_DUPLICATE_DETECTION: |
| 204 | mEnableDuplicateDetection = intent.getBooleanExtra(EXTRA_ENABLE, |
| 205 | true); |
| 206 | break; |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 207 | } |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 208 | |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 209 | } |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 210 | }, intentFilter); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Create a new CellBroadcastHandler. |
| 215 | * @param context the context to use for dispatching Intents |
| 216 | * @return the new handler |
| 217 | */ |
| 218 | public static CellBroadcastHandler makeCellBroadcastHandler(Context context) { |
| 219 | CellBroadcastHandler handler = new CellBroadcastHandler(context); |
| 220 | handler.start(); |
| 221 | return handler; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Handle Cell Broadcast messages from {@code CdmaInboundSmsHandler}. |
| 226 | * 3GPP-format Cell Broadcast messages sent from radio are handled in the subclass. |
| 227 | * |
| 228 | * @param message the message to process |
| 229 | * @return true if need to wait for geo-fencing or an ordered broadcast was sent. |
| 230 | */ |
| 231 | @Override |
| 232 | protected boolean handleSmsMessage(Message message) { |
| 233 | if (message.obj instanceof SmsCbMessage) { |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 234 | if (!isDuplicate((SmsCbMessage) message.obj)) { |
| 235 | handleBroadcastSms((SmsCbMessage) message.obj); |
| 236 | return true; |
| 237 | } |
| 238 | return false; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 239 | } else { |
| 240 | loge("handleMessage got object of type: " + message.obj.getClass().getName()); |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Dispatch a Cell Broadcast message to listeners. |
| 247 | * @param message the Cell Broadcast to broadcast |
| 248 | */ |
| 249 | protected void handleBroadcastSms(SmsCbMessage message) { |
| 250 | int slotIndex = message.getSlotIndex(); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 251 | |
| 252 | // TODO: Database inserting can be time consuming, therefore this should be changed to |
| 253 | // asynchronous. |
| 254 | ContentValues cv = message.getContentValues(); |
Chen Xu | 93fdfeb | 2019-10-21 22:56:37 -0700 | [diff] [blame] | 255 | Uri uri = mContext.getContentResolver().insert(CellBroadcasts.CONTENT_URI, cv); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 256 | |
| 257 | if (message.needGeoFencingCheck()) { |
| 258 | if (DBG) { |
| 259 | log("Request location update for geo-fencing. serialNumber = " |
| 260 | + message.getSerialNumber()); |
| 261 | } |
| 262 | |
| 263 | requestLocationUpdate(location -> { |
| 264 | if (location == null) { |
| 265 | // Broadcast the message directly if the location is not available. |
| 266 | broadcastMessage(message, uri, slotIndex); |
| 267 | } else { |
| 268 | performGeoFencing(message, uri, message.getGeometries(), location, slotIndex); |
| 269 | } |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 270 | }, message.getMaximumWaitingDuration()); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 271 | } else { |
| 272 | if (DBG) { |
| 273 | log("Broadcast the message directly because no geo-fencing required, " |
| 274 | + "serialNumber = " + message.getSerialNumber() |
| 275 | + " needGeoFencing = " + message.needGeoFencingCheck()); |
| 276 | } |
| 277 | broadcastMessage(message, uri, slotIndex); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 282 | * Check if the message is a duplicate |
| 283 | * |
| 284 | * @param message Cell broadcast message |
| 285 | * @return {@code true} if this message is a duplicate |
| 286 | */ |
| 287 | @VisibleForTesting |
| 288 | public boolean isDuplicate(SmsCbMessage message) { |
Jack Yu | dc60760 | 2019-10-31 13:24:43 -0700 | [diff] [blame] | 289 | if (!mEnableDuplicateDetection) { |
| 290 | log("Duplicate detection was disabled for debugging purposes."); |
| 291 | return false; |
| 292 | } |
| 293 | |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 294 | // Find the cell broadcast message identify by the message identifier and serial number |
| 295 | // and is not broadcasted. |
| 296 | String where = CellBroadcasts.RECEIVED_TIME + ">?"; |
| 297 | |
| 298 | int slotIndex = message.getSlotIndex(); |
| 299 | SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService( |
| 300 | Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 301 | int[] subIds = subMgr.getSubscriptionIds(slotIndex); |
| 302 | Resources res; |
| 303 | if (subIds != null) { |
| 304 | res = getResources(subIds[0]); |
| 305 | } else { |
| 306 | res = getResources(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID); |
| 307 | } |
| 308 | |
| 309 | // Only consider cell broadcast messages received within certain period. |
| 310 | // By default it's 24 hours. |
| 311 | long expirationDuration = res.getInteger(R.integer.message_expiration_time); |
| 312 | long dupCheckTime = System.currentTimeMillis() - expirationDuration; |
| 313 | |
Jack Yu | e7591d3 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 314 | // Some carriers require reset duplication detection after airplane mode or reboot. |
| 315 | if (res.getBoolean(R.bool.reset_on_power_cycle_or_airplane_mode)) { |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 316 | dupCheckTime = Long.max(dupCheckTime, mLastAirplaneModeTime); |
Jack Yu | e7591d3 | 2019-11-07 22:59:46 -0800 | [diff] [blame] | 317 | dupCheckTime = Long.max(dupCheckTime, |
| 318 | System.currentTimeMillis() - SystemClock.elapsedRealtime()); |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | List<SmsCbMessage> cbMessages = new ArrayList<>(); |
| 322 | |
| 323 | try (Cursor cursor = mContext.getContentResolver().query(CellBroadcasts.CONTENT_URI, |
Jordan Liu | d3eeb40 | 2019-11-15 11:13:59 -0800 | [diff] [blame] | 324 | CellBroadcastProvider.QUERY_COLUMNS, |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 325 | where, |
| 326 | new String[] {Long.toString(dupCheckTime)}, |
| 327 | null)) { |
| 328 | if (cursor != null) { |
| 329 | while (cursor.moveToNext()) { |
| 330 | cbMessages.add(SmsCbMessage.createFromCursor(cursor)); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | boolean compareMessageBody = res.getBoolean(R.bool.duplicate_compare_body); |
| 336 | |
| 337 | log("Found " + cbMessages.size() + " messages since " |
| 338 | + DateFormat.getDateTimeInstance().format(dupCheckTime)); |
| 339 | for (SmsCbMessage messageToCheck : cbMessages) { |
| 340 | // If messages are from different slots, then we only compare the message body. |
| 341 | if (message.getSlotIndex() != messageToCheck.getSlotIndex()) { |
| 342 | if (TextUtils.equals(message.getMessageBody(), messageToCheck.getMessageBody())) { |
| 343 | log("Duplicate message detected from different slot. " + message); |
| 344 | return true; |
| 345 | } |
| 346 | } else { |
| 347 | // Check serial number if message is from the same carrier. |
| 348 | if (message.getSerialNumber() != messageToCheck.getSerialNumber()) { |
| 349 | // Not a dup. Check next one. |
| 350 | log("Serial number check. Not a dup. " + messageToCheck); |
| 351 | continue; |
| 352 | } |
| 353 | |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 354 | // ETWS primary / secondary should be treated differently. |
| 355 | if (message.isEtwsMessage() && messageToCheck.isEtwsMessage() |
| 356 | && message.getEtwsWarningInfo().isPrimary() |
| 357 | != messageToCheck.getEtwsWarningInfo().isPrimary()) { |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 358 | // Not a dup. Check next one. |
Jack Yu | 6f9a26e | 2019-11-07 23:10:02 -0800 | [diff] [blame] | 359 | log("ETWS primary check. Not a dup. " + messageToCheck); |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 360 | continue; |
| 361 | } |
| 362 | |
Jack Yu | 2003c52 | 2019-10-31 09:47:20 -0700 | [diff] [blame] | 363 | // Check if the message category is different. Some carriers send cell broadcast |
| 364 | // messages on different techs (i.e. GSM / CDMA), so we need to compare service |
| 365 | // category cross techs. |
| 366 | if (message.getServiceCategory() != messageToCheck.getServiceCategory() |
Jack Yu | 6f9a26e | 2019-11-07 23:10:02 -0800 | [diff] [blame] | 367 | && !Objects.equals(mServiceCategoryCrossRATMap.get( |
| 368 | message.getServiceCategory()), messageToCheck.getServiceCategory()) |
| 369 | && !Objects.equals(mServiceCategoryCrossRATMap.get( |
| 370 | messageToCheck.getServiceCategory()), |
| 371 | message.getServiceCategory())) { |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 372 | // Not a dup. Check next one. |
Jack Yu | 6f9a26e | 2019-11-07 23:10:02 -0800 | [diff] [blame] | 373 | log("Service category check. Not a dup. " + messageToCheck); |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 374 | continue; |
| 375 | } |
| 376 | |
| 377 | // Compare message body if needed. |
| 378 | if (!compareMessageBody || TextUtils.equals( |
| 379 | message.getMessageBody(), messageToCheck.getMessageBody())) { |
| 380 | log("Duplicate message detected. " + message); |
| 381 | return true; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | log("Not a duplicate message. " + message); |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | /** |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 391 | * Perform a geo-fencing check for {@code message}. Broadcast the {@code message} if the |
| 392 | * {@code location} is inside the {@code broadcastArea}. |
| 393 | * @param message the message need to geo-fencing check |
| 394 | * @param uri the message's uri |
| 395 | * @param broadcastArea the broadcast area of the message |
| 396 | * @param location current location |
| 397 | */ |
| 398 | protected void performGeoFencing(SmsCbMessage message, Uri uri, List<Geometry> broadcastArea, |
| 399 | LatLng location, int slotIndex) { |
| 400 | |
| 401 | if (DBG) { |
| 402 | logd("Perform geo-fencing check for message identifier = " |
| 403 | + message.getServiceCategory() |
| 404 | + " serialNumber = " + message.getSerialNumber()); |
| 405 | } |
| 406 | |
| 407 | for (Geometry geo : broadcastArea) { |
| 408 | if (geo.contains(location)) { |
| 409 | broadcastMessage(message, uri, slotIndex); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (DBG) { |
| 415 | logd("Device location is outside the broadcast area " |
| 416 | + CbGeoUtils.encodeGeometriesToString(broadcastArea)); |
| 417 | } |
Jack Yu | 2f37c43 | 2019-11-04 21:28:24 -0800 | [diff] [blame] | 418 | |
| 419 | sendMessage(EVENT_BROADCAST_NOT_REQUIRED); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Request a single location update. |
| 424 | * @param callback a callback will be called when the location is available. |
| 425 | * @param maximumWaitTimeSec the maximum wait time of this request. If location is not updated |
| 426 | * within the maximum wait time, {@code callback#onLocationUpadte(null)} will be called. |
| 427 | */ |
| 428 | protected void requestLocationUpdate(LocationUpdateCallback callback, int maximumWaitTimeSec) { |
| 429 | mLocationRequester.requestLocationUpdate(callback, maximumWaitTimeSec); |
| 430 | } |
| 431 | |
| 432 | /** |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 433 | * Get the subscription ID for a phone ID, or INVALID_SUBSCRIPTION_ID if the phone does not |
| 434 | * have an active sub |
| 435 | * @param phoneId the phoneId to use |
| 436 | * @return the associated sub id |
| 437 | */ |
Jordan Liu | cdfc9f3 | 2019-11-21 11:59:07 -0800 | [diff] [blame] | 438 | protected static int getSubIdForPhone(Context context, int phoneId) { |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 439 | SubscriptionManager subMan = |
Jordan Liu | cdfc9f3 | 2019-11-21 11:59:07 -0800 | [diff] [blame] | 440 | (SubscriptionManager) context.getSystemService( |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 441 | Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 442 | int[] subIds = subMan.getSubscriptionIds(phoneId); |
| 443 | if (subIds != null) { |
| 444 | return subIds[0]; |
| 445 | } else { |
| 446 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
Jordan Liu | cdfc9f3 | 2019-11-21 11:59:07 -0800 | [diff] [blame] | 451 | * Put the phone ID and sub ID into an intent as extras. |
| 452 | */ |
| 453 | public static void putPhoneIdAndSubIdExtra(Context context, Intent intent, int phoneId) { |
| 454 | int subId = getSubIdForPhone(context, phoneId); |
| 455 | if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
| 456 | intent.putExtra("subscription", subId); |
| 457 | intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); |
| 458 | } |
| 459 | intent.putExtra("phone", phoneId); |
| 460 | intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, phoneId); |
| 461 | } |
| 462 | |
| 463 | /** |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 464 | * Broadcast the {@code message} to the applications. |
| 465 | * @param message a message need to broadcast |
| 466 | * @param messageUri message's uri |
| 467 | */ |
| 468 | protected void broadcastMessage(@NonNull SmsCbMessage message, @Nullable Uri messageUri, |
| 469 | int slotIndex) { |
| 470 | String receiverPermission; |
Jordan Liu | c4c3b28 | 2019-11-22 11:40:05 -0800 | [diff] [blame] | 471 | String appOp; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 472 | String msg; |
| 473 | Intent intent; |
| 474 | if (message.isEmergencyMessage()) { |
| 475 | msg = "Dispatching emergency SMS CB, SmsCbMessage is: " + message; |
| 476 | log(msg); |
| 477 | mLocalLog.log(msg); |
Chen Xu | eca1f0a | 2019-11-06 20:53:24 -0800 | [diff] [blame] | 478 | intent = new Intent(Telephony.Sms.Intents.ACTION_SMS_EMERGENCY_CB_RECEIVED); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 479 | //Emergency alerts need to be delivered with high priority |
| 480 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 481 | receiverPermission = Manifest.permission.RECEIVE_EMERGENCY_BROADCAST; |
Jordan Liu | c4c3b28 | 2019-11-22 11:40:05 -0800 | [diff] [blame] | 482 | appOp = AppOpsManager.OPSTR_RECEIVE_EMERGENCY_BROADCAST; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 483 | |
| 484 | intent.putExtra(EXTRA_MESSAGE, message); |
Jordan Liu | cdfc9f3 | 2019-11-21 11:59:07 -0800 | [diff] [blame] | 485 | putPhoneIdAndSubIdExtra(mContext, intent, slotIndex); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 486 | |
Jordan Liu | 4a2ff26 | 2019-10-21 12:30:56 -0700 | [diff] [blame] | 487 | if (IS_DEBUGGABLE) { |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 488 | // Send additional broadcast intent to the specified package. This is only for sl4a |
| 489 | // automation tests. |
Jordan Liu | f24590f | 2019-11-14 14:23:49 -0800 | [diff] [blame] | 490 | String[] testPkgs = mContext.getResources().getStringArray( |
| 491 | R.array.config_testCellBroadcastReceiverPkgs); |
| 492 | if (testPkgs != null) { |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 493 | Intent additionalIntent = new Intent(intent); |
Jordan Liu | f24590f | 2019-11-14 14:23:49 -0800 | [diff] [blame] | 494 | for (String pkg : testPkgs) { |
| 495 | additionalIntent.setPackage(pkg); |
Jordan Liu | c4c3b28 | 2019-11-22 11:40:05 -0800 | [diff] [blame] | 496 | mContext.createContextAsUser(UserHandle.ALL, 0).sendOrderedBroadcast( |
Jordan Liu | de4a6ff | 2019-12-06 11:12:50 -0800 | [diff] [blame] | 497 | additionalIntent, receiverPermission, appOp, null, |
Jordan Liu | c4c3b28 | 2019-11-22 11:40:05 -0800 | [diff] [blame] | 498 | getHandler(), Activity.RESULT_OK, null, null); |
Jordan Liu | f24590f | 2019-11-14 14:23:49 -0800 | [diff] [blame] | 499 | } |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | String[] pkgs = mContext.getResources().getStringArray( |
Jordan Liu | f24590f | 2019-11-14 14:23:49 -0800 | [diff] [blame] | 504 | R.array.config_defaultCellBroadcastReceiverPkgs); |
| 505 | if (pkgs != null) { |
| 506 | mReceiverCount.addAndGet(pkgs.length); |
| 507 | for (String pkg : pkgs) { |
| 508 | // Explicitly send the intent to all the configured cell broadcast receivers. |
| 509 | intent.setPackage(pkg); |
Jordan Liu | c4c3b28 | 2019-11-22 11:40:05 -0800 | [diff] [blame] | 510 | mContext.createContextAsUser(UserHandle.ALL, 0).sendOrderedBroadcast( |
| 511 | intent, receiverPermission, appOp, mReceiver, getHandler(), |
| 512 | Activity.RESULT_OK, null, null); |
Jordan Liu | f24590f | 2019-11-14 14:23:49 -0800 | [diff] [blame] | 513 | } |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 514 | } |
| 515 | } else { |
| 516 | msg = "Dispatching SMS CB, SmsCbMessage is: " + message; |
| 517 | log(msg); |
| 518 | mLocalLog.log(msg); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 519 | // Send implicit intent since there are various 3rd party carrier apps listen to |
| 520 | // this intent. |
Jordan Liu | 5b80be2 | 2019-11-22 11:30:20 -0800 | [diff] [blame] | 521 | intent = new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 522 | receiverPermission = Manifest.permission.RECEIVE_SMS; |
Jordan Liu | c4c3b28 | 2019-11-22 11:40:05 -0800 | [diff] [blame] | 523 | appOp = AppOpsManager.OPSTR_RECEIVE_SMS; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 524 | |
| 525 | intent.putExtra(EXTRA_MESSAGE, message); |
Jordan Liu | cdfc9f3 | 2019-11-21 11:59:07 -0800 | [diff] [blame] | 526 | putPhoneIdAndSubIdExtra(mContext, intent, slotIndex); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 527 | |
| 528 | mReceiverCount.incrementAndGet(); |
Jordan Liu | 5b80be2 | 2019-11-22 11:30:20 -0800 | [diff] [blame] | 529 | CellBroadcastIntents.sendOrderedBroadcastForBackgroundReceivers( |
| 530 | mContext, UserHandle.ALL, intent, |
| 531 | receiverPermission, appOp, mReceiver, getHandler(), Activity.RESULT_OK, |
| 532 | null, null); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if (messageUri != null) { |
| 536 | ContentValues cv = new ContentValues(); |
| 537 | cv.put(CellBroadcasts.MESSAGE_BROADCASTED, 1); |
Chen Xu | 93fdfeb | 2019-10-21 22:56:37 -0700 | [diff] [blame] | 538 | mContext.getContentResolver().update(CellBroadcasts.CONTENT_URI, cv, |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 539 | CellBroadcasts._ID + "=?", new String[] {messageUri.getLastPathSegment()}); |
| 540 | } |
| 541 | } |
| 542 | |
Jack Yu | a130927 | 2019-10-24 00:19:49 -0700 | [diff] [blame] | 543 | /** |
| 544 | * Get the device resource based on SIM |
| 545 | * |
| 546 | * @param subId Subscription index |
| 547 | * |
| 548 | * @return The resource |
| 549 | */ |
| 550 | public @NonNull Resources getResources(int subId) { |
| 551 | if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID |
| 552 | || !SubscriptionManager.isValidSubscriptionId(subId)) { |
| 553 | return mContext.getResources(); |
| 554 | } |
| 555 | |
| 556 | if (mResourcesCache.containsKey(subId)) { |
| 557 | return mResourcesCache.get(subId); |
| 558 | } |
| 559 | |
| 560 | Resources res = SubscriptionManager.getResourcesForSubId(mContext, subId); |
| 561 | mResourcesCache.put(subId, res); |
| 562 | |
| 563 | return res; |
| 564 | } |
| 565 | |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 566 | @Override |
| 567 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 568 | pw.println("CellBroadcastHandler:"); |
| 569 | mLocalLog.dump(fd, pw, args); |
| 570 | pw.flush(); |
| 571 | } |
| 572 | |
| 573 | /** The callback interface of a location request. */ |
| 574 | public interface LocationUpdateCallback { |
| 575 | /** |
| 576 | * Call when the location update is available. |
| 577 | * @param location a location in (latitude, longitude) format, or {@code null} if the |
| 578 | * location service is not available. |
| 579 | */ |
| 580 | void onLocationUpdate(@Nullable LatLng location); |
| 581 | } |
| 582 | |
| 583 | private static final class LocationRequester { |
| 584 | private static final String TAG = LocationRequester.class.getSimpleName(); |
| 585 | |
| 586 | /** |
| 587 | * Use as the default maximum wait time if the cell broadcast doesn't specify the value. |
| 588 | * Most of the location request should be responded within 20 seconds. |
| 589 | */ |
| 590 | private static final int DEFAULT_MAXIMUM_WAIT_TIME_SEC = 20; |
| 591 | |
| 592 | /** |
| 593 | * Trigger this event when the {@link LocationManager} is not responded within the given |
| 594 | * time. |
| 595 | */ |
| 596 | private static final int EVENT_LOCATION_REQUEST_TIMEOUT = 1; |
| 597 | |
| 598 | /** Request a single location update. */ |
| 599 | private static final int EVENT_REQUEST_LOCATION_UPDATE = 2; |
| 600 | |
| 601 | /** |
| 602 | * Request location update from network or gps location provider. Network provider will be |
| 603 | * used if available, otherwise use the gps provider. |
| 604 | */ |
| 605 | private static final List<String> LOCATION_PROVIDERS = Arrays.asList( |
| 606 | LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER); |
| 607 | |
| 608 | private final LocationManager mLocationManager; |
| 609 | private final Looper mLooper; |
| 610 | private final List<LocationUpdateCallback> mCallbacks; |
| 611 | private final Context mContext; |
| 612 | private Handler mLocationHandler; |
| 613 | |
| 614 | LocationRequester(Context context, LocationManager locationManager, Looper looper) { |
| 615 | mLocationManager = locationManager; |
| 616 | mLooper = looper; |
| 617 | mCallbacks = new ArrayList<>(); |
| 618 | mContext = context; |
| 619 | mLocationHandler = new LocationHandler(looper); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Request a single location update. If the location is not available, a callback with |
| 624 | * {@code null} location will be called immediately. |
| 625 | * |
| 626 | * @param callback a callback to the response when the location is available |
| 627 | * @param maximumWaitTimeSec the maximum wait time of this request. If location is not |
| 628 | * updated within the maximum wait time, {@code callback#onLocationUpadte(null)} will be |
| 629 | * called. |
| 630 | */ |
| 631 | void requestLocationUpdate(@NonNull LocationUpdateCallback callback, |
| 632 | int maximumWaitTimeSec) { |
| 633 | mLocationHandler.obtainMessage(EVENT_REQUEST_LOCATION_UPDATE, maximumWaitTimeSec, |
| 634 | 0 /* arg2 */, callback).sendToTarget(); |
| 635 | } |
| 636 | |
| 637 | private void onLocationUpdate(@Nullable LatLng location) { |
| 638 | for (LocationUpdateCallback callback : mCallbacks) { |
| 639 | callback.onLocationUpdate(location); |
| 640 | } |
| 641 | mCallbacks.clear(); |
| 642 | } |
| 643 | |
| 644 | private void requestLocationUpdateInternal(@NonNull LocationUpdateCallback callback, |
| 645 | int maximumWaitTimeSec) { |
Jordan Liu | 75ebf63 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 646 | if (DBG) Log.d(TAG, "requestLocationUpdate"); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 647 | if (!isLocationServiceAvailable()) { |
| 648 | if (DBG) { |
Jordan Liu | 75ebf63 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 649 | Log.d(TAG, "Can't request location update because of no location permission"); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 650 | } |
| 651 | callback.onLocationUpdate(null); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (!mLocationHandler.hasMessages(EVENT_LOCATION_REQUEST_TIMEOUT)) { |
| 656 | if (maximumWaitTimeSec == SmsCbMessage.MAXIMUM_WAIT_TIME_NOT_SET) { |
| 657 | maximumWaitTimeSec = DEFAULT_MAXIMUM_WAIT_TIME_SEC; |
| 658 | } |
| 659 | mLocationHandler.sendMessageDelayed( |
| 660 | mLocationHandler.obtainMessage(EVENT_LOCATION_REQUEST_TIMEOUT), |
| 661 | maximumWaitTimeSec * DateUtils.SECOND_IN_MILLIS); |
| 662 | } |
| 663 | |
| 664 | mCallbacks.add(callback); |
| 665 | |
| 666 | for (String provider : LOCATION_PROVIDERS) { |
| 667 | if (mLocationManager.isProviderEnabled(provider)) { |
| 668 | mLocationManager.requestSingleUpdate(provider, mLocationListener, mLooper); |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | private boolean isLocationServiceAvailable() { |
| 675 | if (!hasPermission(Manifest.permission.ACCESS_FINE_LOCATION) |
| 676 | && !hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)) return false; |
| 677 | for (String provider : LOCATION_PROVIDERS) { |
| 678 | if (mLocationManager.isProviderEnabled(provider)) return true; |
| 679 | } |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | private boolean hasPermission(String permission) { |
Philip P. Moltmann | 376f9fa | 2019-10-30 15:25:24 -0700 | [diff] [blame] | 684 | return mContext.checkPermission(permission, Process.myPid(), Process.myUid()) |
| 685 | == PackageManager.PERMISSION_GRANTED; |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | private final LocationListener mLocationListener = new LocationListener() { |
| 689 | @Override |
| 690 | public void onLocationChanged(Location location) { |
| 691 | mLocationHandler.removeMessages(EVENT_LOCATION_REQUEST_TIMEOUT); |
| 692 | onLocationUpdate(new LatLng(location.getLatitude(), location.getLongitude())); |
| 693 | } |
| 694 | |
| 695 | @Override |
| 696 | public void onStatusChanged(String provider, int status, Bundle extras) {} |
| 697 | |
| 698 | @Override |
| 699 | public void onProviderEnabled(String provider) {} |
| 700 | |
| 701 | @Override |
| 702 | public void onProviderDisabled(String provider) {} |
| 703 | }; |
| 704 | |
| 705 | private final class LocationHandler extends Handler { |
| 706 | LocationHandler(Looper looper) { |
| 707 | super(looper); |
| 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public void handleMessage(Message msg) { |
| 712 | switch (msg.what) { |
| 713 | case EVENT_LOCATION_REQUEST_TIMEOUT: |
Jordan Liu | 75ebf63 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 714 | if (DBG) Log.d(TAG, "location request timeout"); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 715 | onLocationUpdate(null); |
| 716 | break; |
| 717 | case EVENT_REQUEST_LOCATION_UPDATE: |
| 718 | requestLocationUpdateInternal((LocationUpdateCallback) msg.obj, msg.arg1); |
| 719 | break; |
| 720 | default: |
Jordan Liu | 75ebf63 | 2019-12-16 15:35:27 -0800 | [diff] [blame] | 721 | Log.e(TAG, "Unsupported message type " + msg.what); |
Jordan Liu | c872fad | 2019-10-11 11:42:03 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | } |