blob: c65c2beb397d7250cf7fb05591a2ccc19b67ab1b [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) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001111 if (provider == null) {
1112 // throw NullPointerException to remain compatible with previous implementation
1113 throw new NullPointerException();
1114 }
1115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // first check for permission to the provider
1117 checkPermissionsSafe(provider);
1118 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001119 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 != PackageManager.PERMISSION_GRANTED)) {
1121 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1122 }
1123
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001124 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001125 LocationProviderProxy proxy = mProvidersByName.get(provider);
Mike Lockwood6ba7ae12009-08-17 08:39:12 -04001126 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 return false;
1128 }
1129
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001130 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132 }
1133
1134 class ProximityAlert {
1135 final int mUid;
1136 final double mLatitude;
1137 final double mLongitude;
1138 final float mRadius;
1139 final long mExpiration;
1140 final PendingIntent mIntent;
1141 final Location mLocation;
1142
1143 public ProximityAlert(int uid, double latitude, double longitude,
1144 float radius, long expiration, PendingIntent intent) {
1145 mUid = uid;
1146 mLatitude = latitude;
1147 mLongitude = longitude;
1148 mRadius = radius;
1149 mExpiration = expiration;
1150 mIntent = intent;
1151
1152 mLocation = new Location("");
1153 mLocation.setLatitude(latitude);
1154 mLocation.setLongitude(longitude);
1155 }
1156
1157 long getExpiration() {
1158 return mExpiration;
1159 }
1160
1161 PendingIntent getIntent() {
1162 return mIntent;
1163 }
1164
1165 boolean isInProximity(double latitude, double longitude) {
1166 Location loc = new Location("");
1167 loc.setLatitude(latitude);
1168 loc.setLongitude(longitude);
1169
1170 double radius = loc.distanceTo(mLocation);
1171 return radius <= mRadius;
1172 }
1173
1174 @Override
1175 public String toString() {
1176 return "ProximityAlert{"
1177 + Integer.toHexString(System.identityHashCode(this))
1178 + " uid " + mUid + mIntent + "}";
1179 }
1180
1181 void dump(PrintWriter pw, String prefix) {
1182 pw.println(prefix + this);
1183 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1184 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1185 pw.println(prefix + "mIntent=" + mIntent);
1186 pw.println(prefix + "mLocation:");
1187 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1188 }
1189 }
1190
1191 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001192 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193
1194 boolean isGpsAvailable = false;
1195
1196 // Note: this is called with the lock held.
1197 public void onLocationChanged(Location loc) {
1198
1199 // If Gps is available, then ignore updates from NetworkLocationProvider
1200 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1201 isGpsAvailable = true;
1202 }
1203 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1204 return;
1205 }
1206
1207 // Process proximity alerts
1208 long now = System.currentTimeMillis();
1209 double latitude = loc.getLatitude();
1210 double longitude = loc.getLongitude();
1211 ArrayList<PendingIntent> intentsToRemove = null;
1212
1213 for (ProximityAlert alert : mProximityAlerts.values()) {
1214 PendingIntent intent = alert.getIntent();
1215 long expiration = alert.getExpiration();
1216
1217 if ((expiration == -1) || (now <= expiration)) {
1218 boolean entered = mProximitiesEntered.contains(alert);
1219 boolean inProximity =
1220 alert.isInProximity(latitude, longitude);
1221 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001222 if (LOCAL_LOGV) {
1223 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225 mProximitiesEntered.add(alert);
1226 Intent enteredIntent = new Intent();
1227 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1228 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001229 synchronized (this) {
1230 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001231 // is called before decrementPendingBroadcasts()
1232 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1233 // call this after broadcasting so we do not increment
1234 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001235 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001238 if (LOCAL_LOGV) {
1239 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
1241 if (intentsToRemove == null) {
1242 intentsToRemove = new ArrayList<PendingIntent>();
1243 }
1244 intentsToRemove.add(intent);
1245 }
1246 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001247 if (LOCAL_LOGV) {
1248 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250 mProximitiesEntered.remove(alert);
1251 Intent exitedIntent = new Intent();
1252 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1253 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001254 synchronized (this) {
1255 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001256 // is called before decrementPendingBroadcasts()
1257 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1258 // call this after broadcasting so we do not increment
1259 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001260 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001263 if (LOCAL_LOGV) {
1264 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 }
1266 if (intentsToRemove == null) {
1267 intentsToRemove = new ArrayList<PendingIntent>();
1268 }
1269 intentsToRemove.add(intent);
1270 }
1271 }
1272 } else {
1273 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001274 if (LOCAL_LOGV) {
1275 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 if (intentsToRemove == null) {
1278 intentsToRemove = new ArrayList<PendingIntent>();
1279 }
1280 intentsToRemove.add(alert.getIntent());
1281 }
1282 }
1283
1284 // Remove expired alerts
1285 if (intentsToRemove != null) {
1286 for (PendingIntent i : intentsToRemove) {
1287 mProximityAlerts.remove(i);
1288 ProximityAlert alert = mProximityAlerts.get(i);
1289 mProximitiesEntered.remove(alert);
1290 }
1291 }
1292
1293 }
1294
1295 // Note: this is called with the lock held.
1296 public void onProviderDisabled(String provider) {
1297 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1298 isGpsAvailable = false;
1299 }
1300 }
1301
1302 // Note: this is called with the lock held.
1303 public void onProviderEnabled(String provider) {
1304 // ignore
1305 }
1306
1307 // Note: this is called with the lock held.
1308 public void onStatusChanged(String provider, int status, Bundle extras) {
1309 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1310 (status != LocationProvider.AVAILABLE)) {
1311 isGpsAvailable = false;
1312 }
1313 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001314
1315 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1316 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001317 // synchronize to ensure incrementPendingBroadcasts()
1318 // is called before decrementPendingBroadcasts()
1319 synchronized (this) {
1320 decrementPendingBroadcasts();
1321 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324
1325 public void addProximityAlert(double latitude, double longitude,
1326 float radius, long expiration, PendingIntent intent) {
1327 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001328 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1330 }
1331 } catch (SecurityException se) {
1332 throw se;
1333 } catch (Exception e) {
1334 Log.e(TAG, "addProximityAlert got exception:", e);
1335 }
1336 }
1337
1338 private void addProximityAlertLocked(double latitude, double longitude,
1339 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001340 if (LOCAL_LOGV) {
1341 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 ", longitude = " + longitude +
1343 ", expiration = " + expiration +
1344 ", intent = " + intent);
1345 }
1346
1347 // Require ability to access all providers for now
1348 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1349 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1350 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1351 }
1352
1353 if (expiration != -1) {
1354 expiration += System.currentTimeMillis();
1355 }
1356 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1357 latitude, longitude, radius, expiration, intent);
1358 mProximityAlerts.put(intent, alert);
1359
Mike Lockwood48f17512009-04-23 09:12:08 -07001360 if (mProximityReceiver == null) {
1361 mProximityListener = new ProximityListener();
1362 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
Mike Lockwood95427cd2009-05-07 13:27:54 -04001364 for (int i = mProviders.size() - 1; i >= 0; i--) {
1365 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001366 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369 }
1370
1371 public void removeProximityAlert(PendingIntent intent) {
1372 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001373 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 removeProximityAlertLocked(intent);
1375 }
1376 } catch (SecurityException se) {
1377 throw se;
1378 } catch (Exception e) {
1379 Log.e(TAG, "removeProximityAlert got exception:", e);
1380 }
1381 }
1382
1383 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001384 if (LOCAL_LOGV) {
1385 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
1387
1388 mProximityAlerts.remove(intent);
1389 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001390 removeUpdatesLocked(mProximityReceiver);
1391 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 }
1395
1396 /**
1397 * @return null if the provider does not exits
1398 * @throw SecurityException if the provider is not allowed to be
1399 * accessed by the caller
1400 */
1401 public Bundle getProviderInfo(String provider) {
1402 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001403 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 return _getProviderInfoLocked(provider);
1405 }
1406 } catch (SecurityException se) {
1407 throw se;
1408 } catch (Exception e) {
1409 Log.e(TAG, "_getProviderInfo got exception:", e);
1410 return null;
1411 }
1412 }
1413
1414 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001415 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 if (p == null) {
1417 return null;
1418 }
1419
1420 checkPermissionsSafe(provider);
1421
1422 Bundle b = new Bundle();
1423 b.putBoolean("network", p.requiresNetwork());
1424 b.putBoolean("satellite", p.requiresSatellite());
1425 b.putBoolean("cell", p.requiresCell());
1426 b.putBoolean("cost", p.hasMonetaryCost());
1427 b.putBoolean("altitude", p.supportsAltitude());
1428 b.putBoolean("speed", p.supportsSpeed());
1429 b.putBoolean("bearing", p.supportsBearing());
1430 b.putInt("power", p.getPowerRequirement());
1431 b.putInt("accuracy", p.getAccuracy());
1432
1433 return b;
1434 }
1435
1436 public boolean isProviderEnabled(String provider) {
1437 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001438 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 return _isProviderEnabledLocked(provider);
1440 }
1441 } catch (SecurityException se) {
1442 throw se;
1443 } catch (Exception e) {
1444 Log.e(TAG, "isProviderEnabled got exception:", e);
1445 return false;
1446 }
1447 }
1448
Mike Lockwood275555c2009-05-01 11:30:34 -04001449 public void reportLocation(Location location) {
1450 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1451 != PackageManager.PERMISSION_GRANTED) {
1452 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1453 }
1454
Mike Lockwood4e50b782009-04-03 08:24:43 -07001455 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1456 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1457 mLocationHandler.sendMessageAtFrontOfQueue(m);
1458 }
1459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 private boolean _isProviderEnabledLocked(String provider) {
1461 checkPermissionsSafe(provider);
1462
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001463 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 if (p == null) {
1465 throw new IllegalArgumentException("provider=" + provider);
1466 }
1467 return isAllowedBySettingsLocked(provider);
1468 }
1469
1470 public Location getLastKnownLocation(String provider) {
1471 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001472 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 return _getLastKnownLocationLocked(provider);
1474 }
1475 } catch (SecurityException se) {
1476 throw se;
1477 } catch (Exception e) {
1478 Log.e(TAG, "getLastKnownLocation got exception:", e);
1479 return null;
1480 }
1481 }
1482
1483 private Location _getLastKnownLocationLocked(String provider) {
1484 checkPermissionsSafe(provider);
1485
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001486 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 if (p == null) {
1488 throw new IllegalArgumentException("provider=" + provider);
1489 }
1490
1491 if (!isAllowedBySettingsLocked(provider)) {
1492 return null;
1493 }
1494
1495 Location location = mLastKnownLocation.get(provider);
1496 if (location == null) {
1497 // Get the persistent last known location for the provider
1498 location = readLastKnownLocationLocked(provider);
1499 if (location != null) {
1500 mLastKnownLocation.put(provider, location);
1501 }
1502 }
1503
1504 return location;
1505 }
1506
1507 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1508 // Always broadcast the first update
1509 if (lastLoc == null) {
1510 return true;
1511 }
1512
1513 // Don't broadcast same location again regardless of condition
1514 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1515 if (loc.getTime() == lastLoc.getTime()) {
1516 return false;
1517 }
1518
1519 // Check whether sufficient distance has been traveled
1520 double minDistance = record.mMinDistance;
1521 if (minDistance > 0.0) {
1522 if (loc.distanceTo(lastLoc) <= minDistance) {
1523 return false;
1524 }
1525 }
1526
1527 return true;
1528 }
1529
Mike Lockwood4e50b782009-04-03 08:24:43 -07001530 private void handleLocationChangedLocked(Location location) {
1531 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1533 if (records == null || records.size() == 0) {
1534 return;
1535 }
1536
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001537 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 if (p == null) {
1539 return;
1540 }
1541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001543 Location lastLocation = mLastKnownLocation.get(provider);
1544 if (lastLocation == null) {
1545 mLastKnownLocation.put(provider, new Location(location));
1546 } else {
1547 lastLocation.set(location);
1548 }
1549 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 // Fetch latest status update time
1552 long newStatusUpdateTime = p.getStatusUpdateTime();
1553
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001554 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 Bundle extras = new Bundle();
1556 int status = p.getStatus(extras);
1557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 ArrayList<Receiver> deadReceivers = null;
1559
1560 // Broadcast location or status to all listeners
1561 final int N = records.size();
1562 for (int i=0; i<N; i++) {
1563 UpdateRecord r = records.get(i);
1564 Receiver receiver = r.mReceiver;
1565
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001566 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001567 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1568 if (lastLoc == null) {
1569 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001570 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001571 } else {
1572 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001574 if (!receiver.callLocationChangedLocked(location)) {
1575 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1576 if (deadReceivers == null) {
1577 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001579 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
1581 }
1582
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001583 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1585 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1586
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001587 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1589 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1590 if (deadReceivers == null) {
1591 deadReceivers = new ArrayList<Receiver>();
1592 }
1593 if (!deadReceivers.contains(receiver)) {
1594 deadReceivers.add(receiver);
1595 }
1596 }
1597 }
1598 }
1599
1600 if (deadReceivers != null) {
1601 for (int i=deadReceivers.size()-1; i>=0; i--) {
1602 removeUpdatesLocked(deadReceivers.get(i));
1603 }
1604 }
1605 }
1606
1607 private class LocationWorkerHandler extends Handler {
1608
1609 @Override
1610 public void handleMessage(Message msg) {
1611 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001612 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1613 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001615 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001616 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001617 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001618
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001619 // notify other providers of the new location
1620 for (int i = mProviders.size() - 1; i >= 0; i--) {
1621 LocationProviderProxy proxy = mProviders.get(i);
1622 if (!provider.equals(proxy.getName())) {
1623 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001624 }
1625 }
1626
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001627 if (isAllowedBySettingsLocked(provider)) {
1628 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
1632 } catch (Exception e) {
1633 // Log, don't crash!
1634 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1635 }
1636 }
1637 }
1638
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001639 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1640 @Override
1641 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 String action = intent.getAction();
1643
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001644 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001646 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1648 if (uid >= 0) {
1649 ArrayList<Receiver> removedRecs = null;
1650 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1651 for (int j=i.size()-1; j>=0; j--) {
1652 UpdateRecord ur = i.get(j);
1653 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1654 if (removedRecs == null) {
1655 removedRecs = new ArrayList<Receiver>();
1656 }
1657 if (!removedRecs.contains(ur.mReceiver)) {
1658 removedRecs.add(ur.mReceiver);
1659 }
1660 }
1661 }
1662 }
1663 ArrayList<ProximityAlert> removedAlerts = null;
1664 for (ProximityAlert i : mProximityAlerts.values()) {
1665 if (i.mUid == uid) {
1666 if (removedAlerts == null) {
1667 removedAlerts = new ArrayList<ProximityAlert>();
1668 }
1669 if (!removedAlerts.contains(i)) {
1670 removedAlerts.add(i);
1671 }
1672 }
1673 }
1674 if (removedRecs != null) {
1675 for (int i=removedRecs.size()-1; i>=0; i--) {
1676 removeUpdatesLocked(removedRecs.get(i));
1677 }
1678 }
1679 if (removedAlerts != null) {
1680 for (int i=removedAlerts.size()-1; i>=0; i--) {
1681 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1682 }
1683 }
1684 }
1685 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001686 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 boolean noConnectivity =
1688 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1689 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001690 mNetworkState = LocationProvider.AVAILABLE;
1691 } else {
1692 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694
1695 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001696 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001697 for (int i = mProviders.size() - 1; i >= 0; i--) {
1698 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001700 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702 }
1703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001706 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707
1708 // Wake locks
1709
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001710 private void incrementPendingBroadcasts() {
1711 synchronized (mWakeLock) {
1712 if (mPendingBroadcasts++ == 0) {
1713 try {
1714 mWakeLock.acquire();
1715 log("Acquired wakelock");
1716 } catch (Exception e) {
1717 // This is to catch a runtime exception thrown when we try to release an
1718 // already released lock.
1719 Log.e(TAG, "exception in acquireWakeLock()", e);
1720 }
1721 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001722 }
1723 }
1724
1725 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001726 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001727 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001728 try {
1729 // Release wake lock
1730 if (mWakeLock.isHeld()) {
1731 mWakeLock.release();
1732 log("Released wakelock");
1733 } else {
1734 log("Can't release wakelock again!");
1735 }
1736 } catch (Exception e) {
1737 // This is to catch a runtime exception thrown when we try to release an
1738 // already released lock.
1739 Log.e(TAG, "exception in releaseWakeLock()", e);
1740 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001741 }
1742 }
1743 }
1744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 // Geocoder
1746
1747 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001748 String language, String country, String variant, String appName, List<Address> addrs) {
1749 if (mGeocodeProvider != null) {
1750 try {
1751 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1752 variant, appName, addrs);
1753 } catch (RemoteException e) {
1754 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001755 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001758 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760
Mike Lockwooda55c3212009-04-15 11:10:11 -04001761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001763 double lowerLeftLatitude, double lowerLeftLongitude,
1764 double upperRightLatitude, double upperRightLongitude, int maxResults,
1765 String language, String country, String variant, String appName, List<Address> addrs) {
1766
1767 if (mGeocodeProvider != null) {
1768 try {
1769 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1770 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1771 maxResults, language, country, variant, appName, addrs);
1772 } catch (RemoteException e) {
1773 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001774 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
1776 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001777 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779
1780 // Mock Providers
1781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 private void checkMockPermissionsSafe() {
1783 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1784 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1785 if (!allowMocks) {
1786 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1787 }
1788
1789 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1790 PackageManager.PERMISSION_GRANTED) {
1791 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1792 }
1793 }
1794
1795 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1796 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1797 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1798 checkMockPermissionsSafe();
1799
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001800 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001801 MockProvider provider = new MockProvider(name, this,
1802 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 requiresCell, hasMonetaryCost, supportsAltitude,
1804 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001805 // remove the real provider if we are replacing GPS or network provider
1806 if (LocationManager.GPS_PROVIDER.equals(name)
1807 || LocationManager.NETWORK_PROVIDER.equals(name)) {
1808 LocationProviderProxy proxy = mProvidersByName.get(name);
1809 if (proxy != null) {
1810 proxy.enableLocationTracking(false);
1811 removeProvider(proxy);
1812 }
1813 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001814 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1816 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001817 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001818 mMockProviders.put(name, provider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001819 mLastKnownLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 updateProvidersLocked();
1821 }
1822 }
1823
1824 public void removeTestProvider(String provider) {
1825 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001826 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001827 MockProvider mockProvider = mMockProviders.get(provider);
1828 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1830 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001831 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001832 mMockProviders.remove(mockProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001833 // reinstall real provider if we were mocking GPS or network provider
1834 if (LocationManager.GPS_PROVIDER.equals(provider) &&
1835 mGpsLocationProvider != null) {
1836 addProvider(mGpsLocationProvider);
1837 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) &&
1838 mNetworkLocationProvider != null) {
1839 addProvider(mNetworkLocationProvider);
1840 }
1841 mLastKnownLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 updateProvidersLocked();
1843 }
1844 }
1845
1846 public void setTestProviderLocation(String provider, Location loc) {
1847 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001848 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001849 MockProvider mockProvider = mMockProviders.get(provider);
1850 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1852 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001853 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1854 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001855 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001856 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
1858 }
1859
1860 public void clearTestProviderLocation(String provider) {
1861 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001862 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001863 MockProvider mockProvider = mMockProviders.get(provider);
1864 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1866 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001867 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 }
1869 }
1870
1871 public void setTestProviderEnabled(String provider, boolean enabled) {
1872 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001873 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001874 MockProvider mockProvider = mMockProviders.get(provider);
1875 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1877 }
1878 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001879 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 mEnabledProviders.add(provider);
1881 mDisabledProviders.remove(provider);
1882 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001883 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 mEnabledProviders.remove(provider);
1885 mDisabledProviders.add(provider);
1886 }
1887 updateProvidersLocked();
1888 }
1889 }
1890
1891 public void clearTestProviderEnabled(String provider) {
1892 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001893 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001894 MockProvider mockProvider = mMockProviders.get(provider);
1895 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1897 }
1898 mEnabledProviders.remove(provider);
1899 mDisabledProviders.remove(provider);
1900 updateProvidersLocked();
1901 }
1902 }
1903
1904 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1905 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001906 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001907 MockProvider mockProvider = mMockProviders.get(provider);
1908 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1910 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001911 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913 }
1914
1915 public void clearTestProviderStatus(String provider) {
1916 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001917 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001918 MockProvider mockProvider = mMockProviders.get(provider);
1919 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1921 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001922 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924 }
1925
1926 private void log(String log) {
1927 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1928 Log.d(TAG, log);
1929 }
1930 }
1931
1932 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1933 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1934 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001935 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 + Binder.getCallingPid()
1937 + ", uid=" + Binder.getCallingUid());
1938 return;
1939 }
1940
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001941 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 pw.println("Current Location Manager state:");
1943 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001945 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001947 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
1949 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001950 for (Receiver i : mReceivers.values()) {
1951 pw.println(" " + i + ":");
1952 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 pw.println(" " + j.getKey() + ":");
1954 j.getValue().dump(pw, " ");
1955 }
1956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 pw.println(" Records by Provider:");
1958 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1959 : mRecordsByProvider.entrySet()) {
1960 pw.println(" " + i.getKey() + ":");
1961 for (UpdateRecord j : i.getValue()) {
1962 pw.println(" " + j + ":");
1963 j.dump(pw, " ");
1964 }
1965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 pw.println(" Last Known Locations:");
1967 for (Map.Entry<String, Location> i
1968 : mLastKnownLocation.entrySet()) {
1969 pw.println(" " + i.getKey() + ":");
1970 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1971 }
1972 if (mProximityAlerts.size() > 0) {
1973 pw.println(" Proximity Alerts:");
1974 for (Map.Entry<PendingIntent, ProximityAlert> i
1975 : mProximityAlerts.entrySet()) {
1976 pw.println(" " + i.getKey() + ":");
1977 i.getValue().dump(pw, " ");
1978 }
1979 }
1980 if (mProximitiesEntered.size() > 0) {
1981 pw.println(" Proximities Entered:");
1982 for (ProximityAlert i : mProximitiesEntered) {
1983 pw.println(" " + i + ":");
1984 i.dump(pw, " ");
1985 }
1986 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001987 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 pw.println(" mProximityListener=" + mProximityListener);
1989 if (mEnabledProviders.size() > 0) {
1990 pw.println(" Enabled Providers:");
1991 for (String i : mEnabledProviders) {
1992 pw.println(" " + i);
1993 }
1994
1995 }
1996 if (mDisabledProviders.size() > 0) {
1997 pw.println(" Disabled Providers:");
1998 for (String i : mDisabledProviders) {
1999 pw.println(" " + i);
2000 }
2001
2002 }
2003 if (mMockProviders.size() > 0) {
2004 pw.println(" Mock Providers:");
2005 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002006 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010 }
2011}