blob: fab97b1e1574039d45ccc4ed59d4509e2c033288 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package com.android.server;
18
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileDescriptor;
22import java.io.FileReader;
23import java.io.FileWriter;
24import java.io.IOException;
25import java.io.PrintWriter;
26import java.util.ArrayList;
27import java.util.HashMap;
28import java.util.HashSet;
29import java.util.List;
30import java.util.Map;
Mike Lockwood9637d472009-04-02 21:41:57 -070031import java.util.Observable;
32import java.util.Observer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import java.util.Set;
34import java.util.regex.Pattern;
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.app.PendingIntent;
37import android.content.BroadcastReceiver;
Mike Lockwood9637d472009-04-02 21:41:57 -070038import android.content.ContentQueryMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.ContentResolver;
40import android.content.Context;
41import android.content.Intent;
42import android.content.IntentFilter;
43import android.content.pm.PackageManager;
Mike Lockwood9637d472009-04-02 21:41:57 -070044import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.location.Address;
Mike Lockwooda55c3212009-04-15 11:10:11 -040046import android.location.IGeocodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040048import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.location.ILocationListener;
50import android.location.ILocationManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070051import android.location.ILocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.location.Location;
53import android.location.LocationManager;
54import android.location.LocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.net.ConnectivityManager;
56import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Binder;
58import android.os.Bundle;
59import android.os.Handler;
60import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070061import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Message;
63import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070064import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.RemoteException;
66import android.os.SystemClock;
67import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.Log;
69import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import com.android.internal.location.GpsLocationProvider;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070072import com.android.internal.location.LocationProviderProxy;
Mike Lockwood7ec434e2009-03-27 07:46:48 -070073import com.android.internal.location.MockProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75/**
76 * The service class that manages LocationProviders and issues location
77 * updates and alerts.
78 *
79 * {@hide}
80 */
Mike Lockwood3d12b512009-04-21 23:25:35 -070081public class LocationManagerService extends ILocationManager.Stub implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private static final String TAG = "LocationManagerService";
The Android Open Source Project10592532009-03-18 17:39:46 -070083 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 // Minimum time interval between last known location writes, in milliseconds.
86 private static final long MIN_LAST_KNOWN_LOCATION_TIME = 60L * 1000L;
87
88 // Max time to hold wake lock for, in milliseconds.
89 private static final long MAX_TIME_FOR_WAKE_LOCK = 60 * 1000L;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 // The last time a location was written, by provider name.
92 private HashMap<String,Long> mLastWriteTime = new HashMap<String,Long>();
93
94 private static final Pattern PATTERN_COMMA = Pattern.compile(",");
95
96 private static final String ACCESS_FINE_LOCATION =
97 android.Manifest.permission.ACCESS_FINE_LOCATION;
98 private static final String ACCESS_COARSE_LOCATION =
99 android.Manifest.permission.ACCESS_COARSE_LOCATION;
100 private static final String ACCESS_MOCK_LOCATION =
101 android.Manifest.permission.ACCESS_MOCK_LOCATION;
102 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
103 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400104 private static final String INSTALL_LOCATION_PROVIDER =
105 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 // Set of providers that are explicitly enabled
108 private final Set<String> mEnabledProviders = new HashSet<String>();
109
110 // Set of providers that are explicitly disabled
111 private final Set<String> mDisabledProviders = new HashSet<String>();
112
113 // Locations, status values, and extras for mock providers
Mike Lockwood7ec434e2009-03-27 07:46:48 -0700114 private final HashMap<String,MockProvider> mMockProviders = new HashMap<String,MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 private static boolean sProvidersLoaded = false;
117
118 private final Context mContext;
Mike Lockwooda55c3212009-04-15 11:10:11 -0400119 private IGeocodeProvider mGeocodeProvider;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400120 private IGpsStatusProvider mGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 private LocationWorkerHandler mLocationHandler;
122
123 // Handler messages
Mike Lockwood4e50b782009-04-03 08:24:43 -0700124 private static final int MESSAGE_LOCATION_CHANGED = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400126 // wakelock variables
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 private final static String WAKELOCK_KEY = "LocationManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 private PowerManager.WakeLock mWakeLock = null;
Mike Lockwood48f17512009-04-23 09:12:08 -0700129 private int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400132 * List of all receivers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400134 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400136
137 /**
138 * List of location providers.
139 */
140 private final ArrayList<LocationProviderProxy> mProviders =
141 new ArrayList<LocationProviderProxy>();
142 private final HashMap<String, LocationProviderProxy> mProvidersByName
143 = new HashMap<String, LocationProviderProxy>();
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400146 * Object used internally for synchronization
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400148 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
150 /**
151 * Mapping from provider name to all its UpdateRecords
152 */
153 private final HashMap<String,ArrayList<UpdateRecord>> mRecordsByProvider =
154 new HashMap<String,ArrayList<UpdateRecord>>();
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 // Proximity listeners
Mike Lockwood48f17512009-04-23 09:12:08 -0700157 private Receiver mProximityReceiver = null;
158 private ILocationListener mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private HashMap<PendingIntent,ProximityAlert> mProximityAlerts =
160 new HashMap<PendingIntent,ProximityAlert>();
161 private HashSet<ProximityAlert> mProximitiesEntered =
162 new HashSet<ProximityAlert>();
163
164 // Last known location for each provider
165 private HashMap<String,Location> mLastKnownLocation =
166 new HashMap<String,Location>();
167
The Android Open Source Project4df24232009-03-05 14:34:35 -0800168 private int mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800169
Mike Lockwood9637d472009-04-02 21:41:57 -0700170 // for Settings change notification
171 private ContentQueryMap mSettings;
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /**
174 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
175 * location updates.
176 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700177 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final ILocationListener mListener;
179 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Object mKey;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400181 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Mike Lockwood48f17512009-04-23 09:12:08 -0700182 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400184 Receiver(ILocationListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 mListener = listener;
186 mPendingIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 mKey = listener.asBinder();
188 }
189
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400190 Receiver(PendingIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 mPendingIntent = intent;
192 mListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 mKey = intent;
194 }
195
196 @Override
197 public boolean equals(Object otherObj) {
198 if (otherObj instanceof Receiver) {
199 return mKey.equals(
200 ((Receiver)otherObj).mKey);
201 }
202 return false;
203 }
204
205 @Override
206 public int hashCode() {
207 return mKey.hashCode();
208 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 @Override
211 public String toString() {
212 if (mListener != null) {
213 return "Receiver{"
214 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400215 + " Listener " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 } else {
217 return "Receiver{"
218 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400219 + " Intent " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221 }
222
223 public boolean isListener() {
224 return mListener != null;
225 }
226
227 public boolean isPendingIntent() {
228 return mPendingIntent != null;
229 }
230
231 public ILocationListener getListener() {
232 if (mListener != null) {
233 return mListener;
234 }
235 throw new IllegalStateException("Request for non-existent listener");
236 }
237
238 public PendingIntent getPendingIntent() {
239 if (mPendingIntent != null) {
240 return mPendingIntent;
241 }
242 throw new IllegalStateException("Request for non-existent intent");
243 }
244
245 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
246 if (mListener != null) {
247 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700248 synchronized (this) {
249 // synchronize to ensure incrementPendingBroadcastsLocked()
250 // is called before decrementPendingBroadcasts()
251 mListener.onStatusChanged(provider, status, extras);
252 if (mListener != mProximityListener) {
253 // call this after broadcasting so we do not increment
254 // if we throw an exeption.
255 incrementPendingBroadcastsLocked();
256 }
257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 } catch (RemoteException e) {
259 return false;
260 }
261 } else {
262 Intent statusChanged = new Intent();
263 statusChanged.putExtras(extras);
264 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
265 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700266 synchronized (this) {
267 // synchronize to ensure incrementPendingBroadcastsLocked()
268 // is called before decrementPendingBroadcasts()
269 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler);
270 // call this after broadcasting so we do not increment
271 // if we throw an exeption.
272 incrementPendingBroadcastsLocked();
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 } catch (PendingIntent.CanceledException e) {
275 return false;
276 }
277 }
278 return true;
279 }
280
281 public boolean callLocationChangedLocked(Location location) {
282 if (mListener != null) {
283 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700284 synchronized (this) {
285 // synchronize to ensure incrementPendingBroadcastsLocked()
286 // is called before decrementPendingBroadcasts()
287 mListener.onLocationChanged(location);
288 if (mListener != mProximityListener) {
289 // call this after broadcasting so we do not increment
290 // if we throw an exeption.
291 incrementPendingBroadcastsLocked();
292 }
293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 } catch (RemoteException e) {
295 return false;
296 }
297 } else {
298 Intent locationChanged = new Intent();
299 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
300 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700301 synchronized (this) {
302 // synchronize to ensure incrementPendingBroadcastsLocked()
303 // is called before decrementPendingBroadcasts()
304 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler);
305 // call this after broadcasting so we do not increment
306 // if we throw an exeption.
307 incrementPendingBroadcastsLocked();
308 }
309 } catch (PendingIntent.CanceledException e) {
310 return false;
311 }
312 }
313 return true;
314 }
315
316 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
317 if (mListener != null) {
318 try {
319 synchronized (this) {
320 // synchronize to ensure incrementPendingBroadcastsLocked()
321 // is called before decrementPendingBroadcasts()
322 if (enabled) {
323 mListener.onProviderEnabled(provider);
324 } else {
325 mListener.onProviderDisabled(provider);
326 }
327 if (mListener != mProximityListener) {
328 // call this after broadcasting so we do not increment
329 // if we throw an exeption.
330 incrementPendingBroadcastsLocked();
331 }
332 }
333 } catch (RemoteException e) {
334 return false;
335 }
336 } else {
337 Intent providerIntent = new Intent();
338 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
339 try {
340 synchronized (this) {
341 // synchronize to ensure incrementPendingBroadcastsLocked()
342 // is called before decrementPendingBroadcasts()
343 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler);
344 // call this after broadcasting so we do not increment
345 // if we throw an exeption.
346 incrementPendingBroadcastsLocked();
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 } catch (PendingIntent.CanceledException e) {
349 return false;
350 }
351 }
352 return true;
353 }
354
355 public void binderDied() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700356 if (LOCAL_LOGV) {
357 Log.v(TAG, "Location listener died");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400359 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 removeUpdatesLocked(this);
361 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700362 synchronized (this) {
363 if (mPendingBroadcasts > 0) {
364 LocationManagerService.this.decrementPendingBroadcasts();
365 mPendingBroadcasts = 0;
366 }
367 }
368 }
369
370 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
371 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400372 synchronized (this) {
373 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700374 }
375 }
376
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400377 // this must be called while synchronized by caller in a synchronized block
378 // containing the sending of the broadcaset
379 private void incrementPendingBroadcastsLocked() {
380 if (mPendingBroadcasts++ == 0) {
381 LocationManagerService.this.incrementPendingBroadcasts();
382 }
383 }
384
385 private void decrementPendingBroadcastsLocked() {
386 if (--mPendingBroadcasts == 0) {
387 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700388 }
389 }
390 }
391
392 public void locationCallbackFinished(ILocationListener listener) {
393 Receiver receiver = getReceiver(listener);
394 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400395 synchronized (receiver) {
396 // so wakelock calls will succeed
397 long identity = Binder.clearCallingIdentity();
398 receiver.decrementPendingBroadcastsLocked();
399 Binder.restoreCallingIdentity(identity);
400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402 }
403
Mike Lockwood9637d472009-04-02 21:41:57 -0700404 private final class SettingsObserver implements Observer {
405 public void update(Observable o, Object arg) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400406 synchronized (mLock) {
Mike Lockwood9637d472009-04-02 21:41:57 -0700407 updateProvidersLocked();
408 }
409 }
410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 private Location readLastKnownLocationLocked(String provider) {
413 Location location = null;
414 String s = null;
415 try {
416 File f = new File(LocationManager.SYSTEM_DIR + "/location."
417 + provider);
418 if (!f.exists()) {
419 return null;
420 }
421 BufferedReader reader = new BufferedReader(new FileReader(f), 256);
422 s = reader.readLine();
423 } catch (IOException e) {
424 Log.w(TAG, "Unable to read last known location", e);
425 }
426
427 if (s == null) {
428 return null;
429 }
430 try {
431 String[] tokens = PATTERN_COMMA.split(s);
432 int idx = 0;
433 long time = Long.parseLong(tokens[idx++]);
434 double latitude = Double.parseDouble(tokens[idx++]);
435 double longitude = Double.parseDouble(tokens[idx++]);
436 double altitude = Double.parseDouble(tokens[idx++]);
437 float bearing = Float.parseFloat(tokens[idx++]);
438 float speed = Float.parseFloat(tokens[idx++]);
439
440 location = new Location(provider);
441 location.setTime(time);
442 location.setLatitude(latitude);
443 location.setLongitude(longitude);
444 location.setAltitude(altitude);
445 location.setBearing(bearing);
446 location.setSpeed(speed);
447 } catch (NumberFormatException nfe) {
448 Log.e(TAG, "NumberFormatException reading last known location", nfe);
449 return null;
450 }
451
452 return location;
453 }
454
455 private void writeLastKnownLocationLocked(String provider,
456 Location location) {
457 long now = SystemClock.elapsedRealtime();
458 Long last = mLastWriteTime.get(provider);
459 if ((last != null)
460 && (now - last.longValue() < MIN_LAST_KNOWN_LOCATION_TIME)) {
461 return;
462 }
463 mLastWriteTime.put(provider, now);
464
465 StringBuilder sb = new StringBuilder(100);
466 sb.append(location.getTime());
467 sb.append(',');
468 sb.append(location.getLatitude());
469 sb.append(',');
470 sb.append(location.getLongitude());
471 sb.append(',');
472 sb.append(location.getAltitude());
473 sb.append(',');
474 sb.append(location.getBearing());
475 sb.append(',');
476 sb.append(location.getSpeed());
477
478 FileWriter writer = null;
479 try {
480 File d = new File(LocationManager.SYSTEM_DIR);
481 if (!d.exists()) {
482 if (!d.mkdirs()) {
483 Log.w(TAG, "Unable to create directory to write location");
484 return;
485 }
486 }
487 File f = new File(LocationManager.SYSTEM_DIR + "/location." + provider);
488 writer = new FileWriter(f);
489 writer.write(sb.toString());
490 } catch (IOException e) {
491 Log.w(TAG, "Unable to write location", e);
492 } finally {
493 if (writer != null) {
494 try {
495 writer.close();
496 } catch (IOException e) {
497 Log.w(TAG, "Exception closing file", e);
498 }
499 }
500 }
501 }
502
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400503 private void addProvider(LocationProviderProxy provider) {
504 mProviders.add(provider);
505 mProvidersByName.put(provider.getName(), provider);
506 }
507
508 private void removeProvider(LocationProviderProxy provider) {
509 mProviders.remove(provider);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700510 provider.unlinkProvider();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400511 mProvidersByName.remove(provider.getName());
512 }
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400515 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 if (sProvidersLoaded) {
517 return;
518 }
519
520 // Load providers
521 loadProvidersLocked();
522 sProvidersLoaded = true;
523 }
524 }
525
526 private void loadProvidersLocked() {
527 try {
528 _loadProvidersLocked();
529 } catch (Exception e) {
530 Log.e(TAG, "Exception loading providers:", e);
531 }
532 }
533
534 private void _loadProvidersLocked() {
535 // Attempt to load "real" providers first
536 if (GpsLocationProvider.isSupported()) {
537 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400538 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
539 mGpsStatusProvider = provider.getGpsStatusProvider();
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400540 LocationProviderProxy proxy = new LocationProviderProxy(LocationManager.GPS_PROVIDER, provider);
541 addProvider(proxy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 updateProvidersLocked();
545 }
546
547 /**
548 * @param context the context that the LocationManagerService runs in
549 */
550 public LocationManagerService(Context context) {
551 super();
552 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700553
554 Thread thread = new Thread(null, this, "LocationManagerService");
555 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
The Android Open Source Project10592532009-03-18 17:39:46 -0700557 if (LOCAL_LOGV) {
558 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
Mike Lockwood3d12b512009-04-21 23:25:35 -0700562 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 // Create a wake lock, needs to be done before calling loadProviders() below
564 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
565 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 // Load providers
568 loadProviders();
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400572 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
573 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
575 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400576 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
Mike Lockwood9637d472009-04-02 21:41:57 -0700578 // listen for settings changes
579 ContentResolver resolver = mContext.getContentResolver();
580 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
581 "(" + Settings.System.NAME + "=?)",
582 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
583 null);
584 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
585 SettingsObserver settingsObserver = new SettingsObserver();
586 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588
Mike Lockwood3d12b512009-04-21 23:25:35 -0700589 public void run()
590 {
591 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
592 Looper.prepare();
593 mLocationHandler = new LocationWorkerHandler();
594 initialize();
595 Looper.loop();
596 }
597
Mike Lockwood275555c2009-05-01 11:30:34 -0400598 public void installLocationProvider(String name, ILocationProvider provider) {
599 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
600 != PackageManager.PERMISSION_GRANTED) {
601 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700602 }
603
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400604 synchronized (mLock) {
Mike Lockwood3681f262009-05-12 10:52:03 -0400605 // check to see if we are reinstalling a dead provider
606 LocationProviderProxy oldProvider = mProvidersByName.get(name);
607 if (oldProvider != null) {
608 if (oldProvider.isDead()) {
609 Log.d(TAG, "replacing dead provider");
610 removeProvider(oldProvider);
611 } else {
612 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
613 }
614 }
615
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400616 LocationProviderProxy proxy = new LocationProviderProxy(name, provider);
617 addProvider(proxy);
618 updateProvidersLocked();
Mike Lockwood275555c2009-05-01 11:30:34 -0400619
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400620 // notify provider of current network state
621 proxy.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624
Mike Lockwood275555c2009-05-01 11:30:34 -0400625 public void installGeocodeProvider(IGeocodeProvider provider) {
626 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
627 != PackageManager.PERMISSION_GRANTED) {
628 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwooda55c3212009-04-15 11:10:11 -0400629 }
630
631 mGeocodeProvider = provider;
632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private boolean isAllowedBySettingsLocked(String provider) {
635 if (mEnabledProviders.contains(provider)) {
636 return true;
637 }
638 if (mDisabledProviders.contains(provider)) {
639 return false;
640 }
641 // Use system settings
642 ContentResolver resolver = mContext.getContentResolver();
643 String allowedProviders = Settings.Secure.getString(resolver,
644 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
645
646 return ((allowedProviders != null) && (allowedProviders.contains(provider)));
647 }
648
649 private void checkPermissionsSafe(String provider) {
650 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400651 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 != PackageManager.PERMISSION_GRANTED)) {
653 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
654 }
655 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400656 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400658 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 != PackageManager.PERMISSION_GRANTED)) {
660 throw new SecurityException(
661 "Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission");
662 }
663 }
664
665 private boolean isAllowedProviderSafe(String provider) {
666 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400667 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 != PackageManager.PERMISSION_GRANTED)) {
669 return false;
670 }
671 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400672 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400674 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 != PackageManager.PERMISSION_GRANTED)) {
676 return false;
677 }
678
679 return true;
680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public List<String> getAllProviders() {
683 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400684 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 return _getAllProvidersLocked();
686 }
687 } catch (SecurityException se) {
688 throw se;
689 } catch (Exception e) {
690 Log.e(TAG, "getAllProviders got exception:", e);
691 return null;
692 }
693 }
694
695 private List<String> _getAllProvidersLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700696 if (LOCAL_LOGV) {
697 Log.v(TAG, "getAllProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400699 ArrayList<String> out = new ArrayList<String>(mProviders.size());
700 for (int i = mProviders.size() - 1; i >= 0; i--) {
701 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 out.add(p.getName());
703 }
704 return out;
705 }
706
707 public List<String> getProviders(boolean enabledOnly) {
708 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400709 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 return _getProvidersLocked(enabledOnly);
711 }
712 } catch (SecurityException se) {
713 throw se;
714 } catch (Exception e) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700715 Log.e(TAG, "getProviders got exception:", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 return null;
717 }
718 }
719
720 private List<String> _getProvidersLocked(boolean enabledOnly) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700721 if (LOCAL_LOGV) {
722 Log.v(TAG, "getProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400724 ArrayList<String> out = new ArrayList<String>(mProviders.size());
725 for (int i = mProviders.size() - 1; i >= 0; i--) {
726 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 String name = p.getName();
728 if (isAllowedProviderSafe(name)) {
729 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
730 continue;
731 }
732 out.add(name);
733 }
734 }
735 return out;
736 }
737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 private void updateProvidersLocked() {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400739 for (int i = mProviders.size() - 1; i >= 0; i--) {
740 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 boolean isEnabled = p.isEnabled();
742 String name = p.getName();
743 boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 if (isEnabled && !shouldBeEnabled) {
746 updateProviderListenersLocked(name, false);
747 } else if (!isEnabled && shouldBeEnabled) {
748 updateProviderListenersLocked(name, true);
749 }
750
751 }
752 }
753
754 private void updateProviderListenersLocked(String provider, boolean enabled) {
755 int listeners = 0;
756
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400757 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 if (p == null) {
759 return;
760 }
761
762 ArrayList<Receiver> deadReceivers = null;
763
764 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
765 if (records != null) {
766 final int N = records.size();
767 for (int i=0; i<N; i++) {
768 UpdateRecord record = records.get(i);
769 // Sends a notification message to the receiver
Mike Lockwood48f17512009-04-23 09:12:08 -0700770 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
771 if (deadReceivers == null) {
772 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
Simon Schoar46866572009-06-10 21:12:10 +0200774 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776 listeners++;
777 }
778 }
779
780 if (deadReceivers != null) {
781 for (int i=deadReceivers.size()-1; i>=0; i--) {
782 removeUpdatesLocked(deadReceivers.get(i));
783 }
784 }
785
786 if (enabled) {
787 p.enable();
788 if (listeners > 0) {
789 p.setMinTime(getMinTimeLocked(provider));
790 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792 } else {
793 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797
798 private long getMinTimeLocked(String provider) {
799 long minTime = Long.MAX_VALUE;
800 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
801 if (records != null) {
802 for (int i=records.size()-1; i>=0; i--) {
803 minTime = Math.min(minTime, records.get(i).mMinTime);
804 }
805 }
806 return minTime;
807 }
808
809 private class UpdateRecord {
810 final String mProvider;
811 final Receiver mReceiver;
812 final long mMinTime;
813 final float mMinDistance;
814 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400815 Location mLastFixBroadcast;
816 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 /**
819 * Note: must be constructed with lock held.
820 */
821 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400822 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 mProvider = provider;
824 mReceiver = receiver;
825 mMinTime = minTime;
826 mMinDistance = minDistance;
827 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
830 if (records == null) {
831 records = new ArrayList<UpdateRecord>();
832 mRecordsByProvider.put(provider, records);
833 }
834 if (!records.contains(this)) {
835 records.add(this);
836 }
837 }
838
839 /**
840 * Method to be called when a record will no longer be used. Calling this multiple times
841 * must have the same effect as calling it once.
842 */
843 void disposeLocked() {
844 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
845 records.remove(this);
846 }
847
848 @Override
849 public String toString() {
850 return "UpdateRecord{"
851 + Integer.toHexString(System.identityHashCode(this))
852 + " " + mProvider + " " + mReceiver + "}";
853 }
854
855 void dump(PrintWriter pw, String prefix) {
856 pw.println(prefix + this);
857 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
858 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400859 pw.println(prefix + "mUid=" + mUid);
860 pw.println(prefix + "mLastFixBroadcast:");
861 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
862 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
864
865 /**
866 * Calls dispose().
867 */
868 @Override protected void finalize() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400869 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 disposeLocked();
871 }
872 }
873 }
874
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400875 private Receiver getReceiver(ILocationListener listener) {
876 IBinder binder = listener.asBinder();
877 Receiver receiver = mReceivers.get(binder);
878 if (receiver == null) {
879 receiver = new Receiver(listener);
880 mReceivers.put(binder, receiver);
881
882 try {
883 if (receiver.isListener()) {
884 receiver.getListener().asBinder().linkToDeath(receiver, 0);
885 }
886 } catch (RemoteException e) {
887 Log.e(TAG, "linkToDeath failed:", e);
888 return null;
889 }
890 }
891 return receiver;
892 }
893
894 private Receiver getReceiver(PendingIntent intent) {
895 Receiver receiver = mReceivers.get(intent);
896 if (receiver == null) {
897 receiver = new Receiver(intent);
898 mReceivers.put(intent, receiver);
899 }
900 return receiver;
901 }
902
903 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
904 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
905 if (records != null) {
906 for (int i = records.size() - 1; i >= 0; i--) {
907 UpdateRecord record = records.get(i);
908 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
909 return true;
910 }
911 }
912 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400913 for (ProximityAlert alert : mProximityAlerts.values()) {
914 if (alert.mUid == uid) {
915 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400916 }
917 }
918 return false;
919 }
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void requestLocationUpdates(String provider,
922 long minTime, float minDistance, ILocationListener listener) {
923
924 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400925 synchronized (mLock) {
926 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928 } catch (SecurityException se) {
929 throw se;
930 } catch (Exception e) {
931 Log.e(TAG, "requestUpdates got exception:", e);
932 }
933 }
934
935 public void requestLocationUpdatesPI(String provider,
936 long minTime, float minDistance, PendingIntent intent) {
937 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400938 synchronized (mLock) {
939 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941 } catch (SecurityException se) {
942 throw se;
943 } catch (Exception e) {
944 Log.e(TAG, "requestUpdates got exception:", e);
945 }
946 }
947
948 private void requestLocationUpdatesLocked(String provider,
949 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700950 if (LOCAL_LOGV) {
951 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400954 LocationProviderProxy proxy = mProvidersByName.get(provider);
955 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 throw new IllegalArgumentException("provider=" + provider);
957 }
958
959 checkPermissionsSafe(provider);
960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 // so wakelock calls will succeed
962 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400963 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 long identity = Binder.clearCallingIdentity();
965 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400966 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
967 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if (oldRecord != null) {
969 oldRecord.disposeLocked();
970 }
971
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400972 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400973 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400974 }
975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
977 if (isProviderEnabled) {
978 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400979 proxy.setMinTime(minTimeForProvider);
980 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700982 // Notify the listener that updates are currently disabled
983 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 } finally {
986 Binder.restoreCallingIdentity(identity);
987 }
988 }
989
990 public void removeUpdates(ILocationListener listener) {
991 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400992 synchronized (mLock) {
993 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
995 } catch (SecurityException se) {
996 throw se;
997 } catch (Exception e) {
998 Log.e(TAG, "removeUpdates got exception:", e);
999 }
1000 }
1001
1002 public void removeUpdatesPI(PendingIntent intent) {
1003 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001004 synchronized (mLock) {
1005 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007 } catch (SecurityException se) {
1008 throw se;
1009 } catch (Exception e) {
1010 Log.e(TAG, "removeUpdates got exception:", e);
1011 }
1012 }
1013
1014 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001015 if (LOCAL_LOGV) {
1016 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
1018
1019 // so wakelock calls will succeed
1020 final int callingUid = Binder.getCallingUid();
1021 long identity = Binder.clearCallingIdentity();
1022 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001023 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1024 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
1027 // Record which providers were associated with this listener
1028 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001029 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 if (oldRecords != null) {
1031 // Call dispose() on the obsolete update records.
1032 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001033 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001034 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1035 if (proxy != null) {
1036 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 }
1039 record.disposeLocked();
1040 }
1041 // Accumulate providers
1042 providers.addAll(oldRecords.keySet());
1043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044
1045 // See if the providers associated with this listener have any
1046 // other listeners; if one does, inform it of the new smallest minTime
1047 // value; if one does not, disable location tracking for it
1048 for (String provider : providers) {
1049 // If provider is already disabled, don't need to do anything
1050 if (!isAllowedBySettingsLocked(provider)) {
1051 continue;
1052 }
1053
1054 boolean hasOtherListener = false;
1055 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1056 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1057 hasOtherListener = true;
1058 }
1059
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001060 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 if (p != null) {
1062 if (hasOtherListener) {
1063 p.setMinTime(getMinTimeLocked(provider));
1064 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 p.enableLocationTracking(false);
1066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 } finally {
1070 Binder.restoreCallingIdentity(identity);
1071 }
1072 }
1073
1074 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001075 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 return false;
1077 }
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001078 if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 PackageManager.PERMISSION_GRANTED) {
1080 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1081 }
1082
1083 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001084 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001086 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 return false;
1088 }
1089 return true;
1090 }
1091
1092 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001093 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001094 try {
1095 mGpsStatusProvider.removeGpsStatusListener(listener);
1096 } catch (Exception e) {
1097 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 }
1101
1102 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
1103 // first check for permission to the provider
1104 checkPermissionsSafe(provider);
1105 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001106 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 != PackageManager.PERMISSION_GRANTED)) {
1108 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1109 }
1110
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001111 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001112 LocationProviderProxy proxy = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 if (provider == null) {
1114 return false;
1115 }
1116
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001117 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119 }
1120
1121 class ProximityAlert {
1122 final int mUid;
1123 final double mLatitude;
1124 final double mLongitude;
1125 final float mRadius;
1126 final long mExpiration;
1127 final PendingIntent mIntent;
1128 final Location mLocation;
1129
1130 public ProximityAlert(int uid, double latitude, double longitude,
1131 float radius, long expiration, PendingIntent intent) {
1132 mUid = uid;
1133 mLatitude = latitude;
1134 mLongitude = longitude;
1135 mRadius = radius;
1136 mExpiration = expiration;
1137 mIntent = intent;
1138
1139 mLocation = new Location("");
1140 mLocation.setLatitude(latitude);
1141 mLocation.setLongitude(longitude);
1142 }
1143
1144 long getExpiration() {
1145 return mExpiration;
1146 }
1147
1148 PendingIntent getIntent() {
1149 return mIntent;
1150 }
1151
1152 boolean isInProximity(double latitude, double longitude) {
1153 Location loc = new Location("");
1154 loc.setLatitude(latitude);
1155 loc.setLongitude(longitude);
1156
1157 double radius = loc.distanceTo(mLocation);
1158 return radius <= mRadius;
1159 }
1160
1161 @Override
1162 public String toString() {
1163 return "ProximityAlert{"
1164 + Integer.toHexString(System.identityHashCode(this))
1165 + " uid " + mUid + mIntent + "}";
1166 }
1167
1168 void dump(PrintWriter pw, String prefix) {
1169 pw.println(prefix + this);
1170 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1171 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1172 pw.println(prefix + "mIntent=" + mIntent);
1173 pw.println(prefix + "mLocation:");
1174 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1175 }
1176 }
1177
1178 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001179 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180
1181 boolean isGpsAvailable = false;
1182
1183 // Note: this is called with the lock held.
1184 public void onLocationChanged(Location loc) {
1185
1186 // If Gps is available, then ignore updates from NetworkLocationProvider
1187 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1188 isGpsAvailable = true;
1189 }
1190 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1191 return;
1192 }
1193
1194 // Process proximity alerts
1195 long now = System.currentTimeMillis();
1196 double latitude = loc.getLatitude();
1197 double longitude = loc.getLongitude();
1198 ArrayList<PendingIntent> intentsToRemove = null;
1199
1200 for (ProximityAlert alert : mProximityAlerts.values()) {
1201 PendingIntent intent = alert.getIntent();
1202 long expiration = alert.getExpiration();
1203
1204 if ((expiration == -1) || (now <= expiration)) {
1205 boolean entered = mProximitiesEntered.contains(alert);
1206 boolean inProximity =
1207 alert.isInProximity(latitude, longitude);
1208 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001209 if (LOCAL_LOGV) {
1210 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 mProximitiesEntered.add(alert);
1213 Intent enteredIntent = new Intent();
1214 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1215 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001216 synchronized (this) {
1217 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001218 // is called before decrementPendingBroadcasts()
1219 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1220 // call this after broadcasting so we do not increment
1221 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001222 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001225 if (LOCAL_LOGV) {
1226 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228 if (intentsToRemove == null) {
1229 intentsToRemove = new ArrayList<PendingIntent>();
1230 }
1231 intentsToRemove.add(intent);
1232 }
1233 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001234 if (LOCAL_LOGV) {
1235 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 mProximitiesEntered.remove(alert);
1238 Intent exitedIntent = new Intent();
1239 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1240 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001241 synchronized (this) {
1242 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001243 // is called before decrementPendingBroadcasts()
1244 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1245 // call this after broadcasting so we do not increment
1246 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001247 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001250 if (LOCAL_LOGV) {
1251 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
1253 if (intentsToRemove == null) {
1254 intentsToRemove = new ArrayList<PendingIntent>();
1255 }
1256 intentsToRemove.add(intent);
1257 }
1258 }
1259 } else {
1260 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001261 if (LOCAL_LOGV) {
1262 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264 if (intentsToRemove == null) {
1265 intentsToRemove = new ArrayList<PendingIntent>();
1266 }
1267 intentsToRemove.add(alert.getIntent());
1268 }
1269 }
1270
1271 // Remove expired alerts
1272 if (intentsToRemove != null) {
1273 for (PendingIntent i : intentsToRemove) {
1274 mProximityAlerts.remove(i);
1275 ProximityAlert alert = mProximityAlerts.get(i);
1276 mProximitiesEntered.remove(alert);
1277 }
1278 }
1279
1280 }
1281
1282 // Note: this is called with the lock held.
1283 public void onProviderDisabled(String provider) {
1284 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1285 isGpsAvailable = false;
1286 }
1287 }
1288
1289 // Note: this is called with the lock held.
1290 public void onProviderEnabled(String provider) {
1291 // ignore
1292 }
1293
1294 // Note: this is called with the lock held.
1295 public void onStatusChanged(String provider, int status, Bundle extras) {
1296 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1297 (status != LocationProvider.AVAILABLE)) {
1298 isGpsAvailable = false;
1299 }
1300 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001301
1302 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1303 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001304 // synchronize to ensure incrementPendingBroadcasts()
1305 // is called before decrementPendingBroadcasts()
1306 synchronized (this) {
1307 decrementPendingBroadcasts();
1308 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311
1312 public void addProximityAlert(double latitude, double longitude,
1313 float radius, long expiration, PendingIntent intent) {
1314 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001315 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1317 }
1318 } catch (SecurityException se) {
1319 throw se;
1320 } catch (Exception e) {
1321 Log.e(TAG, "addProximityAlert got exception:", e);
1322 }
1323 }
1324
1325 private void addProximityAlertLocked(double latitude, double longitude,
1326 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001327 if (LOCAL_LOGV) {
1328 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 ", longitude = " + longitude +
1330 ", expiration = " + expiration +
1331 ", intent = " + intent);
1332 }
1333
1334 // Require ability to access all providers for now
1335 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1336 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1337 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1338 }
1339
1340 if (expiration != -1) {
1341 expiration += System.currentTimeMillis();
1342 }
1343 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1344 latitude, longitude, radius, expiration, intent);
1345 mProximityAlerts.put(intent, alert);
1346
Mike Lockwood48f17512009-04-23 09:12:08 -07001347 if (mProximityReceiver == null) {
1348 mProximityListener = new ProximityListener();
1349 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350
Mike Lockwood95427cd2009-05-07 13:27:54 -04001351 for (int i = mProviders.size() - 1; i >= 0; i--) {
1352 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001353 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 }
1356 }
1357
1358 public void removeProximityAlert(PendingIntent intent) {
1359 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001360 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 removeProximityAlertLocked(intent);
1362 }
1363 } catch (SecurityException se) {
1364 throw se;
1365 } catch (Exception e) {
1366 Log.e(TAG, "removeProximityAlert got exception:", e);
1367 }
1368 }
1369
1370 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001371 if (LOCAL_LOGV) {
1372 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374
1375 mProximityAlerts.remove(intent);
1376 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001377 removeUpdatesLocked(mProximityReceiver);
1378 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 }
1382
1383 /**
1384 * @return null if the provider does not exits
1385 * @throw SecurityException if the provider is not allowed to be
1386 * accessed by the caller
1387 */
1388 public Bundle getProviderInfo(String provider) {
1389 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001390 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 return _getProviderInfoLocked(provider);
1392 }
1393 } catch (SecurityException se) {
1394 throw se;
1395 } catch (Exception e) {
1396 Log.e(TAG, "_getProviderInfo got exception:", e);
1397 return null;
1398 }
1399 }
1400
1401 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001402 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 if (p == null) {
1404 return null;
1405 }
1406
1407 checkPermissionsSafe(provider);
1408
1409 Bundle b = new Bundle();
1410 b.putBoolean("network", p.requiresNetwork());
1411 b.putBoolean("satellite", p.requiresSatellite());
1412 b.putBoolean("cell", p.requiresCell());
1413 b.putBoolean("cost", p.hasMonetaryCost());
1414 b.putBoolean("altitude", p.supportsAltitude());
1415 b.putBoolean("speed", p.supportsSpeed());
1416 b.putBoolean("bearing", p.supportsBearing());
1417 b.putInt("power", p.getPowerRequirement());
1418 b.putInt("accuracy", p.getAccuracy());
1419
1420 return b;
1421 }
1422
1423 public boolean isProviderEnabled(String provider) {
1424 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001425 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 return _isProviderEnabledLocked(provider);
1427 }
1428 } catch (SecurityException se) {
1429 throw se;
1430 } catch (Exception e) {
1431 Log.e(TAG, "isProviderEnabled got exception:", e);
1432 return false;
1433 }
1434 }
1435
Mike Lockwood275555c2009-05-01 11:30:34 -04001436 public void reportLocation(Location location) {
1437 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1438 != PackageManager.PERMISSION_GRANTED) {
1439 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1440 }
1441
Mike Lockwood4e50b782009-04-03 08:24:43 -07001442 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1443 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1444 mLocationHandler.sendMessageAtFrontOfQueue(m);
1445 }
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 private boolean _isProviderEnabledLocked(String provider) {
1448 checkPermissionsSafe(provider);
1449
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001450 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (p == null) {
1452 throw new IllegalArgumentException("provider=" + provider);
1453 }
1454 return isAllowedBySettingsLocked(provider);
1455 }
1456
1457 public Location getLastKnownLocation(String provider) {
1458 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001459 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 return _getLastKnownLocationLocked(provider);
1461 }
1462 } catch (SecurityException se) {
1463 throw se;
1464 } catch (Exception e) {
1465 Log.e(TAG, "getLastKnownLocation got exception:", e);
1466 return null;
1467 }
1468 }
1469
1470 private Location _getLastKnownLocationLocked(String provider) {
1471 checkPermissionsSafe(provider);
1472
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001473 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 if (p == null) {
1475 throw new IllegalArgumentException("provider=" + provider);
1476 }
1477
1478 if (!isAllowedBySettingsLocked(provider)) {
1479 return null;
1480 }
1481
1482 Location location = mLastKnownLocation.get(provider);
1483 if (location == null) {
1484 // Get the persistent last known location for the provider
1485 location = readLastKnownLocationLocked(provider);
1486 if (location != null) {
1487 mLastKnownLocation.put(provider, location);
1488 }
1489 }
1490
1491 return location;
1492 }
1493
1494 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1495 // Always broadcast the first update
1496 if (lastLoc == null) {
1497 return true;
1498 }
1499
1500 // Don't broadcast same location again regardless of condition
1501 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1502 if (loc.getTime() == lastLoc.getTime()) {
1503 return false;
1504 }
1505
1506 // Check whether sufficient distance has been traveled
1507 double minDistance = record.mMinDistance;
1508 if (minDistance > 0.0) {
1509 if (loc.distanceTo(lastLoc) <= minDistance) {
1510 return false;
1511 }
1512 }
1513
1514 return true;
1515 }
1516
Mike Lockwood4e50b782009-04-03 08:24:43 -07001517 private void handleLocationChangedLocked(Location location) {
1518 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1520 if (records == null || records.size() == 0) {
1521 return;
1522 }
1523
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001524 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 if (p == null) {
1526 return;
1527 }
1528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001530 Location lastLocation = mLastKnownLocation.get(provider);
1531 if (lastLocation == null) {
1532 mLastKnownLocation.put(provider, new Location(location));
1533 } else {
1534 lastLocation.set(location);
1535 }
1536 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 // Fetch latest status update time
1539 long newStatusUpdateTime = p.getStatusUpdateTime();
1540
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001541 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 Bundle extras = new Bundle();
1543 int status = p.getStatus(extras);
1544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 ArrayList<Receiver> deadReceivers = null;
1546
1547 // Broadcast location or status to all listeners
1548 final int N = records.size();
1549 for (int i=0; i<N; i++) {
1550 UpdateRecord r = records.get(i);
1551 Receiver receiver = r.mReceiver;
1552
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001553 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001554 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1555 if (lastLoc == null) {
1556 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001557 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001558 } else {
1559 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001561 if (!receiver.callLocationChangedLocked(location)) {
1562 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1563 if (deadReceivers == null) {
1564 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001566 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568 }
1569
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001570 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1572 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1573
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001574 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1576 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1577 if (deadReceivers == null) {
1578 deadReceivers = new ArrayList<Receiver>();
1579 }
1580 if (!deadReceivers.contains(receiver)) {
1581 deadReceivers.add(receiver);
1582 }
1583 }
1584 }
1585 }
1586
1587 if (deadReceivers != null) {
1588 for (int i=deadReceivers.size()-1; i>=0; i--) {
1589 removeUpdatesLocked(deadReceivers.get(i));
1590 }
1591 }
1592 }
1593
1594 private class LocationWorkerHandler extends Handler {
1595
1596 @Override
1597 public void handleMessage(Message msg) {
1598 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001599 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1600 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001602 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001603 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001604 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001605
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001606 // notify other providers of the new location
1607 for (int i = mProviders.size() - 1; i >= 0; i--) {
1608 LocationProviderProxy proxy = mProviders.get(i);
1609 if (!provider.equals(proxy.getName())) {
1610 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001611 }
1612 }
1613
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001614 if (isAllowedBySettingsLocked(provider)) {
1615 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
1619 } catch (Exception e) {
1620 // Log, don't crash!
1621 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1622 }
1623 }
1624 }
1625
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001626 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1627 @Override
1628 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 String action = intent.getAction();
1630
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001631 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001633 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1635 if (uid >= 0) {
1636 ArrayList<Receiver> removedRecs = null;
1637 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1638 for (int j=i.size()-1; j>=0; j--) {
1639 UpdateRecord ur = i.get(j);
1640 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1641 if (removedRecs == null) {
1642 removedRecs = new ArrayList<Receiver>();
1643 }
1644 if (!removedRecs.contains(ur.mReceiver)) {
1645 removedRecs.add(ur.mReceiver);
1646 }
1647 }
1648 }
1649 }
1650 ArrayList<ProximityAlert> removedAlerts = null;
1651 for (ProximityAlert i : mProximityAlerts.values()) {
1652 if (i.mUid == uid) {
1653 if (removedAlerts == null) {
1654 removedAlerts = new ArrayList<ProximityAlert>();
1655 }
1656 if (!removedAlerts.contains(i)) {
1657 removedAlerts.add(i);
1658 }
1659 }
1660 }
1661 if (removedRecs != null) {
1662 for (int i=removedRecs.size()-1; i>=0; i--) {
1663 removeUpdatesLocked(removedRecs.get(i));
1664 }
1665 }
1666 if (removedAlerts != null) {
1667 for (int i=removedAlerts.size()-1; i>=0; i--) {
1668 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1669 }
1670 }
1671 }
1672 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001673 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 boolean noConnectivity =
1675 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1676 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001677 mNetworkState = LocationProvider.AVAILABLE;
1678 } else {
1679 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681
1682 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001683 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001684 for (int i = mProviders.size() - 1; i >= 0; i--) {
1685 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001687 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689 }
1690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001693 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694
1695 // Wake locks
1696
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001697 private void incrementPendingBroadcasts() {
1698 synchronized (mWakeLock) {
1699 if (mPendingBroadcasts++ == 0) {
1700 try {
1701 mWakeLock.acquire();
1702 log("Acquired wakelock");
1703 } catch (Exception e) {
1704 // This is to catch a runtime exception thrown when we try to release an
1705 // already released lock.
1706 Log.e(TAG, "exception in acquireWakeLock()", e);
1707 }
1708 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001709 }
1710 }
1711
1712 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001713 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001714 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001715 try {
1716 // Release wake lock
1717 if (mWakeLock.isHeld()) {
1718 mWakeLock.release();
1719 log("Released wakelock");
1720 } else {
1721 log("Can't release wakelock again!");
1722 }
1723 } catch (Exception e) {
1724 // This is to catch a runtime exception thrown when we try to release an
1725 // already released lock.
1726 Log.e(TAG, "exception in releaseWakeLock()", e);
1727 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001728 }
1729 }
1730 }
1731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 // Geocoder
1733
1734 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001735 String language, String country, String variant, String appName, List<Address> addrs) {
1736 if (mGeocodeProvider != null) {
1737 try {
1738 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1739 variant, appName, addrs);
1740 } catch (RemoteException e) {
1741 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001742 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001745 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747
Mike Lockwooda55c3212009-04-15 11:10:11 -04001748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001750 double lowerLeftLatitude, double lowerLeftLongitude,
1751 double upperRightLatitude, double upperRightLongitude, int maxResults,
1752 String language, String country, String variant, String appName, List<Address> addrs) {
1753
1754 if (mGeocodeProvider != null) {
1755 try {
1756 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1757 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1758 maxResults, language, country, variant, appName, addrs);
1759 } catch (RemoteException e) {
1760 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001761 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001764 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766
1767 // Mock Providers
1768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 private void checkMockPermissionsSafe() {
1770 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1771 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1772 if (!allowMocks) {
1773 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1774 }
1775
1776 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1777 PackageManager.PERMISSION_GRANTED) {
1778 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1779 }
1780 }
1781
1782 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1783 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1784 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1785 checkMockPermissionsSafe();
1786
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001787 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001788 MockProvider provider = new MockProvider(name, this,
1789 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 requiresCell, hasMonetaryCost, supportsAltitude,
1791 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001792 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1794 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001795
1796 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1797 long identity = Binder.clearCallingIdentity();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001798 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001799 mMockProviders.put(name, provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 updateProvidersLocked();
Mike Lockwood95427cd2009-05-07 13:27:54 -04001801 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803 }
1804
1805 public void removeTestProvider(String provider) {
1806 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001807 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001808 MockProvider mockProvider = mMockProviders.get(provider);
1809 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1811 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001812 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001813 mMockProviders.remove(mockProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 updateProvidersLocked();
1815 }
1816 }
1817
1818 public void setTestProviderLocation(String provider, Location loc) {
1819 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001820 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001821 MockProvider mockProvider = mMockProviders.get(provider);
1822 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1824 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001825 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1826 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001827 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001828 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 }
1830 }
1831
1832 public void clearTestProviderLocation(String provider) {
1833 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001834 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001835 MockProvider mockProvider = mMockProviders.get(provider);
1836 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1838 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001839 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841 }
1842
1843 public void setTestProviderEnabled(String provider, boolean enabled) {
1844 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001845 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001846 MockProvider mockProvider = mMockProviders.get(provider);
1847 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1849 }
1850 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001851 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 mEnabledProviders.add(provider);
1853 mDisabledProviders.remove(provider);
1854 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001855 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 mEnabledProviders.remove(provider);
1857 mDisabledProviders.add(provider);
1858 }
1859 updateProvidersLocked();
1860 }
1861 }
1862
1863 public void clearTestProviderEnabled(String provider) {
1864 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001865 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001866 MockProvider mockProvider = mMockProviders.get(provider);
1867 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1869 }
1870 mEnabledProviders.remove(provider);
1871 mDisabledProviders.remove(provider);
1872 updateProvidersLocked();
1873 }
1874 }
1875
1876 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1877 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001878 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001879 MockProvider mockProvider = mMockProviders.get(provider);
1880 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1882 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001883 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885 }
1886
1887 public void clearTestProviderStatus(String provider) {
1888 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001889 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001890 MockProvider mockProvider = mMockProviders.get(provider);
1891 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1893 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001894 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
1896 }
1897
1898 private void log(String log) {
1899 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1900 Log.d(TAG, log);
1901 }
1902 }
1903
1904 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1905 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1906 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001907 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 + Binder.getCallingPid()
1909 + ", uid=" + Binder.getCallingUid());
1910 return;
1911 }
1912
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001913 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 pw.println("Current Location Manager state:");
1915 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001917 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001919 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
1921 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001922 for (Receiver i : mReceivers.values()) {
1923 pw.println(" " + i + ":");
1924 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 pw.println(" " + j.getKey() + ":");
1926 j.getValue().dump(pw, " ");
1927 }
1928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 pw.println(" Records by Provider:");
1930 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1931 : mRecordsByProvider.entrySet()) {
1932 pw.println(" " + i.getKey() + ":");
1933 for (UpdateRecord j : i.getValue()) {
1934 pw.println(" " + j + ":");
1935 j.dump(pw, " ");
1936 }
1937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 pw.println(" Last Known Locations:");
1939 for (Map.Entry<String, Location> i
1940 : mLastKnownLocation.entrySet()) {
1941 pw.println(" " + i.getKey() + ":");
1942 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1943 }
1944 if (mProximityAlerts.size() > 0) {
1945 pw.println(" Proximity Alerts:");
1946 for (Map.Entry<PendingIntent, ProximityAlert> i
1947 : mProximityAlerts.entrySet()) {
1948 pw.println(" " + i.getKey() + ":");
1949 i.getValue().dump(pw, " ");
1950 }
1951 }
1952 if (mProximitiesEntered.size() > 0) {
1953 pw.println(" Proximities Entered:");
1954 for (ProximityAlert i : mProximitiesEntered) {
1955 pw.println(" " + i + ":");
1956 i.dump(pw, " ");
1957 }
1958 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001959 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 pw.println(" mProximityListener=" + mProximityListener);
1961 if (mEnabledProviders.size() > 0) {
1962 pw.println(" Enabled Providers:");
1963 for (String i : mEnabledProviders) {
1964 pw.println(" " + i);
1965 }
1966
1967 }
1968 if (mDisabledProviders.size() > 0) {
1969 pw.println(" Disabled Providers:");
1970 for (String i : mDisabledProviders) {
1971 pw.println(" " + i);
1972 }
1973
1974 }
1975 if (mMockProviders.size() > 0) {
1976 pw.println(" Mock Providers:");
1977 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001978 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
1982 }
1983}