blob: 2201b55beaa2e74bc43f1dd6272384205272062f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileDescriptor;
22import java.io.FileReader;
23import java.io.FileWriter;
24import java.io.IOException;
25import java.io.PrintWriter;
26import java.util.ArrayList;
27import java.util.HashMap;
28import java.util.HashSet;
29import java.util.List;
30import java.util.Map;
Mike Lockwood9637d472009-04-02 21:41:57 -070031import java.util.Observable;
32import java.util.Observer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import java.util.Set;
34import java.util.regex.Pattern;
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.app.PendingIntent;
37import android.content.BroadcastReceiver;
Mike Lockwood9637d472009-04-02 21:41:57 -070038import android.content.ContentQueryMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.ContentResolver;
40import android.content.Context;
41import android.content.Intent;
42import android.content.IntentFilter;
43import android.content.pm.PackageManager;
Mike Lockwood9637d472009-04-02 21:41:57 -070044import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.location.Address;
Mike Lockwooda55c3212009-04-15 11:10:11 -040046import android.location.IGeocodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040048import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.location.ILocationListener;
50import android.location.ILocationManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070051import android.location.ILocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.location.Location;
53import android.location.LocationManager;
54import android.location.LocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.net.ConnectivityManager;
56import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Binder;
58import android.os.Bundle;
59import android.os.Handler;
60import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070061import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Message;
63import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070064import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.RemoteException;
66import android.os.SystemClock;
67import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.Log;
69import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import com.android.internal.location.GpsLocationProvider;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070072import com.android.internal.location.LocationProviderProxy;
Mike Lockwood7ec434e2009-03-27 07:46:48 -070073import com.android.internal.location.MockProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75/**
76 * The service class that manages LocationProviders and issues location
77 * updates and alerts.
78 *
79 * {@hide}
80 */
Mike Lockwood3d12b512009-04-21 23:25:35 -070081public class LocationManagerService extends ILocationManager.Stub implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private static final String TAG = "LocationManagerService";
The Android Open Source Project10592532009-03-18 17:39:46 -070083 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 // Minimum time interval between last known location writes, in milliseconds.
86 private static final long MIN_LAST_KNOWN_LOCATION_TIME = 60L * 1000L;
87
88 // Max time to hold wake lock for, in milliseconds.
89 private static final long MAX_TIME_FOR_WAKE_LOCK = 60 * 1000L;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 // The last time a location was written, by provider name.
92 private HashMap<String,Long> mLastWriteTime = new HashMap<String,Long>();
93
94 private static final Pattern PATTERN_COMMA = Pattern.compile(",");
95
96 private static final String ACCESS_FINE_LOCATION =
97 android.Manifest.permission.ACCESS_FINE_LOCATION;
98 private static final String ACCESS_COARSE_LOCATION =
99 android.Manifest.permission.ACCESS_COARSE_LOCATION;
100 private static final String ACCESS_MOCK_LOCATION =
101 android.Manifest.permission.ACCESS_MOCK_LOCATION;
102 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
103 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400104 private static final String INSTALL_LOCATION_PROVIDER =
105 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 // Set of providers that are explicitly enabled
108 private final Set<String> mEnabledProviders = new HashSet<String>();
109
110 // Set of providers that are explicitly disabled
111 private final Set<String> mDisabledProviders = new HashSet<String>();
112
113 // Locations, status values, and extras for mock providers
Mike Lockwood7ec434e2009-03-27 07:46:48 -0700114 private final HashMap<String,MockProvider> mMockProviders = new HashMap<String,MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 private static boolean sProvidersLoaded = false;
117
118 private final Context mContext;
Mike Lockwooda55c3212009-04-15 11:10:11 -0400119 private IGeocodeProvider mGeocodeProvider;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400120 private IGpsStatusProvider mGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 private LocationWorkerHandler mLocationHandler;
122
123 // Handler messages
Mike Lockwood4e50b782009-04-03 08:24:43 -0700124 private static final int MESSAGE_LOCATION_CHANGED = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400126 // wakelock variables
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 private final static String WAKELOCK_KEY = "LocationManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 private PowerManager.WakeLock mWakeLock = null;
Mike Lockwood48f17512009-04-23 09:12:08 -0700129 private int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400132 * List of all receivers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400134 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400136
137 /**
138 * List of location providers.
139 */
140 private final ArrayList<LocationProviderProxy> mProviders =
141 new ArrayList<LocationProviderProxy>();
142 private final HashMap<String, LocationProviderProxy> mProvidersByName
143 = new HashMap<String, LocationProviderProxy>();
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400146 * Object used internally for synchronization
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400148 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
150 /**
151 * Mapping from provider name to all its UpdateRecords
152 */
153 private final HashMap<String,ArrayList<UpdateRecord>> mRecordsByProvider =
154 new HashMap<String,ArrayList<UpdateRecord>>();
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 // Proximity listeners
Mike Lockwood48f17512009-04-23 09:12:08 -0700157 private Receiver mProximityReceiver = null;
158 private ILocationListener mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private HashMap<PendingIntent,ProximityAlert> mProximityAlerts =
160 new HashMap<PendingIntent,ProximityAlert>();
161 private HashSet<ProximityAlert> mProximitiesEntered =
162 new HashSet<ProximityAlert>();
163
164 // Last known location for each provider
165 private HashMap<String,Location> mLastKnownLocation =
166 new HashMap<String,Location>();
167
The Android Open Source Project4df24232009-03-05 14:34:35 -0800168 private int mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800169
Mike Lockwood9637d472009-04-02 21:41:57 -0700170 // for Settings change notification
171 private ContentQueryMap mSettings;
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /**
174 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
175 * location updates.
176 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700177 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final ILocationListener mListener;
179 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Object mKey;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400181 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Mike Lockwood48f17512009-04-23 09:12:08 -0700182 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400184 Receiver(ILocationListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 mListener = listener;
186 mPendingIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 mKey = listener.asBinder();
188 }
189
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400190 Receiver(PendingIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 mPendingIntent = intent;
192 mListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 mKey = intent;
194 }
195
196 @Override
197 public boolean equals(Object otherObj) {
198 if (otherObj instanceof Receiver) {
199 return mKey.equals(
200 ((Receiver)otherObj).mKey);
201 }
202 return false;
203 }
204
205 @Override
206 public int hashCode() {
207 return mKey.hashCode();
208 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 @Override
211 public String toString() {
212 if (mListener != null) {
213 return "Receiver{"
214 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400215 + " Listener " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 } else {
217 return "Receiver{"
218 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400219 + " Intent " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221 }
222
223 public boolean isListener() {
224 return mListener != null;
225 }
226
227 public boolean isPendingIntent() {
228 return mPendingIntent != null;
229 }
230
231 public ILocationListener getListener() {
232 if (mListener != null) {
233 return mListener;
234 }
235 throw new IllegalStateException("Request for non-existent listener");
236 }
237
238 public PendingIntent getPendingIntent() {
239 if (mPendingIntent != null) {
240 return mPendingIntent;
241 }
242 throw new IllegalStateException("Request for non-existent intent");
243 }
244
245 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
246 if (mListener != null) {
247 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700248 synchronized (this) {
249 // synchronize to ensure incrementPendingBroadcastsLocked()
250 // is called before decrementPendingBroadcasts()
251 mListener.onStatusChanged(provider, status, extras);
252 if (mListener != mProximityListener) {
253 // call this after broadcasting so we do not increment
254 // if we throw an exeption.
255 incrementPendingBroadcastsLocked();
256 }
257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 } catch (RemoteException e) {
259 return false;
260 }
261 } else {
262 Intent statusChanged = new Intent();
263 statusChanged.putExtras(extras);
264 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
265 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700266 synchronized (this) {
267 // synchronize to ensure incrementPendingBroadcastsLocked()
268 // is called before decrementPendingBroadcasts()
269 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler);
270 // call this after broadcasting so we do not increment
271 // if we throw an exeption.
272 incrementPendingBroadcastsLocked();
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 } catch (PendingIntent.CanceledException e) {
275 return false;
276 }
277 }
278 return true;
279 }
280
281 public boolean callLocationChangedLocked(Location location) {
282 if (mListener != null) {
283 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700284 synchronized (this) {
285 // synchronize to ensure incrementPendingBroadcastsLocked()
286 // is called before decrementPendingBroadcasts()
287 mListener.onLocationChanged(location);
288 if (mListener != mProximityListener) {
289 // call this after broadcasting so we do not increment
290 // if we throw an exeption.
291 incrementPendingBroadcastsLocked();
292 }
293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 } catch (RemoteException e) {
295 return false;
296 }
297 } else {
298 Intent locationChanged = new Intent();
299 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
300 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700301 synchronized (this) {
302 // synchronize to ensure incrementPendingBroadcastsLocked()
303 // is called before decrementPendingBroadcasts()
304 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler);
305 // call this after broadcasting so we do not increment
306 // if we throw an exeption.
307 incrementPendingBroadcastsLocked();
308 }
309 } catch (PendingIntent.CanceledException e) {
310 return false;
311 }
312 }
313 return true;
314 }
315
316 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
317 if (mListener != null) {
318 try {
319 synchronized (this) {
320 // synchronize to ensure incrementPendingBroadcastsLocked()
321 // is called before decrementPendingBroadcasts()
322 if (enabled) {
323 mListener.onProviderEnabled(provider);
324 } else {
325 mListener.onProviderDisabled(provider);
326 }
327 if (mListener != mProximityListener) {
328 // call this after broadcasting so we do not increment
329 // if we throw an exeption.
330 incrementPendingBroadcastsLocked();
331 }
332 }
333 } catch (RemoteException e) {
334 return false;
335 }
336 } else {
337 Intent providerIntent = new Intent();
338 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
339 try {
340 synchronized (this) {
341 // synchronize to ensure incrementPendingBroadcastsLocked()
342 // is called before decrementPendingBroadcasts()
343 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler);
344 // call this after broadcasting so we do not increment
345 // if we throw an exeption.
346 incrementPendingBroadcastsLocked();
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 } catch (PendingIntent.CanceledException e) {
349 return false;
350 }
351 }
352 return true;
353 }
354
355 public void binderDied() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700356 if (LOCAL_LOGV) {
357 Log.v(TAG, "Location listener died");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400359 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 removeUpdatesLocked(this);
361 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700362 synchronized (this) {
363 if (mPendingBroadcasts > 0) {
364 LocationManagerService.this.decrementPendingBroadcasts();
365 mPendingBroadcasts = 0;
366 }
367 }
368 }
369
370 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
371 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400372 synchronized (this) {
373 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700374 }
375 }
376
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400377 // this must be called while synchronized by caller in a synchronized block
378 // containing the sending of the broadcaset
379 private void incrementPendingBroadcastsLocked() {
380 if (mPendingBroadcasts++ == 0) {
381 LocationManagerService.this.incrementPendingBroadcasts();
382 }
383 }
384
385 private void decrementPendingBroadcastsLocked() {
386 if (--mPendingBroadcasts == 0) {
387 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700388 }
389 }
390 }
391
392 public void locationCallbackFinished(ILocationListener listener) {
393 Receiver receiver = getReceiver(listener);
394 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400395 synchronized (receiver) {
396 // so wakelock calls will succeed
397 long identity = Binder.clearCallingIdentity();
398 receiver.decrementPendingBroadcastsLocked();
399 Binder.restoreCallingIdentity(identity);
400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402 }
403
Mike Lockwood9637d472009-04-02 21:41:57 -0700404 private final class SettingsObserver implements Observer {
405 public void update(Observable o, Object arg) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400406 synchronized (mLock) {
Mike Lockwood9637d472009-04-02 21:41:57 -0700407 updateProvidersLocked();
408 }
409 }
410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 private Location readLastKnownLocationLocked(String provider) {
413 Location location = null;
414 String s = null;
415 try {
416 File f = new File(LocationManager.SYSTEM_DIR + "/location."
417 + provider);
418 if (!f.exists()) {
419 return null;
420 }
421 BufferedReader reader = new BufferedReader(new FileReader(f), 256);
422 s = reader.readLine();
423 } catch (IOException e) {
424 Log.w(TAG, "Unable to read last known location", e);
425 }
426
427 if (s == null) {
428 return null;
429 }
430 try {
431 String[] tokens = PATTERN_COMMA.split(s);
432 int idx = 0;
433 long time = Long.parseLong(tokens[idx++]);
434 double latitude = Double.parseDouble(tokens[idx++]);
435 double longitude = Double.parseDouble(tokens[idx++]);
436 double altitude = Double.parseDouble(tokens[idx++]);
437 float bearing = Float.parseFloat(tokens[idx++]);
438 float speed = Float.parseFloat(tokens[idx++]);
439
440 location = new Location(provider);
441 location.setTime(time);
442 location.setLatitude(latitude);
443 location.setLongitude(longitude);
444 location.setAltitude(altitude);
445 location.setBearing(bearing);
446 location.setSpeed(speed);
447 } catch (NumberFormatException nfe) {
448 Log.e(TAG, "NumberFormatException reading last known location", nfe);
449 return null;
450 }
451
452 return location;
453 }
454
455 private void writeLastKnownLocationLocked(String provider,
456 Location location) {
457 long now = SystemClock.elapsedRealtime();
458 Long last = mLastWriteTime.get(provider);
459 if ((last != null)
460 && (now - last.longValue() < MIN_LAST_KNOWN_LOCATION_TIME)) {
461 return;
462 }
463 mLastWriteTime.put(provider, now);
464
465 StringBuilder sb = new StringBuilder(100);
466 sb.append(location.getTime());
467 sb.append(',');
468 sb.append(location.getLatitude());
469 sb.append(',');
470 sb.append(location.getLongitude());
471 sb.append(',');
472 sb.append(location.getAltitude());
473 sb.append(',');
474 sb.append(location.getBearing());
475 sb.append(',');
476 sb.append(location.getSpeed());
477
478 FileWriter writer = null;
479 try {
480 File d = new File(LocationManager.SYSTEM_DIR);
481 if (!d.exists()) {
482 if (!d.mkdirs()) {
483 Log.w(TAG, "Unable to create directory to write location");
484 return;
485 }
486 }
487 File f = new File(LocationManager.SYSTEM_DIR + "/location." + provider);
488 writer = new FileWriter(f);
489 writer.write(sb.toString());
490 } catch (IOException e) {
491 Log.w(TAG, "Unable to write location", e);
492 } finally {
493 if (writer != null) {
494 try {
495 writer.close();
496 } catch (IOException e) {
497 Log.w(TAG, "Exception closing file", e);
498 }
499 }
500 }
501 }
502
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400503 private void addProvider(LocationProviderProxy provider) {
504 mProviders.add(provider);
505 mProvidersByName.put(provider.getName(), provider);
506 }
507
508 private void removeProvider(LocationProviderProxy provider) {
509 mProviders.remove(provider);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700510 provider.unlinkProvider();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400511 mProvidersByName.remove(provider.getName());
512 }
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400515 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 if (sProvidersLoaded) {
517 return;
518 }
519
520 // Load providers
521 loadProvidersLocked();
522 sProvidersLoaded = true;
523 }
524 }
525
526 private void loadProvidersLocked() {
527 try {
528 _loadProvidersLocked();
529 } catch (Exception e) {
530 Log.e(TAG, "Exception loading providers:", e);
531 }
532 }
533
534 private void _loadProvidersLocked() {
535 // Attempt to load "real" providers first
536 if (GpsLocationProvider.isSupported()) {
537 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400538 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
539 mGpsStatusProvider = provider.getGpsStatusProvider();
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400540 LocationProviderProxy proxy = new LocationProviderProxy(LocationManager.GPS_PROVIDER, provider);
541 addProvider(proxy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 updateProvidersLocked();
545 }
546
547 /**
548 * @param context the context that the LocationManagerService runs in
549 */
550 public LocationManagerService(Context context) {
551 super();
552 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700553
554 Thread thread = new Thread(null, this, "LocationManagerService");
555 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
The Android Open Source Project10592532009-03-18 17:39:46 -0700557 if (LOCAL_LOGV) {
558 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
Mike Lockwood3d12b512009-04-21 23:25:35 -0700562 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 // Create a wake lock, needs to be done before calling loadProviders() below
564 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
565 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 // Load providers
568 loadProviders();
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400572 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
573 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
575 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400576 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
Mike Lockwood9637d472009-04-02 21:41:57 -0700578 // listen for settings changes
579 ContentResolver resolver = mContext.getContentResolver();
580 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
581 "(" + Settings.System.NAME + "=?)",
582 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
583 null);
584 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
585 SettingsObserver settingsObserver = new SettingsObserver();
586 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588
Mike Lockwood3d12b512009-04-21 23:25:35 -0700589 public void run()
590 {
591 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
592 Looper.prepare();
593 mLocationHandler = new LocationWorkerHandler();
594 initialize();
595 Looper.loop();
596 }
597
Mike Lockwood275555c2009-05-01 11:30:34 -0400598 public void installLocationProvider(String name, ILocationProvider provider) {
599 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
600 != PackageManager.PERMISSION_GRANTED) {
601 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700602 }
603
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400604 synchronized (mLock) {
Mike Lockwood3681f262009-05-12 10:52:03 -0400605 // check to see if we are reinstalling a dead provider
606 LocationProviderProxy oldProvider = mProvidersByName.get(name);
607 if (oldProvider != null) {
608 if (oldProvider.isDead()) {
609 Log.d(TAG, "replacing dead provider");
610 removeProvider(oldProvider);
611 } else {
612 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
613 }
614 }
615
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400616 LocationProviderProxy proxy = new LocationProviderProxy(name, provider);
617 addProvider(proxy);
618 updateProvidersLocked();
Mike Lockwood275555c2009-05-01 11:30:34 -0400619
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400620 // notify provider of current network state
621 proxy.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624
Mike Lockwood275555c2009-05-01 11:30:34 -0400625 public void installGeocodeProvider(IGeocodeProvider provider) {
626 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
627 != PackageManager.PERMISSION_GRANTED) {
628 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwooda55c3212009-04-15 11:10:11 -0400629 }
630
631 mGeocodeProvider = provider;
632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private boolean isAllowedBySettingsLocked(String provider) {
635 if (mEnabledProviders.contains(provider)) {
636 return true;
637 }
638 if (mDisabledProviders.contains(provider)) {
639 return false;
640 }
641 // Use system settings
642 ContentResolver resolver = mContext.getContentResolver();
643 String allowedProviders = Settings.Secure.getString(resolver,
644 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
645
646 return ((allowedProviders != null) && (allowedProviders.contains(provider)));
647 }
648
649 private void checkPermissionsSafe(String provider) {
650 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400651 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 != PackageManager.PERMISSION_GRANTED)) {
653 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
654 }
655 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400656 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400658 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 != PackageManager.PERMISSION_GRANTED)) {
660 throw new SecurityException(
661 "Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission");
662 }
663 }
664
665 private boolean isAllowedProviderSafe(String provider) {
666 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400667 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 != PackageManager.PERMISSION_GRANTED)) {
669 return false;
670 }
671 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400672 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400674 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 != PackageManager.PERMISSION_GRANTED)) {
676 return false;
677 }
678
679 return true;
680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public List<String> getAllProviders() {
683 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400684 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 return _getAllProvidersLocked();
686 }
687 } catch (SecurityException se) {
688 throw se;
689 } catch (Exception e) {
690 Log.e(TAG, "getAllProviders got exception:", e);
691 return null;
692 }
693 }
694
695 private List<String> _getAllProvidersLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700696 if (LOCAL_LOGV) {
697 Log.v(TAG, "getAllProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400699 ArrayList<String> out = new ArrayList<String>(mProviders.size());
700 for (int i = mProviders.size() - 1; i >= 0; i--) {
701 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 out.add(p.getName());
703 }
704 return out;
705 }
706
707 public List<String> getProviders(boolean enabledOnly) {
708 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400709 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 return _getProvidersLocked(enabledOnly);
711 }
712 } catch (SecurityException se) {
713 throw se;
714 } catch (Exception e) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700715 Log.e(TAG, "getProviders got exception:", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 return null;
717 }
718 }
719
720 private List<String> _getProvidersLocked(boolean enabledOnly) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700721 if (LOCAL_LOGV) {
722 Log.v(TAG, "getProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400724 ArrayList<String> out = new ArrayList<String>(mProviders.size());
725 for (int i = mProviders.size() - 1; i >= 0; i--) {
726 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 String name = p.getName();
728 if (isAllowedProviderSafe(name)) {
729 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
730 continue;
731 }
732 out.add(name);
733 }
734 }
735 return out;
736 }
737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 private void updateProvidersLocked() {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400739 for (int i = mProviders.size() - 1; i >= 0; i--) {
740 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 boolean isEnabled = p.isEnabled();
742 String name = p.getName();
743 boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 if (isEnabled && !shouldBeEnabled) {
746 updateProviderListenersLocked(name, false);
747 } else if (!isEnabled && shouldBeEnabled) {
748 updateProviderListenersLocked(name, true);
749 }
750
751 }
752 }
753
754 private void updateProviderListenersLocked(String provider, boolean enabled) {
755 int listeners = 0;
756
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400757 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 if (p == null) {
759 return;
760 }
761
762 ArrayList<Receiver> deadReceivers = null;
763
764 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
765 if (records != null) {
766 final int N = records.size();
767 for (int i=0; i<N; i++) {
768 UpdateRecord record = records.get(i);
769 // Sends a notification message to the receiver
Mike Lockwood48f17512009-04-23 09:12:08 -0700770 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
771 if (deadReceivers == null) {
772 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
Simon Schoar46866572009-06-10 21:12:10 +0200774 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776 listeners++;
777 }
778 }
779
780 if (deadReceivers != null) {
781 for (int i=deadReceivers.size()-1; i>=0; i--) {
782 removeUpdatesLocked(deadReceivers.get(i));
783 }
784 }
785
786 if (enabled) {
787 p.enable();
788 if (listeners > 0) {
789 p.setMinTime(getMinTimeLocked(provider));
790 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792 } else {
793 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797
798 private long getMinTimeLocked(String provider) {
799 long minTime = Long.MAX_VALUE;
800 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
801 if (records != null) {
802 for (int i=records.size()-1; i>=0; i--) {
803 minTime = Math.min(minTime, records.get(i).mMinTime);
804 }
805 }
806 return minTime;
807 }
808
809 private class UpdateRecord {
810 final String mProvider;
811 final Receiver mReceiver;
812 final long mMinTime;
813 final float mMinDistance;
814 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400815 Location mLastFixBroadcast;
816 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 /**
819 * Note: must be constructed with lock held.
820 */
821 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400822 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 mProvider = provider;
824 mReceiver = receiver;
825 mMinTime = minTime;
826 mMinDistance = minDistance;
827 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
830 if (records == null) {
831 records = new ArrayList<UpdateRecord>();
832 mRecordsByProvider.put(provider, records);
833 }
834 if (!records.contains(this)) {
835 records.add(this);
836 }
837 }
838
839 /**
840 * Method to be called when a record will no longer be used. Calling this multiple times
841 * must have the same effect as calling it once.
842 */
843 void disposeLocked() {
844 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
845 records.remove(this);
846 }
847
848 @Override
849 public String toString() {
850 return "UpdateRecord{"
851 + Integer.toHexString(System.identityHashCode(this))
852 + " " + mProvider + " " + mReceiver + "}";
853 }
854
855 void dump(PrintWriter pw, String prefix) {
856 pw.println(prefix + this);
857 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
858 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400859 pw.println(prefix + "mUid=" + mUid);
860 pw.println(prefix + "mLastFixBroadcast:");
861 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
862 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
864
865 /**
866 * Calls dispose().
867 */
868 @Override protected void finalize() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400869 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 disposeLocked();
871 }
872 }
873 }
874
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400875 private Receiver getReceiver(ILocationListener listener) {
876 IBinder binder = listener.asBinder();
877 Receiver receiver = mReceivers.get(binder);
878 if (receiver == null) {
879 receiver = new Receiver(listener);
880 mReceivers.put(binder, receiver);
881
882 try {
883 if (receiver.isListener()) {
884 receiver.getListener().asBinder().linkToDeath(receiver, 0);
885 }
886 } catch (RemoteException e) {
887 Log.e(TAG, "linkToDeath failed:", e);
888 return null;
889 }
890 }
891 return receiver;
892 }
893
894 private Receiver getReceiver(PendingIntent intent) {
895 Receiver receiver = mReceivers.get(intent);
896 if (receiver == null) {
897 receiver = new Receiver(intent);
898 mReceivers.put(intent, receiver);
899 }
900 return receiver;
901 }
902
903 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
904 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
905 if (records != null) {
906 for (int i = records.size() - 1; i >= 0; i--) {
907 UpdateRecord record = records.get(i);
908 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
909 return true;
910 }
911 }
912 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400913 for (ProximityAlert alert : mProximityAlerts.values()) {
914 if (alert.mUid == uid) {
915 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400916 }
917 }
918 return false;
919 }
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void requestLocationUpdates(String provider,
922 long minTime, float minDistance, ILocationListener listener) {
923
924 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400925 synchronized (mLock) {
926 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928 } catch (SecurityException se) {
929 throw se;
930 } catch (Exception e) {
931 Log.e(TAG, "requestUpdates got exception:", e);
932 }
933 }
934
935 public void requestLocationUpdatesPI(String provider,
936 long minTime, float minDistance, PendingIntent intent) {
937 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400938 synchronized (mLock) {
939 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941 } catch (SecurityException se) {
942 throw se;
943 } catch (Exception e) {
944 Log.e(TAG, "requestUpdates got exception:", e);
945 }
946 }
947
948 private void requestLocationUpdatesLocked(String provider,
949 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700950 if (LOCAL_LOGV) {
951 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400954 LocationProviderProxy proxy = mProvidersByName.get(provider);
955 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 throw new IllegalArgumentException("provider=" + provider);
957 }
958
959 checkPermissionsSafe(provider);
960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 // so wakelock calls will succeed
962 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400963 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 long identity = Binder.clearCallingIdentity();
965 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400966 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
967 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if (oldRecord != null) {
969 oldRecord.disposeLocked();
970 }
971
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400972 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400973 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400974 }
975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
977 if (isProviderEnabled) {
978 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400979 proxy.setMinTime(minTimeForProvider);
980 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700982 // Notify the listener that updates are currently disabled
983 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 } finally {
986 Binder.restoreCallingIdentity(identity);
987 }
988 }
989
990 public void removeUpdates(ILocationListener listener) {
991 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400992 synchronized (mLock) {
993 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
995 } catch (SecurityException se) {
996 throw se;
997 } catch (Exception e) {
998 Log.e(TAG, "removeUpdates got exception:", e);
999 }
1000 }
1001
1002 public void removeUpdatesPI(PendingIntent intent) {
1003 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001004 synchronized (mLock) {
1005 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007 } catch (SecurityException se) {
1008 throw se;
1009 } catch (Exception e) {
1010 Log.e(TAG, "removeUpdates got exception:", e);
1011 }
1012 }
1013
1014 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001015 if (LOCAL_LOGV) {
1016 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
1018
1019 // so wakelock calls will succeed
1020 final int callingUid = Binder.getCallingUid();
1021 long identity = Binder.clearCallingIdentity();
1022 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001023 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1024 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
1027 // Record which providers were associated with this listener
1028 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001029 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 if (oldRecords != null) {
1031 // Call dispose() on the obsolete update records.
1032 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001033 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001034 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1035 if (proxy != null) {
1036 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 }
1039 record.disposeLocked();
1040 }
1041 // Accumulate providers
1042 providers.addAll(oldRecords.keySet());
1043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044
1045 // See if the providers associated with this listener have any
1046 // other listeners; if one does, inform it of the new smallest minTime
1047 // value; if one does not, disable location tracking for it
1048 for (String provider : providers) {
1049 // If provider is already disabled, don't need to do anything
1050 if (!isAllowedBySettingsLocked(provider)) {
1051 continue;
1052 }
1053
1054 boolean hasOtherListener = false;
1055 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1056 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1057 hasOtherListener = true;
1058 }
1059
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001060 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 if (p != null) {
1062 if (hasOtherListener) {
1063 p.setMinTime(getMinTimeLocked(provider));
1064 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 p.enableLocationTracking(false);
1066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 } finally {
1070 Binder.restoreCallingIdentity(identity);
1071 }
1072 }
1073
1074 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001075 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 return false;
1077 }
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001078 if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 PackageManager.PERMISSION_GRANTED) {
1080 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1081 }
1082
1083 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001084 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001086 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 return false;
1088 }
1089 return true;
1090 }
1091
1092 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001093 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001094 try {
1095 mGpsStatusProvider.removeGpsStatusListener(listener);
1096 } catch (Exception e) {
1097 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 }
1101
1102 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001103 if (provider == null) {
1104 // throw NullPointerException to remain compatible with previous implementation
1105 throw new NullPointerException();
1106 }
1107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 // first check for permission to the provider
1109 checkPermissionsSafe(provider);
1110 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001111 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 != PackageManager.PERMISSION_GRANTED)) {
1113 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1114 }
1115
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001116 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001117 LocationProviderProxy proxy = mProvidersByName.get(provider);
Mike Lockwood6ba7ae12009-08-17 08:39:12 -04001118 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 return false;
1120 }
1121
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001122 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
1124 }
1125
1126 class ProximityAlert {
1127 final int mUid;
1128 final double mLatitude;
1129 final double mLongitude;
1130 final float mRadius;
1131 final long mExpiration;
1132 final PendingIntent mIntent;
1133 final Location mLocation;
1134
1135 public ProximityAlert(int uid, double latitude, double longitude,
1136 float radius, long expiration, PendingIntent intent) {
1137 mUid = uid;
1138 mLatitude = latitude;
1139 mLongitude = longitude;
1140 mRadius = radius;
1141 mExpiration = expiration;
1142 mIntent = intent;
1143
1144 mLocation = new Location("");
1145 mLocation.setLatitude(latitude);
1146 mLocation.setLongitude(longitude);
1147 }
1148
1149 long getExpiration() {
1150 return mExpiration;
1151 }
1152
1153 PendingIntent getIntent() {
1154 return mIntent;
1155 }
1156
1157 boolean isInProximity(double latitude, double longitude) {
1158 Location loc = new Location("");
1159 loc.setLatitude(latitude);
1160 loc.setLongitude(longitude);
1161
1162 double radius = loc.distanceTo(mLocation);
1163 return radius <= mRadius;
1164 }
1165
1166 @Override
1167 public String toString() {
1168 return "ProximityAlert{"
1169 + Integer.toHexString(System.identityHashCode(this))
1170 + " uid " + mUid + mIntent + "}";
1171 }
1172
1173 void dump(PrintWriter pw, String prefix) {
1174 pw.println(prefix + this);
1175 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1176 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1177 pw.println(prefix + "mIntent=" + mIntent);
1178 pw.println(prefix + "mLocation:");
1179 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1180 }
1181 }
1182
1183 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001184 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185
1186 boolean isGpsAvailable = false;
1187
1188 // Note: this is called with the lock held.
1189 public void onLocationChanged(Location loc) {
1190
1191 // If Gps is available, then ignore updates from NetworkLocationProvider
1192 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1193 isGpsAvailable = true;
1194 }
1195 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1196 return;
1197 }
1198
1199 // Process proximity alerts
1200 long now = System.currentTimeMillis();
1201 double latitude = loc.getLatitude();
1202 double longitude = loc.getLongitude();
1203 ArrayList<PendingIntent> intentsToRemove = null;
1204
1205 for (ProximityAlert alert : mProximityAlerts.values()) {
1206 PendingIntent intent = alert.getIntent();
1207 long expiration = alert.getExpiration();
1208
1209 if ((expiration == -1) || (now <= expiration)) {
1210 boolean entered = mProximitiesEntered.contains(alert);
1211 boolean inProximity =
1212 alert.isInProximity(latitude, longitude);
1213 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001214 if (LOCAL_LOGV) {
1215 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
1217 mProximitiesEntered.add(alert);
1218 Intent enteredIntent = new Intent();
1219 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1220 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001221 synchronized (this) {
1222 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001223 // is called before decrementPendingBroadcasts()
1224 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1225 // call this after broadcasting so we do not increment
1226 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001227 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001230 if (LOCAL_LOGV) {
1231 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 if (intentsToRemove == null) {
1234 intentsToRemove = new ArrayList<PendingIntent>();
1235 }
1236 intentsToRemove.add(intent);
1237 }
1238 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001239 if (LOCAL_LOGV) {
1240 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 mProximitiesEntered.remove(alert);
1243 Intent exitedIntent = new Intent();
1244 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1245 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001246 synchronized (this) {
1247 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001248 // is called before decrementPendingBroadcasts()
1249 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1250 // call this after broadcasting so we do not increment
1251 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001252 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001255 if (LOCAL_LOGV) {
1256 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
1258 if (intentsToRemove == null) {
1259 intentsToRemove = new ArrayList<PendingIntent>();
1260 }
1261 intentsToRemove.add(intent);
1262 }
1263 }
1264 } else {
1265 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001266 if (LOCAL_LOGV) {
1267 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 if (intentsToRemove == null) {
1270 intentsToRemove = new ArrayList<PendingIntent>();
1271 }
1272 intentsToRemove.add(alert.getIntent());
1273 }
1274 }
1275
1276 // Remove expired alerts
1277 if (intentsToRemove != null) {
1278 for (PendingIntent i : intentsToRemove) {
1279 mProximityAlerts.remove(i);
1280 ProximityAlert alert = mProximityAlerts.get(i);
1281 mProximitiesEntered.remove(alert);
1282 }
1283 }
1284
1285 }
1286
1287 // Note: this is called with the lock held.
1288 public void onProviderDisabled(String provider) {
1289 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1290 isGpsAvailable = false;
1291 }
1292 }
1293
1294 // Note: this is called with the lock held.
1295 public void onProviderEnabled(String provider) {
1296 // ignore
1297 }
1298
1299 // Note: this is called with the lock held.
1300 public void onStatusChanged(String provider, int status, Bundle extras) {
1301 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1302 (status != LocationProvider.AVAILABLE)) {
1303 isGpsAvailable = false;
1304 }
1305 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001306
1307 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1308 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001309 // synchronize to ensure incrementPendingBroadcasts()
1310 // is called before decrementPendingBroadcasts()
1311 synchronized (this) {
1312 decrementPendingBroadcasts();
1313 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316
1317 public void addProximityAlert(double latitude, double longitude,
1318 float radius, long expiration, PendingIntent intent) {
1319 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001320 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1322 }
1323 } catch (SecurityException se) {
1324 throw se;
1325 } catch (Exception e) {
1326 Log.e(TAG, "addProximityAlert got exception:", e);
1327 }
1328 }
1329
1330 private void addProximityAlertLocked(double latitude, double longitude,
1331 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001332 if (LOCAL_LOGV) {
1333 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 ", longitude = " + longitude +
1335 ", expiration = " + expiration +
1336 ", intent = " + intent);
1337 }
1338
1339 // Require ability to access all providers for now
1340 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1341 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1342 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1343 }
1344
1345 if (expiration != -1) {
1346 expiration += System.currentTimeMillis();
1347 }
1348 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1349 latitude, longitude, radius, expiration, intent);
1350 mProximityAlerts.put(intent, alert);
1351
Mike Lockwood48f17512009-04-23 09:12:08 -07001352 if (mProximityReceiver == null) {
1353 mProximityListener = new ProximityListener();
1354 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355
Mike Lockwood95427cd2009-05-07 13:27:54 -04001356 for (int i = mProviders.size() - 1; i >= 0; i--) {
1357 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001358 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361 }
1362
1363 public void removeProximityAlert(PendingIntent intent) {
1364 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001365 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 removeProximityAlertLocked(intent);
1367 }
1368 } catch (SecurityException se) {
1369 throw se;
1370 } catch (Exception e) {
1371 Log.e(TAG, "removeProximityAlert got exception:", e);
1372 }
1373 }
1374
1375 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001376 if (LOCAL_LOGV) {
1377 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
1379
1380 mProximityAlerts.remove(intent);
1381 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001382 removeUpdatesLocked(mProximityReceiver);
1383 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386 }
1387
1388 /**
1389 * @return null if the provider does not exits
1390 * @throw SecurityException if the provider is not allowed to be
1391 * accessed by the caller
1392 */
1393 public Bundle getProviderInfo(String provider) {
1394 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001395 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 return _getProviderInfoLocked(provider);
1397 }
1398 } catch (SecurityException se) {
1399 throw se;
1400 } catch (Exception e) {
1401 Log.e(TAG, "_getProviderInfo got exception:", e);
1402 return null;
1403 }
1404 }
1405
1406 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001407 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if (p == null) {
1409 return null;
1410 }
1411
1412 checkPermissionsSafe(provider);
1413
1414 Bundle b = new Bundle();
1415 b.putBoolean("network", p.requiresNetwork());
1416 b.putBoolean("satellite", p.requiresSatellite());
1417 b.putBoolean("cell", p.requiresCell());
1418 b.putBoolean("cost", p.hasMonetaryCost());
1419 b.putBoolean("altitude", p.supportsAltitude());
1420 b.putBoolean("speed", p.supportsSpeed());
1421 b.putBoolean("bearing", p.supportsBearing());
1422 b.putInt("power", p.getPowerRequirement());
1423 b.putInt("accuracy", p.getAccuracy());
1424
1425 return b;
1426 }
1427
1428 public boolean isProviderEnabled(String provider) {
1429 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001430 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 return _isProviderEnabledLocked(provider);
1432 }
1433 } catch (SecurityException se) {
1434 throw se;
1435 } catch (Exception e) {
1436 Log.e(TAG, "isProviderEnabled got exception:", e);
1437 return false;
1438 }
1439 }
1440
Mike Lockwood275555c2009-05-01 11:30:34 -04001441 public void reportLocation(Location location) {
1442 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1443 != PackageManager.PERMISSION_GRANTED) {
1444 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1445 }
1446
Mike Lockwood4e50b782009-04-03 08:24:43 -07001447 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1448 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1449 mLocationHandler.sendMessageAtFrontOfQueue(m);
1450 }
1451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 private boolean _isProviderEnabledLocked(String provider) {
1453 checkPermissionsSafe(provider);
1454
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001455 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 if (p == null) {
1457 throw new IllegalArgumentException("provider=" + provider);
1458 }
1459 return isAllowedBySettingsLocked(provider);
1460 }
1461
1462 public Location getLastKnownLocation(String provider) {
1463 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001464 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 return _getLastKnownLocationLocked(provider);
1466 }
1467 } catch (SecurityException se) {
1468 throw se;
1469 } catch (Exception e) {
1470 Log.e(TAG, "getLastKnownLocation got exception:", e);
1471 return null;
1472 }
1473 }
1474
1475 private Location _getLastKnownLocationLocked(String provider) {
1476 checkPermissionsSafe(provider);
1477
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001478 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 if (p == null) {
1480 throw new IllegalArgumentException("provider=" + provider);
1481 }
1482
1483 if (!isAllowedBySettingsLocked(provider)) {
1484 return null;
1485 }
1486
1487 Location location = mLastKnownLocation.get(provider);
1488 if (location == null) {
1489 // Get the persistent last known location for the provider
1490 location = readLastKnownLocationLocked(provider);
1491 if (location != null) {
1492 mLastKnownLocation.put(provider, location);
1493 }
1494 }
1495
1496 return location;
1497 }
1498
1499 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1500 // Always broadcast the first update
1501 if (lastLoc == null) {
1502 return true;
1503 }
1504
1505 // Don't broadcast same location again regardless of condition
1506 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1507 if (loc.getTime() == lastLoc.getTime()) {
1508 return false;
1509 }
1510
1511 // Check whether sufficient distance has been traveled
1512 double minDistance = record.mMinDistance;
1513 if (minDistance > 0.0) {
1514 if (loc.distanceTo(lastLoc) <= minDistance) {
1515 return false;
1516 }
1517 }
1518
1519 return true;
1520 }
1521
Mike Lockwood4e50b782009-04-03 08:24:43 -07001522 private void handleLocationChangedLocked(Location location) {
1523 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1525 if (records == null || records.size() == 0) {
1526 return;
1527 }
1528
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001529 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (p == null) {
1531 return;
1532 }
1533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001535 Location lastLocation = mLastKnownLocation.get(provider);
1536 if (lastLocation == null) {
1537 mLastKnownLocation.put(provider, new Location(location));
1538 } else {
1539 lastLocation.set(location);
1540 }
1541 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 // Fetch latest status update time
1544 long newStatusUpdateTime = p.getStatusUpdateTime();
1545
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001546 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 Bundle extras = new Bundle();
1548 int status = p.getStatus(extras);
1549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 ArrayList<Receiver> deadReceivers = null;
1551
1552 // Broadcast location or status to all listeners
1553 final int N = records.size();
1554 for (int i=0; i<N; i++) {
1555 UpdateRecord r = records.get(i);
1556 Receiver receiver = r.mReceiver;
1557
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001558 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001559 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1560 if (lastLoc == null) {
1561 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001562 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001563 } else {
1564 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001566 if (!receiver.callLocationChangedLocked(location)) {
1567 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1568 if (deadReceivers == null) {
1569 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001571 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 }
1574
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001575 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1577 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1578
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001579 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1581 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1582 if (deadReceivers == null) {
1583 deadReceivers = new ArrayList<Receiver>();
1584 }
1585 if (!deadReceivers.contains(receiver)) {
1586 deadReceivers.add(receiver);
1587 }
1588 }
1589 }
1590 }
1591
1592 if (deadReceivers != null) {
1593 for (int i=deadReceivers.size()-1; i>=0; i--) {
1594 removeUpdatesLocked(deadReceivers.get(i));
1595 }
1596 }
1597 }
1598
1599 private class LocationWorkerHandler extends Handler {
1600
1601 @Override
1602 public void handleMessage(Message msg) {
1603 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001604 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1605 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001607 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001608 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001609 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001610
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001611 // notify other providers of the new location
1612 for (int i = mProviders.size() - 1; i >= 0; i--) {
1613 LocationProviderProxy proxy = mProviders.get(i);
1614 if (!provider.equals(proxy.getName())) {
1615 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001616 }
1617 }
1618
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001619 if (isAllowedBySettingsLocked(provider)) {
1620 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624 } catch (Exception e) {
1625 // Log, don't crash!
1626 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1627 }
1628 }
1629 }
1630
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001631 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1632 @Override
1633 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 String action = intent.getAction();
1635
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001636 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001638 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1640 if (uid >= 0) {
1641 ArrayList<Receiver> removedRecs = null;
1642 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1643 for (int j=i.size()-1; j>=0; j--) {
1644 UpdateRecord ur = i.get(j);
1645 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1646 if (removedRecs == null) {
1647 removedRecs = new ArrayList<Receiver>();
1648 }
1649 if (!removedRecs.contains(ur.mReceiver)) {
1650 removedRecs.add(ur.mReceiver);
1651 }
1652 }
1653 }
1654 }
1655 ArrayList<ProximityAlert> removedAlerts = null;
1656 for (ProximityAlert i : mProximityAlerts.values()) {
1657 if (i.mUid == uid) {
1658 if (removedAlerts == null) {
1659 removedAlerts = new ArrayList<ProximityAlert>();
1660 }
1661 if (!removedAlerts.contains(i)) {
1662 removedAlerts.add(i);
1663 }
1664 }
1665 }
1666 if (removedRecs != null) {
1667 for (int i=removedRecs.size()-1; i>=0; i--) {
1668 removeUpdatesLocked(removedRecs.get(i));
1669 }
1670 }
1671 if (removedAlerts != null) {
1672 for (int i=removedAlerts.size()-1; i>=0; i--) {
1673 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1674 }
1675 }
1676 }
1677 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001678 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 boolean noConnectivity =
1680 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1681 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001682 mNetworkState = LocationProvider.AVAILABLE;
1683 } else {
1684 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686
1687 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001688 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001689 for (int i = mProviders.size() - 1; i >= 0; i--) {
1690 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001692 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694 }
1695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001698 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699
1700 // Wake locks
1701
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001702 private void incrementPendingBroadcasts() {
1703 synchronized (mWakeLock) {
1704 if (mPendingBroadcasts++ == 0) {
1705 try {
1706 mWakeLock.acquire();
1707 log("Acquired wakelock");
1708 } catch (Exception e) {
1709 // This is to catch a runtime exception thrown when we try to release an
1710 // already released lock.
1711 Log.e(TAG, "exception in acquireWakeLock()", e);
1712 }
1713 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001714 }
1715 }
1716
1717 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001718 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001719 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001720 try {
1721 // Release wake lock
1722 if (mWakeLock.isHeld()) {
1723 mWakeLock.release();
1724 log("Released wakelock");
1725 } else {
1726 log("Can't release wakelock again!");
1727 }
1728 } catch (Exception e) {
1729 // This is to catch a runtime exception thrown when we try to release an
1730 // already released lock.
1731 Log.e(TAG, "exception in releaseWakeLock()", e);
1732 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001733 }
1734 }
1735 }
1736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 // Geocoder
1738
1739 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001740 String language, String country, String variant, String appName, List<Address> addrs) {
1741 if (mGeocodeProvider != null) {
1742 try {
1743 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1744 variant, appName, addrs);
1745 } catch (RemoteException e) {
1746 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001747 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
1749 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001750 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752
Mike Lockwooda55c3212009-04-15 11:10:11 -04001753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001755 double lowerLeftLatitude, double lowerLeftLongitude,
1756 double upperRightLatitude, double upperRightLongitude, int maxResults,
1757 String language, String country, String variant, String appName, List<Address> addrs) {
1758
1759 if (mGeocodeProvider != null) {
1760 try {
1761 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1762 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1763 maxResults, language, country, variant, appName, addrs);
1764 } catch (RemoteException e) {
1765 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001766 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
1768 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001769 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771
1772 // Mock Providers
1773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 private void checkMockPermissionsSafe() {
1775 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1776 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1777 if (!allowMocks) {
1778 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1779 }
1780
1781 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1782 PackageManager.PERMISSION_GRANTED) {
1783 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1784 }
1785 }
1786
1787 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1788 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1789 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1790 checkMockPermissionsSafe();
1791
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001792 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001793 MockProvider provider = new MockProvider(name, this,
1794 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 requiresCell, hasMonetaryCost, supportsAltitude,
1796 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001797 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1799 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001800
1801 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1802 long identity = Binder.clearCallingIdentity();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001803 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001804 mMockProviders.put(name, provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 updateProvidersLocked();
Mike Lockwood95427cd2009-05-07 13:27:54 -04001806 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808 }
1809
1810 public void removeTestProvider(String provider) {
1811 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001812 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001813 MockProvider mockProvider = mMockProviders.get(provider);
1814 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1816 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001817 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001818 mMockProviders.remove(mockProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 updateProvidersLocked();
1820 }
1821 }
1822
1823 public void setTestProviderLocation(String provider, Location loc) {
1824 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001825 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001826 MockProvider mockProvider = mMockProviders.get(provider);
1827 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1829 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001830 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1831 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001832 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001833 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
1835 }
1836
1837 public void clearTestProviderLocation(String provider) {
1838 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001839 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001840 MockProvider mockProvider = mMockProviders.get(provider);
1841 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1843 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001844 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
1846 }
1847
1848 public void setTestProviderEnabled(String provider, boolean enabled) {
1849 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001850 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001851 MockProvider mockProvider = mMockProviders.get(provider);
1852 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1854 }
1855 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001856 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 mEnabledProviders.add(provider);
1858 mDisabledProviders.remove(provider);
1859 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001860 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 mEnabledProviders.remove(provider);
1862 mDisabledProviders.add(provider);
1863 }
1864 updateProvidersLocked();
1865 }
1866 }
1867
1868 public void clearTestProviderEnabled(String provider) {
1869 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001870 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001871 MockProvider mockProvider = mMockProviders.get(provider);
1872 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1874 }
1875 mEnabledProviders.remove(provider);
1876 mDisabledProviders.remove(provider);
1877 updateProvidersLocked();
1878 }
1879 }
1880
1881 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1882 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001883 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001884 MockProvider mockProvider = mMockProviders.get(provider);
1885 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1887 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001888 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890 }
1891
1892 public void clearTestProviderStatus(String provider) {
1893 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001894 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001895 MockProvider mockProvider = mMockProviders.get(provider);
1896 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1898 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001899 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
1901 }
1902
1903 private void log(String log) {
1904 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1905 Log.d(TAG, log);
1906 }
1907 }
1908
1909 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1910 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1911 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001912 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 + Binder.getCallingPid()
1914 + ", uid=" + Binder.getCallingUid());
1915 return;
1916 }
1917
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001918 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 pw.println("Current Location Manager state:");
1920 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001922 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001924 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001927 for (Receiver i : mReceivers.values()) {
1928 pw.println(" " + i + ":");
1929 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 pw.println(" " + j.getKey() + ":");
1931 j.getValue().dump(pw, " ");
1932 }
1933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 pw.println(" Records by Provider:");
1935 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1936 : mRecordsByProvider.entrySet()) {
1937 pw.println(" " + i.getKey() + ":");
1938 for (UpdateRecord j : i.getValue()) {
1939 pw.println(" " + j + ":");
1940 j.dump(pw, " ");
1941 }
1942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 pw.println(" Last Known Locations:");
1944 for (Map.Entry<String, Location> i
1945 : mLastKnownLocation.entrySet()) {
1946 pw.println(" " + i.getKey() + ":");
1947 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1948 }
1949 if (mProximityAlerts.size() > 0) {
1950 pw.println(" Proximity Alerts:");
1951 for (Map.Entry<PendingIntent, ProximityAlert> i
1952 : mProximityAlerts.entrySet()) {
1953 pw.println(" " + i.getKey() + ":");
1954 i.getValue().dump(pw, " ");
1955 }
1956 }
1957 if (mProximitiesEntered.size() > 0) {
1958 pw.println(" Proximities Entered:");
1959 for (ProximityAlert i : mProximitiesEntered) {
1960 pw.println(" " + i + ":");
1961 i.dump(pw, " ");
1962 }
1963 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001964 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 pw.println(" mProximityListener=" + mProximityListener);
1966 if (mEnabledProviders.size() > 0) {
1967 pw.println(" Enabled Providers:");
1968 for (String i : mEnabledProviders) {
1969 pw.println(" " + i);
1970 }
1971
1972 }
1973 if (mDisabledProviders.size() > 0) {
1974 pw.println(" Disabled Providers:");
1975 for (String i : mDisabledProviders) {
1976 pw.println(" " + i);
1977 }
1978
1979 }
1980 if (mMockProviders.size() > 0) {
1981 pw.println(" Mock Providers:");
1982 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001983 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
1987 }
1988}