blob: c924d8dfead7e9fedef98fd5238bf71c413276bd [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 }
921 if (LocationManager.GPS_PROVIDER.equals(provider) ||
922 LocationManager.NETWORK_PROVIDER.equals(provider)) {
923 for (ProximityAlert alert : mProximityAlerts.values()) {
924 if (alert.mUid == uid) {
925 return true;
926 }
927 }
928 }
929 return false;
930 }
931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 public void requestLocationUpdates(String provider,
933 long minTime, float minDistance, ILocationListener listener) {
934
935 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400936 synchronized (mLock) {
937 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939 } catch (SecurityException se) {
940 throw se;
941 } catch (Exception e) {
942 Log.e(TAG, "requestUpdates got exception:", e);
943 }
944 }
945
946 public void requestLocationUpdatesPI(String provider,
947 long minTime, float minDistance, PendingIntent intent) {
948 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400949 synchronized (mLock) {
950 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952 } catch (SecurityException se) {
953 throw se;
954 } catch (Exception e) {
955 Log.e(TAG, "requestUpdates got exception:", e);
956 }
957 }
958
959 private void requestLocationUpdatesLocked(String provider,
960 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700961 if (LOCAL_LOGV) {
962 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400965 LocationProviderProxy proxy = mProvidersByName.get(provider);
966 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 throw new IllegalArgumentException("provider=" + provider);
968 }
969
970 checkPermissionsSafe(provider);
971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 // so wakelock calls will succeed
973 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400974 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 long identity = Binder.clearCallingIdentity();
976 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400977 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
978 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 if (oldRecord != null) {
980 oldRecord.disposeLocked();
981 }
982
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400983 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400984 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400985 }
986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
988 if (isProviderEnabled) {
989 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400990 proxy.setMinTime(minTimeForProvider);
991 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700993 // Notify the listener that updates are currently disabled
994 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996 } finally {
997 Binder.restoreCallingIdentity(identity);
998 }
999 }
1000
1001 public void removeUpdates(ILocationListener listener) {
1002 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001003 synchronized (mLock) {
1004 removeUpdatesLocked(getReceiver(listener));
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 public void removeUpdatesPI(PendingIntent intent) {
1014 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001015 synchronized (mLock) {
1016 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
1018 } catch (SecurityException se) {
1019 throw se;
1020 } catch (Exception e) {
1021 Log.e(TAG, "removeUpdates got exception:", e);
1022 }
1023 }
1024
1025 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001026 if (LOCAL_LOGV) {
1027 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029
1030 // so wakelock calls will succeed
1031 final int callingUid = Binder.getCallingUid();
1032 long identity = Binder.clearCallingIdentity();
1033 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001034 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1035 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037
1038 // Record which providers were associated with this listener
1039 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001040 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 if (oldRecords != null) {
1042 // Call dispose() on the obsolete update records.
1043 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001044 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001045 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1046 if (proxy != null) {
1047 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
1050 record.disposeLocked();
1051 }
1052 // Accumulate providers
1053 providers.addAll(oldRecords.keySet());
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055
1056 // See if the providers associated with this listener have any
1057 // other listeners; if one does, inform it of the new smallest minTime
1058 // value; if one does not, disable location tracking for it
1059 for (String provider : providers) {
1060 // If provider is already disabled, don't need to do anything
1061 if (!isAllowedBySettingsLocked(provider)) {
1062 continue;
1063 }
1064
1065 boolean hasOtherListener = false;
1066 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1067 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1068 hasOtherListener = true;
1069 }
1070
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001071 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 if (p != null) {
1073 if (hasOtherListener) {
1074 p.setMinTime(getMinTimeLocked(provider));
1075 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 p.enableLocationTracking(false);
1077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 } finally {
1081 Binder.restoreCallingIdentity(identity);
1082 }
1083 }
1084
1085 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001086 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 return false;
1088 }
1089 if (mContext.checkCallingPermission(ACCESS_FINE_LOCATION) !=
1090 PackageManager.PERMISSION_GRANTED) {
1091 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1092 }
1093
1094 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001095 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001097 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 return false;
1099 }
1100 return true;
1101 }
1102
1103 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001104 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001105 try {
1106 mGpsStatusProvider.removeGpsStatusListener(listener);
1107 } catch (Exception e) {
1108 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111 }
1112
1113 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
1114 // first check for permission to the provider
1115 checkPermissionsSafe(provider);
1116 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
1117 if ((mContext.checkCallingPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
1118 != PackageManager.PERMISSION_GRANTED)) {
1119 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1120 }
1121
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001122 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001123 LocationProviderProxy proxy = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (provider == null) {
1125 return false;
1126 }
1127
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001128 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
1130 }
1131
1132 class ProximityAlert {
1133 final int mUid;
1134 final double mLatitude;
1135 final double mLongitude;
1136 final float mRadius;
1137 final long mExpiration;
1138 final PendingIntent mIntent;
1139 final Location mLocation;
1140
1141 public ProximityAlert(int uid, double latitude, double longitude,
1142 float radius, long expiration, PendingIntent intent) {
1143 mUid = uid;
1144 mLatitude = latitude;
1145 mLongitude = longitude;
1146 mRadius = radius;
1147 mExpiration = expiration;
1148 mIntent = intent;
1149
1150 mLocation = new Location("");
1151 mLocation.setLatitude(latitude);
1152 mLocation.setLongitude(longitude);
1153 }
1154
1155 long getExpiration() {
1156 return mExpiration;
1157 }
1158
1159 PendingIntent getIntent() {
1160 return mIntent;
1161 }
1162
1163 boolean isInProximity(double latitude, double longitude) {
1164 Location loc = new Location("");
1165 loc.setLatitude(latitude);
1166 loc.setLongitude(longitude);
1167
1168 double radius = loc.distanceTo(mLocation);
1169 return radius <= mRadius;
1170 }
1171
1172 @Override
1173 public String toString() {
1174 return "ProximityAlert{"
1175 + Integer.toHexString(System.identityHashCode(this))
1176 + " uid " + mUid + mIntent + "}";
1177 }
1178
1179 void dump(PrintWriter pw, String prefix) {
1180 pw.println(prefix + this);
1181 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1182 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1183 pw.println(prefix + "mIntent=" + mIntent);
1184 pw.println(prefix + "mLocation:");
1185 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1186 }
1187 }
1188
1189 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001190 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191
1192 boolean isGpsAvailable = false;
1193
1194 // Note: this is called with the lock held.
1195 public void onLocationChanged(Location loc) {
1196
1197 // If Gps is available, then ignore updates from NetworkLocationProvider
1198 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1199 isGpsAvailable = true;
1200 }
1201 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1202 return;
1203 }
1204
1205 // Process proximity alerts
1206 long now = System.currentTimeMillis();
1207 double latitude = loc.getLatitude();
1208 double longitude = loc.getLongitude();
1209 ArrayList<PendingIntent> intentsToRemove = null;
1210
1211 for (ProximityAlert alert : mProximityAlerts.values()) {
1212 PendingIntent intent = alert.getIntent();
1213 long expiration = alert.getExpiration();
1214
1215 if ((expiration == -1) || (now <= expiration)) {
1216 boolean entered = mProximitiesEntered.contains(alert);
1217 boolean inProximity =
1218 alert.isInProximity(latitude, longitude);
1219 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001220 if (LOCAL_LOGV) {
1221 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 mProximitiesEntered.add(alert);
1224 Intent enteredIntent = new Intent();
1225 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1226 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001227 synchronized (this) {
1228 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001229 // is called before decrementPendingBroadcasts()
1230 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1231 // call this after broadcasting so we do not increment
1232 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001233 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001236 if (LOCAL_LOGV) {
1237 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239 if (intentsToRemove == null) {
1240 intentsToRemove = new ArrayList<PendingIntent>();
1241 }
1242 intentsToRemove.add(intent);
1243 }
1244 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001245 if (LOCAL_LOGV) {
1246 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248 mProximitiesEntered.remove(alert);
1249 Intent exitedIntent = new Intent();
1250 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1251 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001252 synchronized (this) {
1253 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001254 // is called before decrementPendingBroadcasts()
1255 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1256 // call this after broadcasting so we do not increment
1257 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001258 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001261 if (LOCAL_LOGV) {
1262 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264 if (intentsToRemove == null) {
1265 intentsToRemove = new ArrayList<PendingIntent>();
1266 }
1267 intentsToRemove.add(intent);
1268 }
1269 }
1270 } else {
1271 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001272 if (LOCAL_LOGV) {
1273 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275 if (intentsToRemove == null) {
1276 intentsToRemove = new ArrayList<PendingIntent>();
1277 }
1278 intentsToRemove.add(alert.getIntent());
1279 }
1280 }
1281
1282 // Remove expired alerts
1283 if (intentsToRemove != null) {
1284 for (PendingIntent i : intentsToRemove) {
1285 mProximityAlerts.remove(i);
1286 ProximityAlert alert = mProximityAlerts.get(i);
1287 mProximitiesEntered.remove(alert);
1288 }
1289 }
1290
1291 }
1292
1293 // Note: this is called with the lock held.
1294 public void onProviderDisabled(String provider) {
1295 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1296 isGpsAvailable = false;
1297 }
1298 }
1299
1300 // Note: this is called with the lock held.
1301 public void onProviderEnabled(String provider) {
1302 // ignore
1303 }
1304
1305 // Note: this is called with the lock held.
1306 public void onStatusChanged(String provider, int status, Bundle extras) {
1307 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1308 (status != LocationProvider.AVAILABLE)) {
1309 isGpsAvailable = false;
1310 }
1311 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001312
1313 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1314 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001315 // synchronize to ensure incrementPendingBroadcasts()
1316 // is called before decrementPendingBroadcasts()
1317 synchronized (this) {
1318 decrementPendingBroadcasts();
1319 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322
1323 public void addProximityAlert(double latitude, double longitude,
1324 float radius, long expiration, PendingIntent intent) {
1325 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001326 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1328 }
1329 } catch (SecurityException se) {
1330 throw se;
1331 } catch (Exception e) {
1332 Log.e(TAG, "addProximityAlert got exception:", e);
1333 }
1334 }
1335
1336 private void addProximityAlertLocked(double latitude, double longitude,
1337 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001338 if (LOCAL_LOGV) {
1339 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 ", longitude = " + longitude +
1341 ", expiration = " + expiration +
1342 ", intent = " + intent);
1343 }
1344
1345 // Require ability to access all providers for now
1346 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1347 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1348 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1349 }
1350
1351 if (expiration != -1) {
1352 expiration += System.currentTimeMillis();
1353 }
1354 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1355 latitude, longitude, radius, expiration, intent);
1356 mProximityAlerts.put(intent, alert);
1357
Mike Lockwood48f17512009-04-23 09:12:08 -07001358 if (mProximityReceiver == null) {
1359 mProximityListener = new ProximityListener();
1360 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001362 LocationProviderProxy provider = mProvidersByName.get(LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 if (provider != null) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001364 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001367 provider = mProvidersByName.get(LocationManager.NETWORK_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 if (provider != null) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001369 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 }
1373
1374 public void removeProximityAlert(PendingIntent intent) {
1375 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001376 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 removeProximityAlertLocked(intent);
1378 }
1379 } catch (SecurityException se) {
1380 throw se;
1381 } catch (Exception e) {
1382 Log.e(TAG, "removeProximityAlert got exception:", e);
1383 }
1384 }
1385
1386 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001387 if (LOCAL_LOGV) {
1388 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390
1391 mProximityAlerts.remove(intent);
1392 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001393 removeUpdatesLocked(mProximityReceiver);
1394 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
1397 }
1398
1399 /**
1400 * @return null if the provider does not exits
1401 * @throw SecurityException if the provider is not allowed to be
1402 * accessed by the caller
1403 */
1404 public Bundle getProviderInfo(String provider) {
1405 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001406 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 return _getProviderInfoLocked(provider);
1408 }
1409 } catch (SecurityException se) {
1410 throw se;
1411 } catch (Exception e) {
1412 Log.e(TAG, "_getProviderInfo got exception:", e);
1413 return null;
1414 }
1415 }
1416
1417 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001418 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (p == null) {
1420 return null;
1421 }
1422
1423 checkPermissionsSafe(provider);
1424
1425 Bundle b = new Bundle();
1426 b.putBoolean("network", p.requiresNetwork());
1427 b.putBoolean("satellite", p.requiresSatellite());
1428 b.putBoolean("cell", p.requiresCell());
1429 b.putBoolean("cost", p.hasMonetaryCost());
1430 b.putBoolean("altitude", p.supportsAltitude());
1431 b.putBoolean("speed", p.supportsSpeed());
1432 b.putBoolean("bearing", p.supportsBearing());
1433 b.putInt("power", p.getPowerRequirement());
1434 b.putInt("accuracy", p.getAccuracy());
1435
1436 return b;
1437 }
1438
1439 public boolean isProviderEnabled(String provider) {
1440 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001441 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 return _isProviderEnabledLocked(provider);
1443 }
1444 } catch (SecurityException se) {
1445 throw se;
1446 } catch (Exception e) {
1447 Log.e(TAG, "isProviderEnabled got exception:", e);
1448 return false;
1449 }
1450 }
1451
Mike Lockwood275555c2009-05-01 11:30:34 -04001452 public void reportLocation(Location location) {
1453 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1454 != PackageManager.PERMISSION_GRANTED) {
1455 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1456 }
1457
Mike Lockwood4e50b782009-04-03 08:24:43 -07001458 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1459 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1460 mLocationHandler.sendMessageAtFrontOfQueue(m);
1461 }
1462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 private boolean _isProviderEnabledLocked(String provider) {
1464 checkPermissionsSafe(provider);
1465
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001466 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 if (p == null) {
1468 throw new IllegalArgumentException("provider=" + provider);
1469 }
1470 return isAllowedBySettingsLocked(provider);
1471 }
1472
1473 public Location getLastKnownLocation(String provider) {
1474 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001475 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 return _getLastKnownLocationLocked(provider);
1477 }
1478 } catch (SecurityException se) {
1479 throw se;
1480 } catch (Exception e) {
1481 Log.e(TAG, "getLastKnownLocation got exception:", e);
1482 return null;
1483 }
1484 }
1485
1486 private Location _getLastKnownLocationLocked(String provider) {
1487 checkPermissionsSafe(provider);
1488
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001489 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 if (p == null) {
1491 throw new IllegalArgumentException("provider=" + provider);
1492 }
1493
1494 if (!isAllowedBySettingsLocked(provider)) {
1495 return null;
1496 }
1497
1498 Location location = mLastKnownLocation.get(provider);
1499 if (location == null) {
1500 // Get the persistent last known location for the provider
1501 location = readLastKnownLocationLocked(provider);
1502 if (location != null) {
1503 mLastKnownLocation.put(provider, location);
1504 }
1505 }
1506
1507 return location;
1508 }
1509
1510 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1511 // Always broadcast the first update
1512 if (lastLoc == null) {
1513 return true;
1514 }
1515
1516 // Don't broadcast same location again regardless of condition
1517 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1518 if (loc.getTime() == lastLoc.getTime()) {
1519 return false;
1520 }
1521
1522 // Check whether sufficient distance has been traveled
1523 double minDistance = record.mMinDistance;
1524 if (minDistance > 0.0) {
1525 if (loc.distanceTo(lastLoc) <= minDistance) {
1526 return false;
1527 }
1528 }
1529
1530 return true;
1531 }
1532
Mike Lockwood4e50b782009-04-03 08:24:43 -07001533 private void handleLocationChangedLocked(Location location) {
1534 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1536 if (records == null || records.size() == 0) {
1537 return;
1538 }
1539
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001540 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 if (p == null) {
1542 return;
1543 }
1544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001546 Location lastLocation = mLastKnownLocation.get(provider);
1547 if (lastLocation == null) {
1548 mLastKnownLocation.put(provider, new Location(location));
1549 } else {
1550 lastLocation.set(location);
1551 }
1552 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 // Fetch latest status update time
1555 long newStatusUpdateTime = p.getStatusUpdateTime();
1556
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001557 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 Bundle extras = new Bundle();
1559 int status = p.getStatus(extras);
1560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 ArrayList<Receiver> deadReceivers = null;
1562
1563 // Broadcast location or status to all listeners
1564 final int N = records.size();
1565 for (int i=0; i<N; i++) {
1566 UpdateRecord r = records.get(i);
1567 Receiver receiver = r.mReceiver;
1568
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001569 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001570 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1571 if (lastLoc == null) {
1572 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001573 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001574 } else {
1575 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001577 if (!receiver.callLocationChangedLocked(location)) {
1578 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1579 if (deadReceivers == null) {
1580 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001582 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 }
1584 }
1585
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001586 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1588 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1589
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001590 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1592 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1593 if (deadReceivers == null) {
1594 deadReceivers = new ArrayList<Receiver>();
1595 }
1596 if (!deadReceivers.contains(receiver)) {
1597 deadReceivers.add(receiver);
1598 }
1599 }
1600 }
1601 }
1602
1603 if (deadReceivers != null) {
1604 for (int i=deadReceivers.size()-1; i>=0; i--) {
1605 removeUpdatesLocked(deadReceivers.get(i));
1606 }
1607 }
1608 }
1609
1610 private class LocationWorkerHandler extends Handler {
1611
1612 @Override
1613 public void handleMessage(Message msg) {
1614 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001615 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1616 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001618 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001619 Location location = (Location) msg.obj;
Mike Lockwood98cb6672009-04-17 18:03:44 -04001620
1621 if (mCollector != null &&
1622 LocationManager.GPS_PROVIDER.equals(location.getProvider())) {
1623 try {
1624 mCollector.updateLocation(location);
1625 } catch (RemoteException e) {
1626 Log.w(TAG, "mCollector.updateLocation failed");
1627 }
1628 }
1629
Mike Lockwood4e50b782009-04-03 08:24:43 -07001630 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 if (!isAllowedBySettingsLocked(provider)) {
1632 return;
1633 }
1634
Mike Lockwooda0e3cd32009-04-21 21:27:33 -07001635 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638 } catch (Exception e) {
1639 // Log, don't crash!
1640 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1641 }
1642 }
1643 }
1644
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001645 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1646 @Override
1647 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 String action = intent.getAction();
1649
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001650 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001652 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1654 if (uid >= 0) {
1655 ArrayList<Receiver> removedRecs = null;
1656 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1657 for (int j=i.size()-1; j>=0; j--) {
1658 UpdateRecord ur = i.get(j);
1659 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1660 if (removedRecs == null) {
1661 removedRecs = new ArrayList<Receiver>();
1662 }
1663 if (!removedRecs.contains(ur.mReceiver)) {
1664 removedRecs.add(ur.mReceiver);
1665 }
1666 }
1667 }
1668 }
1669 ArrayList<ProximityAlert> removedAlerts = null;
1670 for (ProximityAlert i : mProximityAlerts.values()) {
1671 if (i.mUid == uid) {
1672 if (removedAlerts == null) {
1673 removedAlerts = new ArrayList<ProximityAlert>();
1674 }
1675 if (!removedAlerts.contains(i)) {
1676 removedAlerts.add(i);
1677 }
1678 }
1679 }
1680 if (removedRecs != null) {
1681 for (int i=removedRecs.size()-1; i>=0; i--) {
1682 removeUpdatesLocked(removedRecs.get(i));
1683 }
1684 }
1685 if (removedAlerts != null) {
1686 for (int i=removedAlerts.size()-1; i>=0; i--) {
1687 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1688 }
1689 }
1690 }
1691 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001692 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 boolean noConnectivity =
1694 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1695 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696 mNetworkState = LocationProvider.AVAILABLE;
1697 } else {
1698 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700
1701 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001702 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001703 for (int i = mProviders.size() - 1; i >= 0; i--) {
1704 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001706 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
1708 }
1709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001712 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713
1714 // Wake locks
1715
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001716 private void incrementPendingBroadcasts() {
1717 synchronized (mWakeLock) {
1718 if (mPendingBroadcasts++ == 0) {
1719 try {
1720 mWakeLock.acquire();
1721 log("Acquired wakelock");
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 acquireWakeLock()", e);
1726 }
1727 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001728 }
1729 }
1730
1731 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001732 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001733 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001734 try {
1735 // Release wake lock
1736 if (mWakeLock.isHeld()) {
1737 mWakeLock.release();
1738 log("Released wakelock");
1739 } else {
1740 log("Can't release wakelock again!");
1741 }
1742 } catch (Exception e) {
1743 // This is to catch a runtime exception thrown when we try to release an
1744 // already released lock.
1745 Log.e(TAG, "exception in releaseWakeLock()", e);
1746 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001747 }
1748 }
1749 }
1750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 // Geocoder
1752
1753 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001754 String language, String country, String variant, String appName, List<Address> addrs) {
1755 if (mGeocodeProvider != null) {
1756 try {
1757 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1758 variant, appName, addrs);
1759 } catch (RemoteException e) {
1760 Log.e(TAG, "getFromLocation failed", e);
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
Mike Lockwooda55c3212009-04-15 11:10:11 -04001766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001768 double lowerLeftLatitude, double lowerLeftLongitude,
1769 double upperRightLatitude, double upperRightLongitude, int maxResults,
1770 String language, String country, String variant, String appName, List<Address> addrs) {
1771
1772 if (mGeocodeProvider != null) {
1773 try {
1774 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1775 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1776 maxResults, language, country, variant, appName, addrs);
1777 } catch (RemoteException e) {
1778 Log.e(TAG, "getFromLocationName failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001781 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783
1784 // Mock Providers
1785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 private void checkMockPermissionsSafe() {
1787 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1788 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1789 if (!allowMocks) {
1790 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1791 }
1792
1793 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1794 PackageManager.PERMISSION_GRANTED) {
1795 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1796 }
1797 }
1798
1799 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1800 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1801 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1802 checkMockPermissionsSafe();
1803
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001804 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001805 MockProvider provider = new MockProvider(name, this,
1806 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 requiresCell, hasMonetaryCost, supportsAltitude,
1808 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001809 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1811 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001812 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001813 mMockProviders.put(name, provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 updateProvidersLocked();
1815 }
1816 }
1817
1818 public void removeTestProvider(String provider) {
1819 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001820 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001821 MockProvider mockProvider = mMockProviders.get(provider);
1822 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1824 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001825 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001826 mMockProviders.remove(mockProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 updateProvidersLocked();
1828 }
1829 }
1830
1831 public void setTestProviderLocation(String provider, Location loc) {
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.setLocation(loc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840 }
1841
1842 public void clearTestProviderLocation(String provider) {
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 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001849 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
1851 }
1852
1853 public void setTestProviderEnabled(String provider, boolean enabled) {
1854 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001855 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001856 MockProvider mockProvider = mMockProviders.get(provider);
1857 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1859 }
1860 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001861 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 mEnabledProviders.add(provider);
1863 mDisabledProviders.remove(provider);
1864 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001865 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 mEnabledProviders.remove(provider);
1867 mDisabledProviders.add(provider);
1868 }
1869 updateProvidersLocked();
1870 }
1871 }
1872
1873 public void clearTestProviderEnabled(String provider) {
1874 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001875 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001876 MockProvider mockProvider = mMockProviders.get(provider);
1877 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1879 }
1880 mEnabledProviders.remove(provider);
1881 mDisabledProviders.remove(provider);
1882 updateProvidersLocked();
1883 }
1884 }
1885
1886 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
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.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 }
1895 }
1896
1897 public void clearTestProviderStatus(String provider) {
1898 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001899 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001900 MockProvider mockProvider = mMockProviders.get(provider);
1901 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1903 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001904 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
1906 }
1907
1908 private void log(String log) {
1909 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1910 Log.d(TAG, log);
1911 }
1912 }
1913
1914 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1915 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1916 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001917 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 + Binder.getCallingPid()
1919 + ", uid=" + Binder.getCallingUid());
1920 return;
1921 }
1922
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001923 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 pw.println("Current Location Manager state:");
1925 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 pw.println(" mCollector=" + mCollector);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001928 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001930 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
1932 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001933 for (Receiver i : mReceivers.values()) {
1934 pw.println(" " + i + ":");
1935 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 pw.println(" " + j.getKey() + ":");
1937 j.getValue().dump(pw, " ");
1938 }
1939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 pw.println(" Records by Provider:");
1941 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1942 : mRecordsByProvider.entrySet()) {
1943 pw.println(" " + i.getKey() + ":");
1944 for (UpdateRecord j : i.getValue()) {
1945 pw.println(" " + j + ":");
1946 j.dump(pw, " ");
1947 }
1948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 pw.println(" Last Known Locations:");
1950 for (Map.Entry<String, Location> i
1951 : mLastKnownLocation.entrySet()) {
1952 pw.println(" " + i.getKey() + ":");
1953 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1954 }
1955 if (mProximityAlerts.size() > 0) {
1956 pw.println(" Proximity Alerts:");
1957 for (Map.Entry<PendingIntent, ProximityAlert> i
1958 : mProximityAlerts.entrySet()) {
1959 pw.println(" " + i.getKey() + ":");
1960 i.getValue().dump(pw, " ");
1961 }
1962 }
1963 if (mProximitiesEntered.size() > 0) {
1964 pw.println(" Proximities Entered:");
1965 for (ProximityAlert i : mProximitiesEntered) {
1966 pw.println(" " + i + ":");
1967 i.dump(pw, " ");
1968 }
1969 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001970 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 pw.println(" mProximityListener=" + mProximityListener);
1972 if (mEnabledProviders.size() > 0) {
1973 pw.println(" Enabled Providers:");
1974 for (String i : mEnabledProviders) {
1975 pw.println(" " + i);
1976 }
1977
1978 }
1979 if (mDisabledProviders.size() > 0) {
1980 pw.println(" Disabled Providers:");
1981 for (String i : mDisabledProviders) {
1982 pw.println(" " + i);
1983 }
1984
1985 }
1986 if (mMockProviders.size() > 0) {
1987 pw.println(" Mock Providers:");
1988 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001989 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 }
1994}