blob: fc3729050c9ec0d208fe57a402fdcd1a8ac1ca63 [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);
510 mProvidersByName.remove(provider.getName());
511 }
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400514 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 if (sProvidersLoaded) {
516 return;
517 }
518
519 // Load providers
520 loadProvidersLocked();
521 sProvidersLoaded = true;
522 }
523 }
524
525 private void loadProvidersLocked() {
526 try {
527 _loadProvidersLocked();
528 } catch (Exception e) {
529 Log.e(TAG, "Exception loading providers:", e);
530 }
531 }
532
533 private void _loadProvidersLocked() {
534 // Attempt to load "real" providers first
535 if (GpsLocationProvider.isSupported()) {
536 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400537 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
538 mGpsStatusProvider = provider.getGpsStatusProvider();
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400539 LocationProviderProxy proxy = new LocationProviderProxy(LocationManager.GPS_PROVIDER, provider);
540 addProvider(proxy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 updateProvidersLocked();
544 }
545
546 /**
547 * @param context the context that the LocationManagerService runs in
548 */
549 public LocationManagerService(Context context) {
550 super();
551 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700552
553 Thread thread = new Thread(null, this, "LocationManagerService");
554 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555
The Android Open Source Project10592532009-03-18 17:39:46 -0700556 if (LOCAL_LOGV) {
557 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
Mike Lockwood3d12b512009-04-21 23:25:35 -0700561 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 // Create a wake lock, needs to be done before calling loadProviders() below
563 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
564 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 // Load providers
567 loadProviders();
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400571 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
572 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
574 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400575 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576
Mike Lockwood9637d472009-04-02 21:41:57 -0700577 // listen for settings changes
578 ContentResolver resolver = mContext.getContentResolver();
579 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
580 "(" + Settings.System.NAME + "=?)",
581 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
582 null);
583 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
584 SettingsObserver settingsObserver = new SettingsObserver();
585 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587
Mike Lockwood3d12b512009-04-21 23:25:35 -0700588 public void run()
589 {
590 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
591 Looper.prepare();
592 mLocationHandler = new LocationWorkerHandler();
593 initialize();
594 Looper.loop();
595 }
596
Mike Lockwood275555c2009-05-01 11:30:34 -0400597 public void installLocationProvider(String name, ILocationProvider provider) {
598 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
599 != PackageManager.PERMISSION_GRANTED) {
600 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700601 }
602
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400603 synchronized (mLock) {
Mike Lockwood3681f262009-05-12 10:52:03 -0400604 // check to see if we are reinstalling a dead provider
605 LocationProviderProxy oldProvider = mProvidersByName.get(name);
606 if (oldProvider != null) {
607 if (oldProvider.isDead()) {
608 Log.d(TAG, "replacing dead provider");
609 removeProvider(oldProvider);
610 } else {
611 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
612 }
613 }
614
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400615 LocationProviderProxy proxy = new LocationProviderProxy(name, provider);
616 addProvider(proxy);
617 updateProvidersLocked();
Mike Lockwood275555c2009-05-01 11:30:34 -0400618
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400619 // notify provider of current network state
620 proxy.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622 }
623
Mike Lockwood275555c2009-05-01 11:30:34 -0400624 public void installGeocodeProvider(IGeocodeProvider provider) {
625 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
626 != PackageManager.PERMISSION_GRANTED) {
627 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwooda55c3212009-04-15 11:10:11 -0400628 }
629
630 mGeocodeProvider = provider;
631 }
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 private boolean isAllowedBySettingsLocked(String provider) {
634 if (mEnabledProviders.contains(provider)) {
635 return true;
636 }
637 if (mDisabledProviders.contains(provider)) {
638 return false;
639 }
640 // Use system settings
641 ContentResolver resolver = mContext.getContentResolver();
642 String allowedProviders = Settings.Secure.getString(resolver,
643 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
644
645 return ((allowedProviders != null) && (allowedProviders.contains(provider)));
646 }
647
648 private void checkPermissionsSafe(String provider) {
649 if (LocationManager.GPS_PROVIDER.equals(provider)
650 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
651 != PackageManager.PERMISSION_GRANTED)) {
652 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
653 }
654 if (LocationManager.NETWORK_PROVIDER.equals(provider)
655 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
656 != PackageManager.PERMISSION_GRANTED)
657 && (mContext.checkCallingPermission(ACCESS_COARSE_LOCATION)
658 != PackageManager.PERMISSION_GRANTED)) {
659 throw new SecurityException(
660 "Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission");
661 }
662 }
663
664 private boolean isAllowedProviderSafe(String provider) {
665 if (LocationManager.GPS_PROVIDER.equals(provider)
666 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
667 != PackageManager.PERMISSION_GRANTED)) {
668 return false;
669 }
670 if (LocationManager.NETWORK_PROVIDER.equals(provider)
671 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
672 != PackageManager.PERMISSION_GRANTED)
673 && (mContext.checkCallingPermission(ACCESS_COARSE_LOCATION)
674 != PackageManager.PERMISSION_GRANTED)) {
675 return false;
676 }
677
678 return true;
679 }
680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 public List<String> getAllProviders() {
682 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400683 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 return _getAllProvidersLocked();
685 }
686 } catch (SecurityException se) {
687 throw se;
688 } catch (Exception e) {
689 Log.e(TAG, "getAllProviders got exception:", e);
690 return null;
691 }
692 }
693
694 private List<String> _getAllProvidersLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700695 if (LOCAL_LOGV) {
696 Log.v(TAG, "getAllProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400698 ArrayList<String> out = new ArrayList<String>(mProviders.size());
699 for (int i = mProviders.size() - 1; i >= 0; i--) {
700 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 out.add(p.getName());
702 }
703 return out;
704 }
705
706 public List<String> getProviders(boolean enabledOnly) {
707 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400708 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 return _getProvidersLocked(enabledOnly);
710 }
711 } catch (SecurityException se) {
712 throw se;
713 } catch (Exception e) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700714 Log.e(TAG, "getProviders got exception:", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 return null;
716 }
717 }
718
719 private List<String> _getProvidersLocked(boolean enabledOnly) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700720 if (LOCAL_LOGV) {
721 Log.v(TAG, "getProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400723 ArrayList<String> out = new ArrayList<String>(mProviders.size());
724 for (int i = mProviders.size() - 1; i >= 0; i--) {
725 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 String name = p.getName();
727 if (isAllowedProviderSafe(name)) {
728 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
729 continue;
730 }
731 out.add(name);
732 }
733 }
734 return out;
735 }
736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 private void updateProvidersLocked() {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400738 for (int i = mProviders.size() - 1; i >= 0; i--) {
739 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 boolean isEnabled = p.isEnabled();
741 String name = p.getName();
742 boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 if (isEnabled && !shouldBeEnabled) {
745 updateProviderListenersLocked(name, false);
746 } else if (!isEnabled && shouldBeEnabled) {
747 updateProviderListenersLocked(name, true);
748 }
749
750 }
751 }
752
753 private void updateProviderListenersLocked(String provider, boolean enabled) {
754 int listeners = 0;
755
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400756 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 if (p == null) {
758 return;
759 }
760
761 ArrayList<Receiver> deadReceivers = null;
762
763 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
764 if (records != null) {
765 final int N = records.size();
766 for (int i=0; i<N; i++) {
767 UpdateRecord record = records.get(i);
768 // Sends a notification message to the receiver
Mike Lockwood48f17512009-04-23 09:12:08 -0700769 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
770 if (deadReceivers == null) {
771 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
Simon Schoar46866572009-06-10 21:12:10 +0200773 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 listeners++;
776 }
777 }
778
779 if (deadReceivers != null) {
780 for (int i=deadReceivers.size()-1; i>=0; i--) {
781 removeUpdatesLocked(deadReceivers.get(i));
782 }
783 }
784
785 if (enabled) {
786 p.enable();
787 if (listeners > 0) {
788 p.setMinTime(getMinTimeLocked(provider));
789 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791 } else {
792 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796
797 private long getMinTimeLocked(String provider) {
798 long minTime = Long.MAX_VALUE;
799 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
800 if (records != null) {
801 for (int i=records.size()-1; i>=0; i--) {
802 minTime = Math.min(minTime, records.get(i).mMinTime);
803 }
804 }
805 return minTime;
806 }
807
808 private class UpdateRecord {
809 final String mProvider;
810 final Receiver mReceiver;
811 final long mMinTime;
812 final float mMinDistance;
813 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400814 Location mLastFixBroadcast;
815 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816
817 /**
818 * Note: must be constructed with lock held.
819 */
820 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400821 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 mProvider = provider;
823 mReceiver = receiver;
824 mMinTime = minTime;
825 mMinDistance = minDistance;
826 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
828 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
829 if (records == null) {
830 records = new ArrayList<UpdateRecord>();
831 mRecordsByProvider.put(provider, records);
832 }
833 if (!records.contains(this)) {
834 records.add(this);
835 }
836 }
837
838 /**
839 * Method to be called when a record will no longer be used. Calling this multiple times
840 * must have the same effect as calling it once.
841 */
842 void disposeLocked() {
843 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
844 records.remove(this);
845 }
846
847 @Override
848 public String toString() {
849 return "UpdateRecord{"
850 + Integer.toHexString(System.identityHashCode(this))
851 + " " + mProvider + " " + mReceiver + "}";
852 }
853
854 void dump(PrintWriter pw, String prefix) {
855 pw.println(prefix + this);
856 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
857 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400858 pw.println(prefix + "mUid=" + mUid);
859 pw.println(prefix + "mLastFixBroadcast:");
860 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
861 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
863
864 /**
865 * Calls dispose().
866 */
867 @Override protected void finalize() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400868 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 disposeLocked();
870 }
871 }
872 }
873
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400874 private Receiver getReceiver(ILocationListener listener) {
875 IBinder binder = listener.asBinder();
876 Receiver receiver = mReceivers.get(binder);
877 if (receiver == null) {
878 receiver = new Receiver(listener);
879 mReceivers.put(binder, receiver);
880
881 try {
882 if (receiver.isListener()) {
883 receiver.getListener().asBinder().linkToDeath(receiver, 0);
884 }
885 } catch (RemoteException e) {
886 Log.e(TAG, "linkToDeath failed:", e);
887 return null;
888 }
889 }
890 return receiver;
891 }
892
893 private Receiver getReceiver(PendingIntent intent) {
894 Receiver receiver = mReceivers.get(intent);
895 if (receiver == null) {
896 receiver = new Receiver(intent);
897 mReceivers.put(intent, receiver);
898 }
899 return receiver;
900 }
901
902 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
903 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
904 if (records != null) {
905 for (int i = records.size() - 1; i >= 0; i--) {
906 UpdateRecord record = records.get(i);
907 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
908 return true;
909 }
910 }
911 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400912 for (ProximityAlert alert : mProximityAlerts.values()) {
913 if (alert.mUid == uid) {
914 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400915 }
916 }
917 return false;
918 }
919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public void requestLocationUpdates(String provider,
921 long minTime, float minDistance, ILocationListener listener) {
922
923 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400924 synchronized (mLock) {
925 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927 } catch (SecurityException se) {
928 throw se;
929 } catch (Exception e) {
930 Log.e(TAG, "requestUpdates got exception:", e);
931 }
932 }
933
934 public void requestLocationUpdatesPI(String provider,
935 long minTime, float minDistance, PendingIntent intent) {
936 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400937 synchronized (mLock) {
938 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940 } catch (SecurityException se) {
941 throw se;
942 } catch (Exception e) {
943 Log.e(TAG, "requestUpdates got exception:", e);
944 }
945 }
946
947 private void requestLocationUpdatesLocked(String provider,
948 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700949 if (LOCAL_LOGV) {
950 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400953 LocationProviderProxy proxy = mProvidersByName.get(provider);
954 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 throw new IllegalArgumentException("provider=" + provider);
956 }
957
958 checkPermissionsSafe(provider);
959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 // so wakelock calls will succeed
961 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400962 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 long identity = Binder.clearCallingIdentity();
964 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400965 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
966 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 if (oldRecord != null) {
968 oldRecord.disposeLocked();
969 }
970
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400971 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400972 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
976 if (isProviderEnabled) {
977 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400978 proxy.setMinTime(minTimeForProvider);
979 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700981 // Notify the listener that updates are currently disabled
982 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 } finally {
985 Binder.restoreCallingIdentity(identity);
986 }
987 }
988
989 public void removeUpdates(ILocationListener listener) {
990 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400991 synchronized (mLock) {
992 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 } catch (SecurityException se) {
995 throw se;
996 } catch (Exception e) {
997 Log.e(TAG, "removeUpdates got exception:", e);
998 }
999 }
1000
1001 public void removeUpdatesPI(PendingIntent intent) {
1002 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001003 synchronized (mLock) {
1004 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006 } catch (SecurityException se) {
1007 throw se;
1008 } catch (Exception e) {
1009 Log.e(TAG, "removeUpdates got exception:", e);
1010 }
1011 }
1012
1013 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001014 if (LOCAL_LOGV) {
1015 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
1017
1018 // so wakelock calls will succeed
1019 final int callingUid = Binder.getCallingUid();
1020 long identity = Binder.clearCallingIdentity();
1021 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001022 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1023 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025
1026 // Record which providers were associated with this listener
1027 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001028 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 if (oldRecords != null) {
1030 // Call dispose() on the obsolete update records.
1031 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001032 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001033 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1034 if (proxy != null) {
1035 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037 }
1038 record.disposeLocked();
1039 }
1040 // Accumulate providers
1041 providers.addAll(oldRecords.keySet());
1042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043
1044 // See if the providers associated with this listener have any
1045 // other listeners; if one does, inform it of the new smallest minTime
1046 // value; if one does not, disable location tracking for it
1047 for (String provider : providers) {
1048 // If provider is already disabled, don't need to do anything
1049 if (!isAllowedBySettingsLocked(provider)) {
1050 continue;
1051 }
1052
1053 boolean hasOtherListener = false;
1054 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1055 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1056 hasOtherListener = true;
1057 }
1058
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001059 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 if (p != null) {
1061 if (hasOtherListener) {
1062 p.setMinTime(getMinTimeLocked(provider));
1063 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 p.enableLocationTracking(false);
1065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 } finally {
1069 Binder.restoreCallingIdentity(identity);
1070 }
1071 }
1072
1073 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001074 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 return false;
1076 }
1077 if (mContext.checkCallingPermission(ACCESS_FINE_LOCATION) !=
1078 PackageManager.PERMISSION_GRANTED) {
1079 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1080 }
1081
1082 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001083 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001085 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 return false;
1087 }
1088 return true;
1089 }
1090
1091 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001092 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001093 try {
1094 mGpsStatusProvider.removeGpsStatusListener(listener);
1095 } catch (Exception e) {
1096 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099 }
1100
1101 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
1102 // first check for permission to the provider
1103 checkPermissionsSafe(provider);
1104 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
1105 if ((mContext.checkCallingPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
1106 != PackageManager.PERMISSION_GRANTED)) {
1107 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1108 }
1109
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001110 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001111 LocationProviderProxy proxy = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (provider == null) {
1113 return false;
1114 }
1115
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001116 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118 }
1119
1120 class ProximityAlert {
1121 final int mUid;
1122 final double mLatitude;
1123 final double mLongitude;
1124 final float mRadius;
1125 final long mExpiration;
1126 final PendingIntent mIntent;
1127 final Location mLocation;
1128
1129 public ProximityAlert(int uid, double latitude, double longitude,
1130 float radius, long expiration, PendingIntent intent) {
1131 mUid = uid;
1132 mLatitude = latitude;
1133 mLongitude = longitude;
1134 mRadius = radius;
1135 mExpiration = expiration;
1136 mIntent = intent;
1137
1138 mLocation = new Location("");
1139 mLocation.setLatitude(latitude);
1140 mLocation.setLongitude(longitude);
1141 }
1142
1143 long getExpiration() {
1144 return mExpiration;
1145 }
1146
1147 PendingIntent getIntent() {
1148 return mIntent;
1149 }
1150
1151 boolean isInProximity(double latitude, double longitude) {
1152 Location loc = new Location("");
1153 loc.setLatitude(latitude);
1154 loc.setLongitude(longitude);
1155
1156 double radius = loc.distanceTo(mLocation);
1157 return radius <= mRadius;
1158 }
1159
1160 @Override
1161 public String toString() {
1162 return "ProximityAlert{"
1163 + Integer.toHexString(System.identityHashCode(this))
1164 + " uid " + mUid + mIntent + "}";
1165 }
1166
1167 void dump(PrintWriter pw, String prefix) {
1168 pw.println(prefix + this);
1169 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1170 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1171 pw.println(prefix + "mIntent=" + mIntent);
1172 pw.println(prefix + "mLocation:");
1173 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1174 }
1175 }
1176
1177 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001178 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179
1180 boolean isGpsAvailable = false;
1181
1182 // Note: this is called with the lock held.
1183 public void onLocationChanged(Location loc) {
1184
1185 // If Gps is available, then ignore updates from NetworkLocationProvider
1186 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1187 isGpsAvailable = true;
1188 }
1189 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1190 return;
1191 }
1192
1193 // Process proximity alerts
1194 long now = System.currentTimeMillis();
1195 double latitude = loc.getLatitude();
1196 double longitude = loc.getLongitude();
1197 ArrayList<PendingIntent> intentsToRemove = null;
1198
1199 for (ProximityAlert alert : mProximityAlerts.values()) {
1200 PendingIntent intent = alert.getIntent();
1201 long expiration = alert.getExpiration();
1202
1203 if ((expiration == -1) || (now <= expiration)) {
1204 boolean entered = mProximitiesEntered.contains(alert);
1205 boolean inProximity =
1206 alert.isInProximity(latitude, longitude);
1207 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001208 if (LOCAL_LOGV) {
1209 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 mProximitiesEntered.add(alert);
1212 Intent enteredIntent = new Intent();
1213 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1214 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001215 synchronized (this) {
1216 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001217 // is called before decrementPendingBroadcasts()
1218 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1219 // call this after broadcasting so we do not increment
1220 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001221 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001224 if (LOCAL_LOGV) {
1225 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 if (intentsToRemove == null) {
1228 intentsToRemove = new ArrayList<PendingIntent>();
1229 }
1230 intentsToRemove.add(intent);
1231 }
1232 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001233 if (LOCAL_LOGV) {
1234 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 mProximitiesEntered.remove(alert);
1237 Intent exitedIntent = new Intent();
1238 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1239 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001240 synchronized (this) {
1241 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001242 // is called before decrementPendingBroadcasts()
1243 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1244 // call this after broadcasting so we do not increment
1245 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001246 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001249 if (LOCAL_LOGV) {
1250 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252 if (intentsToRemove == null) {
1253 intentsToRemove = new ArrayList<PendingIntent>();
1254 }
1255 intentsToRemove.add(intent);
1256 }
1257 }
1258 } else {
1259 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001260 if (LOCAL_LOGV) {
1261 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 if (intentsToRemove == null) {
1264 intentsToRemove = new ArrayList<PendingIntent>();
1265 }
1266 intentsToRemove.add(alert.getIntent());
1267 }
1268 }
1269
1270 // Remove expired alerts
1271 if (intentsToRemove != null) {
1272 for (PendingIntent i : intentsToRemove) {
1273 mProximityAlerts.remove(i);
1274 ProximityAlert alert = mProximityAlerts.get(i);
1275 mProximitiesEntered.remove(alert);
1276 }
1277 }
1278
1279 }
1280
1281 // Note: this is called with the lock held.
1282 public void onProviderDisabled(String provider) {
1283 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1284 isGpsAvailable = false;
1285 }
1286 }
1287
1288 // Note: this is called with the lock held.
1289 public void onProviderEnabled(String provider) {
1290 // ignore
1291 }
1292
1293 // Note: this is called with the lock held.
1294 public void onStatusChanged(String provider, int status, Bundle extras) {
1295 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1296 (status != LocationProvider.AVAILABLE)) {
1297 isGpsAvailable = false;
1298 }
1299 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001300
1301 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1302 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001303 // synchronize to ensure incrementPendingBroadcasts()
1304 // is called before decrementPendingBroadcasts()
1305 synchronized (this) {
1306 decrementPendingBroadcasts();
1307 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310
1311 public void addProximityAlert(double latitude, double longitude,
1312 float radius, long expiration, PendingIntent intent) {
1313 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001314 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1316 }
1317 } catch (SecurityException se) {
1318 throw se;
1319 } catch (Exception e) {
1320 Log.e(TAG, "addProximityAlert got exception:", e);
1321 }
1322 }
1323
1324 private void addProximityAlertLocked(double latitude, double longitude,
1325 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001326 if (LOCAL_LOGV) {
1327 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 ", longitude = " + longitude +
1329 ", expiration = " + expiration +
1330 ", intent = " + intent);
1331 }
1332
1333 // Require ability to access all providers for now
1334 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1335 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1336 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1337 }
1338
1339 if (expiration != -1) {
1340 expiration += System.currentTimeMillis();
1341 }
1342 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1343 latitude, longitude, radius, expiration, intent);
1344 mProximityAlerts.put(intent, alert);
1345
Mike Lockwood48f17512009-04-23 09:12:08 -07001346 if (mProximityReceiver == null) {
1347 mProximityListener = new ProximityListener();
1348 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
Mike Lockwood95427cd2009-05-07 13:27:54 -04001350 for (int i = mProviders.size() - 1; i >= 0; i--) {
1351 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001352 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355 }
1356
1357 public void removeProximityAlert(PendingIntent intent) {
1358 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001359 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 removeProximityAlertLocked(intent);
1361 }
1362 } catch (SecurityException se) {
1363 throw se;
1364 } catch (Exception e) {
1365 Log.e(TAG, "removeProximityAlert got exception:", e);
1366 }
1367 }
1368
1369 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001370 if (LOCAL_LOGV) {
1371 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373
1374 mProximityAlerts.remove(intent);
1375 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001376 removeUpdatesLocked(mProximityReceiver);
1377 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380 }
1381
1382 /**
1383 * @return null if the provider does not exits
1384 * @throw SecurityException if the provider is not allowed to be
1385 * accessed by the caller
1386 */
1387 public Bundle getProviderInfo(String provider) {
1388 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001389 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 return _getProviderInfoLocked(provider);
1391 }
1392 } catch (SecurityException se) {
1393 throw se;
1394 } catch (Exception e) {
1395 Log.e(TAG, "_getProviderInfo got exception:", e);
1396 return null;
1397 }
1398 }
1399
1400 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001401 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 if (p == null) {
1403 return null;
1404 }
1405
1406 checkPermissionsSafe(provider);
1407
1408 Bundle b = new Bundle();
1409 b.putBoolean("network", p.requiresNetwork());
1410 b.putBoolean("satellite", p.requiresSatellite());
1411 b.putBoolean("cell", p.requiresCell());
1412 b.putBoolean("cost", p.hasMonetaryCost());
1413 b.putBoolean("altitude", p.supportsAltitude());
1414 b.putBoolean("speed", p.supportsSpeed());
1415 b.putBoolean("bearing", p.supportsBearing());
1416 b.putInt("power", p.getPowerRequirement());
1417 b.putInt("accuracy", p.getAccuracy());
1418
1419 return b;
1420 }
1421
1422 public boolean isProviderEnabled(String provider) {
1423 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001424 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 return _isProviderEnabledLocked(provider);
1426 }
1427 } catch (SecurityException se) {
1428 throw se;
1429 } catch (Exception e) {
1430 Log.e(TAG, "isProviderEnabled got exception:", e);
1431 return false;
1432 }
1433 }
1434
Mike Lockwood275555c2009-05-01 11:30:34 -04001435 public void reportLocation(Location location) {
1436 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1437 != PackageManager.PERMISSION_GRANTED) {
1438 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1439 }
1440
Mike Lockwood4e50b782009-04-03 08:24:43 -07001441 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1442 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1443 mLocationHandler.sendMessageAtFrontOfQueue(m);
1444 }
1445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 private boolean _isProviderEnabledLocked(String provider) {
1447 checkPermissionsSafe(provider);
1448
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001449 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 if (p == null) {
1451 throw new IllegalArgumentException("provider=" + provider);
1452 }
1453 return isAllowedBySettingsLocked(provider);
1454 }
1455
1456 public Location getLastKnownLocation(String provider) {
1457 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001458 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 return _getLastKnownLocationLocked(provider);
1460 }
1461 } catch (SecurityException se) {
1462 throw se;
1463 } catch (Exception e) {
1464 Log.e(TAG, "getLastKnownLocation got exception:", e);
1465 return null;
1466 }
1467 }
1468
1469 private Location _getLastKnownLocationLocked(String provider) {
1470 checkPermissionsSafe(provider);
1471
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001472 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (p == null) {
1474 throw new IllegalArgumentException("provider=" + provider);
1475 }
1476
1477 if (!isAllowedBySettingsLocked(provider)) {
1478 return null;
1479 }
1480
1481 Location location = mLastKnownLocation.get(provider);
1482 if (location == null) {
1483 // Get the persistent last known location for the provider
1484 location = readLastKnownLocationLocked(provider);
1485 if (location != null) {
1486 mLastKnownLocation.put(provider, location);
1487 }
1488 }
1489
1490 return location;
1491 }
1492
1493 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1494 // Always broadcast the first update
1495 if (lastLoc == null) {
1496 return true;
1497 }
1498
1499 // Don't broadcast same location again regardless of condition
1500 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1501 if (loc.getTime() == lastLoc.getTime()) {
1502 return false;
1503 }
1504
1505 // Check whether sufficient distance has been traveled
1506 double minDistance = record.mMinDistance;
1507 if (minDistance > 0.0) {
1508 if (loc.distanceTo(lastLoc) <= minDistance) {
1509 return false;
1510 }
1511 }
1512
1513 return true;
1514 }
1515
Mike Lockwood4e50b782009-04-03 08:24:43 -07001516 private void handleLocationChangedLocked(Location location) {
1517 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1519 if (records == null || records.size() == 0) {
1520 return;
1521 }
1522
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001523 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 if (p == null) {
1525 return;
1526 }
1527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001529 Location lastLocation = mLastKnownLocation.get(provider);
1530 if (lastLocation == null) {
1531 mLastKnownLocation.put(provider, new Location(location));
1532 } else {
1533 lastLocation.set(location);
1534 }
1535 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 // Fetch latest status update time
1538 long newStatusUpdateTime = p.getStatusUpdateTime();
1539
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001540 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 Bundle extras = new Bundle();
1542 int status = p.getStatus(extras);
1543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 ArrayList<Receiver> deadReceivers = null;
1545
1546 // Broadcast location or status to all listeners
1547 final int N = records.size();
1548 for (int i=0; i<N; i++) {
1549 UpdateRecord r = records.get(i);
1550 Receiver receiver = r.mReceiver;
1551
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001552 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001553 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1554 if (lastLoc == null) {
1555 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001556 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001557 } else {
1558 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001560 if (!receiver.callLocationChangedLocked(location)) {
1561 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1562 if (deadReceivers == null) {
1563 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001565 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
1567 }
1568
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001569 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1571 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1572
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001573 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1575 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1576 if (deadReceivers == null) {
1577 deadReceivers = new ArrayList<Receiver>();
1578 }
1579 if (!deadReceivers.contains(receiver)) {
1580 deadReceivers.add(receiver);
1581 }
1582 }
1583 }
1584 }
1585
1586 if (deadReceivers != null) {
1587 for (int i=deadReceivers.size()-1; i>=0; i--) {
1588 removeUpdatesLocked(deadReceivers.get(i));
1589 }
1590 }
1591 }
1592
1593 private class LocationWorkerHandler extends Handler {
1594
1595 @Override
1596 public void handleMessage(Message msg) {
1597 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001598 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1599 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001601 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001602 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001603 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001604
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001605 // notify other providers of the new location
1606 for (int i = mProviders.size() - 1; i >= 0; i--) {
1607 LocationProviderProxy proxy = mProviders.get(i);
1608 if (!provider.equals(proxy.getName())) {
1609 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001610 }
1611 }
1612
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001613 if (isAllowedBySettingsLocked(provider)) {
1614 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618 } catch (Exception e) {
1619 // Log, don't crash!
1620 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1621 }
1622 }
1623 }
1624
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001625 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1626 @Override
1627 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 String action = intent.getAction();
1629
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001630 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001632 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1634 if (uid >= 0) {
1635 ArrayList<Receiver> removedRecs = null;
1636 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1637 for (int j=i.size()-1; j>=0; j--) {
1638 UpdateRecord ur = i.get(j);
1639 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1640 if (removedRecs == null) {
1641 removedRecs = new ArrayList<Receiver>();
1642 }
1643 if (!removedRecs.contains(ur.mReceiver)) {
1644 removedRecs.add(ur.mReceiver);
1645 }
1646 }
1647 }
1648 }
1649 ArrayList<ProximityAlert> removedAlerts = null;
1650 for (ProximityAlert i : mProximityAlerts.values()) {
1651 if (i.mUid == uid) {
1652 if (removedAlerts == null) {
1653 removedAlerts = new ArrayList<ProximityAlert>();
1654 }
1655 if (!removedAlerts.contains(i)) {
1656 removedAlerts.add(i);
1657 }
1658 }
1659 }
1660 if (removedRecs != null) {
1661 for (int i=removedRecs.size()-1; i>=0; i--) {
1662 removeUpdatesLocked(removedRecs.get(i));
1663 }
1664 }
1665 if (removedAlerts != null) {
1666 for (int i=removedAlerts.size()-1; i>=0; i--) {
1667 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1668 }
1669 }
1670 }
1671 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001672 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 boolean noConnectivity =
1674 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1675 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001676 mNetworkState = LocationProvider.AVAILABLE;
1677 } else {
1678 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680
1681 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001682 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001683 for (int i = mProviders.size() - 1; i >= 0; i--) {
1684 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001686 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
1688 }
1689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001692 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693
1694 // Wake locks
1695
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001696 private void incrementPendingBroadcasts() {
1697 synchronized (mWakeLock) {
1698 if (mPendingBroadcasts++ == 0) {
1699 try {
1700 mWakeLock.acquire();
1701 log("Acquired wakelock");
1702 } catch (Exception e) {
1703 // This is to catch a runtime exception thrown when we try to release an
1704 // already released lock.
1705 Log.e(TAG, "exception in acquireWakeLock()", e);
1706 }
1707 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001708 }
1709 }
1710
1711 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001712 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001713 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001714 try {
1715 // Release wake lock
1716 if (mWakeLock.isHeld()) {
1717 mWakeLock.release();
1718 log("Released wakelock");
1719 } else {
1720 log("Can't release wakelock again!");
1721 }
1722 } catch (Exception e) {
1723 // This is to catch a runtime exception thrown when we try to release an
1724 // already released lock.
1725 Log.e(TAG, "exception in releaseWakeLock()", e);
1726 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001727 }
1728 }
1729 }
1730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 // Geocoder
1732
1733 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001734 String language, String country, String variant, String appName, List<Address> addrs) {
1735 if (mGeocodeProvider != null) {
1736 try {
1737 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1738 variant, appName, addrs);
1739 } catch (RemoteException e) {
1740 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001741 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001744 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
1746
Mike Lockwooda55c3212009-04-15 11:10:11 -04001747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001749 double lowerLeftLatitude, double lowerLeftLongitude,
1750 double upperRightLatitude, double upperRightLongitude, int maxResults,
1751 String language, String country, String variant, String appName, List<Address> addrs) {
1752
1753 if (mGeocodeProvider != null) {
1754 try {
1755 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1756 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1757 maxResults, language, country, variant, appName, addrs);
1758 } catch (RemoteException e) {
1759 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001760 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
1762 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001763 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765
1766 // Mock Providers
1767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 private void checkMockPermissionsSafe() {
1769 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1770 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1771 if (!allowMocks) {
1772 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1773 }
1774
1775 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1776 PackageManager.PERMISSION_GRANTED) {
1777 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1778 }
1779 }
1780
1781 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1782 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1783 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1784 checkMockPermissionsSafe();
1785
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001786 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001787 MockProvider provider = new MockProvider(name, this,
1788 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 requiresCell, hasMonetaryCost, supportsAltitude,
1790 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001791 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1793 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001794
1795 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1796 long identity = Binder.clearCallingIdentity();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001797 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001798 mMockProviders.put(name, provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 updateProvidersLocked();
Mike Lockwood95427cd2009-05-07 13:27:54 -04001800 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802 }
1803
1804 public void removeTestProvider(String provider) {
1805 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001806 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001807 MockProvider mockProvider = mMockProviders.get(provider);
1808 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1810 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001811 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001812 mMockProviders.remove(mockProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 updateProvidersLocked();
1814 }
1815 }
1816
1817 public void setTestProviderLocation(String provider, Location loc) {
1818 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001819 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001820 MockProvider mockProvider = mMockProviders.get(provider);
1821 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1823 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001824 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1825 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001826 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001827 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 }
1829 }
1830
1831 public void clearTestProviderLocation(String provider) {
1832 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001833 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001834 MockProvider mockProvider = mMockProviders.get(provider);
1835 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1837 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001838 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840 }
1841
1842 public void setTestProviderEnabled(String provider, boolean enabled) {
1843 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001844 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001845 MockProvider mockProvider = mMockProviders.get(provider);
1846 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1848 }
1849 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001850 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 mEnabledProviders.add(provider);
1852 mDisabledProviders.remove(provider);
1853 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001854 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 mEnabledProviders.remove(provider);
1856 mDisabledProviders.add(provider);
1857 }
1858 updateProvidersLocked();
1859 }
1860 }
1861
1862 public void clearTestProviderEnabled(String provider) {
1863 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001864 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001865 MockProvider mockProvider = mMockProviders.get(provider);
1866 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1868 }
1869 mEnabledProviders.remove(provider);
1870 mDisabledProviders.remove(provider);
1871 updateProvidersLocked();
1872 }
1873 }
1874
1875 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1876 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001877 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001878 MockProvider mockProvider = mMockProviders.get(provider);
1879 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1881 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001882 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
1884 }
1885
1886 public void clearTestProviderStatus(String provider) {
1887 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001888 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001889 MockProvider mockProvider = mMockProviders.get(provider);
1890 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1892 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001893 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 }
1895 }
1896
1897 private void log(String log) {
1898 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1899 Log.d(TAG, log);
1900 }
1901 }
1902
1903 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1904 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1905 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001906 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 + Binder.getCallingPid()
1908 + ", uid=" + Binder.getCallingUid());
1909 return;
1910 }
1911
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001912 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 pw.println("Current Location Manager state:");
1914 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001916 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001918 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
1920 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001921 for (Receiver i : mReceivers.values()) {
1922 pw.println(" " + i + ":");
1923 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 pw.println(" " + j.getKey() + ":");
1925 j.getValue().dump(pw, " ");
1926 }
1927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 pw.println(" Records by Provider:");
1929 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1930 : mRecordsByProvider.entrySet()) {
1931 pw.println(" " + i.getKey() + ":");
1932 for (UpdateRecord j : i.getValue()) {
1933 pw.println(" " + j + ":");
1934 j.dump(pw, " ");
1935 }
1936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 pw.println(" Last Known Locations:");
1938 for (Map.Entry<String, Location> i
1939 : mLastKnownLocation.entrySet()) {
1940 pw.println(" " + i.getKey() + ":");
1941 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1942 }
1943 if (mProximityAlerts.size() > 0) {
1944 pw.println(" Proximity Alerts:");
1945 for (Map.Entry<PendingIntent, ProximityAlert> i
1946 : mProximityAlerts.entrySet()) {
1947 pw.println(" " + i.getKey() + ":");
1948 i.getValue().dump(pw, " ");
1949 }
1950 }
1951 if (mProximitiesEntered.size() > 0) {
1952 pw.println(" Proximities Entered:");
1953 for (ProximityAlert i : mProximitiesEntered) {
1954 pw.println(" " + i + ":");
1955 i.dump(pw, " ");
1956 }
1957 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001958 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 pw.println(" mProximityListener=" + mProximityListener);
1960 if (mEnabledProviders.size() > 0) {
1961 pw.println(" Enabled Providers:");
1962 for (String i : mEnabledProviders) {
1963 pw.println(" " + i);
1964 }
1965
1966 }
1967 if (mDisabledProviders.size() > 0) {
1968 pw.println(" Disabled Providers:");
1969 for (String i : mDisabledProviders) {
1970 pw.println(" " + i);
1971 }
1972
1973 }
1974 if (mMockProviders.size() > 0) {
1975 pw.println(" Mock Providers:");
1976 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001977 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
1981 }
1982}