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