blob: 7c33e3736c4a60ee92677414fdf42e0d1d3f1c09 [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);
857 records.remove(this);
858 }
859
860 @Override
861 public String toString() {
862 return "UpdateRecord{"
863 + Integer.toHexString(System.identityHashCode(this))
864 + " " + mProvider + " " + mReceiver + "}";
865 }
866
867 void dump(PrintWriter pw, String prefix) {
868 pw.println(prefix + this);
869 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
870 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400871 pw.println(prefix + "mUid=" + mUid);
872 pw.println(prefix + "mLastFixBroadcast:");
873 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
874 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876
877 /**
878 * Calls dispose().
879 */
880 @Override protected void finalize() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400881 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 disposeLocked();
883 }
884 }
885 }
886
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400887 private Receiver getReceiver(ILocationListener listener) {
888 IBinder binder = listener.asBinder();
889 Receiver receiver = mReceivers.get(binder);
890 if (receiver == null) {
891 receiver = new Receiver(listener);
892 mReceivers.put(binder, receiver);
893
894 try {
895 if (receiver.isListener()) {
896 receiver.getListener().asBinder().linkToDeath(receiver, 0);
897 }
898 } catch (RemoteException e) {
899 Log.e(TAG, "linkToDeath failed:", e);
900 return null;
901 }
902 }
903 return receiver;
904 }
905
906 private Receiver getReceiver(PendingIntent intent) {
907 Receiver receiver = mReceivers.get(intent);
908 if (receiver == null) {
909 receiver = new Receiver(intent);
910 mReceivers.put(intent, receiver);
911 }
912 return receiver;
913 }
914
915 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
916 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
917 if (records != null) {
918 for (int i = records.size() - 1; i >= 0; i--) {
919 UpdateRecord record = records.get(i);
920 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
921 return true;
922 }
923 }
924 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400925 for (ProximityAlert alert : mProximityAlerts.values()) {
926 if (alert.mUid == uid) {
927 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400928 }
929 }
930 return false;
931 }
932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 public void requestLocationUpdates(String provider,
934 long minTime, float minDistance, ILocationListener listener) {
935
936 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400937 synchronized (mLock) {
938 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940 } catch (SecurityException se) {
941 throw se;
942 } catch (Exception e) {
943 Log.e(TAG, "requestUpdates got exception:", e);
944 }
945 }
946
947 public void requestLocationUpdatesPI(String provider,
948 long minTime, float minDistance, PendingIntent intent) {
949 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400950 synchronized (mLock) {
951 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953 } catch (SecurityException se) {
954 throw se;
955 } catch (Exception e) {
956 Log.e(TAG, "requestUpdates got exception:", e);
957 }
958 }
959
960 private void requestLocationUpdatesLocked(String provider,
961 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700962 if (LOCAL_LOGV) {
963 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400966 LocationProviderProxy proxy = mProvidersByName.get(provider);
967 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 throw new IllegalArgumentException("provider=" + provider);
969 }
970
971 checkPermissionsSafe(provider);
972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 // so wakelock calls will succeed
974 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400975 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 long identity = Binder.clearCallingIdentity();
977 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400978 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
979 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if (oldRecord != null) {
981 oldRecord.disposeLocked();
982 }
983
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400984 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400985 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400986 }
987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
989 if (isProviderEnabled) {
990 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400991 proxy.setMinTime(minTimeForProvider);
992 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700994 // Notify the listener that updates are currently disabled
995 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997 } finally {
998 Binder.restoreCallingIdentity(identity);
999 }
1000 }
1001
1002 public void removeUpdates(ILocationListener listener) {
1003 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001004 synchronized (mLock) {
1005 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007 } catch (SecurityException se) {
1008 throw se;
1009 } catch (Exception e) {
1010 Log.e(TAG, "removeUpdates got exception:", e);
1011 }
1012 }
1013
1014 public void removeUpdatesPI(PendingIntent intent) {
1015 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001016 synchronized (mLock) {
1017 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 } catch (SecurityException se) {
1020 throw se;
1021 } catch (Exception e) {
1022 Log.e(TAG, "removeUpdates got exception:", e);
1023 }
1024 }
1025
1026 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001027 if (LOCAL_LOGV) {
1028 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
1030
1031 // so wakelock calls will succeed
1032 final int callingUid = Binder.getCallingUid();
1033 long identity = Binder.clearCallingIdentity();
1034 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001035 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1036 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038
1039 // Record which providers were associated with this listener
1040 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001041 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 if (oldRecords != null) {
1043 // Call dispose() on the obsolete update records.
1044 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001045 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001046 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
1047 if (proxy != null) {
1048 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050 }
1051 record.disposeLocked();
1052 }
1053 // Accumulate providers
1054 providers.addAll(oldRecords.keySet());
1055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
1057 // See if the providers associated with this listener have any
1058 // other listeners; if one does, inform it of the new smallest minTime
1059 // value; if one does not, disable location tracking for it
1060 for (String provider : providers) {
1061 // If provider is already disabled, don't need to do anything
1062 if (!isAllowedBySettingsLocked(provider)) {
1063 continue;
1064 }
1065
1066 boolean hasOtherListener = false;
1067 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
1068 if (recordsForProvider != null && recordsForProvider.size() > 0) {
1069 hasOtherListener = true;
1070 }
1071
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001072 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (p != null) {
1074 if (hasOtherListener) {
1075 p.setMinTime(getMinTimeLocked(provider));
1076 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 p.enableLocationTracking(false);
1078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 } finally {
1082 Binder.restoreCallingIdentity(identity);
1083 }
1084 }
1085
1086 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001087 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 return false;
1089 }
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001090 if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 PackageManager.PERMISSION_GRANTED) {
1092 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1093 }
1094
1095 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001096 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001098 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 return false;
1100 }
1101 return true;
1102 }
1103
1104 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001105 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001106 try {
1107 mGpsStatusProvider.removeGpsStatusListener(listener);
1108 } catch (Exception e) {
1109 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
1110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112 }
1113
1114 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001115 if (provider == null) {
1116 // throw NullPointerException to remain compatible with previous implementation
1117 throw new NullPointerException();
1118 }
1119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // first check for permission to the provider
1121 checkPermissionsSafe(provider);
1122 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001123 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 != PackageManager.PERMISSION_GRANTED)) {
1125 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1126 }
1127
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001128 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001129 LocationProviderProxy proxy = mProvidersByName.get(provider);
Mike Lockwood6ba7ae12009-08-17 08:39:12 -04001130 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 return false;
1132 }
1133
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001134 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 }
1137
Danke Xie22d1f9f2009-08-18 18:28:45 -04001138 public boolean sendNiResponse(int notifId, int userResponse)
1139 {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001140 if (Binder.getCallingUid() != Process.myUid()) {
1141 throw new SecurityException(
1142 "calling sendNiResponse from outside of the system is not allowed");
1143 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001144 try {
1145 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
1146 }
1147 catch (RemoteException e)
1148 {
1149 Log.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
1150 return false;
1151 }
1152 }
1153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 class ProximityAlert {
1155 final int mUid;
1156 final double mLatitude;
1157 final double mLongitude;
1158 final float mRadius;
1159 final long mExpiration;
1160 final PendingIntent mIntent;
1161 final Location mLocation;
1162
1163 public ProximityAlert(int uid, double latitude, double longitude,
1164 float radius, long expiration, PendingIntent intent) {
1165 mUid = uid;
1166 mLatitude = latitude;
1167 mLongitude = longitude;
1168 mRadius = radius;
1169 mExpiration = expiration;
1170 mIntent = intent;
1171
1172 mLocation = new Location("");
1173 mLocation.setLatitude(latitude);
1174 mLocation.setLongitude(longitude);
1175 }
1176
1177 long getExpiration() {
1178 return mExpiration;
1179 }
1180
1181 PendingIntent getIntent() {
1182 return mIntent;
1183 }
1184
1185 boolean isInProximity(double latitude, double longitude) {
1186 Location loc = new Location("");
1187 loc.setLatitude(latitude);
1188 loc.setLongitude(longitude);
1189
1190 double radius = loc.distanceTo(mLocation);
1191 return radius <= mRadius;
1192 }
1193
1194 @Override
1195 public String toString() {
1196 return "ProximityAlert{"
1197 + Integer.toHexString(System.identityHashCode(this))
1198 + " uid " + mUid + mIntent + "}";
1199 }
1200
1201 void dump(PrintWriter pw, String prefix) {
1202 pw.println(prefix + this);
1203 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1204 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1205 pw.println(prefix + "mIntent=" + mIntent);
1206 pw.println(prefix + "mLocation:");
1207 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1208 }
1209 }
1210
1211 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001212 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213
1214 boolean isGpsAvailable = false;
1215
1216 // Note: this is called with the lock held.
1217 public void onLocationChanged(Location loc) {
1218
1219 // If Gps is available, then ignore updates from NetworkLocationProvider
1220 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1221 isGpsAvailable = true;
1222 }
1223 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1224 return;
1225 }
1226
1227 // Process proximity alerts
1228 long now = System.currentTimeMillis();
1229 double latitude = loc.getLatitude();
1230 double longitude = loc.getLongitude();
1231 ArrayList<PendingIntent> intentsToRemove = null;
1232
1233 for (ProximityAlert alert : mProximityAlerts.values()) {
1234 PendingIntent intent = alert.getIntent();
1235 long expiration = alert.getExpiration();
1236
1237 if ((expiration == -1) || (now <= expiration)) {
1238 boolean entered = mProximitiesEntered.contains(alert);
1239 boolean inProximity =
1240 alert.isInProximity(latitude, longitude);
1241 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001242 if (LOCAL_LOGV) {
1243 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245 mProximitiesEntered.add(alert);
1246 Intent enteredIntent = new Intent();
1247 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1248 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001249 synchronized (this) {
1250 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001251 // is called before decrementPendingBroadcasts()
1252 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1253 // call this after broadcasting so we do not increment
1254 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001255 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001258 if (LOCAL_LOGV) {
1259 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 if (intentsToRemove == null) {
1262 intentsToRemove = new ArrayList<PendingIntent>();
1263 }
1264 intentsToRemove.add(intent);
1265 }
1266 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001267 if (LOCAL_LOGV) {
1268 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 }
1270 mProximitiesEntered.remove(alert);
1271 Intent exitedIntent = new Intent();
1272 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1273 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001274 synchronized (this) {
1275 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001276 // is called before decrementPendingBroadcasts()
1277 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1278 // call this after broadcasting so we do not increment
1279 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001280 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001283 if (LOCAL_LOGV) {
1284 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 if (intentsToRemove == null) {
1287 intentsToRemove = new ArrayList<PendingIntent>();
1288 }
1289 intentsToRemove.add(intent);
1290 }
1291 }
1292 } else {
1293 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001294 if (LOCAL_LOGV) {
1295 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297 if (intentsToRemove == null) {
1298 intentsToRemove = new ArrayList<PendingIntent>();
1299 }
1300 intentsToRemove.add(alert.getIntent());
1301 }
1302 }
1303
1304 // Remove expired alerts
1305 if (intentsToRemove != null) {
1306 for (PendingIntent i : intentsToRemove) {
1307 mProximityAlerts.remove(i);
1308 ProximityAlert alert = mProximityAlerts.get(i);
1309 mProximitiesEntered.remove(alert);
1310 }
1311 }
1312
1313 }
1314
1315 // Note: this is called with the lock held.
1316 public void onProviderDisabled(String provider) {
1317 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1318 isGpsAvailable = false;
1319 }
1320 }
1321
1322 // Note: this is called with the lock held.
1323 public void onProviderEnabled(String provider) {
1324 // ignore
1325 }
1326
1327 // Note: this is called with the lock held.
1328 public void onStatusChanged(String provider, int status, Bundle extras) {
1329 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1330 (status != LocationProvider.AVAILABLE)) {
1331 isGpsAvailable = false;
1332 }
1333 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001334
1335 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1336 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001337 // synchronize to ensure incrementPendingBroadcasts()
1338 // is called before decrementPendingBroadcasts()
1339 synchronized (this) {
1340 decrementPendingBroadcasts();
1341 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344
1345 public void addProximityAlert(double latitude, double longitude,
1346 float radius, long expiration, PendingIntent intent) {
1347 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001348 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1350 }
1351 } catch (SecurityException se) {
1352 throw se;
1353 } catch (Exception e) {
1354 Log.e(TAG, "addProximityAlert got exception:", e);
1355 }
1356 }
1357
1358 private void addProximityAlertLocked(double latitude, double longitude,
1359 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001360 if (LOCAL_LOGV) {
1361 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 ", longitude = " + longitude +
1363 ", expiration = " + expiration +
1364 ", intent = " + intent);
1365 }
1366
1367 // Require ability to access all providers for now
1368 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1369 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1370 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1371 }
1372
1373 if (expiration != -1) {
1374 expiration += System.currentTimeMillis();
1375 }
1376 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1377 latitude, longitude, radius, expiration, intent);
1378 mProximityAlerts.put(intent, alert);
1379
Mike Lockwood48f17512009-04-23 09:12:08 -07001380 if (mProximityReceiver == null) {
1381 mProximityListener = new ProximityListener();
1382 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383
Mike Lockwood95427cd2009-05-07 13:27:54 -04001384 for (int i = mProviders.size() - 1; i >= 0; i--) {
1385 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001386 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389 }
1390
1391 public void removeProximityAlert(PendingIntent intent) {
1392 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001393 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 removeProximityAlertLocked(intent);
1395 }
1396 } catch (SecurityException se) {
1397 throw se;
1398 } catch (Exception e) {
1399 Log.e(TAG, "removeProximityAlert got exception:", e);
1400 }
1401 }
1402
1403 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001404 if (LOCAL_LOGV) {
1405 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407
1408 mProximityAlerts.remove(intent);
1409 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001410 removeUpdatesLocked(mProximityReceiver);
1411 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414 }
1415
1416 /**
1417 * @return null if the provider does not exits
1418 * @throw SecurityException if the provider is not allowed to be
1419 * accessed by the caller
1420 */
1421 public Bundle getProviderInfo(String provider) {
1422 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001423 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 return _getProviderInfoLocked(provider);
1425 }
1426 } catch (SecurityException se) {
1427 throw se;
1428 } catch (Exception e) {
1429 Log.e(TAG, "_getProviderInfo got exception:", e);
1430 return null;
1431 }
1432 }
1433
1434 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001435 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 if (p == null) {
1437 return null;
1438 }
1439
1440 checkPermissionsSafe(provider);
1441
1442 Bundle b = new Bundle();
1443 b.putBoolean("network", p.requiresNetwork());
1444 b.putBoolean("satellite", p.requiresSatellite());
1445 b.putBoolean("cell", p.requiresCell());
1446 b.putBoolean("cost", p.hasMonetaryCost());
1447 b.putBoolean("altitude", p.supportsAltitude());
1448 b.putBoolean("speed", p.supportsSpeed());
1449 b.putBoolean("bearing", p.supportsBearing());
1450 b.putInt("power", p.getPowerRequirement());
1451 b.putInt("accuracy", p.getAccuracy());
1452
1453 return b;
1454 }
1455
1456 public boolean isProviderEnabled(String provider) {
1457 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001458 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 return _isProviderEnabledLocked(provider);
1460 }
1461 } catch (SecurityException se) {
1462 throw se;
1463 } catch (Exception e) {
1464 Log.e(TAG, "isProviderEnabled got exception:", e);
1465 return false;
1466 }
1467 }
1468
Mike Lockwood275555c2009-05-01 11:30:34 -04001469 public void reportLocation(Location location) {
1470 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1471 != PackageManager.PERMISSION_GRANTED) {
1472 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1473 }
1474
Mike Lockwood4e50b782009-04-03 08:24:43 -07001475 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1476 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1477 mLocationHandler.sendMessageAtFrontOfQueue(m);
1478 }
1479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 private boolean _isProviderEnabledLocked(String provider) {
1481 checkPermissionsSafe(provider);
1482
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001483 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (p == null) {
1485 throw new IllegalArgumentException("provider=" + provider);
1486 }
1487 return isAllowedBySettingsLocked(provider);
1488 }
1489
1490 public Location getLastKnownLocation(String provider) {
1491 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001492 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 return _getLastKnownLocationLocked(provider);
1494 }
1495 } catch (SecurityException se) {
1496 throw se;
1497 } catch (Exception e) {
1498 Log.e(TAG, "getLastKnownLocation got exception:", e);
1499 return null;
1500 }
1501 }
1502
1503 private Location _getLastKnownLocationLocked(String provider) {
1504 checkPermissionsSafe(provider);
1505
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001506 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 if (p == null) {
1508 throw new IllegalArgumentException("provider=" + provider);
1509 }
1510
1511 if (!isAllowedBySettingsLocked(provider)) {
1512 return null;
1513 }
1514
1515 Location location = mLastKnownLocation.get(provider);
1516 if (location == null) {
1517 // Get the persistent last known location for the provider
1518 location = readLastKnownLocationLocked(provider);
1519 if (location != null) {
1520 mLastKnownLocation.put(provider, location);
1521 }
1522 }
1523
1524 return location;
1525 }
1526
1527 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1528 // Always broadcast the first update
1529 if (lastLoc == null) {
1530 return true;
1531 }
1532
1533 // Don't broadcast same location again regardless of condition
1534 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1535 if (loc.getTime() == lastLoc.getTime()) {
1536 return false;
1537 }
1538
1539 // Check whether sufficient distance has been traveled
1540 double minDistance = record.mMinDistance;
1541 if (minDistance > 0.0) {
1542 if (loc.distanceTo(lastLoc) <= minDistance) {
1543 return false;
1544 }
1545 }
1546
1547 return true;
1548 }
1549
Mike Lockwood4e50b782009-04-03 08:24:43 -07001550 private void handleLocationChangedLocked(Location location) {
1551 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1553 if (records == null || records.size() == 0) {
1554 return;
1555 }
1556
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001557 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (p == null) {
1559 return;
1560 }
1561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001563 Location lastLocation = mLastKnownLocation.get(provider);
1564 if (lastLocation == null) {
1565 mLastKnownLocation.put(provider, new Location(location));
1566 } else {
1567 lastLocation.set(location);
1568 }
1569 writeLastKnownLocationLocked(provider, location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 // Fetch latest status update time
1572 long newStatusUpdateTime = p.getStatusUpdateTime();
1573
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001574 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 Bundle extras = new Bundle();
1576 int status = p.getStatus(extras);
1577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 ArrayList<Receiver> deadReceivers = null;
1579
1580 // Broadcast location or status to all listeners
1581 final int N = records.size();
1582 for (int i=0; i<N; i++) {
1583 UpdateRecord r = records.get(i);
1584 Receiver receiver = r.mReceiver;
1585
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001586 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001587 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1588 if (lastLoc == null) {
1589 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001590 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001591 } else {
1592 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001594 if (!receiver.callLocationChangedLocked(location)) {
1595 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1596 if (deadReceivers == null) {
1597 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001599 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601 }
1602
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001603 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1605 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1606
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001607 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1609 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1610 if (deadReceivers == null) {
1611 deadReceivers = new ArrayList<Receiver>();
1612 }
1613 if (!deadReceivers.contains(receiver)) {
1614 deadReceivers.add(receiver);
1615 }
1616 }
1617 }
1618 }
1619
1620 if (deadReceivers != null) {
1621 for (int i=deadReceivers.size()-1; i>=0; i--) {
1622 removeUpdatesLocked(deadReceivers.get(i));
1623 }
1624 }
1625 }
1626
1627 private class LocationWorkerHandler extends Handler {
1628
1629 @Override
1630 public void handleMessage(Message msg) {
1631 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001632 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1633 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001635 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001636 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001637 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001638
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001639 // notify other providers of the new location
1640 for (int i = mProviders.size() - 1; i >= 0; i--) {
1641 LocationProviderProxy proxy = mProviders.get(i);
1642 if (!provider.equals(proxy.getName())) {
1643 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001644 }
1645 }
1646
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001647 if (isAllowedBySettingsLocked(provider)) {
1648 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652 } catch (Exception e) {
1653 // Log, don't crash!
1654 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1655 }
1656 }
1657 }
1658
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001659 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1660 @Override
1661 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 String action = intent.getAction();
1663
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001664 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001666 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1668 if (uid >= 0) {
1669 ArrayList<Receiver> removedRecs = null;
1670 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1671 for (int j=i.size()-1; j>=0; j--) {
1672 UpdateRecord ur = i.get(j);
1673 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1674 if (removedRecs == null) {
1675 removedRecs = new ArrayList<Receiver>();
1676 }
1677 if (!removedRecs.contains(ur.mReceiver)) {
1678 removedRecs.add(ur.mReceiver);
1679 }
1680 }
1681 }
1682 }
1683 ArrayList<ProximityAlert> removedAlerts = null;
1684 for (ProximityAlert i : mProximityAlerts.values()) {
1685 if (i.mUid == uid) {
1686 if (removedAlerts == null) {
1687 removedAlerts = new ArrayList<ProximityAlert>();
1688 }
1689 if (!removedAlerts.contains(i)) {
1690 removedAlerts.add(i);
1691 }
1692 }
1693 }
1694 if (removedRecs != null) {
1695 for (int i=removedRecs.size()-1; i>=0; i--) {
1696 removeUpdatesLocked(removedRecs.get(i));
1697 }
1698 }
1699 if (removedAlerts != null) {
1700 for (int i=removedAlerts.size()-1; i>=0; i--) {
1701 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1702 }
1703 }
1704 }
1705 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001706 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 boolean noConnectivity =
1708 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1709 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 mNetworkState = LocationProvider.AVAILABLE;
1711 } else {
1712 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
1714
1715 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001716 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001717 for (int i = mProviders.size() - 1; i >= 0; i--) {
1718 LocationProviderProxy provider = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 if (provider.requiresNetwork()) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001720 provider.updateNetworkState(mNetworkState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722 }
1723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001726 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727
1728 // Wake locks
1729
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001730 private void incrementPendingBroadcasts() {
1731 synchronized (mWakeLock) {
1732 if (mPendingBroadcasts++ == 0) {
1733 try {
1734 mWakeLock.acquire();
1735 log("Acquired wakelock");
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 acquireWakeLock()", e);
1740 }
1741 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001742 }
1743 }
1744
1745 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001746 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001747 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001748 try {
1749 // Release wake lock
1750 if (mWakeLock.isHeld()) {
1751 mWakeLock.release();
1752 log("Released wakelock");
1753 } else {
1754 log("Can't release wakelock again!");
1755 }
1756 } catch (Exception e) {
1757 // This is to catch a runtime exception thrown when we try to release an
1758 // already released lock.
1759 Log.e(TAG, "exception in releaseWakeLock()", e);
1760 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001761 }
1762 }
1763 }
1764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 // Geocoder
1766
1767 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001768 String language, String country, String variant, String appName, List<Address> addrs) {
1769 if (mGeocodeProvider != null) {
1770 try {
1771 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, language, country,
1772 variant, appName, addrs);
1773 } catch (RemoteException e) {
1774 Log.e(TAG, "getFromLocation failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001775 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001778 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780
Mike Lockwooda55c3212009-04-15 11:10:11 -04001781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001783 double lowerLeftLatitude, double lowerLeftLongitude,
1784 double upperRightLatitude, double upperRightLongitude, int maxResults,
1785 String language, String country, String variant, String appName, List<Address> addrs) {
1786
1787 if (mGeocodeProvider != null) {
1788 try {
1789 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1790 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1791 maxResults, language, country, variant, appName, addrs);
1792 } catch (RemoteException e) {
1793 Log.e(TAG, "getFromLocationName failed", e);
Mike Lockwood3681f262009-05-12 10:52:03 -04001794 mGeocodeProvider = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001797 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799
1800 // Mock Providers
1801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 private void checkMockPermissionsSafe() {
1803 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1804 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1805 if (!allowMocks) {
1806 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1807 }
1808
1809 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1810 PackageManager.PERMISSION_GRANTED) {
1811 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1812 }
1813 }
1814
1815 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1816 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1817 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1818 checkMockPermissionsSafe();
1819
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001820 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001821 MockProvider provider = new MockProvider(name, this,
1822 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 requiresCell, hasMonetaryCost, supportsAltitude,
1824 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001825 // remove the real provider if we are replacing GPS or network provider
1826 if (LocationManager.GPS_PROVIDER.equals(name)
1827 || LocationManager.NETWORK_PROVIDER.equals(name)) {
1828 LocationProviderProxy proxy = mProvidersByName.get(name);
1829 if (proxy != null) {
1830 proxy.enableLocationTracking(false);
1831 removeProvider(proxy);
1832 }
1833 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001834 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1836 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001837 addProvider(new LocationProviderProxy(name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001838 mMockProviders.put(name, provider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001839 mLastKnownLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 updateProvidersLocked();
1841 }
1842 }
1843
1844 public void removeTestProvider(String provider) {
1845 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001846 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001847 MockProvider mockProvider = mMockProviders.get(provider);
1848 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1850 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001851 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001852 mMockProviders.remove(mockProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001853 // reinstall real provider if we were mocking GPS or network provider
1854 if (LocationManager.GPS_PROVIDER.equals(provider) &&
1855 mGpsLocationProvider != null) {
1856 addProvider(mGpsLocationProvider);
1857 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) &&
1858 mNetworkLocationProvider != null) {
1859 addProvider(mNetworkLocationProvider);
1860 }
1861 mLastKnownLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 updateProvidersLocked();
1863 }
1864 }
1865
1866 public void setTestProviderLocation(String provider, Location loc) {
1867 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001868 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001869 MockProvider mockProvider = mMockProviders.get(provider);
1870 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1872 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001873 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1874 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001875 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001876 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
1878 }
1879
1880 public void clearTestProviderLocation(String provider) {
1881 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001882 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001883 MockProvider mockProvider = mMockProviders.get(provider);
1884 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1886 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001887 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889 }
1890
1891 public void setTestProviderEnabled(String provider, boolean enabled) {
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 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001899 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 mEnabledProviders.add(provider);
1901 mDisabledProviders.remove(provider);
1902 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001903 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 mEnabledProviders.remove(provider);
1905 mDisabledProviders.add(provider);
1906 }
1907 updateProvidersLocked();
1908 }
1909 }
1910
1911 public void clearTestProviderEnabled(String provider) {
1912 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001913 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001914 MockProvider mockProvider = mMockProviders.get(provider);
1915 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1917 }
1918 mEnabledProviders.remove(provider);
1919 mDisabledProviders.remove(provider);
1920 updateProvidersLocked();
1921 }
1922 }
1923
1924 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1925 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001926 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001927 MockProvider mockProvider = mMockProviders.get(provider);
1928 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1930 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001931 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 }
1934
1935 public void clearTestProviderStatus(String provider) {
1936 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001937 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001938 MockProvider mockProvider = mMockProviders.get(provider);
1939 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1941 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001942 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 }
1945
1946 private void log(String log) {
1947 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1948 Log.d(TAG, log);
1949 }
1950 }
1951
1952 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1953 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1954 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001955 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 + Binder.getCallingPid()
1957 + ", uid=" + Binder.getCallingUid());
1958 return;
1959 }
1960
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001961 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 pw.println("Current Location Manager state:");
1963 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001965 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001967 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001970 for (Receiver i : mReceivers.values()) {
1971 pw.println(" " + i + ":");
1972 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 pw.println(" " + j.getKey() + ":");
1974 j.getValue().dump(pw, " ");
1975 }
1976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 pw.println(" Records by Provider:");
1978 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1979 : mRecordsByProvider.entrySet()) {
1980 pw.println(" " + i.getKey() + ":");
1981 for (UpdateRecord j : i.getValue()) {
1982 pw.println(" " + j + ":");
1983 j.dump(pw, " ");
1984 }
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 pw.println(" Last Known Locations:");
1987 for (Map.Entry<String, Location> i
1988 : mLastKnownLocation.entrySet()) {
1989 pw.println(" " + i.getKey() + ":");
1990 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1991 }
1992 if (mProximityAlerts.size() > 0) {
1993 pw.println(" Proximity Alerts:");
1994 for (Map.Entry<PendingIntent, ProximityAlert> i
1995 : mProximityAlerts.entrySet()) {
1996 pw.println(" " + i.getKey() + ":");
1997 i.getValue().dump(pw, " ");
1998 }
1999 }
2000 if (mProximitiesEntered.size() > 0) {
2001 pw.println(" Proximities Entered:");
2002 for (ProximityAlert i : mProximitiesEntered) {
2003 pw.println(" " + i + ":");
2004 i.dump(pw, " ");
2005 }
2006 }
Mike Lockwood48f17512009-04-23 09:12:08 -07002007 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 pw.println(" mProximityListener=" + mProximityListener);
2009 if (mEnabledProviders.size() > 0) {
2010 pw.println(" Enabled Providers:");
2011 for (String i : mEnabledProviders) {
2012 pw.println(" " + i);
2013 }
2014
2015 }
2016 if (mDisabledProviders.size() > 0) {
2017 pw.println(" Disabled Providers:");
2018 for (String i : mDisabledProviders) {
2019 pw.println(" " + i);
2020 }
2021
2022 }
2023 if (mMockProviders.size() > 0) {
2024 pw.println(" Mock Providers:");
2025 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002026 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
2030 }
2031}