blob: dfab17e8fa6d322ae9f9d68eb0812d49a13b87c7 [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;
Danke Xie22d1f9f2009-08-18 18:28:45 -040052import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.location.Location;
54import android.location.LocationManager;
55import android.location.LocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.net.ConnectivityManager;
57import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Binder;
59import android.os.Bundle;
60import android.os.Handler;
61import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070062import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Message;
64import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070065import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.RemoteException;
67import android.os.SystemClock;
68import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.Log;
70import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import com.android.internal.location.GpsLocationProvider;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070073import com.android.internal.location.LocationProviderProxy;
Mike Lockwood7ec434e2009-03-27 07:46:48 -070074import com.android.internal.location.MockProvider;
Danke Xie22d1f9f2009-08-18 18:28:45 -040075import com.android.internal.location.GpsNetInitiatedHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77/**
78 * The service class that manages LocationProviders and issues location
79 * updates and alerts.
80 *
81 * {@hide}
82 */
Mike Lockwood3d12b512009-04-21 23:25:35 -070083public class LocationManagerService extends ILocationManager.Stub implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private static final String TAG = "LocationManagerService";
The Android Open Source Project10592532009-03-18 17:39:46 -070085 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 // Minimum time interval between last known location writes, in milliseconds.
88 private static final long MIN_LAST_KNOWN_LOCATION_TIME = 60L * 1000L;
89
90 // Max time to hold wake lock for, in milliseconds.
91 private static final long MAX_TIME_FOR_WAKE_LOCK = 60 * 1000L;
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 // The last time a location was written, by provider name.
94 private HashMap<String,Long> mLastWriteTime = new HashMap<String,Long>();
95
96 private static final Pattern PATTERN_COMMA = Pattern.compile(",");
97
98 private static final String ACCESS_FINE_LOCATION =
99 android.Manifest.permission.ACCESS_FINE_LOCATION;
100 private static final String ACCESS_COARSE_LOCATION =
101 android.Manifest.permission.ACCESS_COARSE_LOCATION;
102 private static final String ACCESS_MOCK_LOCATION =
103 android.Manifest.permission.ACCESS_MOCK_LOCATION;
104 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
105 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400106 private static final String INSTALL_LOCATION_PROVIDER =
107 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 // Set of providers that are explicitly enabled
110 private final Set<String> mEnabledProviders = new HashSet<String>();
111
112 // Set of providers that are explicitly disabled
113 private final Set<String> mDisabledProviders = new HashSet<String>();
114
115 // Locations, status values, and extras for mock providers
Mike Lockwood7ec434e2009-03-27 07:46:48 -0700116 private final HashMap<String,MockProvider> mMockProviders = new HashMap<String,MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
118 private static boolean sProvidersLoaded = false;
119
120 private final Context mContext;
Mike Lockwooda55c3212009-04-15 11:10:11 -0400121 private IGeocodeProvider mGeocodeProvider;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400122 private IGpsStatusProvider mGpsStatusProvider;
Danke Xie22d1f9f2009-08-18 18:28:45 -0400123 private INetInitiatedListener mNetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private LocationWorkerHandler mLocationHandler;
125
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700126 // Cache the real providers for use in addTestProvider() and removeTestProvider()
127 LocationProviderProxy mNetworkLocationProvider;
128 LocationProviderProxy mGpsLocationProvider;
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 // Handler messages
Mike Lockwood4e50b782009-04-03 08:24:43 -0700131 private static final int MESSAGE_LOCATION_CHANGED = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400133 // wakelock variables
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private final static String WAKELOCK_KEY = "LocationManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 private PowerManager.WakeLock mWakeLock = null;
Mike Lockwood48f17512009-04-23 09:12:08 -0700136 private int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400139 * List of all receivers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400141 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400143
144 /**
145 * List of location providers.
146 */
147 private final ArrayList<LocationProviderProxy> mProviders =
148 new ArrayList<LocationProviderProxy>();
149 private final HashMap<String, LocationProviderProxy> mProvidersByName
150 = new HashMap<String, LocationProviderProxy>();
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400153 * Object used internally for synchronization
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400155 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 /**
158 * Mapping from provider name to all its UpdateRecords
159 */
160 private final HashMap<String,ArrayList<UpdateRecord>> mRecordsByProvider =
161 new HashMap<String,ArrayList<UpdateRecord>>();
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 // Proximity listeners
Mike Lockwood48f17512009-04-23 09:12:08 -0700164 private Receiver mProximityReceiver = null;
165 private ILocationListener mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private HashMap<PendingIntent,ProximityAlert> mProximityAlerts =
167 new HashMap<PendingIntent,ProximityAlert>();
168 private HashSet<ProximityAlert> mProximitiesEntered =
169 new HashSet<ProximityAlert>();
170
171 // Last known location for each provider
172 private HashMap<String,Location> mLastKnownLocation =
173 new HashMap<String,Location>();
174
The Android Open Source Project4df24232009-03-05 14:34:35 -0800175 private int mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800176
Mike Lockwood9637d472009-04-02 21:41:57 -0700177 // for Settings change notification
178 private ContentQueryMap mSettings;
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
181 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
182 * location updates.
183 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700184 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final ILocationListener mListener;
186 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final Object mKey;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400188 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Mike Lockwood48f17512009-04-23 09:12:08 -0700189 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400191 Receiver(ILocationListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 mListener = listener;
193 mPendingIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 mKey = listener.asBinder();
195 }
196
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400197 Receiver(PendingIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 mPendingIntent = intent;
199 mListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mKey = intent;
201 }
202
203 @Override
204 public boolean equals(Object otherObj) {
205 if (otherObj instanceof Receiver) {
206 return mKey.equals(
207 ((Receiver)otherObj).mKey);
208 }
209 return false;
210 }
211
212 @Override
213 public int hashCode() {
214 return mKey.hashCode();
215 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 @Override
218 public String toString() {
219 if (mListener != null) {
220 return "Receiver{"
221 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400222 + " Listener " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 } else {
224 return "Receiver{"
225 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400226 + " Intent " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 }
228 }
229
230 public boolean isListener() {
231 return mListener != null;
232 }
233
234 public boolean isPendingIntent() {
235 return mPendingIntent != null;
236 }
237
238 public ILocationListener getListener() {
239 if (mListener != null) {
240 return mListener;
241 }
242 throw new IllegalStateException("Request for non-existent listener");
243 }
244
245 public PendingIntent getPendingIntent() {
246 if (mPendingIntent != null) {
247 return mPendingIntent;
248 }
249 throw new IllegalStateException("Request for non-existent intent");
250 }
251
252 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
253 if (mListener != null) {
254 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700255 synchronized (this) {
256 // synchronize to ensure incrementPendingBroadcastsLocked()
257 // is called before decrementPendingBroadcasts()
258 mListener.onStatusChanged(provider, status, extras);
259 if (mListener != mProximityListener) {
260 // call this after broadcasting so we do not increment
261 // if we throw an exeption.
262 incrementPendingBroadcastsLocked();
263 }
264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 } catch (RemoteException e) {
266 return false;
267 }
268 } else {
269 Intent statusChanged = new Intent();
270 statusChanged.putExtras(extras);
271 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
272 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700273 synchronized (this) {
274 // synchronize to ensure incrementPendingBroadcastsLocked()
275 // is called before decrementPendingBroadcasts()
276 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler);
277 // call this after broadcasting so we do not increment
278 // if we throw an exeption.
279 incrementPendingBroadcastsLocked();
280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 } catch (PendingIntent.CanceledException e) {
282 return false;
283 }
284 }
285 return true;
286 }
287
288 public boolean callLocationChangedLocked(Location location) {
289 if (mListener != null) {
290 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700291 synchronized (this) {
292 // synchronize to ensure incrementPendingBroadcastsLocked()
293 // is called before decrementPendingBroadcasts()
294 mListener.onLocationChanged(location);
295 if (mListener != mProximityListener) {
296 // call this after broadcasting so we do not increment
297 // if we throw an exeption.
298 incrementPendingBroadcastsLocked();
299 }
300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 } catch (RemoteException e) {
302 return false;
303 }
304 } else {
305 Intent locationChanged = new Intent();
306 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
307 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700308 synchronized (this) {
309 // synchronize to ensure incrementPendingBroadcastsLocked()
310 // is called before decrementPendingBroadcasts()
311 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler);
312 // call this after broadcasting so we do not increment
313 // if we throw an exeption.
314 incrementPendingBroadcastsLocked();
315 }
316 } catch (PendingIntent.CanceledException e) {
317 return false;
318 }
319 }
320 return true;
321 }
322
323 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
324 if (mListener != null) {
325 try {
326 synchronized (this) {
327 // synchronize to ensure incrementPendingBroadcastsLocked()
328 // is called before decrementPendingBroadcasts()
329 if (enabled) {
330 mListener.onProviderEnabled(provider);
331 } else {
332 mListener.onProviderDisabled(provider);
333 }
334 if (mListener != mProximityListener) {
335 // call this after broadcasting so we do not increment
336 // if we throw an exeption.
337 incrementPendingBroadcastsLocked();
338 }
339 }
340 } catch (RemoteException e) {
341 return false;
342 }
343 } else {
344 Intent providerIntent = new Intent();
345 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
346 try {
347 synchronized (this) {
348 // synchronize to ensure incrementPendingBroadcastsLocked()
349 // is called before decrementPendingBroadcasts()
350 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler);
351 // call this after broadcasting so we do not increment
352 // if we throw an exeption.
353 incrementPendingBroadcastsLocked();
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 } catch (PendingIntent.CanceledException e) {
356 return false;
357 }
358 }
359 return true;
360 }
361
362 public void binderDied() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700363 if (LOCAL_LOGV) {
364 Log.v(TAG, "Location listener died");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400366 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 removeUpdatesLocked(this);
368 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700369 synchronized (this) {
370 if (mPendingBroadcasts > 0) {
371 LocationManagerService.this.decrementPendingBroadcasts();
372 mPendingBroadcasts = 0;
373 }
374 }
375 }
376
377 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
378 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400379 synchronized (this) {
380 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700381 }
382 }
383
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400384 // this must be called while synchronized by caller in a synchronized block
385 // containing the sending of the broadcaset
386 private void incrementPendingBroadcastsLocked() {
387 if (mPendingBroadcasts++ == 0) {
388 LocationManagerService.this.incrementPendingBroadcasts();
389 }
390 }
391
392 private void decrementPendingBroadcastsLocked() {
393 if (--mPendingBroadcasts == 0) {
394 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700395 }
396 }
397 }
398
399 public void locationCallbackFinished(ILocationListener listener) {
400 Receiver receiver = getReceiver(listener);
401 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400402 synchronized (receiver) {
403 // so wakelock calls will succeed
404 long identity = Binder.clearCallingIdentity();
405 receiver.decrementPendingBroadcastsLocked();
406 Binder.restoreCallingIdentity(identity);
407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 }
410
Mike Lockwood9637d472009-04-02 21:41:57 -0700411 private final class SettingsObserver implements Observer {
412 public void update(Observable o, Object arg) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400413 synchronized (mLock) {
Mike Lockwood9637d472009-04-02 21:41:57 -0700414 updateProvidersLocked();
415 }
416 }
417 }
418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 private Location readLastKnownLocationLocked(String provider) {
420 Location location = null;
421 String s = null;
422 try {
423 File f = new File(LocationManager.SYSTEM_DIR + "/location."
424 + provider);
425 if (!f.exists()) {
426 return null;
427 }
428 BufferedReader reader = new BufferedReader(new FileReader(f), 256);
429 s = reader.readLine();
430 } catch (IOException e) {
431 Log.w(TAG, "Unable to read last known location", e);
432 }
433
434 if (s == null) {
435 return null;
436 }
437 try {
438 String[] tokens = PATTERN_COMMA.split(s);
439 int idx = 0;
440 long time = Long.parseLong(tokens[idx++]);
441 double latitude = Double.parseDouble(tokens[idx++]);
442 double longitude = Double.parseDouble(tokens[idx++]);
443 double altitude = Double.parseDouble(tokens[idx++]);
444 float bearing = Float.parseFloat(tokens[idx++]);
445 float speed = Float.parseFloat(tokens[idx++]);
446
447 location = new Location(provider);
448 location.setTime(time);
449 location.setLatitude(latitude);
450 location.setLongitude(longitude);
451 location.setAltitude(altitude);
452 location.setBearing(bearing);
453 location.setSpeed(speed);
454 } catch (NumberFormatException nfe) {
455 Log.e(TAG, "NumberFormatException reading last known location", nfe);
456 return null;
457 }
458
459 return location;
460 }
461
462 private void writeLastKnownLocationLocked(String provider,
463 Location location) {
464 long now = SystemClock.elapsedRealtime();
465 Long last = mLastWriteTime.get(provider);
466 if ((last != null)
467 && (now - last.longValue() < MIN_LAST_KNOWN_LOCATION_TIME)) {
468 return;
469 }
470 mLastWriteTime.put(provider, now);
471
472 StringBuilder sb = new StringBuilder(100);
473 sb.append(location.getTime());
474 sb.append(',');
475 sb.append(location.getLatitude());
476 sb.append(',');
477 sb.append(location.getLongitude());
478 sb.append(',');
479 sb.append(location.getAltitude());
480 sb.append(',');
481 sb.append(location.getBearing());
482 sb.append(',');
483 sb.append(location.getSpeed());
484
485 FileWriter writer = null;
486 try {
487 File d = new File(LocationManager.SYSTEM_DIR);
488 if (!d.exists()) {
489 if (!d.mkdirs()) {
490 Log.w(TAG, "Unable to create directory to write location");
491 return;
492 }
493 }
494 File f = new File(LocationManager.SYSTEM_DIR + "/location." + provider);
495 writer = new FileWriter(f);
496 writer.write(sb.toString());
497 } catch (IOException e) {
498 Log.w(TAG, "Unable to write location", e);
499 } finally {
500 if (writer != null) {
501 try {
502 writer.close();
503 } catch (IOException e) {
504 Log.w(TAG, "Exception closing file", e);
505 }
506 }
507 }
508 }
509
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400510 private void addProvider(LocationProviderProxy provider) {
511 mProviders.add(provider);
512 mProvidersByName.put(provider.getName(), provider);
513 }
514
515 private void removeProvider(LocationProviderProxy provider) {
516 mProviders.remove(provider);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700517 provider.unlinkProvider();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400518 mProvidersByName.remove(provider.getName());
519 }
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400522 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 if (sProvidersLoaded) {
524 return;
525 }
526
527 // Load providers
528 loadProvidersLocked();
529 sProvidersLoaded = true;
530 }
531 }
532
533 private void loadProvidersLocked() {
534 try {
535 _loadProvidersLocked();
536 } catch (Exception e) {
537 Log.e(TAG, "Exception loading providers:", e);
538 }
539 }
540
541 private void _loadProvidersLocked() {
542 // Attempt to load "real" providers first
543 if (GpsLocationProvider.isSupported()) {
544 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400545 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
546 mGpsStatusProvider = provider.getGpsStatusProvider();
Danke Xie22d1f9f2009-08-18 18:28:45 -0400547 mNetInitiatedListener = provider.getNetInitiatedListener();
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400548 LocationProviderProxy proxy = new LocationProviderProxy(LocationManager.GPS_PROVIDER, provider);
549 addProvider(proxy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700550 mGpsLocationProvider = proxy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 updateProvidersLocked();
554 }
555
556 /**
557 * @param context the context that the LocationManagerService runs in
558 */
559 public LocationManagerService(Context context) {
560 super();
561 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700562
563 Thread thread = new Thread(null, this, "LocationManagerService");
564 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
The Android Open Source Project10592532009-03-18 17:39:46 -0700566 if (LOCAL_LOGV) {
567 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Mike Lockwood3d12b512009-04-21 23:25:35 -0700571 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 // Create a wake lock, needs to be done before calling loadProviders() below
573 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
574 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 // Load providers
577 loadProviders();
578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400581 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
582 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
584 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400585 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
Mike Lockwood9637d472009-04-02 21:41:57 -0700587 // listen for settings changes
588 ContentResolver resolver = mContext.getContentResolver();
589 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
590 "(" + Settings.System.NAME + "=?)",
591 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
592 null);
593 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
594 SettingsObserver settingsObserver = new SettingsObserver();
595 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597
Mike Lockwood3d12b512009-04-21 23:25:35 -0700598 public void run()
599 {
600 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
601 Looper.prepare();
602 mLocationHandler = new LocationWorkerHandler();
603 initialize();
604 Looper.loop();
605 }
606
Mike Lockwood275555c2009-05-01 11:30:34 -0400607 public void installLocationProvider(String name, ILocationProvider provider) {
608 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
609 != PackageManager.PERMISSION_GRANTED) {
610 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwoode932f7f2009-04-06 10:51:26 -0700611 }
612
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400613 synchronized (mLock) {
Mike Lockwood3681f262009-05-12 10:52:03 -0400614 // check to see if we are reinstalling a dead provider
615 LocationProviderProxy oldProvider = mProvidersByName.get(name);
616 if (oldProvider != null) {
617 if (oldProvider.isDead()) {
618 Log.d(TAG, "replacing dead provider");
619 removeProvider(oldProvider);
620 } else {
621 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
622 }
623 }
624
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400625 LocationProviderProxy proxy = new LocationProviderProxy(name, provider);
626 addProvider(proxy);
627 updateProvidersLocked();
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700628 if (LocationManager.NETWORK_PROVIDER.equals(name)) {
629 mNetworkLocationProvider = proxy;
630 }
Mike Lockwood275555c2009-05-01 11:30:34 -0400631
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400632 // notify provider of current network state
633 proxy.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
635 }
636
Mike Lockwood275555c2009-05-01 11:30:34 -0400637 public void installGeocodeProvider(IGeocodeProvider provider) {
638 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
639 != PackageManager.PERMISSION_GRANTED) {
640 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
Mike Lockwooda55c3212009-04-15 11:10:11 -0400641 }
642
643 mGeocodeProvider = provider;
644 }
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private boolean isAllowedBySettingsLocked(String provider) {
647 if (mEnabledProviders.contains(provider)) {
648 return true;
649 }
650 if (mDisabledProviders.contains(provider)) {
651 return false;
652 }
653 // Use system settings
654 ContentResolver resolver = mContext.getContentResolver();
655 String allowedProviders = Settings.Secure.getString(resolver,
656 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
657
658 return ((allowedProviders != null) && (allowedProviders.contains(provider)));
659 }
660
661 private void checkPermissionsSafe(String provider) {
662 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400663 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 != PackageManager.PERMISSION_GRANTED)) {
665 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
666 }
667 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400668 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400670 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 != PackageManager.PERMISSION_GRANTED)) {
672 throw new SecurityException(
673 "Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission");
674 }
675 }
676
677 private boolean isAllowedProviderSafe(String provider) {
678 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400679 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 != PackageManager.PERMISSION_GRANTED)) {
681 return false;
682 }
683 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400684 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400686 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 != PackageManager.PERMISSION_GRANTED)) {
688 return false;
689 }
690
691 return true;
692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 public List<String> getAllProviders() {
695 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400696 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 return _getAllProvidersLocked();
698 }
699 } catch (SecurityException se) {
700 throw se;
701 } catch (Exception e) {
702 Log.e(TAG, "getAllProviders got exception:", e);
703 return null;
704 }
705 }
706
707 private List<String> _getAllProvidersLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700708 if (LOCAL_LOGV) {
709 Log.v(TAG, "getAllProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400711 ArrayList<String> out = new ArrayList<String>(mProviders.size());
712 for (int i = mProviders.size() - 1; i >= 0; i--) {
713 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 out.add(p.getName());
715 }
716 return out;
717 }
718
719 public List<String> getProviders(boolean enabledOnly) {
720 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400721 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 return _getProvidersLocked(enabledOnly);
723 }
724 } catch (SecurityException se) {
725 throw se;
726 } catch (Exception e) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700727 Log.e(TAG, "getProviders got exception:", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 return null;
729 }
730 }
731
732 private List<String> _getProvidersLocked(boolean enabledOnly) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700733 if (LOCAL_LOGV) {
734 Log.v(TAG, "getProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400736 ArrayList<String> out = new ArrayList<String>(mProviders.size());
737 for (int i = mProviders.size() - 1; i >= 0; i--) {
738 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 String name = p.getName();
740 if (isAllowedProviderSafe(name)) {
741 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
742 continue;
743 }
744 out.add(name);
745 }
746 }
747 return out;
748 }
749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 private void updateProvidersLocked() {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400751 for (int i = mProviders.size() - 1; i >= 0; i--) {
752 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 boolean isEnabled = p.isEnabled();
754 String name = p.getName();
755 boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 if (isEnabled && !shouldBeEnabled) {
758 updateProviderListenersLocked(name, false);
759 } else if (!isEnabled && shouldBeEnabled) {
760 updateProviderListenersLocked(name, true);
761 }
762
763 }
764 }
765
766 private void updateProviderListenersLocked(String provider, boolean enabled) {
767 int listeners = 0;
768
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400769 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 if (p == null) {
771 return;
772 }
773
774 ArrayList<Receiver> deadReceivers = null;
775
776 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
777 if (records != null) {
778 final int N = records.size();
779 for (int i=0; i<N; i++) {
780 UpdateRecord record = records.get(i);
781 // Sends a notification message to the receiver
Mike Lockwood48f17512009-04-23 09:12:08 -0700782 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
783 if (deadReceivers == null) {
784 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
Simon Schoar46866572009-06-10 21:12:10 +0200786 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
788 listeners++;
789 }
790 }
791
792 if (deadReceivers != null) {
793 for (int i=deadReceivers.size()-1; i>=0; i--) {
794 removeUpdatesLocked(deadReceivers.get(i));
795 }
796 }
797
798 if (enabled) {
799 p.enable();
800 if (listeners > 0) {
801 p.setMinTime(getMinTimeLocked(provider));
802 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804 } else {
805 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 private long getMinTimeLocked(String provider) {
811 long minTime = Long.MAX_VALUE;
812 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
813 if (records != null) {
814 for (int i=records.size()-1; i>=0; i--) {
815 minTime = Math.min(minTime, records.get(i).mMinTime);
816 }
817 }
818 return minTime;
819 }
820
821 private class UpdateRecord {
822 final String mProvider;
823 final Receiver mReceiver;
824 final long mMinTime;
825 final float mMinDistance;
826 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400827 Location mLastFixBroadcast;
828 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
830 /**
831 * Note: must be constructed with lock held.
832 */
833 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400834 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 mProvider = provider;
836 mReceiver = receiver;
837 mMinTime = minTime;
838 mMinDistance = minDistance;
839 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840
841 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
842 if (records == null) {
843 records = new ArrayList<UpdateRecord>();
844 mRecordsByProvider.put(provider, records);
845 }
846 if (!records.contains(this)) {
847 records.add(this);
848 }
849 }
850
851 /**
852 * Method to be called when a record will no longer be used. Calling this multiple times
853 * must have the same effect as calling it once.
854 */
855 void disposeLocked() {
856 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
Mike Lockwood3a76fd62009-09-01 07:26:56 -0400857 if (records != null) {
858 records.remove(this);
859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 }
861
862 @Override
863 public String toString() {
864 return "UpdateRecord{"
865 + Integer.toHexString(System.identityHashCode(this))
866 + " " + mProvider + " " + mReceiver + "}";
867 }
868
869 void dump(PrintWriter pw, String prefix) {
870 pw.println(prefix + this);
871 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
872 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400873 pw.println(prefix + "mUid=" + mUid);
874 pw.println(prefix + "mLastFixBroadcast:");
875 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
876 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400880 private Receiver getReceiver(ILocationListener listener) {
881 IBinder binder = listener.asBinder();
882 Receiver receiver = mReceivers.get(binder);
883 if (receiver == null) {
884 receiver = new Receiver(listener);
885 mReceivers.put(binder, receiver);
886
887 try {
888 if (receiver.isListener()) {
889 receiver.getListener().asBinder().linkToDeath(receiver, 0);
890 }
891 } catch (RemoteException e) {
892 Log.e(TAG, "linkToDeath failed:", e);
893 return null;
894 }
895 }
896 return receiver;
897 }
898
899 private Receiver getReceiver(PendingIntent intent) {
900 Receiver receiver = mReceivers.get(intent);
901 if (receiver == null) {
902 receiver = new Receiver(intent);
903 mReceivers.put(intent, receiver);
904 }
905 return receiver;
906 }
907
908 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
909 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
910 if (records != null) {
911 for (int i = records.size() - 1; i >= 0; i--) {
912 UpdateRecord record = records.get(i);
913 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
914 return true;
915 }
916 }
917 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400918 for (ProximityAlert alert : mProximityAlerts.values()) {
919 if (alert.mUid == uid) {
920 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400921 }
922 }
923 return false;
924 }
925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 public void requestLocationUpdates(String provider,
927 long minTime, float minDistance, ILocationListener listener) {
928
929 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400930 synchronized (mLock) {
931 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933 } catch (SecurityException se) {
934 throw se;
935 } catch (Exception e) {
936 Log.e(TAG, "requestUpdates got exception:", e);
937 }
938 }
939
940 public void requestLocationUpdatesPI(String provider,
941 long minTime, float minDistance, PendingIntent intent) {
942 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400943 synchronized (mLock) {
944 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
946 } catch (SecurityException se) {
947 throw se;
948 } catch (Exception e) {
949 Log.e(TAG, "requestUpdates got exception:", e);
950 }
951 }
952
953 private void requestLocationUpdatesLocked(String provider,
954 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700955 if (LOCAL_LOGV) {
956 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400959 LocationProviderProxy proxy = mProvidersByName.get(provider);
960 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 throw new IllegalArgumentException("provider=" + provider);
962 }
963
964 checkPermissionsSafe(provider);
965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 // so wakelock calls will succeed
967 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400968 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 long identity = Binder.clearCallingIdentity();
970 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400971 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
972 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 if (oldRecord != null) {
974 oldRecord.disposeLocked();
975 }
976
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400977 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400978 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400979 }
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
982 if (isProviderEnabled) {
983 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400984 proxy.setMinTime(minTimeForProvider);
985 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700987 // Notify the listener that updates are currently disabled
988 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
990 } finally {
991 Binder.restoreCallingIdentity(identity);
992 }
993 }
994
995 public void removeUpdates(ILocationListener listener) {
996 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400997 synchronized (mLock) {
998 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000 } catch (SecurityException se) {
1001 throw se;
1002 } catch (Exception e) {
1003 Log.e(TAG, "removeUpdates got exception:", e);
1004 }
1005 }
1006
1007 public void removeUpdatesPI(PendingIntent intent) {
1008 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001009 synchronized (mLock) {
1010 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
1012 } catch (SecurityException se) {
1013 throw se;
1014 } catch (Exception e) {
1015 Log.e(TAG, "removeUpdates got exception:", e);
1016 }
1017 }
1018
1019 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001020 if (LOCAL_LOGV) {
1021 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023
1024 // so wakelock calls will succeed
1025 final int callingUid = Binder.getCallingUid();
1026 long identity = Binder.clearCallingIdentity();
1027 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001028 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1029 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
1031
1032 // Record which providers were associated with this listener
1033 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001034 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 if (oldRecords != null) {
1036 // Call dispose() on the obsolete update records.
1037 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001038 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001039 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1040 if (proxy != null) {
1041 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
1044 record.disposeLocked();
1045 }
1046 // Accumulate providers
1047 providers.addAll(oldRecords.keySet());
1048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049
1050 // See if the providers associated with this listener have any
1051 // other listeners; if one does, inform it of the new smallest minTime
1052 // value; if one does not, disable location tracking for it
1053 for (String provider : providers) {
1054 // If provider is already disabled, don't need to do anything
1055 if (!isAllowedBySettingsLocked(provider)) {
1056 continue;
1057 }
1058
1059 boolean hasOtherListener = false;
1060 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1061 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1062 hasOtherListener = true;
1063 }
1064
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001065 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 if (p != null) {
1067 if (hasOtherListener) {
1068 p.setMinTime(getMinTimeLocked(provider));
1069 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 p.enableLocationTracking(false);
1071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 } finally {
1075 Binder.restoreCallingIdentity(identity);
1076 }
1077 }
1078
1079 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001080 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 return false;
1082 }
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001083 if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 PackageManager.PERMISSION_GRANTED) {
1085 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1086 }
1087
1088 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001089 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001091 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 return false;
1093 }
1094 return true;
1095 }
1096
1097 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001098 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001099 try {
1100 mGpsStatusProvider.removeGpsStatusListener(listener);
1101 } catch (Exception e) {
1102 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105 }
1106
1107 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001108 if (provider == null) {
1109 // throw NullPointerException to remain compatible with previous implementation
1110 throw new NullPointerException();
1111 }
1112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 // first check for permission to the provider
1114 checkPermissionsSafe(provider);
1115 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001116 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 != PackageManager.PERMISSION_GRANTED)) {
1118 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1119 }
1120
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001121 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001122 LocationProviderProxy proxy = mProvidersByName.get(provider);
Mike Lockwood6ba7ae12009-08-17 08:39:12 -04001123 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 return false;
1125 }
1126
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001127 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 }
1130
Danke Xie22d1f9f2009-08-18 18:28:45 -04001131 public boolean sendNiResponse(int notifId, int userResponse)
1132 {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001133 if (Binder.getCallingUid() != Process.myUid()) {
1134 throw new SecurityException(
1135 "calling sendNiResponse from outside of the system is not allowed");
1136 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001137 try {
1138 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
1139 }
1140 catch (RemoteException e)
1141 {
1142 Log.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
1143 return false;
1144 }
1145 }
1146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 class ProximityAlert {
1148 final int mUid;
1149 final double mLatitude;
1150 final double mLongitude;
1151 final float mRadius;
1152 final long mExpiration;
1153 final PendingIntent mIntent;
1154 final Location mLocation;
1155
1156 public ProximityAlert(int uid, double latitude, double longitude,
1157 float radius, long expiration, PendingIntent intent) {
1158 mUid = uid;
1159 mLatitude = latitude;
1160 mLongitude = longitude;
1161 mRadius = radius;
1162 mExpiration = expiration;
1163 mIntent = intent;
1164
1165 mLocation = new Location("");
1166 mLocation.setLatitude(latitude);
1167 mLocation.setLongitude(longitude);
1168 }
1169
1170 long getExpiration() {
1171 return mExpiration;
1172 }
1173
1174 PendingIntent getIntent() {
1175 return mIntent;
1176 }
1177
1178 boolean isInProximity(double latitude, double longitude) {
1179 Location loc = new Location("");
1180 loc.setLatitude(latitude);
1181 loc.setLongitude(longitude);
1182
1183 double radius = loc.distanceTo(mLocation);
1184 return radius <= mRadius;
1185 }
1186
1187 @Override
1188 public String toString() {
1189 return "ProximityAlert{"
1190 + Integer.toHexString(System.identityHashCode(this))
1191 + " uid " + mUid + mIntent + "}";
1192 }
1193
1194 void dump(PrintWriter pw, String prefix) {
1195 pw.println(prefix + this);
1196 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1197 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1198 pw.println(prefix + "mIntent=" + mIntent);
1199 pw.println(prefix + "mLocation:");
1200 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1201 }
1202 }
1203
1204 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001205 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206
1207 boolean isGpsAvailable = false;
1208
1209 // Note: this is called with the lock held.
1210 public void onLocationChanged(Location loc) {
1211
1212 // If Gps is available, then ignore updates from NetworkLocationProvider
1213 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1214 isGpsAvailable = true;
1215 }
1216 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1217 return;
1218 }
1219
1220 // Process proximity alerts
1221 long now = System.currentTimeMillis();
1222 double latitude = loc.getLatitude();
1223 double longitude = loc.getLongitude();
1224 ArrayList<PendingIntent> intentsToRemove = null;
1225
1226 for (ProximityAlert alert : mProximityAlerts.values()) {
1227 PendingIntent intent = alert.getIntent();
1228 long expiration = alert.getExpiration();
1229
1230 if ((expiration == -1) || (now <= expiration)) {
1231 boolean entered = mProximitiesEntered.contains(alert);
1232 boolean inProximity =
1233 alert.isInProximity(latitude, longitude);
1234 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001235 if (LOCAL_LOGV) {
1236 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 mProximitiesEntered.add(alert);
1239 Intent enteredIntent = new Intent();
1240 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1241 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001242 synchronized (this) {
1243 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001244 // is called before decrementPendingBroadcasts()
1245 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1246 // call this after broadcasting so we do not increment
1247 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001248 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001251 if (LOCAL_LOGV) {
1252 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 if (intentsToRemove == null) {
1255 intentsToRemove = new ArrayList<PendingIntent>();
1256 }
1257 intentsToRemove.add(intent);
1258 }
1259 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001260 if (LOCAL_LOGV) {
1261 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 mProximitiesEntered.remove(alert);
1264 Intent exitedIntent = new Intent();
1265 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1266 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001267 synchronized (this) {
1268 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001269 // is called before decrementPendingBroadcasts()
1270 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1271 // call this after broadcasting so we do not increment
1272 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001273 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001276 if (LOCAL_LOGV) {
1277 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
1279 if (intentsToRemove == null) {
1280 intentsToRemove = new ArrayList<PendingIntent>();
1281 }
1282 intentsToRemove.add(intent);
1283 }
1284 }
1285 } else {
1286 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001287 if (LOCAL_LOGV) {
1288 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290 if (intentsToRemove == null) {
1291 intentsToRemove = new ArrayList<PendingIntent>();
1292 }
1293 intentsToRemove.add(alert.getIntent());
1294 }
1295 }
1296
1297 // Remove expired alerts
1298 if (intentsToRemove != null) {
1299 for (PendingIntent i : intentsToRemove) {
1300 mProximityAlerts.remove(i);
1301 ProximityAlert alert = mProximityAlerts.get(i);
1302 mProximitiesEntered.remove(alert);
1303 }
1304 }
1305
1306 }
1307
1308 // Note: this is called with the lock held.
1309 public void onProviderDisabled(String provider) {
1310 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1311 isGpsAvailable = false;
1312 }
1313 }
1314
1315 // Note: this is called with the lock held.
1316 public void onProviderEnabled(String provider) {
1317 // ignore
1318 }
1319
1320 // Note: this is called with the lock held.
1321 public void onStatusChanged(String provider, int status, Bundle extras) {
1322 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1323 (status != LocationProvider.AVAILABLE)) {
1324 isGpsAvailable = false;
1325 }
1326 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001327
1328 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1329 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001330 // synchronize to ensure incrementPendingBroadcasts()
1331 // is called before decrementPendingBroadcasts()
1332 synchronized (this) {
1333 decrementPendingBroadcasts();
1334 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337
1338 public void addProximityAlert(double latitude, double longitude,
1339 float radius, long expiration, PendingIntent intent) {
1340 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001341 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1343 }
1344 } catch (SecurityException se) {
1345 throw se;
1346 } catch (Exception e) {
1347 Log.e(TAG, "addProximityAlert got exception:", e);
1348 }
1349 }
1350
1351 private void addProximityAlertLocked(double latitude, double longitude,
1352 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001353 if (LOCAL_LOGV) {
1354 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 ", longitude = " + longitude +
1356 ", expiration = " + expiration +
1357 ", intent = " + intent);
1358 }
1359
1360 // Require ability to access all providers for now
1361 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1362 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1363 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1364 }
1365
1366 if (expiration != -1) {
1367 expiration += System.currentTimeMillis();
1368 }
1369 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1370 latitude, longitude, radius, expiration, intent);
1371 mProximityAlerts.put(intent, alert);
1372
Mike Lockwood48f17512009-04-23 09:12:08 -07001373 if (mProximityReceiver == null) {
1374 mProximityListener = new ProximityListener();
1375 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376
Mike Lockwood95427cd2009-05-07 13:27:54 -04001377 for (int i = mProviders.size() - 1; i >= 0; i--) {
1378 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001379 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382 }
1383
1384 public void removeProximityAlert(PendingIntent intent) {
1385 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001386 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 removeProximityAlertLocked(intent);
1388 }
1389 } catch (SecurityException se) {
1390 throw se;
1391 } catch (Exception e) {
1392 Log.e(TAG, "removeProximityAlert got exception:", e);
1393 }
1394 }
1395
1396 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001397 if (LOCAL_LOGV) {
1398 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
1400
1401 mProximityAlerts.remove(intent);
1402 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001403 removeUpdatesLocked(mProximityReceiver);
1404 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 }
1408
1409 /**
1410 * @return null if the provider does not exits
1411 * @throw SecurityException if the provider is not allowed to be
1412 * accessed by the caller
1413 */
1414 public Bundle getProviderInfo(String provider) {
1415 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001416 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 return _getProviderInfoLocked(provider);
1418 }
1419 } catch (SecurityException se) {
1420 throw se;
1421 } catch (Exception e) {
1422 Log.e(TAG, "_getProviderInfo got exception:", e);
1423 return null;
1424 }
1425 }
1426
1427 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001428 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (p == null) {
1430 return null;
1431 }
1432
1433 checkPermissionsSafe(provider);
1434
1435 Bundle b = new Bundle();
1436 b.putBoolean("network", p.requiresNetwork());
1437 b.putBoolean("satellite", p.requiresSatellite());
1438 b.putBoolean("cell", p.requiresCell());
1439 b.putBoolean("cost", p.hasMonetaryCost());
1440 b.putBoolean("altitude", p.supportsAltitude());
1441 b.putBoolean("speed", p.supportsSpeed());
1442 b.putBoolean("bearing", p.supportsBearing());
1443 b.putInt("power", p.getPowerRequirement());
1444 b.putInt("accuracy", p.getAccuracy());
1445
1446 return b;
1447 }
1448
1449 public boolean isProviderEnabled(String provider) {
1450 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001451 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 return _isProviderEnabledLocked(provider);
1453 }
1454 } catch (SecurityException se) {
1455 throw se;
1456 } catch (Exception e) {
1457 Log.e(TAG, "isProviderEnabled got exception:", e);
1458 return false;
1459 }
1460 }
1461
Mike Lockwood275555c2009-05-01 11:30:34 -04001462 public void reportLocation(Location location) {
1463 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1464 != PackageManager.PERMISSION_GRANTED) {
1465 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1466 }
1467
Mike Lockwood4e50b782009-04-03 08:24:43 -07001468 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1469 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1470 mLocationHandler.sendMessageAtFrontOfQueue(m);
1471 }
1472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 private boolean _isProviderEnabledLocked(String provider) {
1474 checkPermissionsSafe(provider);
1475
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001476 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 if (p == null) {
1478 throw new IllegalArgumentException("provider=" + provider);
1479 }
1480 return isAllowedBySettingsLocked(provider);
1481 }
1482
1483 public Location getLastKnownLocation(String provider) {
1484 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001485 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 return _getLastKnownLocationLocked(provider);
1487 }
1488 } catch (SecurityException se) {
1489 throw se;
1490 } catch (Exception e) {
1491 Log.e(TAG, "getLastKnownLocation got exception:", e);
1492 return null;
1493 }
1494 }
1495
1496 private Location _getLastKnownLocationLocked(String provider) {
1497 checkPermissionsSafe(provider);
1498
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001499 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 if (p == null) {
1501 throw new IllegalArgumentException("provider=" + provider);
1502 }
1503
1504 if (!isAllowedBySettingsLocked(provider)) {
1505 return null;
1506 }
1507
1508 Location location = mLastKnownLocation.get(provider);
1509 if (location == null) {
1510 // Get the persistent last known location for the provider
1511 location = readLastKnownLocationLocked(provider);
1512 if (location != null) {
1513 mLastKnownLocation.put(provider, location);
1514 }
1515 }
1516
1517 return location;
1518 }
1519
1520 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1521 // Always broadcast the first update
1522 if (lastLoc == null) {
1523 return true;
1524 }
1525
1526 // Don't broadcast same location again regardless of condition
1527 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1528 if (loc.getTime() == lastLoc.getTime()) {
1529 return false;
1530 }
1531
1532 // Check whether sufficient distance has been traveled
1533 double minDistance = record.mMinDistance;
1534 if (minDistance > 0.0) {
1535 if (loc.distanceTo(lastLoc) <= minDistance) {
1536 return false;
1537 }
1538 }
1539
1540 return true;
1541 }
1542
Mike Lockwood4e50b782009-04-03 08:24:43 -07001543 private void handleLocationChangedLocked(Location location) {
1544 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1546 if (records == null || records.size() == 0) {
1547 return;
1548 }
1549
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001550 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (p == null) {
1552 return;
1553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001556 Location lastLocation = mLastKnownLocation.get(provider);
1557 if (lastLocation == null) {
1558 mLastKnownLocation.put(provider, new Location(location));
1559 } else {
1560 lastLocation.set(location);
1561 }
1562 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 // Fetch latest status update time
1565 long newStatusUpdateTime = p.getStatusUpdateTime();
1566
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001567 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 Bundle extras = new Bundle();
1569 int status = p.getStatus(extras);
1570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 ArrayList<Receiver> deadReceivers = null;
1572
1573 // Broadcast location or status to all listeners
1574 final int N = records.size();
1575 for (int i=0; i<N; i++) {
1576 UpdateRecord r = records.get(i);
1577 Receiver receiver = r.mReceiver;
1578
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001579 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001580 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1581 if (lastLoc == null) {
1582 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001583 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001584 } else {
1585 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001587 if (!receiver.callLocationChangedLocked(location)) {
1588 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1589 if (deadReceivers == null) {
1590 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001592 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594 }
1595
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001596 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1598 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1599
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001600 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1602 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1603 if (deadReceivers == null) {
1604 deadReceivers = new ArrayList<Receiver>();
1605 }
1606 if (!deadReceivers.contains(receiver)) {
1607 deadReceivers.add(receiver);
1608 }
1609 }
1610 }
1611 }
1612
1613 if (deadReceivers != null) {
1614 for (int i=deadReceivers.size()-1; i>=0; i--) {
1615 removeUpdatesLocked(deadReceivers.get(i));
1616 }
1617 }
1618 }
1619
1620 private class LocationWorkerHandler extends Handler {
1621
1622 @Override
1623 public void handleMessage(Message msg) {
1624 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001625 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1626 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001628 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001629 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001630 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001631
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001632 // notify other providers of the new location
1633 for (int i = mProviders.size() - 1; i >= 0; i--) {
1634 LocationProviderProxy proxy = mProviders.get(i);
1635 if (!provider.equals(proxy.getName())) {
1636 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001637 }
1638 }
1639
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001640 if (isAllowedBySettingsLocked(provider)) {
1641 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 } catch (Exception e) {
1646 // Log, don't crash!
1647 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1648 }
1649 }
1650 }
1651
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001652 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1653 @Override
1654 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 String action = intent.getAction();
1656
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001657 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001659 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1661 if (uid >= 0) {
1662 ArrayList<Receiver> removedRecs = null;
1663 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1664 for (int j=i.size()-1; j>=0; j--) {
1665 UpdateRecord ur = i.get(j);
1666 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1667 if (removedRecs == null) {
1668 removedRecs = new ArrayList<Receiver>();
1669 }
1670 if (!removedRecs.contains(ur.mReceiver)) {
1671 removedRecs.add(ur.mReceiver);
1672 }
1673 }
1674 }
1675 }
1676 ArrayList<ProximityAlert> removedAlerts = null;
1677 for (ProximityAlert i : mProximityAlerts.values()) {
1678 if (i.mUid == uid) {
1679 if (removedAlerts == null) {
1680 removedAlerts = new ArrayList<ProximityAlert>();
1681 }
1682 if (!removedAlerts.contains(i)) {
1683 removedAlerts.add(i);
1684 }
1685 }
1686 }
1687 if (removedRecs != null) {
1688 for (int i=removedRecs.size()-1; i>=0; i--) {
1689 removeUpdatesLocked(removedRecs.get(i));
1690 }
1691 }
1692 if (removedAlerts != null) {
1693 for (int i=removedAlerts.size()-1; i>=0; i--) {
1694 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1695 }
1696 }
1697 }
1698 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001699 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 boolean noConnectivity =
1701 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1702 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001703 mNetworkState = LocationProvider.AVAILABLE;
1704 } else {
1705 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
1707
1708 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001709 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001710 for (int i = mProviders.size() - 1; i >= 0; i--) {
1711 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001713 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
1715 }
1716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001719 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720
1721 // Wake locks
1722
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001723 private void incrementPendingBroadcasts() {
1724 synchronized (mWakeLock) {
1725 if (mPendingBroadcasts++ == 0) {
1726 try {
1727 mWakeLock.acquire();
1728 log("Acquired wakelock");
1729 } catch (Exception e) {
1730 // This is to catch a runtime exception thrown when we try to release an
1731 // already released lock.
1732 Log.e(TAG, "exception in acquireWakeLock()", e);
1733 }
1734 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001735 }
1736 }
1737
1738 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001739 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001740 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001741 try {
1742 // Release wake lock
1743 if (mWakeLock.isHeld()) {
1744 mWakeLock.release();
1745 log("Released wakelock");
1746 } else {
1747 log("Can't release wakelock again!");
1748 }
1749 } catch (Exception e) {
1750 // This is to catch a runtime exception thrown when we try to release an
1751 // already released lock.
1752 Log.e(TAG, "exception in releaseWakeLock()", e);
1753 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001754 }
1755 }
1756 }
1757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 // Geocoder
1759
1760 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001761 String language, String country, String variant, String appName, List<Address> addrs) {
1762 if (mGeocodeProvider != null) {
1763 try {
1764 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1765 variant, appName, addrs);
1766 } catch (RemoteException e) {
1767 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001768 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001771 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
1773
Mike Lockwooda55c3212009-04-15 11:10:11 -04001774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001776 double lowerLeftLatitude, double lowerLeftLongitude,
1777 double upperRightLatitude, double upperRightLongitude, int maxResults,
1778 String language, String country, String variant, String appName, List<Address> addrs) {
1779
1780 if (mGeocodeProvider != null) {
1781 try {
1782 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1783 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1784 maxResults, language, country, variant, appName, addrs);
1785 } catch (RemoteException e) {
1786 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001787 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001790 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 }
1792
1793 // Mock Providers
1794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 private void checkMockPermissionsSafe() {
1796 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1797 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1798 if (!allowMocks) {
1799 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1800 }
1801
1802 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1803 PackageManager.PERMISSION_GRANTED) {
1804 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1805 }
1806 }
1807
1808 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1809 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1810 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1811 checkMockPermissionsSafe();
1812
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001813 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001814 MockProvider provider = new MockProvider(name, this,
1815 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 requiresCell, hasMonetaryCost, supportsAltitude,
1817 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001818 // remove the real provider if we are replacing GPS or network provider
1819 if (LocationManager.GPS_PROVIDER.equals(name)
1820 || LocationManager.NETWORK_PROVIDER.equals(name)) {
1821 LocationProviderProxy proxy = mProvidersByName.get(name);
1822 if (proxy != null) {
1823 proxy.enableLocationTracking(false);
1824 removeProvider(proxy);
1825 }
1826 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001827 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1829 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001830 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001831 mMockProviders.put(name, provider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001832 mLastKnownLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 updateProvidersLocked();
1834 }
1835 }
1836
1837 public void removeTestProvider(String provider) {
1838 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001839 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001840 MockProvider mockProvider = mMockProviders.get(provider);
1841 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1843 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001844 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001845 mMockProviders.remove(mockProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001846 // reinstall real provider if we were mocking GPS or network provider
1847 if (LocationManager.GPS_PROVIDER.equals(provider) &&
1848 mGpsLocationProvider != null) {
1849 addProvider(mGpsLocationProvider);
1850 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) &&
1851 mNetworkLocationProvider != null) {
1852 addProvider(mNetworkLocationProvider);
1853 }
1854 mLastKnownLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 updateProvidersLocked();
1856 }
1857 }
1858
1859 public void setTestProviderLocation(String provider, Location loc) {
1860 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001861 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001862 MockProvider mockProvider = mMockProviders.get(provider);
1863 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1865 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001866 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1867 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001868 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001869 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871 }
1872
1873 public void clearTestProviderLocation(String provider) {
1874 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001875 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001876 MockProvider mockProvider = mMockProviders.get(provider);
1877 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1879 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001880 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882 }
1883
1884 public void setTestProviderEnabled(String provider, boolean enabled) {
1885 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001886 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001887 MockProvider mockProvider = mMockProviders.get(provider);
1888 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1890 }
1891 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001892 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 mEnabledProviders.add(provider);
1894 mDisabledProviders.remove(provider);
1895 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001896 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 mEnabledProviders.remove(provider);
1898 mDisabledProviders.add(provider);
1899 }
1900 updateProvidersLocked();
1901 }
1902 }
1903
1904 public void clearTestProviderEnabled(String provider) {
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 }
1911 mEnabledProviders.remove(provider);
1912 mDisabledProviders.remove(provider);
1913 updateProvidersLocked();
1914 }
1915 }
1916
1917 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1918 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001919 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001920 MockProvider mockProvider = mMockProviders.get(provider);
1921 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1923 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001924 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926 }
1927
1928 public void clearTestProviderStatus(String provider) {
1929 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001930 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001931 MockProvider mockProvider = mMockProviders.get(provider);
1932 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1934 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001935 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937 }
1938
1939 private void log(String log) {
1940 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1941 Log.d(TAG, log);
1942 }
1943 }
1944
1945 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1946 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1947 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001948 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 + Binder.getCallingPid()
1950 + ", uid=" + Binder.getCallingUid());
1951 return;
1952 }
1953
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001954 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 pw.println("Current Location Manager state:");
1956 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001958 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001960 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001963 for (Receiver i : mReceivers.values()) {
1964 pw.println(" " + i + ":");
1965 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 pw.println(" " + j.getKey() + ":");
1967 j.getValue().dump(pw, " ");
1968 }
1969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 pw.println(" Records by Provider:");
1971 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1972 : mRecordsByProvider.entrySet()) {
1973 pw.println(" " + i.getKey() + ":");
1974 for (UpdateRecord j : i.getValue()) {
1975 pw.println(" " + j + ":");
1976 j.dump(pw, " ");
1977 }
1978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 pw.println(" Last Known Locations:");
1980 for (Map.Entry<String, Location> i
1981 : mLastKnownLocation.entrySet()) {
1982 pw.println(" " + i.getKey() + ":");
1983 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1984 }
1985 if (mProximityAlerts.size() > 0) {
1986 pw.println(" Proximity Alerts:");
1987 for (Map.Entry<PendingIntent, ProximityAlert> i
1988 : mProximityAlerts.entrySet()) {
1989 pw.println(" " + i.getKey() + ":");
1990 i.getValue().dump(pw, " ");
1991 }
1992 }
1993 if (mProximitiesEntered.size() > 0) {
1994 pw.println(" Proximities Entered:");
1995 for (ProximityAlert i : mProximitiesEntered) {
1996 pw.println(" " + i + ":");
1997 i.dump(pw, " ");
1998 }
1999 }
Mike Lockwood48f17512009-04-23 09:12:08 -07002000 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 pw.println(" mProximityListener=" + mProximityListener);
2002 if (mEnabledProviders.size() > 0) {
2003 pw.println(" Enabled Providers:");
2004 for (String i : mEnabledProviders) {
2005 pw.println(" " + i);
2006 }
2007
2008 }
2009 if (mDisabledProviders.size() > 0) {
2010 pw.println(" Disabled Providers:");
2011 for (String i : mDisabledProviders) {
2012 pw.println(" " + i);
2013 }
2014
2015 }
2016 if (mMockProviders.size() > 0) {
2017 pw.println(" Mock Providers:");
2018 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002019 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
2023 }
2024}