blob: 14c834b722dad386c4430d7859cfb65b7c6e00e8 [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;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070049import android.location.ILocationCollector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.location.ILocationListener;
51import android.location.ILocationManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070052import android.location.ILocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.location.Location;
54import android.location.LocationManager;
55import android.location.LocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.net.ConnectivityManager;
57import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Binder;
59import android.os.Bundle;
60import android.os.Handler;
61import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070062import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Message;
64import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070065import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.RemoteException;
67import android.os.SystemClock;
68import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.Config;
70import android.util.Log;
71import android.util.PrintWriterPrinter;
72import android.util.SparseIntArray;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import com.android.internal.location.GpsLocationProvider;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070075import com.android.internal.location.LocationProviderProxy;
Mike Lockwood7ec434e2009-03-27 07:46:48 -070076import com.android.internal.location.MockProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import com.android.server.am.BatteryStatsService;
78
79/**
80 * The service class that manages LocationProviders and issues location
81 * updates and alerts.
82 *
83 * {@hide}
84 */
Mike Lockwood3d12b512009-04-21 23:25:35 -070085public class LocationManagerService extends ILocationManager.Stub implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String TAG = "LocationManagerService";
The Android Open Source Project10592532009-03-18 17:39:46 -070087 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89 // Minimum time interval between last known location writes, in milliseconds.
90 private static final long MIN_LAST_KNOWN_LOCATION_TIME = 60L * 1000L;
91
92 // Max time to hold wake lock for, in milliseconds.
93 private static final long MAX_TIME_FOR_WAKE_LOCK = 60 * 1000L;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 // The last time a location was written, by provider name.
96 private HashMap<String,Long> mLastWriteTime = new HashMap<String,Long>();
97
98 private static final Pattern PATTERN_COMMA = Pattern.compile(",");
99
100 private static final String ACCESS_FINE_LOCATION =
101 android.Manifest.permission.ACCESS_FINE_LOCATION;
102 private static final String ACCESS_COARSE_LOCATION =
103 android.Manifest.permission.ACCESS_COARSE_LOCATION;
104 private static final String ACCESS_MOCK_LOCATION =
105 android.Manifest.permission.ACCESS_MOCK_LOCATION;
106 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
107 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400108 private static final String INSTALL_LOCATION_PROVIDER =
109 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
110 private static final String INSTALL_LOCATION_COLLECTOR =
111 android.Manifest.permission.INSTALL_LOCATION_COLLECTOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 // Set of providers that are explicitly enabled
114 private final Set<String> mEnabledProviders = new HashSet<String>();
115
116 // Set of providers that are explicitly disabled
117 private final Set<String> mDisabledProviders = new HashSet<String>();
118
119 // Locations, status values, and extras for mock providers
Mike Lockwood7ec434e2009-03-27 07:46:48 -0700120 private final HashMap<String,MockProvider> mMockProviders = new HashMap<String,MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 private static boolean sProvidersLoaded = false;
123
124 private final Context mContext;
Mike Lockwooda55c3212009-04-15 11:10:11 -0400125 private IGeocodeProvider mGeocodeProvider;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400126 private IGpsStatusProvider mGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 private LocationWorkerHandler mLocationHandler;
128
129 // Handler messages
Mike Lockwood4e50b782009-04-03 08:24:43 -0700130 private static final int MESSAGE_LOCATION_CHANGED = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400132 // wakelock variables
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 private final static String WAKELOCK_KEY = "LocationManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private PowerManager.WakeLock mWakeLock = null;
Mike Lockwood48f17512009-04-23 09:12:08 -0700135 private int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400138 * List of all receivers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400140 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400142
143 /**
144 * List of location providers.
145 */
146 private final ArrayList<LocationProviderProxy> mProviders =
147 new ArrayList<LocationProviderProxy>();
148 private final HashMap<String, LocationProviderProxy> mProvidersByName
149 = new HashMap<String, LocationProviderProxy>();
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400152 * Object used internally for synchronization
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400154 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 /**
157 * Mapping from provider name to all its UpdateRecords
158 */
159 private final HashMap<String,ArrayList<UpdateRecord>> mRecordsByProvider =
160 new HashMap<String,ArrayList<UpdateRecord>>();
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 // Proximity listeners
Mike Lockwood48f17512009-04-23 09:12:08 -0700163 private Receiver mProximityReceiver = null;
164 private ILocationListener mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private HashMap<PendingIntent,ProximityAlert> mProximityAlerts =
166 new HashMap<PendingIntent,ProximityAlert>();
167 private HashSet<ProximityAlert> mProximitiesEntered =
168 new HashSet<ProximityAlert>();
169
170 // Last known location for each provider
171 private HashMap<String,Location> mLastKnownLocation =
172 new HashMap<String,Location>();
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // Location collector
175 private ILocationCollector mCollector;
176
The Android Open Source Project4df24232009-03-05 14:34:35 -0800177 private int mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800178
Mike Lockwood9637d472009-04-02 21:41:57 -0700179 // for Settings change notification
180 private ContentQueryMap mSettings;
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
183 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
184 * location updates.
185 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700186 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final ILocationListener mListener;
188 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 final Object mKey;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400190 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Mike Lockwood48f17512009-04-23 09:12:08 -0700191 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400193 Receiver(ILocationListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 mListener = listener;
195 mPendingIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 mKey = listener.asBinder();
197 }
198
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400199 Receiver(PendingIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mPendingIntent = intent;
201 mListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 mKey = intent;
203 }
204
205 @Override
206 public boolean equals(Object otherObj) {
207 if (otherObj instanceof Receiver) {
208 return mKey.equals(
209 ((Receiver)otherObj).mKey);
210 }
211 return false;
212 }
213
214 @Override
215 public int hashCode() {
216 return mKey.hashCode();
217 }
218
219
220 @Override
221 public String toString() {
222 if (mListener != null) {
223 return "Receiver{"
224 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400225 + " Listener " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 } else {
227 return "Receiver{"
228 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400229 + " Intent " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231 }
232
233 public boolean isListener() {
234 return mListener != null;
235 }
236
237 public boolean isPendingIntent() {
238 return mPendingIntent != null;
239 }
240
241 public ILocationListener getListener() {
242 if (mListener != null) {
243 return mListener;
244 }
245 throw new IllegalStateException("Request for non-existent listener");
246 }
247
248 public PendingIntent getPendingIntent() {
249 if (mPendingIntent != null) {
250 return mPendingIntent;
251 }
252 throw new IllegalStateException("Request for non-existent intent");
253 }
254
255 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
256 if (mListener != null) {
257 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700258 synchronized (this) {
259 // synchronize to ensure incrementPendingBroadcastsLocked()
260 // is called before decrementPendingBroadcasts()
261 mListener.onStatusChanged(provider, status, extras);
262 if (mListener != mProximityListener) {
263 // call this after broadcasting so we do not increment
264 // if we throw an exeption.
265 incrementPendingBroadcastsLocked();
266 }
267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 } catch (RemoteException e) {
269 return false;
270 }
271 } else {
272 Intent statusChanged = new Intent();
273 statusChanged.putExtras(extras);
274 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
275 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700276 synchronized (this) {
277 // synchronize to ensure incrementPendingBroadcastsLocked()
278 // is called before decrementPendingBroadcasts()
279 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler);
280 // call this after broadcasting so we do not increment
281 // if we throw an exeption.
282 incrementPendingBroadcastsLocked();
283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 } catch (PendingIntent.CanceledException e) {
285 return false;
286 }
287 }
288 return true;
289 }
290
291 public boolean callLocationChangedLocked(Location location) {
292 if (mListener != null) {
293 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700294 synchronized (this) {
295 // synchronize to ensure incrementPendingBroadcastsLocked()
296 // is called before decrementPendingBroadcasts()
297 mListener.onLocationChanged(location);
298 if (mListener != mProximityListener) {
299 // call this after broadcasting so we do not increment
300 // if we throw an exeption.
301 incrementPendingBroadcastsLocked();
302 }
303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 } catch (RemoteException e) {
305 return false;
306 }
307 } else {
308 Intent locationChanged = new Intent();
309 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
310 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700311 synchronized (this) {
312 // synchronize to ensure incrementPendingBroadcastsLocked()
313 // is called before decrementPendingBroadcasts()
314 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler);
315 // call this after broadcasting so we do not increment
316 // if we throw an exeption.
317 incrementPendingBroadcastsLocked();
318 }
319 } catch (PendingIntent.CanceledException e) {
320 return false;
321 }
322 }
323 return true;
324 }
325
326 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
327 if (mListener != null) {
328 try {
329 synchronized (this) {
330 // synchronize to ensure incrementPendingBroadcastsLocked()
331 // is called before decrementPendingBroadcasts()
332 if (enabled) {
333 mListener.onProviderEnabled(provider);
334 } else {
335 mListener.onProviderDisabled(provider);
336 }
337 if (mListener != mProximityListener) {
338 // call this after broadcasting so we do not increment
339 // if we throw an exeption.
340 incrementPendingBroadcastsLocked();
341 }
342 }
343 } catch (RemoteException e) {
344 return false;
345 }
346 } else {
347 Intent providerIntent = new Intent();
348 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
349 try {
350 synchronized (this) {
351 // synchronize to ensure incrementPendingBroadcastsLocked()
352 // is called before decrementPendingBroadcasts()
353 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler);
354 // call this after broadcasting so we do not increment
355 // if we throw an exeption.
356 incrementPendingBroadcastsLocked();
357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 } catch (PendingIntent.CanceledException e) {
359 return false;
360 }
361 }
362 return true;
363 }
364
365 public void binderDied() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700366 if (LOCAL_LOGV) {
367 Log.v(TAG, "Location listener died");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400369 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 removeUpdatesLocked(this);
371 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700372 synchronized (this) {
373 if (mPendingBroadcasts > 0) {
374 LocationManagerService.this.decrementPendingBroadcasts();
375 mPendingBroadcasts = 0;
376 }
377 }
378 }
379
380 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
381 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400382 synchronized (this) {
383 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700384 }
385 }
386
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400387 // this must be called while synchronized by caller in a synchronized block
388 // containing the sending of the broadcaset
389 private void incrementPendingBroadcastsLocked() {
390 if (mPendingBroadcasts++ == 0) {
391 LocationManagerService.this.incrementPendingBroadcasts();
392 }
393 }
394
395 private void decrementPendingBroadcastsLocked() {
396 if (--mPendingBroadcasts == 0) {
397 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700398 }
399 }
400 }
401
402 public void locationCallbackFinished(ILocationListener listener) {
403 Receiver receiver = getReceiver(listener);
404 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400405 synchronized (receiver) {
406 // so wakelock calls will succeed
407 long identity = Binder.clearCallingIdentity();
408 receiver.decrementPendingBroadcastsLocked();
409 Binder.restoreCallingIdentity(identity);
410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412 }
413
Mike Lockwood9637d472009-04-02 21:41:57 -0700414 private final class SettingsObserver implements Observer {
415 public void update(Observable o, Object arg) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400416 synchronized (mLock) {
Mike Lockwood9637d472009-04-02 21:41:57 -0700417 updateProvidersLocked();
418 }
419 }
420 }
421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 private Location readLastKnownLocationLocked(String provider) {
423 Location location = null;
424 String s = null;
425 try {
426 File f = new File(LocationManager.SYSTEM_DIR + "/location."
427 + provider);
428 if (!f.exists()) {
429 return null;
430 }
431 BufferedReader reader = new BufferedReader(new FileReader(f), 256);
432 s = reader.readLine();
433 } catch (IOException e) {
434 Log.w(TAG, "Unable to read last known location", e);
435 }
436
437 if (s == null) {
438 return null;
439 }
440 try {
441 String[] tokens = PATTERN_COMMA.split(s);
442 int idx = 0;
443 long time = Long.parseLong(tokens[idx++]);
444 double latitude = Double.parseDouble(tokens[idx++]);
445 double longitude = Double.parseDouble(tokens[idx++]);
446 double altitude = Double.parseDouble(tokens[idx++]);
447 float bearing = Float.parseFloat(tokens[idx++]);
448 float speed = Float.parseFloat(tokens[idx++]);
449
450 location = new Location(provider);
451 location.setTime(time);
452 location.setLatitude(latitude);
453 location.setLongitude(longitude);
454 location.setAltitude(altitude);
455 location.setBearing(bearing);
456 location.setSpeed(speed);
457 } catch (NumberFormatException nfe) {
458 Log.e(TAG, "NumberFormatException reading last known location", nfe);
459 return null;
460 }
461
462 return location;
463 }
464
465 private void writeLastKnownLocationLocked(String provider,
466 Location location) {
467 long now = SystemClock.elapsedRealtime();
468 Long last = mLastWriteTime.get(provider);
469 if ((last != null)
470 && (now - last.longValue() < MIN_LAST_KNOWN_LOCATION_TIME)) {
471 return;
472 }
473 mLastWriteTime.put(provider, now);
474
475 StringBuilder sb = new StringBuilder(100);
476 sb.append(location.getTime());
477 sb.append(',');
478 sb.append(location.getLatitude());
479 sb.append(',');
480 sb.append(location.getLongitude());
481 sb.append(',');
482 sb.append(location.getAltitude());
483 sb.append(',');
484 sb.append(location.getBearing());
485 sb.append(',');
486 sb.append(location.getSpeed());
487
488 FileWriter writer = null;
489 try {
490 File d = new File(LocationManager.SYSTEM_DIR);
491 if (!d.exists()) {
492 if (!d.mkdirs()) {
493 Log.w(TAG, "Unable to create directory to write location");
494 return;
495 }
496 }
497 File f = new File(LocationManager.SYSTEM_DIR + "/location." + provider);
498 writer = new FileWriter(f);
499 writer.write(sb.toString());
500 } catch (IOException e) {
501 Log.w(TAG, "Unable to write location", e);
502 } finally {
503 if (writer != null) {
504 try {
505 writer.close();
506 } catch (IOException e) {
507 Log.w(TAG, "Exception closing file", e);
508 }
509 }
510 }
511 }
512
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400513 private void addProvider(LocationProviderProxy provider) {
514 mProviders.add(provider);
515 mProvidersByName.put(provider.getName(), provider);
516 }
517
518 private void removeProvider(LocationProviderProxy provider) {
519 mProviders.remove(provider);
520 mProvidersByName.remove(provider.getName());
521 }
522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400524 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 if (sProvidersLoaded) {
526 return;
527 }
528
529 // Load providers
530 loadProvidersLocked();
531 sProvidersLoaded = true;
532 }
533 }
534
535 private void loadProvidersLocked() {
536 try {
537 _loadProvidersLocked();
538 } catch (Exception e) {
539 Log.e(TAG, "Exception loading providers:", e);
540 }
541 }
542
543 private void _loadProvidersLocked() {
544 // Attempt to load "real" providers first
545 if (GpsLocationProvider.isSupported()) {
546 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400547 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
548 mGpsStatusProvider = provider.getGpsStatusProvider();
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400549 LocationProviderProxy proxy = new LocationProviderProxy(LocationManager.GPS_PROVIDER, provider);
550 addProvider(proxy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 updateProvidersLocked();
554 }
555
556 /**
557 * @param context the context that the LocationManagerService runs in
558 */
559 public LocationManagerService(Context context) {
560 super();
561 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700562
563 Thread thread = new Thread(null, this, "LocationManagerService");
564 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
The Android Open Source Project10592532009-03-18 17:39:46 -0700566 if (LOCAL_LOGV) {
567 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Mike Lockwood3d12b512009-04-21 23:25:35 -0700571 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 // Create a wake lock, needs to be done before calling loadProviders() below
573 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
574 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 // Load providers
577 loadProviders();
578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400581 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
582 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
584 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400585 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
Mike Lockwood9637d472009-04-02 21:41:57 -0700587 // listen for settings changes
588 ContentResolver resolver = mContext.getContentResolver();
589 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
590 "(" + Settings.System.NAME + "=?)",
591 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
592 null);
593 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
594 SettingsObserver settingsObserver = new SettingsObserver();
595 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597
Mike Lockwood3d12b512009-04-21 23:25:35 -0700598 public void run()
599 {
600 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
601 Looper.prepare();
602 mLocationHandler = new LocationWorkerHandler();
603 initialize();
604 Looper.loop();
605 }
606
Mike Lockwood275555c2009-05-01 11:30:34 -0400607 public void installLocationProvider(String name, ILocationProvider provider) {
608 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
609 != PackageManager.PERMISSION_GRANTED) {
610 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700611 }
612
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400613 synchronized (mLock) {
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400614 LocationProviderProxy proxy = new LocationProviderProxy(name, provider);
615 addProvider(proxy);
616 updateProvidersLocked();
Mike Lockwood275555c2009-05-01 11:30:34 -0400617
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400618 // notify provider of current network state
619 proxy.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
621 }
622
Mike Lockwood275555c2009-05-01 11:30:34 -0400623 public void installLocationCollector(ILocationCollector collector) {
624 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_COLLECTOR)
625 != PackageManager.PERMISSION_GRANTED) {
626 throw new SecurityException("Requires INSTALL_LOCATION_COLLECTOR permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700627 }
628
Mike Lockwood275555c2009-05-01 11:30:34 -0400629 // FIXME - only support one collector
Mike Lockwood98cb6672009-04-17 18:03:44 -0400630 mCollector = collector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
Mike Lockwood275555c2009-05-01 11:30:34 -0400633 public void installGeocodeProvider(IGeocodeProvider provider) {
634 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
635 != PackageManager.PERMISSION_GRANTED) {
636 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwooda55c3212009-04-15 11:10:11 -0400637 }
638
639 mGeocodeProvider = provider;
640 }
641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private boolean isAllowedBySettingsLocked(String provider) {
643 if (mEnabledProviders.contains(provider)) {
644 return true;
645 }
646 if (mDisabledProviders.contains(provider)) {
647 return false;
648 }
649 // Use system settings
650 ContentResolver resolver = mContext.getContentResolver();
651 String allowedProviders = Settings.Secure.getString(resolver,
652 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
653
654 return ((allowedProviders != null) && (allowedProviders.contains(provider)));
655 }
656
657 private void checkPermissionsSafe(String provider) {
658 if (LocationManager.GPS_PROVIDER.equals(provider)
659 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
660 != PackageManager.PERMISSION_GRANTED)) {
661 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
662 }
663 if (LocationManager.NETWORK_PROVIDER.equals(provider)
664 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
665 != PackageManager.PERMISSION_GRANTED)
666 && (mContext.checkCallingPermission(ACCESS_COARSE_LOCATION)
667 != PackageManager.PERMISSION_GRANTED)) {
668 throw new SecurityException(
669 "Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission");
670 }
671 }
672
673 private boolean isAllowedProviderSafe(String provider) {
674 if (LocationManager.GPS_PROVIDER.equals(provider)
675 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
676 != PackageManager.PERMISSION_GRANTED)) {
677 return false;
678 }
679 if (LocationManager.NETWORK_PROVIDER.equals(provider)
680 && (mContext.checkCallingPermission(ACCESS_FINE_LOCATION)
681 != PackageManager.PERMISSION_GRANTED)
682 && (mContext.checkCallingPermission(ACCESS_COARSE_LOCATION)
683 != PackageManager.PERMISSION_GRANTED)) {
684 return false;
685 }
686
687 return true;
688 }
689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 public List<String> getAllProviders() {
691 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400692 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 return _getAllProvidersLocked();
694 }
695 } catch (SecurityException se) {
696 throw se;
697 } catch (Exception e) {
698 Log.e(TAG, "getAllProviders got exception:", e);
699 return null;
700 }
701 }
702
703 private List<String> _getAllProvidersLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700704 if (LOCAL_LOGV) {
705 Log.v(TAG, "getAllProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400707 ArrayList<String> out = new ArrayList<String>(mProviders.size());
708 for (int i = mProviders.size() - 1; i >= 0; i--) {
709 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 out.add(p.getName());
711 }
712 return out;
713 }
714
715 public List<String> getProviders(boolean enabledOnly) {
716 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400717 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 return _getProvidersLocked(enabledOnly);
719 }
720 } catch (SecurityException se) {
721 throw se;
722 } catch (Exception e) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700723 Log.e(TAG, "getProviders got exception:", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 return null;
725 }
726 }
727
728 private List<String> _getProvidersLocked(boolean enabledOnly) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700729 if (LOCAL_LOGV) {
730 Log.v(TAG, "getProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400732 ArrayList<String> out = new ArrayList<String>(mProviders.size());
733 for (int i = mProviders.size() - 1; i >= 0; i--) {
734 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 String name = p.getName();
736 if (isAllowedProviderSafe(name)) {
737 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
738 continue;
739 }
740 out.add(name);
741 }
742 }
743 return out;
744 }
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 private void updateProvidersLocked() {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400747 for (int i = mProviders.size() - 1; i >= 0; i--) {
748 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 boolean isEnabled = p.isEnabled();
750 String name = p.getName();
751 boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 if (isEnabled && !shouldBeEnabled) {
754 updateProviderListenersLocked(name, false);
755 } else if (!isEnabled && shouldBeEnabled) {
756 updateProviderListenersLocked(name, true);
757 }
758
759 }
760 }
761
762 private void updateProviderListenersLocked(String provider, boolean enabled) {
763 int listeners = 0;
764
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400765 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 if (p == null) {
767 return;
768 }
769
770 ArrayList<Receiver> deadReceivers = null;
771
772 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
773 if (records != null) {
774 final int N = records.size();
775 for (int i=0; i<N; i++) {
776 UpdateRecord record = records.get(i);
777 // Sends a notification message to the receiver
Mike Lockwood48f17512009-04-23 09:12:08 -0700778 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
779 if (deadReceivers == null) {
780 deadReceivers = new ArrayList<Receiver>();
781 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784 listeners++;
785 }
786 }
787
788 if (deadReceivers != null) {
789 for (int i=deadReceivers.size()-1; i>=0; i--) {
790 removeUpdatesLocked(deadReceivers.get(i));
791 }
792 }
793
794 if (enabled) {
795 p.enable();
796 if (listeners > 0) {
797 p.setMinTime(getMinTimeLocked(provider));
798 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800 } else {
801 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
806 private long getMinTimeLocked(String provider) {
807 long minTime = Long.MAX_VALUE;
808 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
809 if (records != null) {
810 for (int i=records.size()-1; i>=0; i--) {
811 minTime = Math.min(minTime, records.get(i).mMinTime);
812 }
813 }
814 return minTime;
815 }
816
817 private class UpdateRecord {
818 final String mProvider;
819 final Receiver mReceiver;
820 final long mMinTime;
821 final float mMinDistance;
822 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400823 Location mLastFixBroadcast;
824 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825
826 /**
827 * Note: must be constructed with lock held.
828 */
829 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400830 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 mProvider = provider;
832 mReceiver = receiver;
833 mMinTime = minTime;
834 mMinDistance = minDistance;
835 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
837 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
838 if (records == null) {
839 records = new ArrayList<UpdateRecord>();
840 mRecordsByProvider.put(provider, records);
841 }
842 if (!records.contains(this)) {
843 records.add(this);
844 }
845 }
846
847 /**
848 * Method to be called when a record will no longer be used. Calling this multiple times
849 * must have the same effect as calling it once.
850 */
851 void disposeLocked() {
852 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
853 records.remove(this);
854 }
855
856 @Override
857 public String toString() {
858 return "UpdateRecord{"
859 + Integer.toHexString(System.identityHashCode(this))
860 + " " + mProvider + " " + mReceiver + "}";
861 }
862
863 void dump(PrintWriter pw, String prefix) {
864 pw.println(prefix + this);
865 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
866 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400867 pw.println(prefix + "mUid=" + mUid);
868 pw.println(prefix + "mLastFixBroadcast:");
869 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
870 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872
873 /**
874 * Calls dispose().
875 */
876 @Override protected void finalize() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400877 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 disposeLocked();
879 }
880 }
881 }
882
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400883 private Receiver getReceiver(ILocationListener listener) {
884 IBinder binder = listener.asBinder();
885 Receiver receiver = mReceivers.get(binder);
886 if (receiver == null) {
887 receiver = new Receiver(listener);
888 mReceivers.put(binder, receiver);
889
890 try {
891 if (receiver.isListener()) {
892 receiver.getListener().asBinder().linkToDeath(receiver, 0);
893 }
894 } catch (RemoteException e) {
895 Log.e(TAG, "linkToDeath failed:", e);
896 return null;
897 }
898 }
899 return receiver;
900 }
901
902 private Receiver getReceiver(PendingIntent intent) {
903 Receiver receiver = mReceivers.get(intent);
904 if (receiver == null) {
905 receiver = new Receiver(intent);
906 mReceivers.put(intent, receiver);
907 }
908 return receiver;
909 }
910
911 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
912 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
913 if (records != null) {
914 for (int i = records.size() - 1; i >= 0; i--) {
915 UpdateRecord record = records.get(i);
916 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
917 return true;
918 }
919 }
920 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400921 for (ProximityAlert alert : mProximityAlerts.values()) {
922 if (alert.mUid == uid) {
923 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400924 }
925 }
926 return false;
927 }
928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 public void requestLocationUpdates(String provider,
930 long minTime, float minDistance, ILocationListener listener) {
931
932 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400933 synchronized (mLock) {
934 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 } catch (SecurityException se) {
937 throw se;
938 } catch (Exception e) {
939 Log.e(TAG, "requestUpdates got exception:", e);
940 }
941 }
942
943 public void requestLocationUpdatesPI(String provider,
944 long minTime, float minDistance, PendingIntent intent) {
945 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400946 synchronized (mLock) {
947 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949 } catch (SecurityException se) {
950 throw se;
951 } catch (Exception e) {
952 Log.e(TAG, "requestUpdates got exception:", e);
953 }
954 }
955
956 private void requestLocationUpdatesLocked(String provider,
957 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700958 if (LOCAL_LOGV) {
959 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400962 LocationProviderProxy proxy = mProvidersByName.get(provider);
963 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 throw new IllegalArgumentException("provider=" + provider);
965 }
966
967 checkPermissionsSafe(provider);
968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 // so wakelock calls will succeed
970 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400971 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 long identity = Binder.clearCallingIdentity();
973 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400974 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
975 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 if (oldRecord != null) {
977 oldRecord.disposeLocked();
978 }
979
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400980 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400981 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400982 }
983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
985 if (isProviderEnabled) {
986 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400987 proxy.setMinTime(minTimeForProvider);
988 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700990 // Notify the listener that updates are currently disabled
991 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993 } finally {
994 Binder.restoreCallingIdentity(identity);
995 }
996 }
997
998 public void removeUpdates(ILocationListener listener) {
999 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001000 synchronized (mLock) {
1001 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 } catch (SecurityException se) {
1004 throw se;
1005 } catch (Exception e) {
1006 Log.e(TAG, "removeUpdates got exception:", e);
1007 }
1008 }
1009
1010 public void removeUpdatesPI(PendingIntent intent) {
1011 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001012 synchronized (mLock) {
1013 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015 } catch (SecurityException se) {
1016 throw se;
1017 } catch (Exception e) {
1018 Log.e(TAG, "removeUpdates got exception:", e);
1019 }
1020 }
1021
1022 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001023 if (LOCAL_LOGV) {
1024 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
1027 // so wakelock calls will succeed
1028 final int callingUid = Binder.getCallingUid();
1029 long identity = Binder.clearCallingIdentity();
1030 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001031 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1032 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
1035 // Record which providers were associated with this listener
1036 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001037 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 if (oldRecords != null) {
1039 // Call dispose() on the obsolete update records.
1040 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001041 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001042 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1043 if (proxy != null) {
1044 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 }
1047 record.disposeLocked();
1048 }
1049 // Accumulate providers
1050 providers.addAll(oldRecords.keySet());
1051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052
1053 // See if the providers associated with this listener have any
1054 // other listeners; if one does, inform it of the new smallest minTime
1055 // value; if one does not, disable location tracking for it
1056 for (String provider : providers) {
1057 // If provider is already disabled, don't need to do anything
1058 if (!isAllowedBySettingsLocked(provider)) {
1059 continue;
1060 }
1061
1062 boolean hasOtherListener = false;
1063 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1064 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1065 hasOtherListener = true;
1066 }
1067
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001068 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 if (p != null) {
1070 if (hasOtherListener) {
1071 p.setMinTime(getMinTimeLocked(provider));
1072 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 p.enableLocationTracking(false);
1074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 } finally {
1078 Binder.restoreCallingIdentity(identity);
1079 }
1080 }
1081
1082 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001083 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 return false;
1085 }
1086 if (mContext.checkCallingPermission(ACCESS_FINE_LOCATION) !=
1087 PackageManager.PERMISSION_GRANTED) {
1088 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1089 }
1090
1091 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001092 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001094 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 return false;
1096 }
1097 return true;
1098 }
1099
1100 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001101 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001102 try {
1103 mGpsStatusProvider.removeGpsStatusListener(listener);
1104 } catch (Exception e) {
1105 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108 }
1109
1110 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
1111 // first check for permission to the provider
1112 checkPermissionsSafe(provider);
1113 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
1114 if ((mContext.checkCallingPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
1115 != PackageManager.PERMISSION_GRANTED)) {
1116 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1117 }
1118
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001119 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001120 LocationProviderProxy proxy = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 if (provider == null) {
1122 return false;
1123 }
1124
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001125 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
1128
1129 class ProximityAlert {
1130 final int mUid;
1131 final double mLatitude;
1132 final double mLongitude;
1133 final float mRadius;
1134 final long mExpiration;
1135 final PendingIntent mIntent;
1136 final Location mLocation;
1137
1138 public ProximityAlert(int uid, double latitude, double longitude,
1139 float radius, long expiration, PendingIntent intent) {
1140 mUid = uid;
1141 mLatitude = latitude;
1142 mLongitude = longitude;
1143 mRadius = radius;
1144 mExpiration = expiration;
1145 mIntent = intent;
1146
1147 mLocation = new Location("");
1148 mLocation.setLatitude(latitude);
1149 mLocation.setLongitude(longitude);
1150 }
1151
1152 long getExpiration() {
1153 return mExpiration;
1154 }
1155
1156 PendingIntent getIntent() {
1157 return mIntent;
1158 }
1159
1160 boolean isInProximity(double latitude, double longitude) {
1161 Location loc = new Location("");
1162 loc.setLatitude(latitude);
1163 loc.setLongitude(longitude);
1164
1165 double radius = loc.distanceTo(mLocation);
1166 return radius <= mRadius;
1167 }
1168
1169 @Override
1170 public String toString() {
1171 return "ProximityAlert{"
1172 + Integer.toHexString(System.identityHashCode(this))
1173 + " uid " + mUid + mIntent + "}";
1174 }
1175
1176 void dump(PrintWriter pw, String prefix) {
1177 pw.println(prefix + this);
1178 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1179 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1180 pw.println(prefix + "mIntent=" + mIntent);
1181 pw.println(prefix + "mLocation:");
1182 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1183 }
1184 }
1185
1186 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001187 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188
1189 boolean isGpsAvailable = false;
1190
1191 // Note: this is called with the lock held.
1192 public void onLocationChanged(Location loc) {
1193
1194 // If Gps is available, then ignore updates from NetworkLocationProvider
1195 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1196 isGpsAvailable = true;
1197 }
1198 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1199 return;
1200 }
1201
1202 // Process proximity alerts
1203 long now = System.currentTimeMillis();
1204 double latitude = loc.getLatitude();
1205 double longitude = loc.getLongitude();
1206 ArrayList<PendingIntent> intentsToRemove = null;
1207
1208 for (ProximityAlert alert : mProximityAlerts.values()) {
1209 PendingIntent intent = alert.getIntent();
1210 long expiration = alert.getExpiration();
1211
1212 if ((expiration == -1) || (now <= expiration)) {
1213 boolean entered = mProximitiesEntered.contains(alert);
1214 boolean inProximity =
1215 alert.isInProximity(latitude, longitude);
1216 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001217 if (LOCAL_LOGV) {
1218 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220 mProximitiesEntered.add(alert);
1221 Intent enteredIntent = new Intent();
1222 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1223 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001224 synchronized (this) {
1225 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001226 // is called before decrementPendingBroadcasts()
1227 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1228 // call this after broadcasting so we do not increment
1229 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001230 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001233 if (LOCAL_LOGV) {
1234 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 if (intentsToRemove == null) {
1237 intentsToRemove = new ArrayList<PendingIntent>();
1238 }
1239 intentsToRemove.add(intent);
1240 }
1241 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001242 if (LOCAL_LOGV) {
1243 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245 mProximitiesEntered.remove(alert);
1246 Intent exitedIntent = new Intent();
1247 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1248 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001249 synchronized (this) {
1250 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001251 // is called before decrementPendingBroadcasts()
1252 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1253 // call this after broadcasting so we do not increment
1254 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001255 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001258 if (LOCAL_LOGV) {
1259 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 if (intentsToRemove == null) {
1262 intentsToRemove = new ArrayList<PendingIntent>();
1263 }
1264 intentsToRemove.add(intent);
1265 }
1266 }
1267 } else {
1268 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001269 if (LOCAL_LOGV) {
1270 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272 if (intentsToRemove == null) {
1273 intentsToRemove = new ArrayList<PendingIntent>();
1274 }
1275 intentsToRemove.add(alert.getIntent());
1276 }
1277 }
1278
1279 // Remove expired alerts
1280 if (intentsToRemove != null) {
1281 for (PendingIntent i : intentsToRemove) {
1282 mProximityAlerts.remove(i);
1283 ProximityAlert alert = mProximityAlerts.get(i);
1284 mProximitiesEntered.remove(alert);
1285 }
1286 }
1287
1288 }
1289
1290 // Note: this is called with the lock held.
1291 public void onProviderDisabled(String provider) {
1292 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1293 isGpsAvailable = false;
1294 }
1295 }
1296
1297 // Note: this is called with the lock held.
1298 public void onProviderEnabled(String provider) {
1299 // ignore
1300 }
1301
1302 // Note: this is called with the lock held.
1303 public void onStatusChanged(String provider, int status, Bundle extras) {
1304 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1305 (status != LocationProvider.AVAILABLE)) {
1306 isGpsAvailable = false;
1307 }
1308 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001309
1310 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1311 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001312 // synchronize to ensure incrementPendingBroadcasts()
1313 // is called before decrementPendingBroadcasts()
1314 synchronized (this) {
1315 decrementPendingBroadcasts();
1316 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
1320 public void addProximityAlert(double latitude, double longitude,
1321 float radius, long expiration, PendingIntent intent) {
1322 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001323 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1325 }
1326 } catch (SecurityException se) {
1327 throw se;
1328 } catch (Exception e) {
1329 Log.e(TAG, "addProximityAlert got exception:", e);
1330 }
1331 }
1332
1333 private void addProximityAlertLocked(double latitude, double longitude,
1334 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001335 if (LOCAL_LOGV) {
1336 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 ", longitude = " + longitude +
1338 ", expiration = " + expiration +
1339 ", intent = " + intent);
1340 }
1341
1342 // Require ability to access all providers for now
1343 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1344 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1345 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1346 }
1347
1348 if (expiration != -1) {
1349 expiration += System.currentTimeMillis();
1350 }
1351 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1352 latitude, longitude, radius, expiration, intent);
1353 mProximityAlerts.put(intent, alert);
1354
Mike Lockwood48f17512009-04-23 09:12:08 -07001355 if (mProximityReceiver == null) {
1356 mProximityListener = new ProximityListener();
1357 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
Mike Lockwood95427cd2009-05-07 13:27:54 -04001359 for (int i = mProviders.size() - 1; i >= 0; i--) {
1360 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001361 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
1365
1366 public void removeProximityAlert(PendingIntent intent) {
1367 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001368 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 removeProximityAlertLocked(intent);
1370 }
1371 } catch (SecurityException se) {
1372 throw se;
1373 } catch (Exception e) {
1374 Log.e(TAG, "removeProximityAlert got exception:", e);
1375 }
1376 }
1377
1378 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001379 if (LOCAL_LOGV) {
1380 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382
1383 mProximityAlerts.remove(intent);
1384 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001385 removeUpdatesLocked(mProximityReceiver);
1386 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389 }
1390
1391 /**
1392 * @return null if the provider does not exits
1393 * @throw SecurityException if the provider is not allowed to be
1394 * accessed by the caller
1395 */
1396 public Bundle getProviderInfo(String provider) {
1397 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001398 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 return _getProviderInfoLocked(provider);
1400 }
1401 } catch (SecurityException se) {
1402 throw se;
1403 } catch (Exception e) {
1404 Log.e(TAG, "_getProviderInfo got exception:", e);
1405 return null;
1406 }
1407 }
1408
1409 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001410 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 if (p == null) {
1412 return null;
1413 }
1414
1415 checkPermissionsSafe(provider);
1416
1417 Bundle b = new Bundle();
1418 b.putBoolean("network", p.requiresNetwork());
1419 b.putBoolean("satellite", p.requiresSatellite());
1420 b.putBoolean("cell", p.requiresCell());
1421 b.putBoolean("cost", p.hasMonetaryCost());
1422 b.putBoolean("altitude", p.supportsAltitude());
1423 b.putBoolean("speed", p.supportsSpeed());
1424 b.putBoolean("bearing", p.supportsBearing());
1425 b.putInt("power", p.getPowerRequirement());
1426 b.putInt("accuracy", p.getAccuracy());
1427
1428 return b;
1429 }
1430
1431 public boolean isProviderEnabled(String provider) {
1432 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001433 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 return _isProviderEnabledLocked(provider);
1435 }
1436 } catch (SecurityException se) {
1437 throw se;
1438 } catch (Exception e) {
1439 Log.e(TAG, "isProviderEnabled got exception:", e);
1440 return false;
1441 }
1442 }
1443
Mike Lockwood275555c2009-05-01 11:30:34 -04001444 public void reportLocation(Location location) {
1445 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1446 != PackageManager.PERMISSION_GRANTED) {
1447 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1448 }
1449
Mike Lockwood4e50b782009-04-03 08:24:43 -07001450 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1451 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1452 mLocationHandler.sendMessageAtFrontOfQueue(m);
1453 }
1454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 private boolean _isProviderEnabledLocked(String provider) {
1456 checkPermissionsSafe(provider);
1457
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001458 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 if (p == null) {
1460 throw new IllegalArgumentException("provider=" + provider);
1461 }
1462 return isAllowedBySettingsLocked(provider);
1463 }
1464
1465 public Location getLastKnownLocation(String provider) {
1466 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001467 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 return _getLastKnownLocationLocked(provider);
1469 }
1470 } catch (SecurityException se) {
1471 throw se;
1472 } catch (Exception e) {
1473 Log.e(TAG, "getLastKnownLocation got exception:", e);
1474 return null;
1475 }
1476 }
1477
1478 private Location _getLastKnownLocationLocked(String provider) {
1479 checkPermissionsSafe(provider);
1480
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001481 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (p == null) {
1483 throw new IllegalArgumentException("provider=" + provider);
1484 }
1485
1486 if (!isAllowedBySettingsLocked(provider)) {
1487 return null;
1488 }
1489
1490 Location location = mLastKnownLocation.get(provider);
1491 if (location == null) {
1492 // Get the persistent last known location for the provider
1493 location = readLastKnownLocationLocked(provider);
1494 if (location != null) {
1495 mLastKnownLocation.put(provider, location);
1496 }
1497 }
1498
1499 return location;
1500 }
1501
1502 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1503 // Always broadcast the first update
1504 if (lastLoc == null) {
1505 return true;
1506 }
1507
1508 // Don't broadcast same location again regardless of condition
1509 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1510 if (loc.getTime() == lastLoc.getTime()) {
1511 return false;
1512 }
1513
1514 // Check whether sufficient distance has been traveled
1515 double minDistance = record.mMinDistance;
1516 if (minDistance > 0.0) {
1517 if (loc.distanceTo(lastLoc) <= minDistance) {
1518 return false;
1519 }
1520 }
1521
1522 return true;
1523 }
1524
Mike Lockwood4e50b782009-04-03 08:24:43 -07001525 private void handleLocationChangedLocked(Location location) {
1526 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1528 if (records == null || records.size() == 0) {
1529 return;
1530 }
1531
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001532 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 if (p == null) {
1534 return;
1535 }
1536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001538 Location lastLocation = mLastKnownLocation.get(provider);
1539 if (lastLocation == null) {
1540 mLastKnownLocation.put(provider, new Location(location));
1541 } else {
1542 lastLocation.set(location);
1543 }
1544 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 // Fetch latest status update time
1547 long newStatusUpdateTime = p.getStatusUpdateTime();
1548
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001549 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 Bundle extras = new Bundle();
1551 int status = p.getStatus(extras);
1552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 ArrayList<Receiver> deadReceivers = null;
1554
1555 // Broadcast location or status to all listeners
1556 final int N = records.size();
1557 for (int i=0; i<N; i++) {
1558 UpdateRecord r = records.get(i);
1559 Receiver receiver = r.mReceiver;
1560
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001561 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001562 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1563 if (lastLoc == null) {
1564 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001565 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001566 } else {
1567 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001569 if (!receiver.callLocationChangedLocked(location)) {
1570 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1571 if (deadReceivers == null) {
1572 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001574 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576 }
1577
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001578 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1580 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1581
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001582 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1584 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1585 if (deadReceivers == null) {
1586 deadReceivers = new ArrayList<Receiver>();
1587 }
1588 if (!deadReceivers.contains(receiver)) {
1589 deadReceivers.add(receiver);
1590 }
1591 }
1592 }
1593 }
1594
1595 if (deadReceivers != null) {
1596 for (int i=deadReceivers.size()-1; i>=0; i--) {
1597 removeUpdatesLocked(deadReceivers.get(i));
1598 }
1599 }
1600 }
1601
1602 private class LocationWorkerHandler extends Handler {
1603
1604 @Override
1605 public void handleMessage(Message msg) {
1606 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001607 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1608 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001610 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001611 Location location = (Location) msg.obj;
Mike Lockwood98cb6672009-04-17 18:03:44 -04001612
1613 if (mCollector != null &&
1614 LocationManager.GPS_PROVIDER.equals(location.getProvider())) {
1615 try {
1616 mCollector.updateLocation(location);
1617 } catch (RemoteException e) {
1618 Log.w(TAG, "mCollector.updateLocation failed");
1619 }
1620 }
1621
Mike Lockwood4e50b782009-04-03 08:24:43 -07001622 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 if (!isAllowedBySettingsLocked(provider)) {
1624 return;
1625 }
1626
Mike Lockwooda0e3cd32009-04-21 21:27:33 -07001627 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630 } catch (Exception e) {
1631 // Log, don't crash!
1632 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1633 }
1634 }
1635 }
1636
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001637 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1638 @Override
1639 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 String action = intent.getAction();
1641
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001642 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001644 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1646 if (uid >= 0) {
1647 ArrayList<Receiver> removedRecs = null;
1648 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1649 for (int j=i.size()-1; j>=0; j--) {
1650 UpdateRecord ur = i.get(j);
1651 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1652 if (removedRecs == null) {
1653 removedRecs = new ArrayList<Receiver>();
1654 }
1655 if (!removedRecs.contains(ur.mReceiver)) {
1656 removedRecs.add(ur.mReceiver);
1657 }
1658 }
1659 }
1660 }
1661 ArrayList<ProximityAlert> removedAlerts = null;
1662 for (ProximityAlert i : mProximityAlerts.values()) {
1663 if (i.mUid == uid) {
1664 if (removedAlerts == null) {
1665 removedAlerts = new ArrayList<ProximityAlert>();
1666 }
1667 if (!removedAlerts.contains(i)) {
1668 removedAlerts.add(i);
1669 }
1670 }
1671 }
1672 if (removedRecs != null) {
1673 for (int i=removedRecs.size()-1; i>=0; i--) {
1674 removeUpdatesLocked(removedRecs.get(i));
1675 }
1676 }
1677 if (removedAlerts != null) {
1678 for (int i=removedAlerts.size()-1; i>=0; i--) {
1679 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1680 }
1681 }
1682 }
1683 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001684 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 boolean noConnectivity =
1686 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1687 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001688 mNetworkState = LocationProvider.AVAILABLE;
1689 } else {
1690 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
1692
1693 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001694 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001695 for (int i = mProviders.size() - 1; i >= 0; i--) {
1696 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001698 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700 }
1701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001704 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705
1706 // Wake locks
1707
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001708 private void incrementPendingBroadcasts() {
1709 synchronized (mWakeLock) {
1710 if (mPendingBroadcasts++ == 0) {
1711 try {
1712 mWakeLock.acquire();
1713 log("Acquired wakelock");
1714 } catch (Exception e) {
1715 // This is to catch a runtime exception thrown when we try to release an
1716 // already released lock.
1717 Log.e(TAG, "exception in acquireWakeLock()", e);
1718 }
1719 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001720 }
1721 }
1722
1723 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001724 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001725 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001726 try {
1727 // Release wake lock
1728 if (mWakeLock.isHeld()) {
1729 mWakeLock.release();
1730 log("Released wakelock");
1731 } else {
1732 log("Can't release wakelock again!");
1733 }
1734 } catch (Exception e) {
1735 // This is to catch a runtime exception thrown when we try to release an
1736 // already released lock.
1737 Log.e(TAG, "exception in releaseWakeLock()", e);
1738 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001739 }
1740 }
1741 }
1742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 // Geocoder
1744
1745 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001746 String language, String country, String variant, String appName, List<Address> addrs) {
1747 if (mGeocodeProvider != null) {
1748 try {
1749 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1750 variant, appName, addrs);
1751 } catch (RemoteException e) {
1752 Log.e(TAG, "getFromLocation failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001755 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757
Mike Lockwooda55c3212009-04-15 11:10:11 -04001758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001760 double lowerLeftLatitude, double lowerLeftLongitude,
1761 double upperRightLatitude, double upperRightLongitude, int maxResults,
1762 String language, String country, String variant, String appName, List<Address> addrs) {
1763
1764 if (mGeocodeProvider != null) {
1765 try {
1766 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1767 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1768 maxResults, language, country, variant, appName, addrs);
1769 } catch (RemoteException e) {
1770 Log.e(TAG, "getFromLocationName failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
1772 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001773 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775
1776 // Mock Providers
1777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 private void checkMockPermissionsSafe() {
1779 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1780 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1781 if (!allowMocks) {
1782 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1783 }
1784
1785 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1786 PackageManager.PERMISSION_GRANTED) {
1787 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1788 }
1789 }
1790
1791 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1792 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1793 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1794 checkMockPermissionsSafe();
1795
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001796 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001797 MockProvider provider = new MockProvider(name, this,
1798 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 requiresCell, hasMonetaryCost, supportsAltitude,
1800 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001801 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1803 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001804
1805 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1806 long identity = Binder.clearCallingIdentity();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001807 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001808 mMockProviders.put(name, provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 updateProvidersLocked();
Mike Lockwood95427cd2009-05-07 13:27:54 -04001810 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
1812 }
1813
1814 public void removeTestProvider(String provider) {
1815 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001816 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001817 MockProvider mockProvider = mMockProviders.get(provider);
1818 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1820 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001821 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001822 mMockProviders.remove(mockProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 updateProvidersLocked();
1824 }
1825 }
1826
1827 public void setTestProviderLocation(String provider, Location loc) {
1828 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001829 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001830 MockProvider mockProvider = mMockProviders.get(provider);
1831 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1833 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001834 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1835 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001836 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001837 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839 }
1840
1841 public void clearTestProviderLocation(String provider) {
1842 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001843 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001844 MockProvider mockProvider = mMockProviders.get(provider);
1845 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1847 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001848 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
1850 }
1851
1852 public void setTestProviderEnabled(String provider, boolean enabled) {
1853 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001854 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001855 MockProvider mockProvider = mMockProviders.get(provider);
1856 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1858 }
1859 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001860 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 mEnabledProviders.add(provider);
1862 mDisabledProviders.remove(provider);
1863 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001864 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 mEnabledProviders.remove(provider);
1866 mDisabledProviders.add(provider);
1867 }
1868 updateProvidersLocked();
1869 }
1870 }
1871
1872 public void clearTestProviderEnabled(String provider) {
1873 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001874 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001875 MockProvider mockProvider = mMockProviders.get(provider);
1876 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1878 }
1879 mEnabledProviders.remove(provider);
1880 mDisabledProviders.remove(provider);
1881 updateProvidersLocked();
1882 }
1883 }
1884
1885 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1886 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001887 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001888 MockProvider mockProvider = mMockProviders.get(provider);
1889 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1891 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001892 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894 }
1895
1896 public void clearTestProviderStatus(String provider) {
1897 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001898 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001899 MockProvider mockProvider = mMockProviders.get(provider);
1900 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1902 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001903 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905 }
1906
1907 private void log(String log) {
1908 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1909 Log.d(TAG, log);
1910 }
1911 }
1912
1913 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1914 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1915 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001916 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 + Binder.getCallingPid()
1918 + ", uid=" + Binder.getCallingUid());
1919 return;
1920 }
1921
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001922 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 pw.println("Current Location Manager state:");
1924 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 pw.println(" mCollector=" + mCollector);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001927 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001929 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
1931 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001932 for (Receiver i : mReceivers.values()) {
1933 pw.println(" " + i + ":");
1934 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 pw.println(" " + j.getKey() + ":");
1936 j.getValue().dump(pw, " ");
1937 }
1938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 pw.println(" Records by Provider:");
1940 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1941 : mRecordsByProvider.entrySet()) {
1942 pw.println(" " + i.getKey() + ":");
1943 for (UpdateRecord j : i.getValue()) {
1944 pw.println(" " + j + ":");
1945 j.dump(pw, " ");
1946 }
1947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 pw.println(" Last Known Locations:");
1949 for (Map.Entry<String, Location> i
1950 : mLastKnownLocation.entrySet()) {
1951 pw.println(" " + i.getKey() + ":");
1952 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1953 }
1954 if (mProximityAlerts.size() > 0) {
1955 pw.println(" Proximity Alerts:");
1956 for (Map.Entry<PendingIntent, ProximityAlert> i
1957 : mProximityAlerts.entrySet()) {
1958 pw.println(" " + i.getKey() + ":");
1959 i.getValue().dump(pw, " ");
1960 }
1961 }
1962 if (mProximitiesEntered.size() > 0) {
1963 pw.println(" Proximities Entered:");
1964 for (ProximityAlert i : mProximitiesEntered) {
1965 pw.println(" " + i + ":");
1966 i.dump(pw, " ");
1967 }
1968 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001969 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 pw.println(" mProximityListener=" + mProximityListener);
1971 if (mEnabledProviders.size() > 0) {
1972 pw.println(" Enabled Providers:");
1973 for (String i : mEnabledProviders) {
1974 pw.println(" " + i);
1975 }
1976
1977 }
1978 if (mDisabledProviders.size() > 0) {
1979 pw.println(" Disabled Providers:");
1980 for (String i : mDisabledProviders) {
1981 pw.println(" " + i);
1982 }
1983
1984 }
1985 if (mMockProviders.size() > 0) {
1986 pw.println(" Mock Providers:");
1987 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001988 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
1990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
1992 }
1993}