Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1 | /* |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 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.server.location; |
| 18 | |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 22 | import android.app.AppOpsManager; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 23 | import android.app.PendingIntent; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 26 | import android.location.Geofence; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 27 | import android.location.Location; |
| 28 | import android.location.LocationListener; |
| 29 | import android.location.LocationManager; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 30 | import android.location.LocationRequest; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 33 | import android.os.Looper; |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 34 | import android.os.Message; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 35 | import android.os.PowerManager; |
| 36 | import android.os.SystemClock; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 38 | import android.util.Slog; |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 39 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 40 | import com.android.server.FgThread; |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 41 | import com.android.server.LocationManagerService; |
Lifu Tang | 519f0d0 | 2018-04-12 16:39:39 -0700 | [diff] [blame] | 42 | import com.android.server.PendingIntentUtils; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 43 | |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 44 | import java.io.PrintWriter; |
| 45 | import java.util.Iterator; |
| 46 | import java.util.LinkedList; |
| 47 | import java.util.List; |
| 48 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 49 | public class GeofenceManager implements LocationListener, PendingIntent.OnFinished { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 50 | private static final String TAG = "GeofenceManager"; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 51 | private static final boolean D = Log.isLoggable(TAG, Log.DEBUG); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 52 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 53 | private static final int MSG_UPDATE_FENCES = 1; |
| 54 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Assume a maximum land speed, as a heuristic to throttle location updates. |
| 57 | * (Air travel should result in an airplane mode toggle which will |
| 58 | * force a new location update anyway). |
| 59 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 60 | private static final int MAX_SPEED_M_S = 100; // 360 km/hr (high speed train) |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 61 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Maximum age after which a location is no longer considered fresh enough to use. |
| 64 | */ |
| 65 | private static final long MAX_AGE_NANOS = 5 * 60 * 1000000000L; // five minutes |
| 66 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Least frequent update interval allowed. |
| 70 | */ |
| 71 | private static final long MAX_INTERVAL_MS = 2 * 60 * 60 * 1000; // two hours |
| 72 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 73 | private final Context mContext; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 74 | private final GeofenceHandler mHandler; |
| 75 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 76 | private final LocationManager mLocationManager; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 77 | private final AppOpsManager mAppOps; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 78 | private final PowerManager.WakeLock mWakeLock; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 79 | |
| 80 | private final LocationSettingsStore mSettingsStore; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 81 | |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 82 | private final Object mLock = new Object(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 83 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 84 | // access to members below is synchronized on mLock |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 85 | /** |
| 86 | * A list containing all registered geofences. |
| 87 | */ |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 88 | private List<GeofenceState> mFences = new LinkedList<>(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 89 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 90 | /** |
| 91 | * This is set true when we have an active request for {@link Location} updates via |
| 92 | * {@link LocationManager#requestLocationUpdates(LocationRequest, LocationListener, |
| 93 | * android.os.Looper). |
| 94 | */ |
| 95 | private boolean mReceivingLocationUpdates; |
| 96 | |
| 97 | /** |
| 98 | * The update interval component of the current active {@link Location} update request. |
| 99 | */ |
| 100 | private long mLocationUpdateInterval; |
| 101 | |
| 102 | /** |
| 103 | * The {@link Location} most recently received via {@link #onLocationChanged(Location)}. |
| 104 | */ |
| 105 | private Location mLastLocationUpdate; |
| 106 | |
| 107 | /** |
| 108 | * This is set true when a {@link Location} is received via |
| 109 | * {@link #onLocationChanged(Location)} or {@link #scheduleUpdateFencesLocked()}, and cleared |
| 110 | * when that Location has been processed via {@link #updateFences()} |
| 111 | */ |
| 112 | private boolean mPendingUpdate; |
| 113 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 114 | public GeofenceManager(Context context, LocationSettingsStore settingsStore) { |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 115 | mContext = context; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 116 | mHandler = new GeofenceHandler(FgThread.getHandler().getLooper()); |
Lifu Tang | c94ef4d | 2017-03-23 23:48:00 -0700 | [diff] [blame] | 117 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 118 | mLocationManager = mContext.getSystemService(LocationManager.class); |
| 119 | mAppOps = mContext.getSystemService(AppOpsManager.class); |
| 120 | |
| 121 | mWakeLock = mContext.getSystemService(PowerManager.class).newWakeLock( |
| 122 | PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 123 | |
| 124 | mSettingsStore = settingsStore; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 127 | public void addFence(LocationRequest request, Geofence geofence, PendingIntent intent, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 128 | int allowedResolutionLevel, int uid, String packageName, @Nullable String featureId, |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 129 | @NonNull String listenerIdentifier) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 130 | if (D) { |
| 131 | Slog.d(TAG, "addFence: request=" + request + ", geofence=" + geofence |
| 132 | + ", intent=" + intent + ", uid=" + uid + ", packageName=" + packageName); |
| 133 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 134 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 135 | GeofenceState state = new GeofenceState(geofence, |
Soonil Nagarkar | 95768ce | 2019-11-05 15:22:44 -0800 | [diff] [blame] | 136 | request.getExpirationRealtimeMs(SystemClock.elapsedRealtime()), |
| 137 | allowedResolutionLevel, uid, packageName, featureId, listenerIdentifier, intent); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 138 | synchronized (mLock) { |
| 139 | // first make sure it doesn't already exist |
| 140 | for (int i = mFences.size() - 1; i >= 0; i--) { |
| 141 | GeofenceState w = mFences.get(i); |
| 142 | if (geofence.equals(w.mFence) && intent.equals(w.mIntent)) { |
| 143 | // already exists, remove the old one |
| 144 | mFences.remove(i); |
| 145 | break; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 148 | mFences.add(state); |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 149 | scheduleUpdateFencesLocked(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | public void removeFence(Geofence fence, PendingIntent intent) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 154 | if (D) { |
| 155 | Slog.d(TAG, "removeFence: fence=" + fence + ", intent=" + intent); |
| 156 | } |
| 157 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 158 | synchronized (mLock) { |
| 159 | Iterator<GeofenceState> iter = mFences.iterator(); |
| 160 | while (iter.hasNext()) { |
| 161 | GeofenceState state = iter.next(); |
| 162 | if (state.mIntent.equals(intent)) { |
| 163 | |
| 164 | if (fence == null) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 165 | // always remove |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 166 | iter.remove(); |
| 167 | } else { |
| 168 | // just remove matching fences |
| 169 | if (fence.equals(state.mFence)) { |
| 170 | iter.remove(); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 175 | scheduleUpdateFencesLocked(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | public void removeFence(String packageName) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 180 | if (D) { |
| 181 | Slog.d(TAG, "removeFence: packageName=" + packageName); |
| 182 | } |
| 183 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 184 | synchronized (mLock) { |
| 185 | Iterator<GeofenceState> iter = mFences.iterator(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 186 | while (iter.hasNext()) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 187 | GeofenceState state = iter.next(); |
| 188 | if (state.mPackageName.equals(packageName)) { |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 189 | iter.remove(); |
| 190 | } |
| 191 | } |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 192 | scheduleUpdateFencesLocked(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 196 | private void removeExpiredFencesLocked() { |
| 197 | long time = SystemClock.elapsedRealtime(); |
| 198 | Iterator<GeofenceState> iter = mFences.iterator(); |
| 199 | while (iter.hasNext()) { |
| 200 | GeofenceState state = iter.next(); |
| 201 | if (state.mExpireAt < time) { |
| 202 | iter.remove(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 207 | private void scheduleUpdateFencesLocked() { |
| 208 | if (!mPendingUpdate) { |
| 209 | mPendingUpdate = true; |
| 210 | mHandler.sendEmptyMessage(MSG_UPDATE_FENCES); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns the location received most recently from {@link #onLocationChanged(Location)}, |
| 216 | * or consult {@link LocationManager#getLastLocation()} if none has arrived. Does not return |
| 217 | * either if the location would be too stale to be useful. |
| 218 | * |
| 219 | * @return a fresh, valid Location, or null if none is available |
| 220 | */ |
| 221 | private Location getFreshLocationLocked() { |
| 222 | // Prefer mLastLocationUpdate to LocationManager.getLastLocation(). |
| 223 | Location location = mReceivingLocationUpdates ? mLastLocationUpdate : null; |
| 224 | if (location == null && !mFences.isEmpty()) { |
| 225 | location = mLocationManager.getLastLocation(); |
| 226 | } |
| 227 | |
| 228 | // Early out for null location. |
| 229 | if (location == null) { |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | // Early out for stale location. |
| 234 | long now = SystemClock.elapsedRealtimeNanos(); |
| 235 | if (now - location.getElapsedRealtimeNanos() > MAX_AGE_NANOS) { |
| 236 | return null; |
| 237 | } |
| 238 | |
| 239 | // Made it this far? Return our fresh, valid location. |
| 240 | return location; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * The geofence update loop. This function removes expired fences, then tests the most |
| 245 | * recently-received {@link Location} against each registered {@link GeofenceState}, sending |
| 246 | * {@link Intent}s for geofences that have been tripped. It also adjusts the active location |
| 247 | * update request with {@link LocationManager} as appropriate for any active geofences. |
| 248 | */ |
| 249 | // Runs on the handler. |
| 250 | private void updateFences() { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 251 | List<PendingIntent> enterIntents = new LinkedList<>(); |
| 252 | List<PendingIntent> exitIntents = new LinkedList<>(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 253 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 254 | synchronized (mLock) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 255 | mPendingUpdate = false; |
| 256 | |
| 257 | // Remove expired fences. |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 258 | removeExpiredFencesLocked(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 259 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 260 | // Get a location to work with, either received via onLocationChanged() or |
| 261 | // via LocationManager.getLastLocation(). |
| 262 | Location location = getFreshLocationLocked(); |
| 263 | |
| 264 | // Update all fences. |
| 265 | // Keep track of the distance to the nearest fence. |
| 266 | double minFenceDistance = Double.MAX_VALUE; |
| 267 | boolean needUpdates = false; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 268 | for (GeofenceState state : mFences) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 269 | if (mSettingsStore.isLocationPackageBlacklisted(ActivityManager.getCurrentUser(), |
| 270 | state.mPackageName)) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 271 | if (D) { |
| 272 | Slog.d(TAG, "skipping geofence processing for blacklisted app: " |
| 273 | + state.mPackageName); |
| 274 | } |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 275 | continue; |
| 276 | } |
| 277 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 278 | int op = LocationManagerService.resolutionLevelToOp(state.mAllowedResolutionLevel); |
| 279 | if (op >= 0) { |
| 280 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_FINE_LOCATION, state.mUid, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 281 | state.mPackageName, state.mFeatureId, state.mListenerIdentifier) |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 282 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 283 | if (D) { |
| 284 | Slog.d(TAG, "skipping geofence processing for no op app: " |
| 285 | + state.mPackageName); |
| 286 | } |
| 287 | continue; |
| 288 | } |
| 289 | } |
| 290 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 291 | needUpdates = true; |
| 292 | if (location != null) { |
| 293 | int event = state.processLocation(location); |
| 294 | if ((event & GeofenceState.FLAG_ENTER) != 0) { |
| 295 | enterIntents.add(state.mIntent); |
| 296 | } |
| 297 | if ((event & GeofenceState.FLAG_EXIT) != 0) { |
| 298 | exitIntents.add(state.mIntent); |
| 299 | } |
| 300 | |
| 301 | // FIXME: Ideally this code should take into account the accuracy of the |
| 302 | // location fix that was used to calculate the distance in the first place. |
| 303 | double fenceDistance = state.getDistanceToBoundary(); // MAX_VALUE if unknown |
| 304 | if (fenceDistance < minFenceDistance) { |
| 305 | minFenceDistance = fenceDistance; |
| 306 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 309 | |
| 310 | // Request or cancel location updates if needed. |
| 311 | if (needUpdates) { |
| 312 | // Request location updates. |
| 313 | // Compute a location update interval based on the distance to the nearest fence. |
| 314 | long intervalMs; |
| 315 | if (location != null && Double.compare(minFenceDistance, Double.MAX_VALUE) != 0) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 316 | intervalMs = (long) Math.min(MAX_INTERVAL_MS, Math.max( |
| 317 | mSettingsStore.getBackgroundThrottleProximityAlertIntervalMs(), |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 318 | minFenceDistance * 1000 / MAX_SPEED_M_S)); |
| 319 | } else { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 320 | intervalMs = mSettingsStore.getBackgroundThrottleProximityAlertIntervalMs(); |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 321 | } |
| 322 | if (!mReceivingLocationUpdates || mLocationUpdateInterval != intervalMs) { |
| 323 | mReceivingLocationUpdates = true; |
| 324 | mLocationUpdateInterval = intervalMs; |
| 325 | mLastLocationUpdate = location; |
| 326 | |
| 327 | LocationRequest request = new LocationRequest(); |
| 328 | request.setInterval(intervalMs).setFastestInterval(0); |
| 329 | mLocationManager.requestLocationUpdates(request, this, mHandler.getLooper()); |
| 330 | } |
| 331 | } else { |
| 332 | // Cancel location updates. |
| 333 | if (mReceivingLocationUpdates) { |
| 334 | mReceivingLocationUpdates = false; |
| 335 | mLocationUpdateInterval = 0; |
| 336 | mLastLocationUpdate = null; |
| 337 | |
| 338 | mLocationManager.removeUpdates(this); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (D) { |
| 343 | Slog.d(TAG, "updateFences: location=" + location |
| 344 | + ", mFences.size()=" + mFences.size() |
| 345 | + ", mReceivingLocationUpdates=" + mReceivingLocationUpdates |
| 346 | + ", mLocationUpdateInterval=" + mLocationUpdateInterval |
| 347 | + ", mLastLocationUpdate=" + mLastLocationUpdate); |
| 348 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // release lock before sending intents |
| 352 | for (PendingIntent intent : exitIntents) { |
| 353 | sendIntentExit(intent); |
| 354 | } |
| 355 | for (PendingIntent intent : enterIntents) { |
| 356 | sendIntentEnter(intent); |
| 357 | } |
| 358 | } |
| 359 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 360 | private void sendIntentEnter(PendingIntent pendingIntent) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 361 | if (D) { |
| 362 | Slog.d(TAG, "sendIntentEnter: pendingIntent=" + pendingIntent); |
| 363 | } |
| 364 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 365 | Intent intent = new Intent(); |
| 366 | intent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true); |
| 367 | sendIntent(pendingIntent, intent); |
| 368 | } |
| 369 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 370 | private void sendIntentExit(PendingIntent pendingIntent) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 371 | if (D) { |
| 372 | Slog.d(TAG, "sendIntentExit: pendingIntent=" + pendingIntent); |
| 373 | } |
| 374 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 375 | Intent intent = new Intent(); |
| 376 | intent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false); |
| 377 | sendIntent(pendingIntent, intent); |
| 378 | } |
| 379 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 380 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 381 | mWakeLock.acquire(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 382 | try { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 383 | pendingIntent.send(mContext, 0, intent, this, null, |
Lifu Tang | 519f0d0 | 2018-04-12 16:39:39 -0700 | [diff] [blame] | 384 | android.Manifest.permission.ACCESS_FINE_LOCATION, |
| 385 | PendingIntentUtils.createDontSendToRestrictedAppsBundle(null)); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 386 | } catch (PendingIntent.CanceledException e) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 387 | removeFence(null, pendingIntent); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 388 | mWakeLock.release(); |
| 389 | } |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 390 | // ...otherwise, mWakeLock.release() gets called by onSendFinished() |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 393 | // Runs on the handler (which was passed into LocationManager.requestLocationUpdates()) |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 394 | @Override |
| 395 | public void onLocationChanged(Location location) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 396 | synchronized (mLock) { |
| 397 | if (mReceivingLocationUpdates) { |
| 398 | mLastLocationUpdate = location; |
| 399 | } |
| 400 | |
| 401 | // Update the fences immediately before returning in |
| 402 | // case the caller is holding a wakelock. |
| 403 | if (mPendingUpdate) { |
| 404 | mHandler.removeMessages(MSG_UPDATE_FENCES); |
| 405 | } else { |
| 406 | mPendingUpdate = true; |
| 407 | } |
| 408 | } |
| 409 | updateFences(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 413 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 414 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 415 | |
| 416 | @Override |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 417 | public void onProviderEnabled(String provider) { |
| 418 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 419 | |
| 420 | @Override |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 421 | public void onProviderDisabled(String provider) { |
| 422 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 423 | |
| 424 | @Override |
| 425 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 426 | String resultData, Bundle resultExtras) { |
| 427 | mWakeLock.release(); |
| 428 | } |
| 429 | |
| 430 | public void dump(PrintWriter pw) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 431 | for (GeofenceState state : mFences) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 432 | pw.println(state.mPackageName + " " + state.mFence); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 435 | |
| 436 | private final class GeofenceHandler extends Handler { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 437 | private GeofenceHandler(Looper looper) { |
| 438 | super(looper); |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public void handleMessage(Message msg) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 443 | if (msg.what == MSG_UPDATE_FENCES) { |
| 444 | updateFences(); |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 448 | } |