blob: 3f268c933f14fabc0950657b43d7e4df1e463cf8 [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
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700123 // Cache the real providers for use in addTestProvider() and removeTestProvider()
124 LocationProviderProxy mNetworkLocationProvider;
125 LocationProviderProxy mGpsLocationProvider;
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 // Handler messages
Mike Lockwood4e50b782009-04-03 08:24:43 -0700128 private static final int MESSAGE_LOCATION_CHANGED = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400130 // wakelock variables
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 private final static String WAKELOCK_KEY = "LocationManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 private PowerManager.WakeLock mWakeLock = null;
Mike Lockwood48f17512009-04-23 09:12:08 -0700133 private int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400136 * List of all receivers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400138 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400140
141 /**
142 * List of location providers.
143 */
144 private final ArrayList<LocationProviderProxy> mProviders =
145 new ArrayList<LocationProviderProxy>();
146 private final HashMap<String, LocationProviderProxy> mProvidersByName
147 = new HashMap<String, LocationProviderProxy>();
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400150 * Object used internally for synchronization
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400152 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 /**
155 * Mapping from provider name to all its UpdateRecords
156 */
157 private final HashMap<String,ArrayList<UpdateRecord>> mRecordsByProvider =
158 new HashMap<String,ArrayList<UpdateRecord>>();
159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 // Proximity listeners
Mike Lockwood48f17512009-04-23 09:12:08 -0700161 private Receiver mProximityReceiver = null;
162 private ILocationListener mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 private HashMap<PendingIntent,ProximityAlert> mProximityAlerts =
164 new HashMap<PendingIntent,ProximityAlert>();
165 private HashSet<ProximityAlert> mProximitiesEntered =
166 new HashSet<ProximityAlert>();
167
168 // Last known location for each provider
169 private HashMap<String,Location> mLastKnownLocation =
170 new HashMap<String,Location>();
171
The Android Open Source Project4df24232009-03-05 14:34:35 -0800172 private int mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800173
Mike Lockwood9637d472009-04-02 21:41:57 -0700174 // for Settings change notification
175 private ContentQueryMap mSettings;
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 /**
178 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
179 * location updates.
180 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700181 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final ILocationListener mListener;
183 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final Object mKey;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400185 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Mike Lockwood48f17512009-04-23 09:12:08 -0700186 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400188 Receiver(ILocationListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 mListener = listener;
190 mPendingIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 mKey = listener.asBinder();
192 }
193
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400194 Receiver(PendingIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 mPendingIntent = intent;
196 mListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 mKey = intent;
198 }
199
200 @Override
201 public boolean equals(Object otherObj) {
202 if (otherObj instanceof Receiver) {
203 return mKey.equals(
204 ((Receiver)otherObj).mKey);
205 }
206 return false;
207 }
208
209 @Override
210 public int hashCode() {
211 return mKey.hashCode();
212 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 @Override
215 public String toString() {
216 if (mListener != null) {
217 return "Receiver{"
218 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400219 + " Listener " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 } else {
221 return "Receiver{"
222 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400223 + " Intent " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 }
225 }
226
227 public boolean isListener() {
228 return mListener != null;
229 }
230
231 public boolean isPendingIntent() {
232 return mPendingIntent != null;
233 }
234
235 public ILocationListener getListener() {
236 if (mListener != null) {
237 return mListener;
238 }
239 throw new IllegalStateException("Request for non-existent listener");
240 }
241
242 public PendingIntent getPendingIntent() {
243 if (mPendingIntent != null) {
244 return mPendingIntent;
245 }
246 throw new IllegalStateException("Request for non-existent intent");
247 }
248
249 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
250 if (mListener != null) {
251 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700252 synchronized (this) {
253 // synchronize to ensure incrementPendingBroadcastsLocked()
254 // is called before decrementPendingBroadcasts()
255 mListener.onStatusChanged(provider, status, extras);
256 if (mListener != mProximityListener) {
257 // call this after broadcasting so we do not increment
258 // if we throw an exeption.
259 incrementPendingBroadcastsLocked();
260 }
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 } catch (RemoteException e) {
263 return false;
264 }
265 } else {
266 Intent statusChanged = new Intent();
267 statusChanged.putExtras(extras);
268 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
269 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700270 synchronized (this) {
271 // synchronize to ensure incrementPendingBroadcastsLocked()
272 // is called before decrementPendingBroadcasts()
273 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler);
274 // call this after broadcasting so we do not increment
275 // if we throw an exeption.
276 incrementPendingBroadcastsLocked();
277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 } catch (PendingIntent.CanceledException e) {
279 return false;
280 }
281 }
282 return true;
283 }
284
285 public boolean callLocationChangedLocked(Location location) {
286 if (mListener != null) {
287 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700288 synchronized (this) {
289 // synchronize to ensure incrementPendingBroadcastsLocked()
290 // is called before decrementPendingBroadcasts()
291 mListener.onLocationChanged(location);
292 if (mListener != mProximityListener) {
293 // call this after broadcasting so we do not increment
294 // if we throw an exeption.
295 incrementPendingBroadcastsLocked();
296 }
297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 } catch (RemoteException e) {
299 return false;
300 }
301 } else {
302 Intent locationChanged = new Intent();
303 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
304 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700305 synchronized (this) {
306 // synchronize to ensure incrementPendingBroadcastsLocked()
307 // is called before decrementPendingBroadcasts()
308 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler);
309 // call this after broadcasting so we do not increment
310 // if we throw an exeption.
311 incrementPendingBroadcastsLocked();
312 }
313 } catch (PendingIntent.CanceledException e) {
314 return false;
315 }
316 }
317 return true;
318 }
319
320 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
321 if (mListener != null) {
322 try {
323 synchronized (this) {
324 // synchronize to ensure incrementPendingBroadcastsLocked()
325 // is called before decrementPendingBroadcasts()
326 if (enabled) {
327 mListener.onProviderEnabled(provider);
328 } else {
329 mListener.onProviderDisabled(provider);
330 }
331 if (mListener != mProximityListener) {
332 // call this after broadcasting so we do not increment
333 // if we throw an exeption.
334 incrementPendingBroadcastsLocked();
335 }
336 }
337 } catch (RemoteException e) {
338 return false;
339 }
340 } else {
341 Intent providerIntent = new Intent();
342 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
343 try {
344 synchronized (this) {
345 // synchronize to ensure incrementPendingBroadcastsLocked()
346 // is called before decrementPendingBroadcasts()
347 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler);
348 // call this after broadcasting so we do not increment
349 // if we throw an exeption.
350 incrementPendingBroadcastsLocked();
351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 } catch (PendingIntent.CanceledException e) {
353 return false;
354 }
355 }
356 return true;
357 }
358
359 public void binderDied() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700360 if (LOCAL_LOGV) {
361 Log.v(TAG, "Location listener died");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400363 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 removeUpdatesLocked(this);
365 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700366 synchronized (this) {
367 if (mPendingBroadcasts > 0) {
368 LocationManagerService.this.decrementPendingBroadcasts();
369 mPendingBroadcasts = 0;
370 }
371 }
372 }
373
374 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
375 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400376 synchronized (this) {
377 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700378 }
379 }
380
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400381 // this must be called while synchronized by caller in a synchronized block
382 // containing the sending of the broadcaset
383 private void incrementPendingBroadcastsLocked() {
384 if (mPendingBroadcasts++ == 0) {
385 LocationManagerService.this.incrementPendingBroadcasts();
386 }
387 }
388
389 private void decrementPendingBroadcastsLocked() {
390 if (--mPendingBroadcasts == 0) {
391 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700392 }
393 }
394 }
395
396 public void locationCallbackFinished(ILocationListener listener) {
397 Receiver receiver = getReceiver(listener);
398 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400399 synchronized (receiver) {
400 // so wakelock calls will succeed
401 long identity = Binder.clearCallingIdentity();
402 receiver.decrementPendingBroadcastsLocked();
403 Binder.restoreCallingIdentity(identity);
404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406 }
407
Mike Lockwood9637d472009-04-02 21:41:57 -0700408 private final class SettingsObserver implements Observer {
409 public void update(Observable o, Object arg) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400410 synchronized (mLock) {
Mike Lockwood9637d472009-04-02 21:41:57 -0700411 updateProvidersLocked();
412 }
413 }
414 }
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 private Location readLastKnownLocationLocked(String provider) {
417 Location location = null;
418 String s = null;
419 try {
420 File f = new File(LocationManager.SYSTEM_DIR + "/location."
421 + provider);
422 if (!f.exists()) {
423 return null;
424 }
425 BufferedReader reader = new BufferedReader(new FileReader(f), 256);
426 s = reader.readLine();
427 } catch (IOException e) {
428 Log.w(TAG, "Unable to read last known location", e);
429 }
430
431 if (s == null) {
432 return null;
433 }
434 try {
435 String[] tokens = PATTERN_COMMA.split(s);
436 int idx = 0;
437 long time = Long.parseLong(tokens[idx++]);
438 double latitude = Double.parseDouble(tokens[idx++]);
439 double longitude = Double.parseDouble(tokens[idx++]);
440 double altitude = Double.parseDouble(tokens[idx++]);
441 float bearing = Float.parseFloat(tokens[idx++]);
442 float speed = Float.parseFloat(tokens[idx++]);
443
444 location = new Location(provider);
445 location.setTime(time);
446 location.setLatitude(latitude);
447 location.setLongitude(longitude);
448 location.setAltitude(altitude);
449 location.setBearing(bearing);
450 location.setSpeed(speed);
451 } catch (NumberFormatException nfe) {
452 Log.e(TAG, "NumberFormatException reading last known location", nfe);
453 return null;
454 }
455
456 return location;
457 }
458
459 private void writeLastKnownLocationLocked(String provider,
460 Location location) {
461 long now = SystemClock.elapsedRealtime();
462 Long last = mLastWriteTime.get(provider);
463 if ((last != null)
464 && (now - last.longValue() < MIN_LAST_KNOWN_LOCATION_TIME)) {
465 return;
466 }
467 mLastWriteTime.put(provider, now);
468
469 StringBuilder sb = new StringBuilder(100);
470 sb.append(location.getTime());
471 sb.append(',');
472 sb.append(location.getLatitude());
473 sb.append(',');
474 sb.append(location.getLongitude());
475 sb.append(',');
476 sb.append(location.getAltitude());
477 sb.append(',');
478 sb.append(location.getBearing());
479 sb.append(',');
480 sb.append(location.getSpeed());
481
482 FileWriter writer = null;
483 try {
484 File d = new File(LocationManager.SYSTEM_DIR);
485 if (!d.exists()) {
486 if (!d.mkdirs()) {
487 Log.w(TAG, "Unable to create directory to write location");
488 return;
489 }
490 }
491 File f = new File(LocationManager.SYSTEM_DIR + "/location." + provider);
492 writer = new FileWriter(f);
493 writer.write(sb.toString());
494 } catch (IOException e) {
495 Log.w(TAG, "Unable to write location", e);
496 } finally {
497 if (writer != null) {
498 try {
499 writer.close();
500 } catch (IOException e) {
501 Log.w(TAG, "Exception closing file", e);
502 }
503 }
504 }
505 }
506
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400507 private void addProvider(LocationProviderProxy provider) {
508 mProviders.add(provider);
509 mProvidersByName.put(provider.getName(), provider);
510 }
511
512 private void removeProvider(LocationProviderProxy provider) {
513 mProviders.remove(provider);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700514 provider.unlinkProvider();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400515 mProvidersByName.remove(provider.getName());
516 }
517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400519 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 if (sProvidersLoaded) {
521 return;
522 }
523
524 // Load providers
525 loadProvidersLocked();
526 sProvidersLoaded = true;
527 }
528 }
529
530 private void loadProvidersLocked() {
531 try {
532 _loadProvidersLocked();
533 } catch (Exception e) {
534 Log.e(TAG, "Exception loading providers:", e);
535 }
536 }
537
538 private void _loadProvidersLocked() {
539 // Attempt to load "real" providers first
540 if (GpsLocationProvider.isSupported()) {
541 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400542 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
543 mGpsStatusProvider = provider.getGpsStatusProvider();
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400544 LocationProviderProxy proxy = new LocationProviderProxy(LocationManager.GPS_PROVIDER, provider);
545 addProvider(proxy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700546 mGpsLocationProvider = proxy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 updateProvidersLocked();
550 }
551
552 /**
553 * @param context the context that the LocationManagerService runs in
554 */
555 public LocationManagerService(Context context) {
556 super();
557 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700558
559 Thread thread = new Thread(null, this, "LocationManagerService");
560 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
The Android Open Source Project10592532009-03-18 17:39:46 -0700562 if (LOCAL_LOGV) {
563 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566
Mike Lockwood3d12b512009-04-21 23:25:35 -0700567 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 // Create a wake lock, needs to be done before calling loadProviders() below
569 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
570 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 // Load providers
573 loadProviders();
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400577 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
578 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
580 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400581 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582
Mike Lockwood9637d472009-04-02 21:41:57 -0700583 // listen for settings changes
584 ContentResolver resolver = mContext.getContentResolver();
585 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
586 "(" + Settings.System.NAME + "=?)",
587 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
588 null);
589 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
590 SettingsObserver settingsObserver = new SettingsObserver();
591 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593
Mike Lockwood3d12b512009-04-21 23:25:35 -0700594 public void run()
595 {
596 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
597 Looper.prepare();
598 mLocationHandler = new LocationWorkerHandler();
599 initialize();
600 Looper.loop();
601 }
602
Mike Lockwood275555c2009-05-01 11:30:34 -0400603 public void installLocationProvider(String name, ILocationProvider provider) {
604 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
605 != PackageManager.PERMISSION_GRANTED) {
606 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700607 }
608
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400609 synchronized (mLock) {
Mike Lockwood3681f262009-05-12 10:52:03 -0400610 // check to see if we are reinstalling a dead provider
611 LocationProviderProxy oldProvider = mProvidersByName.get(name);
612 if (oldProvider != null) {
613 if (oldProvider.isDead()) {
614 Log.d(TAG, "replacing dead provider");
615 removeProvider(oldProvider);
616 } else {
617 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
618 }
619 }
620
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400621 LocationProviderProxy proxy = new LocationProviderProxy(name, provider);
622 addProvider(proxy);
623 updateProvidersLocked();
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700624 if (LocationManager.NETWORK_PROVIDER.equals(name)) {
625 mNetworkLocationProvider = proxy;
626 }
Mike Lockwood275555c2009-05-01 11:30:34 -0400627
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400628 // notify provider of current network state
629 proxy.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631 }
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)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400659 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 != PackageManager.PERMISSION_GRANTED)) {
661 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
662 }
663 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400664 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400666 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 != 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)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400675 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 != PackageManager.PERMISSION_GRANTED)) {
677 return false;
678 }
679 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400680 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400682 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 != 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>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
Simon Schoar46866572009-06-10 21:12:10 +0200782 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784 listeners++;
785 }
786 }
787
788 if (deadReceivers != null) {
789 for (int i=deadReceivers.size()-1; i>=0; i--) {
790 removeUpdatesLocked(deadReceivers.get(i));
791 }
792 }
793
794 if (enabled) {
795 p.enable();
796 if (listeners > 0) {
797 p.setMinTime(getMinTimeLocked(provider));
798 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800 } else {
801 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
806 private long getMinTimeLocked(String provider) {
807 long minTime = Long.MAX_VALUE;
808 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
809 if (records != null) {
810 for (int i=records.size()-1; i>=0; i--) {
811 minTime = Math.min(minTime, records.get(i).mMinTime);
812 }
813 }
814 return minTime;
815 }
816
817 private class UpdateRecord {
818 final String mProvider;
819 final Receiver mReceiver;
820 final long mMinTime;
821 final float mMinDistance;
822 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400823 Location mLastFixBroadcast;
824 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825
826 /**
827 * Note: must be constructed with lock held.
828 */
829 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400830 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 mProvider = provider;
832 mReceiver = receiver;
833 mMinTime = minTime;
834 mMinDistance = minDistance;
835 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
837 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
838 if (records == null) {
839 records = new ArrayList<UpdateRecord>();
840 mRecordsByProvider.put(provider, records);
841 }
842 if (!records.contains(this)) {
843 records.add(this);
844 }
845 }
846
847 /**
848 * Method to be called when a record will no longer be used. Calling this multiple times
849 * must have the same effect as calling it once.
850 */
851 void disposeLocked() {
852 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
853 records.remove(this);
854 }
855
856 @Override
857 public String toString() {
858 return "UpdateRecord{"
859 + Integer.toHexString(System.identityHashCode(this))
860 + " " + mProvider + " " + mReceiver + "}";
861 }
862
863 void dump(PrintWriter pw, String prefix) {
864 pw.println(prefix + this);
865 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
866 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400867 pw.println(prefix + "mUid=" + mUid);
868 pw.println(prefix + "mLastFixBroadcast:");
869 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
870 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872
873 /**
874 * Calls dispose().
875 */
876 @Override protected void finalize() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400877 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 disposeLocked();
879 }
880 }
881 }
882
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400883 private Receiver getReceiver(ILocationListener listener) {
884 IBinder binder = listener.asBinder();
885 Receiver receiver = mReceivers.get(binder);
886 if (receiver == null) {
887 receiver = new Receiver(listener);
888 mReceivers.put(binder, receiver);
889
890 try {
891 if (receiver.isListener()) {
892 receiver.getListener().asBinder().linkToDeath(receiver, 0);
893 }
894 } catch (RemoteException e) {
895 Log.e(TAG, "linkToDeath failed:", e);
896 return null;
897 }
898 }
899 return receiver;
900 }
901
902 private Receiver getReceiver(PendingIntent intent) {
903 Receiver receiver = mReceivers.get(intent);
904 if (receiver == null) {
905 receiver = new Receiver(intent);
906 mReceivers.put(intent, receiver);
907 }
908 return receiver;
909 }
910
911 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
912 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
913 if (records != null) {
914 for (int i = records.size() - 1; i >= 0; i--) {
915 UpdateRecord record = records.get(i);
916 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
917 return true;
918 }
919 }
920 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400921 for (ProximityAlert alert : mProximityAlerts.values()) {
922 if (alert.mUid == uid) {
923 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400924 }
925 }
926 return false;
927 }
928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 public void requestLocationUpdates(String provider,
930 long minTime, float minDistance, ILocationListener listener) {
931
932 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400933 synchronized (mLock) {
934 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 } catch (SecurityException se) {
937 throw se;
938 } catch (Exception e) {
939 Log.e(TAG, "requestUpdates got exception:", e);
940 }
941 }
942
943 public void requestLocationUpdatesPI(String provider,
944 long minTime, float minDistance, PendingIntent intent) {
945 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400946 synchronized (mLock) {
947 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949 } catch (SecurityException se) {
950 throw se;
951 } catch (Exception e) {
952 Log.e(TAG, "requestUpdates got exception:", e);
953 }
954 }
955
956 private void requestLocationUpdatesLocked(String provider,
957 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700958 if (LOCAL_LOGV) {
959 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400962 LocationProviderProxy proxy = mProvidersByName.get(provider);
963 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 throw new IllegalArgumentException("provider=" + provider);
965 }
966
967 checkPermissionsSafe(provider);
968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 // so wakelock calls will succeed
970 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400971 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 long identity = Binder.clearCallingIdentity();
973 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400974 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
975 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 if (oldRecord != null) {
977 oldRecord.disposeLocked();
978 }
979
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400980 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400981 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400982 }
983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
985 if (isProviderEnabled) {
986 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400987 proxy.setMinTime(minTimeForProvider);
988 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700990 // Notify the listener that updates are currently disabled
991 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993 } finally {
994 Binder.restoreCallingIdentity(identity);
995 }
996 }
997
998 public void removeUpdates(ILocationListener listener) {
999 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001000 synchronized (mLock) {
1001 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 } catch (SecurityException se) {
1004 throw se;
1005 } catch (Exception e) {
1006 Log.e(TAG, "removeUpdates got exception:", e);
1007 }
1008 }
1009
1010 public void removeUpdatesPI(PendingIntent intent) {
1011 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001012 synchronized (mLock) {
1013 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015 } catch (SecurityException se) {
1016 throw se;
1017 } catch (Exception e) {
1018 Log.e(TAG, "removeUpdates got exception:", e);
1019 }
1020 }
1021
1022 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001023 if (LOCAL_LOGV) {
1024 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
1027 // so wakelock calls will succeed
1028 final int callingUid = Binder.getCallingUid();
1029 long identity = Binder.clearCallingIdentity();
1030 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001031 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1032 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
1035 // Record which providers were associated with this listener
1036 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001037 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 if (oldRecords != null) {
1039 // Call dispose() on the obsolete update records.
1040 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001041 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001042 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1043 if (proxy != null) {
1044 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 }
1047 record.disposeLocked();
1048 }
1049 // Accumulate providers
1050 providers.addAll(oldRecords.keySet());
1051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052
1053 // See if the providers associated with this listener have any
1054 // other listeners; if one does, inform it of the new smallest minTime
1055 // value; if one does not, disable location tracking for it
1056 for (String provider : providers) {
1057 // If provider is already disabled, don't need to do anything
1058 if (!isAllowedBySettingsLocked(provider)) {
1059 continue;
1060 }
1061
1062 boolean hasOtherListener = false;
1063 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1064 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1065 hasOtherListener = true;
1066 }
1067
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001068 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 if (p != null) {
1070 if (hasOtherListener) {
1071 p.setMinTime(getMinTimeLocked(provider));
1072 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 p.enableLocationTracking(false);
1074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 } finally {
1078 Binder.restoreCallingIdentity(identity);
1079 }
1080 }
1081
1082 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001083 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 return false;
1085 }
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001086 if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 PackageManager.PERMISSION_GRANTED) {
1088 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1089 }
1090
1091 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001092 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001094 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 return false;
1096 }
1097 return true;
1098 }
1099
1100 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001101 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001102 try {
1103 mGpsStatusProvider.removeGpsStatusListener(listener);
1104 } catch (Exception e) {
1105 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108 }
1109
1110 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
1111 // first check for permission to the provider
1112 checkPermissionsSafe(provider);
1113 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001114 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 != PackageManager.PERMISSION_GRANTED)) {
1116 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1117 }
1118
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001119 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001120 LocationProviderProxy proxy = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 if (provider == null) {
1122 return false;
1123 }
1124
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001125 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
1128
1129 class ProximityAlert {
1130 final int mUid;
1131 final double mLatitude;
1132 final double mLongitude;
1133 final float mRadius;
1134 final long mExpiration;
1135 final PendingIntent mIntent;
1136 final Location mLocation;
1137
1138 public ProximityAlert(int uid, double latitude, double longitude,
1139 float radius, long expiration, PendingIntent intent) {
1140 mUid = uid;
1141 mLatitude = latitude;
1142 mLongitude = longitude;
1143 mRadius = radius;
1144 mExpiration = expiration;
1145 mIntent = intent;
1146
1147 mLocation = new Location("");
1148 mLocation.setLatitude(latitude);
1149 mLocation.setLongitude(longitude);
1150 }
1151
1152 long getExpiration() {
1153 return mExpiration;
1154 }
1155
1156 PendingIntent getIntent() {
1157 return mIntent;
1158 }
1159
1160 boolean isInProximity(double latitude, double longitude) {
1161 Location loc = new Location("");
1162 loc.setLatitude(latitude);
1163 loc.setLongitude(longitude);
1164
1165 double radius = loc.distanceTo(mLocation);
1166 return radius <= mRadius;
1167 }
1168
1169 @Override
1170 public String toString() {
1171 return "ProximityAlert{"
1172 + Integer.toHexString(System.identityHashCode(this))
1173 + " uid " + mUid + mIntent + "}";
1174 }
1175
1176 void dump(PrintWriter pw, String prefix) {
1177 pw.println(prefix + this);
1178 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1179 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1180 pw.println(prefix + "mIntent=" + mIntent);
1181 pw.println(prefix + "mLocation:");
1182 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1183 }
1184 }
1185
1186 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001187 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188
1189 boolean isGpsAvailable = false;
1190
1191 // Note: this is called with the lock held.
1192 public void onLocationChanged(Location loc) {
1193
1194 // If Gps is available, then ignore updates from NetworkLocationProvider
1195 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1196 isGpsAvailable = true;
1197 }
1198 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1199 return;
1200 }
1201
1202 // Process proximity alerts
1203 long now = System.currentTimeMillis();
1204 double latitude = loc.getLatitude();
1205 double longitude = loc.getLongitude();
1206 ArrayList<PendingIntent> intentsToRemove = null;
1207
1208 for (ProximityAlert alert : mProximityAlerts.values()) {
1209 PendingIntent intent = alert.getIntent();
1210 long expiration = alert.getExpiration();
1211
1212 if ((expiration == -1) || (now <= expiration)) {
1213 boolean entered = mProximitiesEntered.contains(alert);
1214 boolean inProximity =
1215 alert.isInProximity(latitude, longitude);
1216 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001217 if (LOCAL_LOGV) {
1218 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220 mProximitiesEntered.add(alert);
1221 Intent enteredIntent = new Intent();
1222 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1223 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001224 synchronized (this) {
1225 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001226 // is called before decrementPendingBroadcasts()
1227 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1228 // call this after broadcasting so we do not increment
1229 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001230 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001233 if (LOCAL_LOGV) {
1234 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 if (intentsToRemove == null) {
1237 intentsToRemove = new ArrayList<PendingIntent>();
1238 }
1239 intentsToRemove.add(intent);
1240 }
1241 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001242 if (LOCAL_LOGV) {
1243 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245 mProximitiesEntered.remove(alert);
1246 Intent exitedIntent = new Intent();
1247 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1248 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001249 synchronized (this) {
1250 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001251 // is called before decrementPendingBroadcasts()
1252 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1253 // call this after broadcasting so we do not increment
1254 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001255 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001258 if (LOCAL_LOGV) {
1259 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 if (intentsToRemove == null) {
1262 intentsToRemove = new ArrayList<PendingIntent>();
1263 }
1264 intentsToRemove.add(intent);
1265 }
1266 }
1267 } else {
1268 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001269 if (LOCAL_LOGV) {
1270 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272 if (intentsToRemove == null) {
1273 intentsToRemove = new ArrayList<PendingIntent>();
1274 }
1275 intentsToRemove.add(alert.getIntent());
1276 }
1277 }
1278
1279 // Remove expired alerts
1280 if (intentsToRemove != null) {
1281 for (PendingIntent i : intentsToRemove) {
1282 mProximityAlerts.remove(i);
1283 ProximityAlert alert = mProximityAlerts.get(i);
1284 mProximitiesEntered.remove(alert);
1285 }
1286 }
1287
1288 }
1289
1290 // Note: this is called with the lock held.
1291 public void onProviderDisabled(String provider) {
1292 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1293 isGpsAvailable = false;
1294 }
1295 }
1296
1297 // Note: this is called with the lock held.
1298 public void onProviderEnabled(String provider) {
1299 // ignore
1300 }
1301
1302 // Note: this is called with the lock held.
1303 public void onStatusChanged(String provider, int status, Bundle extras) {
1304 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1305 (status != LocationProvider.AVAILABLE)) {
1306 isGpsAvailable = false;
1307 }
1308 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001309
1310 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1311 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001312 // synchronize to ensure incrementPendingBroadcasts()
1313 // is called before decrementPendingBroadcasts()
1314 synchronized (this) {
1315 decrementPendingBroadcasts();
1316 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
1320 public void addProximityAlert(double latitude, double longitude,
1321 float radius, long expiration, PendingIntent intent) {
1322 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001323 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1325 }
1326 } catch (SecurityException se) {
1327 throw se;
1328 } catch (Exception e) {
1329 Log.e(TAG, "addProximityAlert got exception:", e);
1330 }
1331 }
1332
1333 private void addProximityAlertLocked(double latitude, double longitude,
1334 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001335 if (LOCAL_LOGV) {
1336 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 ", longitude = " + longitude +
1338 ", expiration = " + expiration +
1339 ", intent = " + intent);
1340 }
1341
1342 // Require ability to access all providers for now
1343 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1344 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1345 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1346 }
1347
1348 if (expiration != -1) {
1349 expiration += System.currentTimeMillis();
1350 }
1351 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1352 latitude, longitude, radius, expiration, intent);
1353 mProximityAlerts.put(intent, alert);
1354
Mike Lockwood48f17512009-04-23 09:12:08 -07001355 if (mProximityReceiver == null) {
1356 mProximityListener = new ProximityListener();
1357 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
Mike Lockwood95427cd2009-05-07 13:27:54 -04001359 for (int i = mProviders.size() - 1; i >= 0; i--) {
1360 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001361 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
1365
1366 public void removeProximityAlert(PendingIntent intent) {
1367 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001368 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 removeProximityAlertLocked(intent);
1370 }
1371 } catch (SecurityException se) {
1372 throw se;
1373 } catch (Exception e) {
1374 Log.e(TAG, "removeProximityAlert got exception:", e);
1375 }
1376 }
1377
1378 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001379 if (LOCAL_LOGV) {
1380 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382
1383 mProximityAlerts.remove(intent);
1384 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001385 removeUpdatesLocked(mProximityReceiver);
1386 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389 }
1390
1391 /**
1392 * @return null if the provider does not exits
1393 * @throw SecurityException if the provider is not allowed to be
1394 * accessed by the caller
1395 */
1396 public Bundle getProviderInfo(String provider) {
1397 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001398 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 return _getProviderInfoLocked(provider);
1400 }
1401 } catch (SecurityException se) {
1402 throw se;
1403 } catch (Exception e) {
1404 Log.e(TAG, "_getProviderInfo got exception:", e);
1405 return null;
1406 }
1407 }
1408
1409 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001410 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 if (p == null) {
1412 return null;
1413 }
1414
1415 checkPermissionsSafe(provider);
1416
1417 Bundle b = new Bundle();
1418 b.putBoolean("network", p.requiresNetwork());
1419 b.putBoolean("satellite", p.requiresSatellite());
1420 b.putBoolean("cell", p.requiresCell());
1421 b.putBoolean("cost", p.hasMonetaryCost());
1422 b.putBoolean("altitude", p.supportsAltitude());
1423 b.putBoolean("speed", p.supportsSpeed());
1424 b.putBoolean("bearing", p.supportsBearing());
1425 b.putInt("power", p.getPowerRequirement());
1426 b.putInt("accuracy", p.getAccuracy());
1427
1428 return b;
1429 }
1430
1431 public boolean isProviderEnabled(String provider) {
1432 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001433 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 return _isProviderEnabledLocked(provider);
1435 }
1436 } catch (SecurityException se) {
1437 throw se;
1438 } catch (Exception e) {
1439 Log.e(TAG, "isProviderEnabled got exception:", e);
1440 return false;
1441 }
1442 }
1443
Mike Lockwood275555c2009-05-01 11:30:34 -04001444 public void reportLocation(Location location) {
1445 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1446 != PackageManager.PERMISSION_GRANTED) {
1447 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1448 }
1449
Mike Lockwood4e50b782009-04-03 08:24:43 -07001450 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1451 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1452 mLocationHandler.sendMessageAtFrontOfQueue(m);
1453 }
1454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 private boolean _isProviderEnabledLocked(String provider) {
1456 checkPermissionsSafe(provider);
1457
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001458 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 if (p == null) {
1460 throw new IllegalArgumentException("provider=" + provider);
1461 }
1462 return isAllowedBySettingsLocked(provider);
1463 }
1464
1465 public Location getLastKnownLocation(String provider) {
1466 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001467 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 return _getLastKnownLocationLocked(provider);
1469 }
1470 } catch (SecurityException se) {
1471 throw se;
1472 } catch (Exception e) {
1473 Log.e(TAG, "getLastKnownLocation got exception:", e);
1474 return null;
1475 }
1476 }
1477
1478 private Location _getLastKnownLocationLocked(String provider) {
1479 checkPermissionsSafe(provider);
1480
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001481 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (p == null) {
1483 throw new IllegalArgumentException("provider=" + provider);
1484 }
1485
1486 if (!isAllowedBySettingsLocked(provider)) {
1487 return null;
1488 }
1489
1490 Location location = mLastKnownLocation.get(provider);
1491 if (location == null) {
1492 // Get the persistent last known location for the provider
1493 location = readLastKnownLocationLocked(provider);
1494 if (location != null) {
1495 mLastKnownLocation.put(provider, location);
1496 }
1497 }
1498
1499 return location;
1500 }
1501
1502 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1503 // Always broadcast the first update
1504 if (lastLoc == null) {
1505 return true;
1506 }
1507
1508 // Don't broadcast same location again regardless of condition
1509 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1510 if (loc.getTime() == lastLoc.getTime()) {
1511 return false;
1512 }
1513
1514 // Check whether sufficient distance has been traveled
1515 double minDistance = record.mMinDistance;
1516 if (minDistance > 0.0) {
1517 if (loc.distanceTo(lastLoc) <= minDistance) {
1518 return false;
1519 }
1520 }
1521
1522 return true;
1523 }
1524
Mike Lockwood4e50b782009-04-03 08:24:43 -07001525 private void handleLocationChangedLocked(Location location) {
1526 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1528 if (records == null || records.size() == 0) {
1529 return;
1530 }
1531
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001532 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 if (p == null) {
1534 return;
1535 }
1536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001538 Location lastLocation = mLastKnownLocation.get(provider);
1539 if (lastLocation == null) {
1540 mLastKnownLocation.put(provider, new Location(location));
1541 } else {
1542 lastLocation.set(location);
1543 }
1544 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 // Fetch latest status update time
1547 long newStatusUpdateTime = p.getStatusUpdateTime();
1548
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001549 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 Bundle extras = new Bundle();
1551 int status = p.getStatus(extras);
1552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 ArrayList<Receiver> deadReceivers = null;
1554
1555 // Broadcast location or status to all listeners
1556 final int N = records.size();
1557 for (int i=0; i<N; i++) {
1558 UpdateRecord r = records.get(i);
1559 Receiver receiver = r.mReceiver;
1560
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001561 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001562 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1563 if (lastLoc == null) {
1564 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001565 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001566 } else {
1567 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001569 if (!receiver.callLocationChangedLocked(location)) {
1570 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1571 if (deadReceivers == null) {
1572 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001574 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576 }
1577
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001578 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1580 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1581
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001582 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1584 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1585 if (deadReceivers == null) {
1586 deadReceivers = new ArrayList<Receiver>();
1587 }
1588 if (!deadReceivers.contains(receiver)) {
1589 deadReceivers.add(receiver);
1590 }
1591 }
1592 }
1593 }
1594
1595 if (deadReceivers != null) {
1596 for (int i=deadReceivers.size()-1; i>=0; i--) {
1597 removeUpdatesLocked(deadReceivers.get(i));
1598 }
1599 }
1600 }
1601
1602 private class LocationWorkerHandler extends Handler {
1603
1604 @Override
1605 public void handleMessage(Message msg) {
1606 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001607 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1608 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001610 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001611 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001612 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001613
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001614 // notify other providers of the new location
1615 for (int i = mProviders.size() - 1; i >= 0; i--) {
1616 LocationProviderProxy proxy = mProviders.get(i);
1617 if (!provider.equals(proxy.getName())) {
1618 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001619 }
1620 }
1621
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001622 if (isAllowedBySettingsLocked(provider)) {
1623 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
1627 } catch (Exception e) {
1628 // Log, don't crash!
1629 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1630 }
1631 }
1632 }
1633
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001634 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1635 @Override
1636 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 String action = intent.getAction();
1638
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001639 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001641 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1643 if (uid >= 0) {
1644 ArrayList<Receiver> removedRecs = null;
1645 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1646 for (int j=i.size()-1; j>=0; j--) {
1647 UpdateRecord ur = i.get(j);
1648 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1649 if (removedRecs == null) {
1650 removedRecs = new ArrayList<Receiver>();
1651 }
1652 if (!removedRecs.contains(ur.mReceiver)) {
1653 removedRecs.add(ur.mReceiver);
1654 }
1655 }
1656 }
1657 }
1658 ArrayList<ProximityAlert> removedAlerts = null;
1659 for (ProximityAlert i : mProximityAlerts.values()) {
1660 if (i.mUid == uid) {
1661 if (removedAlerts == null) {
1662 removedAlerts = new ArrayList<ProximityAlert>();
1663 }
1664 if (!removedAlerts.contains(i)) {
1665 removedAlerts.add(i);
1666 }
1667 }
1668 }
1669 if (removedRecs != null) {
1670 for (int i=removedRecs.size()-1; i>=0; i--) {
1671 removeUpdatesLocked(removedRecs.get(i));
1672 }
1673 }
1674 if (removedAlerts != null) {
1675 for (int i=removedAlerts.size()-1; i>=0; i--) {
1676 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1677 }
1678 }
1679 }
1680 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001681 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 boolean noConnectivity =
1683 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1684 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001685 mNetworkState = LocationProvider.AVAILABLE;
1686 } else {
1687 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689
1690 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001691 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001692 for (int i = mProviders.size() - 1; i >= 0; i--) {
1693 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001695 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697 }
1698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001701 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702
1703 // Wake locks
1704
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001705 private void incrementPendingBroadcasts() {
1706 synchronized (mWakeLock) {
1707 if (mPendingBroadcasts++ == 0) {
1708 try {
1709 mWakeLock.acquire();
1710 log("Acquired wakelock");
1711 } catch (Exception e) {
1712 // This is to catch a runtime exception thrown when we try to release an
1713 // already released lock.
1714 Log.e(TAG, "exception in acquireWakeLock()", e);
1715 }
1716 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001717 }
1718 }
1719
1720 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001721 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001722 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001723 try {
1724 // Release wake lock
1725 if (mWakeLock.isHeld()) {
1726 mWakeLock.release();
1727 log("Released wakelock");
1728 } else {
1729 log("Can't release wakelock again!");
1730 }
1731 } catch (Exception e) {
1732 // This is to catch a runtime exception thrown when we try to release an
1733 // already released lock.
1734 Log.e(TAG, "exception in releaseWakeLock()", e);
1735 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001736 }
1737 }
1738 }
1739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 // Geocoder
1741
1742 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001743 String language, String country, String variant, String appName, List<Address> addrs) {
1744 if (mGeocodeProvider != null) {
1745 try {
1746 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1747 variant, appName, addrs);
1748 } catch (RemoteException e) {
1749 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001750 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001753 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
1755
Mike Lockwooda55c3212009-04-15 11:10:11 -04001756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001758 double lowerLeftLatitude, double lowerLeftLongitude,
1759 double upperRightLatitude, double upperRightLongitude, int maxResults,
1760 String language, String country, String variant, String appName, List<Address> addrs) {
1761
1762 if (mGeocodeProvider != null) {
1763 try {
1764 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1765 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1766 maxResults, language, country, variant, appName, addrs);
1767 } catch (RemoteException e) {
1768 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001769 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001772 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774
1775 // Mock Providers
1776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 private void checkMockPermissionsSafe() {
1778 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1779 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1780 if (!allowMocks) {
1781 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1782 }
1783
1784 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1785 PackageManager.PERMISSION_GRANTED) {
1786 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1787 }
1788 }
1789
1790 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1791 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1792 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1793 checkMockPermissionsSafe();
1794
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001795 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001796 MockProvider provider = new MockProvider(name, this,
1797 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 requiresCell, hasMonetaryCost, supportsAltitude,
1799 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001800 // remove the real provider if we are replacing GPS or network provider
1801 if (LocationManager.GPS_PROVIDER.equals(name)
1802 || LocationManager.NETWORK_PROVIDER.equals(name)) {
1803 LocationProviderProxy proxy = mProvidersByName.get(name);
1804 if (proxy != null) {
1805 proxy.enableLocationTracking(false);
1806 removeProvider(proxy);
1807 }
1808 }
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);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001814 mLastKnownLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 updateProvidersLocked();
1816 }
1817 }
1818
1819 public void removeTestProvider(String provider) {
1820 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001821 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001822 MockProvider mockProvider = mMockProviders.get(provider);
1823 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1825 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001826 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001827 mMockProviders.remove(mockProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001828 // reinstall real provider if we were mocking GPS or network provider
1829 if (LocationManager.GPS_PROVIDER.equals(provider) &&
1830 mGpsLocationProvider != null) {
1831 addProvider(mGpsLocationProvider);
1832 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) &&
1833 mNetworkLocationProvider != null) {
1834 addProvider(mNetworkLocationProvider);
1835 }
1836 mLastKnownLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 updateProvidersLocked();
1838 }
1839 }
1840
1841 public void setTestProviderLocation(String provider, Location loc) {
1842 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001843 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001844 MockProvider mockProvider = mMockProviders.get(provider);
1845 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1847 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001848 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1849 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001850 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001851 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853 }
1854
1855 public void clearTestProviderLocation(String provider) {
1856 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001857 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001858 MockProvider mockProvider = mMockProviders.get(provider);
1859 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1861 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001862 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 }
1864 }
1865
1866 public void setTestProviderEnabled(String provider, boolean enabled) {
1867 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001868 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001869 MockProvider mockProvider = mMockProviders.get(provider);
1870 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1872 }
1873 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001874 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 mEnabledProviders.add(provider);
1876 mDisabledProviders.remove(provider);
1877 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001878 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 mEnabledProviders.remove(provider);
1880 mDisabledProviders.add(provider);
1881 }
1882 updateProvidersLocked();
1883 }
1884 }
1885
1886 public void clearTestProviderEnabled(String provider) {
1887 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001888 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001889 MockProvider mockProvider = mMockProviders.get(provider);
1890 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1892 }
1893 mEnabledProviders.remove(provider);
1894 mDisabledProviders.remove(provider);
1895 updateProvidersLocked();
1896 }
1897 }
1898
1899 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1900 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001901 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001902 MockProvider mockProvider = mMockProviders.get(provider);
1903 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1905 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001906 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 }
1909
1910 public void clearTestProviderStatus(String provider) {
1911 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001912 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001913 MockProvider mockProvider = mMockProviders.get(provider);
1914 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1916 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001917 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919 }
1920
1921 private void log(String log) {
1922 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1923 Log.d(TAG, log);
1924 }
1925 }
1926
1927 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1928 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1929 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001930 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 + Binder.getCallingPid()
1932 + ", uid=" + Binder.getCallingUid());
1933 return;
1934 }
1935
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001936 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 pw.println("Current Location Manager state:");
1938 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001940 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001942 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001945 for (Receiver i : mReceivers.values()) {
1946 pw.println(" " + i + ":");
1947 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 pw.println(" " + j.getKey() + ":");
1949 j.getValue().dump(pw, " ");
1950 }
1951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 pw.println(" Records by Provider:");
1953 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1954 : mRecordsByProvider.entrySet()) {
1955 pw.println(" " + i.getKey() + ":");
1956 for (UpdateRecord j : i.getValue()) {
1957 pw.println(" " + j + ":");
1958 j.dump(pw, " ");
1959 }
1960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 pw.println(" Last Known Locations:");
1962 for (Map.Entry<String, Location> i
1963 : mLastKnownLocation.entrySet()) {
1964 pw.println(" " + i.getKey() + ":");
1965 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1966 }
1967 if (mProximityAlerts.size() > 0) {
1968 pw.println(" Proximity Alerts:");
1969 for (Map.Entry<PendingIntent, ProximityAlert> i
1970 : mProximityAlerts.entrySet()) {
1971 pw.println(" " + i.getKey() + ":");
1972 i.getValue().dump(pw, " ");
1973 }
1974 }
1975 if (mProximitiesEntered.size() > 0) {
1976 pw.println(" Proximities Entered:");
1977 for (ProximityAlert i : mProximitiesEntered) {
1978 pw.println(" " + i + ":");
1979 i.dump(pw, " ");
1980 }
1981 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001982 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 pw.println(" mProximityListener=" + mProximityListener);
1984 if (mEnabledProviders.size() > 0) {
1985 pw.println(" Enabled Providers:");
1986 for (String i : mEnabledProviders) {
1987 pw.println(" " + i);
1988 }
1989
1990 }
1991 if (mDisabledProviders.size() > 0) {
1992 pw.println(" Disabled Providers:");
1993 for (String i : mDisabledProviders) {
1994 pw.println(" " + i);
1995 }
1996
1997 }
1998 if (mMockProviders.size() > 0) {
1999 pw.println(" Mock Providers:");
2000 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002001 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
2003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
2005 }
2006}