1e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly/*
24cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease * Copyright (C) 2012 The Android Open Source Project
3e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly *
4e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
5e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * you may not use this file except in compliance with the License.
6e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * You may obtain a copy of the License at
7e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly *
8e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
9e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly *
10e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * Unless required by applicable law or agreed to in writing, software
11e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
12e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * See the License for the specific language governing permissions and
14e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly * limitations under the License.
15e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly */
16e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
17e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellypackage com.android.server.location;
18e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
19e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport java.io.PrintWriter;
20e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport java.util.Iterator;
21e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport java.util.LinkedList;
22e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport java.util.List;
23e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
24e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.app.PendingIntent;
25e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.content.Context;
26e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.content.Intent;
276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.location.Geofence;
28e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.location.Location;
29e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.location.LocationListener;
30e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.location.LocationManager;
316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.location.LocationRequest;
32e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.os.Bundle;
334cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Leaseimport android.os.Handler;
344cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Leaseimport android.os.Message;
35e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.os.PowerManager;
36e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.os.SystemClock;
374cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Leaseimport android.util.Slog;
384035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly
394035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pellyimport com.android.server.LocationManagerService;
40e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
41e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellypublic class GeofenceManager implements LocationListener, PendingIntent.OnFinished {
426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private static final String TAG = "GeofenceManager";
434035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly    private static final boolean D = LocationManagerService.D;
44e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
454cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private static final int MSG_UPDATE_FENCES = 1;
464cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
47e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    /**
48e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly     * Assume a maximum land speed, as a heuristic to throttle location updates.
49e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly     * (Air travel should result in an airplane mode toggle which will
50e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly     * force a new location update anyway).
51e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly     */
526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private static final int MAX_SPEED_M_S = 100;  // 360 km/hr (high speed train)
53e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
544cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
554cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * Maximum age after which a location is no longer considered fresh enough to use.
564cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
574cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private static final long MAX_AGE_NANOS = 5 * 60 * 1000000000L; // five minutes
584cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
594cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
604cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * Most frequent update interval allowed.
614cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
624cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private static final long MIN_INTERVAL_MS = 1 * 60 * 1000; // one minute
634cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
644cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
654cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * Least frequent update interval allowed.
664cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
674cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private static final long MAX_INTERVAL_MS = 2 * 60 * 60 * 1000; // two hours
684cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final Context mContext;
706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final LocationManager mLocationManager;
716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final PowerManager.WakeLock mWakeLock;
724cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private final GeofenceHandler mHandler;
734035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly    private final LocationBlacklist mBlacklist;
74e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private Object mLock = new Object();
766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    // access to members below is synchronized on mLock
784cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
794cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * A list containing all registered geofences.
804cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private List<GeofenceState> mFences = new LinkedList<GeofenceState>();
82e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
834cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
844cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * This is set true when we have an active request for {@link Location} updates via
854cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * {@link LocationManager#requestLocationUpdates(LocationRequest, LocationListener,
864cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * android.os.Looper).
874cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
884cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private boolean mReceivingLocationUpdates;
894cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
904cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
914cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * The update interval component of the current active {@link Location} update request.
924cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
934cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private long mLocationUpdateInterval;
944cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
954cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
964cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * The {@link Location} most recently received via {@link #onLocationChanged(Location)}.
974cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
984cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private Location mLastLocationUpdate;
994cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
1004cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
1014cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * This is set true when a {@link Location} is received via
1024cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * {@link #onLocationChanged(Location)} or {@link #scheduleUpdateFencesLocked()}, and cleared
1034cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * when that Location has been processed via {@link #updateFences()}
1044cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
1054cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private boolean mPendingUpdate;
1064cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
1074035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly    public GeofenceManager(Context context, LocationBlacklist blacklist) {
108e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        mContext = context;
109e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
110e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
111e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
1124cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        mHandler = new GeofenceHandler();
1134035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly        mBlacklist = blacklist;
1146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1164cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    public void addFence(LocationRequest request, Geofence geofence, PendingIntent intent,
1174cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            int uid, String packageName) {
1184cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (D) {
1194cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            Slog.d(TAG, "addFence: request=" + request + ", geofence=" + geofence
1204cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    + ", intent=" + intent + ", uid=" + uid + ", packageName=" + packageName);
1214cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
1226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1234cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        GeofenceState state = new GeofenceState(geofence,
1244cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                request.getExpireAt(), packageName, intent);
1256fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        synchronized (mLock) {
1266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            // first make sure it doesn't already exist
1276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            for (int i = mFences.size() - 1; i >= 0; i--) {
1286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                GeofenceState w = mFences.get(i);
1296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                if (geofence.equals(w.mFence) && intent.equals(w.mIntent)) {
1306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    // already exists, remove the old one
1316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    mFences.remove(i);
1326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    break;
1336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                }
1346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            }
1356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mFences.add(state);
1364cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            scheduleUpdateFencesLocked();
137e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
138e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
139e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
1406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void removeFence(Geofence fence, PendingIntent intent) {
1414cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (D) {
1424cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            Slog.d(TAG, "removeFence: fence=" + fence + ", intent=" + intent);
1434cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
1444cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
1456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        synchronized (mLock) {
1466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Iterator<GeofenceState> iter = mFences.iterator();
147e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            while (iter.hasNext()) {
1486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                GeofenceState state = iter.next();
1496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                if (state.mIntent.equals(intent)) {
1506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    if (fence == null) {
1524cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        // always remove
1536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                        iter.remove();
1546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    } else {
1556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                        // just remove matching fences
1566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                        if (fence.equals(state.mFence)) {
1576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                            iter.remove();
1586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                        }
1596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    }
160e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly                }
161e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            }
1624cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            scheduleUpdateFencesLocked();
163e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
164e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
165e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
166e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public void removeFence(String packageName) {
1674cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (D) {
1684cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            Slog.d(TAG, "removeFence: packageName=" + packageName);
1694cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
1704cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
1716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        synchronized (mLock) {
1726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Iterator<GeofenceState> iter = mFences.iterator();
173e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            while (iter.hasNext()) {
1746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                GeofenceState state = iter.next();
1756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                if (state.mPackageName.equals(packageName)) {
176e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly                    iter.remove();
177e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly                }
178e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            }
1794cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            scheduleUpdateFencesLocked();
180e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
181e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
182e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
1836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private void removeExpiredFencesLocked() {
1846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        long time = SystemClock.elapsedRealtime();
1856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        Iterator<GeofenceState> iter = mFences.iterator();
1866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        while (iter.hasNext()) {
1876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            GeofenceState state = iter.next();
1886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            if (state.mExpireAt < time) {
1896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                iter.remove();
190e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            }
191e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
192e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
193e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
1944cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private void scheduleUpdateFencesLocked() {
1954cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (!mPendingUpdate) {
1964cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            mPendingUpdate = true;
1974cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            mHandler.sendEmptyMessage(MSG_UPDATE_FENCES);
1984cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
1994cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    }
2004cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2014cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
2024cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * Returns the location received most recently from {@link #onLocationChanged(Location)},
2034cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * or consult {@link LocationManager#getLastLocation()} if none has arrived. Does not return
2044cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * either if the location would be too stale to be useful.
2054cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     *
2064cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * @return a fresh, valid Location, or null if none is available
2074cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
2084cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private Location getFreshLocationLocked() {
2094cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        // Prefer mLastLocationUpdate to LocationManager.getLastLocation().
2104cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        Location location = mReceivingLocationUpdates ? mLastLocationUpdate : null;
2114cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (location == null && !mFences.isEmpty()) {
2124cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            location = mLocationManager.getLastLocation();
2134cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
2144cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2154cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        // Early out for null location.
2164cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (location == null) {
2174cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            return null;
2184cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
2194cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2204cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        // Early out for stale location.
2214cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        long now = SystemClock.elapsedRealtimeNanos();
2224cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (now - location.getElapsedRealtimeNanos() > MAX_AGE_NANOS) {
2234cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            return null;
2244cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
2254cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2264cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        // Made it this far? Return our fresh, valid location.
2274cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        return location;
2284cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    }
2294cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2304cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    /**
2314cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * The geofence update loop. This function removes expired fences, then tests the most
2324cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * recently-received {@link Location} against each registered {@link GeofenceState}, sending
2334cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * {@link Intent}s for geofences that have been tripped. It also adjusts the active location
2344cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     * update request with {@link LocationManager} as appropriate for any active geofences.
2354cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease     */
2364cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    // Runs on the handler.
2374cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private void updateFences() {
238e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        List<PendingIntent> enterIntents = new LinkedList<PendingIntent>();
239e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        List<PendingIntent> exitIntents = new LinkedList<PendingIntent>();
240e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
2416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        synchronized (mLock) {
2424cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            mPendingUpdate = false;
2434cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2444cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // Remove expired fences.
2456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            removeExpiredFencesLocked();
246e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
2474cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // Get a location to work with, either received via onLocationChanged() or
2484cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // via LocationManager.getLastLocation().
2494cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            Location location = getFreshLocationLocked();
2504cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2514cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // Update all fences.
2524cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // Keep track of the distance to the nearest fence.
2534cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            double minFenceDistance = Double.MAX_VALUE;
2544cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            boolean needUpdates = false;
2556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            for (GeofenceState state : mFences) {
2564035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly                if (mBlacklist.isBlacklisted(state.mPackageName)) {
2574cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    if (D) {
2584cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        Slog.d(TAG, "skipping geofence processing for blacklisted app: "
2594cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                                + state.mPackageName);
2604cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    }
2614035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly                    continue;
2624035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly                }
2634035f5a7c191a68bc9a5912ce44c43c82e9e5dbfNick Pelly
2644cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                needUpdates = true;
2654cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                if (location != null) {
2664cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    int event = state.processLocation(location);
2674cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    if ((event & GeofenceState.FLAG_ENTER) != 0) {
2684cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        enterIntents.add(state.mIntent);
2694cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    }
2704cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    if ((event & GeofenceState.FLAG_EXIT) != 0) {
2714cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        exitIntents.add(state.mIntent);
2724cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    }
2734cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2744cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    // FIXME: Ideally this code should take into account the accuracy of the
2754cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    // location fix that was used to calculate the distance in the first place.
2764cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    double fenceDistance = state.getDistanceToBoundary(); // MAX_VALUE if unknown
2774cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    if (fenceDistance < minFenceDistance) {
2784cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        minFenceDistance = fenceDistance;
2794cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    }
2804cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                }
2814cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            }
2824cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2834cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // Request or cancel location updates if needed.
2844cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            if (needUpdates) {
2854cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                // Request location updates.
2864cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                // Compute a location update interval based on the distance to the nearest fence.
2874cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                long intervalMs;
2884cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                if (location != null && Double.compare(minFenceDistance, Double.MAX_VALUE) != 0) {
2894cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    intervalMs = (long)Math.min(MAX_INTERVAL_MS, Math.max(MIN_INTERVAL_MS,
2904cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                            minFenceDistance * 1000 / MAX_SPEED_M_S));
2914cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                } else {
2924cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    intervalMs = MIN_INTERVAL_MS;
293e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly                }
2944cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                if (!mReceivingLocationUpdates || mLocationUpdateInterval != intervalMs) {
2954cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mReceivingLocationUpdates = true;
2964cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mLocationUpdateInterval = intervalMs;
2974cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mLastLocationUpdate = location;
2984cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
2994cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    LocationRequest request = new LocationRequest();
3004cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    request.setInterval(intervalMs).setFastestInterval(0);
3014cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mLocationManager.requestLocationUpdates(request, this, mHandler.getLooper());
302e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly                }
3034cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            } else {
3044cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                // Cancel location updates.
3054cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                if (mReceivingLocationUpdates) {
3064cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mReceivingLocationUpdates = false;
3074cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mLocationUpdateInterval = 0;
3084cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mLastLocationUpdate = null;
3094cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
3104cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    mLocationManager.removeUpdates(this);
3114cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                }
3124cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            }
3134cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
3144cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            if (D) {
3154cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                Slog.d(TAG, "updateFences: location=" + location
3164cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        + ", mFences.size()=" + mFences.size()
3174cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        + ", mReceivingLocationUpdates=" + mReceivingLocationUpdates
3184cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        + ", mLocationUpdateInterval=" + mLocationUpdateInterval
3194cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                        + ", mLastLocationUpdate=" + mLastLocationUpdate);
320e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            }
321e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
322e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
323e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        // release lock before sending intents
324e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        for (PendingIntent intent : exitIntents) {
325e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            sendIntentExit(intent);
326e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
327e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        for (PendingIntent intent : enterIntents) {
328e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            sendIntentEnter(intent);
329e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
330e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
331e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
3326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private void sendIntentEnter(PendingIntent pendingIntent) {
3334cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (D) {
3344cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            Slog.d(TAG, "sendIntentEnter: pendingIntent=" + pendingIntent);
3354cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
3364cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
337e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        Intent intent = new Intent();
338e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        intent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
339e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        sendIntent(pendingIntent, intent);
340e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
341e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
3426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private void sendIntentExit(PendingIntent pendingIntent) {
3434cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        if (D) {
3444cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            Slog.d(TAG, "sendIntentExit: pendingIntent=" + pendingIntent);
3454cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
3464cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
347e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        Intent intent = new Intent();
348e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        intent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
349e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        sendIntent(pendingIntent, intent);
350e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
351e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
3526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private void sendIntent(PendingIntent pendingIntent, Intent intent) {
3534cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        mWakeLock.acquire();
354e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        try {
3554cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            pendingIntent.send(mContext, 0, intent, this, null,
3564cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    android.Manifest.permission.ACCESS_FINE_LOCATION);
357e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        } catch (PendingIntent.CanceledException e) {
3586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            removeFence(null, pendingIntent);
359e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            mWakeLock.release();
360e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
3614cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        // ...otherwise, mWakeLock.release() gets called by onSendFinished()
362e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
363e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
3644cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    // Runs on the handler (which was passed into LocationManager.requestLocationUpdates())
3654cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    @Override
3664cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    public void onLocationChanged(Location location) {
3674cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        synchronized (mLock) {
3684cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            if (mReceivingLocationUpdates) {
3694cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                mLastLocationUpdate = location;
370e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            }
371e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
3724cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // Update the fences immediately before returning in
3734cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            // case the caller is holding a wakelock.
3744cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            if (mPendingUpdate) {
3754cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                mHandler.removeMessages(MSG_UPDATE_FENCES);
3764cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            } else {
3774cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                mPendingUpdate = true;
3784cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            }
379e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
3804cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        updateFences();
381e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
382e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
383e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    @Override
384e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public void onStatusChanged(String provider, int status, Bundle extras) { }
385e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
386e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    @Override
387e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public void onProviderEnabled(String provider) { }
388e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
389e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    @Override
390e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public void onProviderDisabled(String provider) { }
391e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
392e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    @Override
393e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode,
394e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            String resultData, Bundle resultExtras) {
395e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        mWakeLock.release();
396e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
397e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
398e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public void dump(PrintWriter pw) {
399e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        pw.println("  Geofences:");
4006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
4016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        for (GeofenceState state : mFences) {
402e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            pw.append("    ");
4036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            pw.append(state.mPackageName);
404e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            pw.append(" ");
4056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            pw.append(state.mFence.toString());
406e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly            pw.append("\n");
407e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        }
408e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    }
4094cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
4104cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    private final class GeofenceHandler extends Handler {
4114cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        public GeofenceHandler() {
4124cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            super(true /*async*/);
4134cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
4144cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease
4154cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        @Override
4164cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        public void handleMessage(Message msg) {
4174cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            switch (msg.what) {
4184cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                case MSG_UPDATE_FENCES: {
4194cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    updateFences();
4204cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                    break;
4214cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease                }
4224cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease            }
4234cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease        }
4244cd0a50b26eeb68517d03bc0cafc18e98bfc1fecVictoria Lease    }
425e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly}
426