blob: fff6c54ac83d6132a23484bb75b6e9e6c18905fa [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.List;
25import java.util.Map;
Mike Lockwood9637d472009-04-02 21:41:57 -070026import java.util.Observable;
27import java.util.Observer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.app.PendingIntent;
31import android.content.BroadcastReceiver;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050032import android.content.ComponentName;
Mike Lockwood9637d472009-04-02 21:41:57 -070033import android.content.ContentQueryMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.ContentResolver;
35import android.content.Context;
36import android.content.Intent;
37import android.content.IntentFilter;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050038import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.PackageManager;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050040import android.content.res.Resources;
Mike Lockwood9637d472009-04-02 21:41:57 -070041import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.location.Address;
Mike Lockwood34901402010-01-04 12:14:21 -050043import android.location.GeocoderParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040045import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.location.ILocationListener;
47import android.location.ILocationManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070048import android.location.ILocationProvider;
Danke Xie22d1f9f2009-08-18 18:28:45 -040049import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.location.Location;
51import android.location.LocationManager;
52import android.location.LocationProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.net.ConnectivityManager;
Mike Lockwood03d24672009-10-08 15:45:03 -040054import android.net.NetworkInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Binder;
57import android.os.Bundle;
58import android.os.Handler;
59import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070060import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Message;
62import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070063import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.Log;
67import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Mike Lockwood628fd6d2010-01-25 22:46:13 -050069import com.android.internal.location.GeocoderProxy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import com.android.internal.location.GpsLocationProvider;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050071import com.android.internal.location.GpsNetInitiatedHandler;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070072import com.android.internal.location.LocationProviderProxy;
Mike Lockwood7ec434e2009-03-27 07:46:48 -070073import com.android.internal.location.MockProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75/**
76 * The service class that manages LocationProviders and issues location
77 * updates and alerts.
78 *
79 * {@hide}
80 */
Mike Lockwood3d12b512009-04-21 23:25:35 -070081public class LocationManagerService extends ILocationManager.Stub implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private static final String TAG = "LocationManagerService";
The Android Open Source Project10592532009-03-18 17:39:46 -070083 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 // The last time a location was written, by provider name.
86 private HashMap<String,Long> mLastWriteTime = new HashMap<String,Long>();
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 private static final String ACCESS_FINE_LOCATION =
89 android.Manifest.permission.ACCESS_FINE_LOCATION;
90 private static final String ACCESS_COARSE_LOCATION =
91 android.Manifest.permission.ACCESS_COARSE_LOCATION;
92 private static final String ACCESS_MOCK_LOCATION =
93 android.Manifest.permission.ACCESS_MOCK_LOCATION;
94 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
95 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -040096 private static final String INSTALL_LOCATION_PROVIDER =
97 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 // Set of providers that are explicitly enabled
100 private final Set<String> mEnabledProviders = new HashSet<String>();
101
102 // Set of providers that are explicitly disabled
103 private final Set<String> mDisabledProviders = new HashSet<String>();
104
105 // Locations, status values, and extras for mock providers
Mike Lockwood7ec434e2009-03-27 07:46:48 -0700106 private final HashMap<String,MockProvider> mMockProviders = new HashMap<String,MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private static boolean sProvidersLoaded = false;
109
110 private final Context mContext;
Mike Lockwood628fd6d2010-01-25 22:46:13 -0500111 private GeocoderProxy mGeocodeProvider;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400112 private IGpsStatusProvider mGpsStatusProvider;
Danke Xie22d1f9f2009-08-18 18:28:45 -0400113 private INetInitiatedListener mNetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 private LocationWorkerHandler mLocationHandler;
115
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700116 // Cache the real providers for use in addTestProvider() and removeTestProvider()
117 LocationProviderProxy mNetworkLocationProvider;
118 LocationProviderProxy mGpsLocationProvider;
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 // Handler messages
Mike Lockwood4e50b782009-04-03 08:24:43 -0700121 private static final int MESSAGE_LOCATION_CHANGED = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400123 // wakelock variables
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private final static String WAKELOCK_KEY = "LocationManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 private PowerManager.WakeLock mWakeLock = null;
Mike Lockwood48f17512009-04-23 09:12:08 -0700126 private int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400129 * List of all receivers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400131 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400133
134 /**
135 * List of location providers.
136 */
137 private final ArrayList<LocationProviderProxy> mProviders =
138 new ArrayList<LocationProviderProxy>();
139 private final HashMap<String, LocationProviderProxy> mProvidersByName
140 = new HashMap<String, LocationProviderProxy>();
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400143 * Object used internally for synchronization
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 */
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400145 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
147 /**
148 * Mapping from provider name to all its UpdateRecords
149 */
150 private final HashMap<String,ArrayList<UpdateRecord>> mRecordsByProvider =
151 new HashMap<String,ArrayList<UpdateRecord>>();
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 // Proximity listeners
Mike Lockwood48f17512009-04-23 09:12:08 -0700154 private Receiver mProximityReceiver = null;
155 private ILocationListener mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 private HashMap<PendingIntent,ProximityAlert> mProximityAlerts =
157 new HashMap<PendingIntent,ProximityAlert>();
158 private HashSet<ProximityAlert> mProximitiesEntered =
159 new HashSet<ProximityAlert>();
160
161 // Last known location for each provider
162 private HashMap<String,Location> mLastKnownLocation =
163 new HashMap<String,Location>();
164
The Android Open Source Project4df24232009-03-05 14:34:35 -0800165 private int mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800166
Mike Lockwood9637d472009-04-02 21:41:57 -0700167 // for Settings change notification
168 private ContentQueryMap mSettings;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
171 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
172 * location updates.
173 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700174 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 final ILocationListener mListener;
176 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 final Object mKey;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400178 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Mike Lockwood48f17512009-04-23 09:12:08 -0700179 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400181 Receiver(ILocationListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 mListener = listener;
183 mPendingIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 mKey = listener.asBinder();
185 }
186
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400187 Receiver(PendingIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 mPendingIntent = intent;
189 mListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 mKey = intent;
191 }
192
193 @Override
194 public boolean equals(Object otherObj) {
195 if (otherObj instanceof Receiver) {
196 return mKey.equals(
197 ((Receiver)otherObj).mKey);
198 }
199 return false;
200 }
201
202 @Override
203 public int hashCode() {
204 return mKey.hashCode();
205 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 @Override
208 public String toString() {
209 if (mListener != null) {
210 return "Receiver{"
211 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400212 + " Listener " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 } else {
214 return "Receiver{"
215 + Integer.toHexString(System.identityHashCode(this))
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400216 + " Intent " + mKey + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
218 }
219
220 public boolean isListener() {
221 return mListener != null;
222 }
223
224 public boolean isPendingIntent() {
225 return mPendingIntent != null;
226 }
227
228 public ILocationListener getListener() {
229 if (mListener != null) {
230 return mListener;
231 }
232 throw new IllegalStateException("Request for non-existent listener");
233 }
234
235 public PendingIntent getPendingIntent() {
236 if (mPendingIntent != null) {
237 return mPendingIntent;
238 }
239 throw new IllegalStateException("Request for non-existent intent");
240 }
241
242 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
243 if (mListener != null) {
244 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700245 synchronized (this) {
246 // synchronize to ensure incrementPendingBroadcastsLocked()
247 // is called before decrementPendingBroadcasts()
248 mListener.onStatusChanged(provider, status, extras);
249 if (mListener != mProximityListener) {
250 // call this after broadcasting so we do not increment
251 // if we throw an exeption.
252 incrementPendingBroadcastsLocked();
253 }
254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 } catch (RemoteException e) {
256 return false;
257 }
258 } else {
259 Intent statusChanged = new Intent();
260 statusChanged.putExtras(extras);
261 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
262 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700263 synchronized (this) {
264 // synchronize to ensure incrementPendingBroadcastsLocked()
265 // is called before decrementPendingBroadcasts()
266 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler);
267 // call this after broadcasting so we do not increment
268 // if we throw an exeption.
269 incrementPendingBroadcastsLocked();
270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 } catch (PendingIntent.CanceledException e) {
272 return false;
273 }
274 }
275 return true;
276 }
277
278 public boolean callLocationChangedLocked(Location location) {
279 if (mListener != null) {
280 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700281 synchronized (this) {
282 // synchronize to ensure incrementPendingBroadcastsLocked()
283 // is called before decrementPendingBroadcasts()
284 mListener.onLocationChanged(location);
285 if (mListener != mProximityListener) {
286 // call this after broadcasting so we do not increment
287 // if we throw an exeption.
288 incrementPendingBroadcastsLocked();
289 }
290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 } catch (RemoteException e) {
292 return false;
293 }
294 } else {
295 Intent locationChanged = new Intent();
296 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
297 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700298 synchronized (this) {
299 // synchronize to ensure incrementPendingBroadcastsLocked()
300 // is called before decrementPendingBroadcasts()
301 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler);
302 // call this after broadcasting so we do not increment
303 // if we throw an exeption.
304 incrementPendingBroadcastsLocked();
305 }
306 } catch (PendingIntent.CanceledException e) {
307 return false;
308 }
309 }
310 return true;
311 }
312
313 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
314 if (mListener != null) {
315 try {
316 synchronized (this) {
317 // synchronize to ensure incrementPendingBroadcastsLocked()
318 // is called before decrementPendingBroadcasts()
319 if (enabled) {
320 mListener.onProviderEnabled(provider);
321 } else {
322 mListener.onProviderDisabled(provider);
323 }
324 if (mListener != mProximityListener) {
325 // call this after broadcasting so we do not increment
326 // if we throw an exeption.
327 incrementPendingBroadcastsLocked();
328 }
329 }
330 } catch (RemoteException e) {
331 return false;
332 }
333 } else {
334 Intent providerIntent = new Intent();
335 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
336 try {
337 synchronized (this) {
338 // synchronize to ensure incrementPendingBroadcastsLocked()
339 // is called before decrementPendingBroadcasts()
340 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler);
341 // call this after broadcasting so we do not increment
342 // if we throw an exeption.
343 incrementPendingBroadcastsLocked();
344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 } catch (PendingIntent.CanceledException e) {
346 return false;
347 }
348 }
349 return true;
350 }
351
352 public void binderDied() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700353 if (LOCAL_LOGV) {
354 Log.v(TAG, "Location listener died");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400356 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 removeUpdatesLocked(this);
358 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700359 synchronized (this) {
360 if (mPendingBroadcasts > 0) {
361 LocationManagerService.this.decrementPendingBroadcasts();
362 mPendingBroadcasts = 0;
363 }
364 }
365 }
366
367 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
368 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400369 synchronized (this) {
370 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700371 }
372 }
373
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400374 // this must be called while synchronized by caller in a synchronized block
375 // containing the sending of the broadcaset
376 private void incrementPendingBroadcastsLocked() {
377 if (mPendingBroadcasts++ == 0) {
378 LocationManagerService.this.incrementPendingBroadcasts();
379 }
380 }
381
382 private void decrementPendingBroadcastsLocked() {
383 if (--mPendingBroadcasts == 0) {
384 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700385 }
386 }
387 }
388
389 public void locationCallbackFinished(ILocationListener listener) {
Joshua Bartel080b61b2009-10-05 12:44:46 -0400390 //Do not use getReceiver here as that will add the ILocationListener to
391 //the receiver list if it is not found. If it is not found then the
392 //LocationListener was removed when it had a pending broadcast and should
393 //not be added back.
394 IBinder binder = listener.asBinder();
395 Receiver receiver = mReceivers.get(binder);
Mike Lockwood48f17512009-04-23 09:12:08 -0700396 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400397 synchronized (receiver) {
398 // so wakelock calls will succeed
399 long identity = Binder.clearCallingIdentity();
400 receiver.decrementPendingBroadcastsLocked();
401 Binder.restoreCallingIdentity(identity);
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404 }
405
Mike Lockwood9637d472009-04-02 21:41:57 -0700406 private final class SettingsObserver implements Observer {
407 public void update(Observable o, Object arg) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400408 synchronized (mLock) {
Mike Lockwood9637d472009-04-02 21:41:57 -0700409 updateProvidersLocked();
410 }
411 }
412 }
413
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400414 private void addProvider(LocationProviderProxy provider) {
415 mProviders.add(provider);
416 mProvidersByName.put(provider.getName(), provider);
417 }
418
419 private void removeProvider(LocationProviderProxy provider) {
420 mProviders.remove(provider);
421 mProvidersByName.remove(provider.getName());
422 }
423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 private void loadProviders() {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400425 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 if (sProvidersLoaded) {
427 return;
428 }
429
430 // Load providers
431 loadProvidersLocked();
432 sProvidersLoaded = true;
433 }
434 }
435
436 private void loadProvidersLocked() {
437 try {
438 _loadProvidersLocked();
439 } catch (Exception e) {
440 Log.e(TAG, "Exception loading providers:", e);
441 }
442 }
443
444 private void _loadProvidersLocked() {
445 // Attempt to load "real" providers first
446 if (GpsLocationProvider.isSupported()) {
447 // Create a gps location provider
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400448 GpsLocationProvider provider = new GpsLocationProvider(mContext, this);
449 mGpsStatusProvider = provider.getGpsStatusProvider();
Danke Xie22d1f9f2009-08-18 18:28:45 -0400450 mNetInitiatedListener = provider.getNetInitiatedListener();
Mike Lockwood628fd6d2010-01-25 22:46:13 -0500451 LocationProviderProxy proxy =
452 new LocationProviderProxy(mContext, LocationManager.GPS_PROVIDER, provider);
Mike Lockwood8dfe5d82009-05-07 11:49:01 -0400453 addProvider(proxy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -0700454 mGpsLocationProvider = proxy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456
Mike Lockwood628fd6d2010-01-25 22:46:13 -0500457 // initialize external network location and geocoder services
458 Resources resources = mContext.getResources();
459 String serviceName = resources.getString(
460 com.android.internal.R.string.config_networkLocationProvider);
461 if (serviceName != null) {
462 mNetworkLocationProvider =
463 new LocationProviderProxy(mContext, LocationManager.NETWORK_PROVIDER,
464 serviceName, mLocationHandler);
465 addProvider(mNetworkLocationProvider);
466 }
467
468 serviceName = resources.getString(com.android.internal.R.string.config_geocodeProvider);
469 if (serviceName != null) {
470 mGeocodeProvider = new GeocoderProxy(mContext, serviceName);
471 }
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 updateProvidersLocked();
474 }
475
476 /**
477 * @param context the context that the LocationManagerService runs in
478 */
479 public LocationManagerService(Context context) {
480 super();
481 mContext = context;
Mike Lockwood3d12b512009-04-21 23:25:35 -0700482
483 Thread thread = new Thread(null, this, "LocationManagerService");
484 thread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
The Android Open Source Project10592532009-03-18 17:39:46 -0700486 if (LOCAL_LOGV) {
487 Log.v(TAG, "Constructed LocationManager Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
Mike Lockwood3d12b512009-04-21 23:25:35 -0700489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
Mike Lockwood3d12b512009-04-21 23:25:35 -0700491 private void initialize() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 // Create a wake lock, needs to be done before calling loadProviders() below
493 PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
494 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 // Load providers
497 loadProviders();
498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 // Register for Network (Wifi or Mobile) updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 IntentFilter intentFilter = new IntentFilter();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400501 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
502 // Register for Package Manager updates
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
504 intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400505 mContext.registerReceiver(mBroadcastReceiver, intentFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800506 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800507 mContext.registerReceiver(mBroadcastReceiver, sdFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
Mike Lockwood9637d472009-04-02 21:41:57 -0700509 // listen for settings changes
510 ContentResolver resolver = mContext.getContentResolver();
511 Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
512 "(" + Settings.System.NAME + "=?)",
513 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
514 null);
515 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
516 SettingsObserver settingsObserver = new SettingsObserver();
517 mSettings.addObserver(settingsObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519
Mike Lockwood3d12b512009-04-21 23:25:35 -0700520 public void run()
521 {
522 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
523 Looper.prepare();
524 mLocationHandler = new LocationWorkerHandler();
525 initialize();
526 Looper.loop();
527 }
528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 private boolean isAllowedBySettingsLocked(String provider) {
530 if (mEnabledProviders.contains(provider)) {
531 return true;
532 }
533 if (mDisabledProviders.contains(provider)) {
534 return false;
535 }
536 // Use system settings
537 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
Brad Larson8eb3ea62009-12-29 11:47:55 -0600539 return Settings.Secure.isLocationProviderEnabled(resolver, provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
541
542 private void checkPermissionsSafe(String provider) {
543 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400544 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 != PackageManager.PERMISSION_GRANTED)) {
546 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
547 }
548 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400549 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400551 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 != PackageManager.PERMISSION_GRANTED)) {
553 throw new SecurityException(
554 "Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission");
555 }
556 }
557
558 private boolean isAllowedProviderSafe(String provider) {
559 if (LocationManager.GPS_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400560 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 != PackageManager.PERMISSION_GRANTED)) {
562 return false;
563 }
564 if (LocationManager.NETWORK_PROVIDER.equals(provider)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400565 && (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 != PackageManager.PERMISSION_GRANTED)
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400567 && (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 != PackageManager.PERMISSION_GRANTED)) {
569 return false;
570 }
571
572 return true;
573 }
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 public List<String> getAllProviders() {
576 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400577 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 return _getAllProvidersLocked();
579 }
580 } catch (SecurityException se) {
581 throw se;
582 } catch (Exception e) {
583 Log.e(TAG, "getAllProviders got exception:", e);
584 return null;
585 }
586 }
587
588 private List<String> _getAllProvidersLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700589 if (LOCAL_LOGV) {
590 Log.v(TAG, "getAllProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400592 ArrayList<String> out = new ArrayList<String>(mProviders.size());
593 for (int i = mProviders.size() - 1; i >= 0; i--) {
594 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 out.add(p.getName());
596 }
597 return out;
598 }
599
600 public List<String> getProviders(boolean enabledOnly) {
601 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400602 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 return _getProvidersLocked(enabledOnly);
604 }
605 } catch (SecurityException se) {
606 throw se;
607 } catch (Exception e) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700608 Log.e(TAG, "getProviders got exception:", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 return null;
610 }
611 }
612
613 private List<String> _getProvidersLocked(boolean enabledOnly) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700614 if (LOCAL_LOGV) {
615 Log.v(TAG, "getProviders");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400617 ArrayList<String> out = new ArrayList<String>(mProviders.size());
618 for (int i = mProviders.size() - 1; i >= 0; i--) {
619 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 String name = p.getName();
621 if (isAllowedProviderSafe(name)) {
622 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
623 continue;
624 }
625 out.add(name);
626 }
627 }
628 return out;
629 }
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 private void updateProvidersLocked() {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400632 for (int i = mProviders.size() - 1; i >= 0; i--) {
633 LocationProviderProxy p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 boolean isEnabled = p.isEnabled();
635 String name = p.getName();
636 boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 if (isEnabled && !shouldBeEnabled) {
639 updateProviderListenersLocked(name, false);
640 } else if (!isEnabled && shouldBeEnabled) {
641 updateProviderListenersLocked(name, true);
642 }
643
644 }
645 }
646
647 private void updateProviderListenersLocked(String provider, boolean enabled) {
648 int listeners = 0;
649
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400650 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 if (p == null) {
652 return;
653 }
654
655 ArrayList<Receiver> deadReceivers = null;
656
657 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
658 if (records != null) {
659 final int N = records.size();
660 for (int i=0; i<N; i++) {
661 UpdateRecord record = records.get(i);
662 // Sends a notification message to the receiver
Mike Lockwood48f17512009-04-23 09:12:08 -0700663 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
664 if (deadReceivers == null) {
665 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
Simon Schoar46866572009-06-10 21:12:10 +0200667 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669 listeners++;
670 }
671 }
672
673 if (deadReceivers != null) {
674 for (int i=deadReceivers.size()-1; i>=0; i--) {
675 removeUpdatesLocked(deadReceivers.get(i));
676 }
677 }
678
679 if (enabled) {
680 p.enable();
681 if (listeners > 0) {
682 p.setMinTime(getMinTimeLocked(provider));
683 p.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 } else {
686 p.enableLocationTracking(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
690
691 private long getMinTimeLocked(String provider) {
692 long minTime = Long.MAX_VALUE;
693 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
694 if (records != null) {
695 for (int i=records.size()-1; i>=0; i--) {
696 minTime = Math.min(minTime, records.get(i).mMinTime);
697 }
698 }
699 return minTime;
700 }
701
702 private class UpdateRecord {
703 final String mProvider;
704 final Receiver mReceiver;
705 final long mMinTime;
706 final float mMinDistance;
707 final int mUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400708 Location mLastFixBroadcast;
709 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
711 /**
712 * Note: must be constructed with lock held.
713 */
714 UpdateRecord(String provider, long minTime, float minDistance,
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400715 Receiver receiver, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 mProvider = provider;
717 mReceiver = receiver;
718 mMinTime = minTime;
719 mMinDistance = minDistance;
720 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
722 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
723 if (records == null) {
724 records = new ArrayList<UpdateRecord>();
725 mRecordsByProvider.put(provider, records);
726 }
727 if (!records.contains(this)) {
728 records.add(this);
729 }
730 }
731
732 /**
733 * Method to be called when a record will no longer be used. Calling this multiple times
734 * must have the same effect as calling it once.
735 */
736 void disposeLocked() {
737 ArrayList<UpdateRecord> records = mRecordsByProvider.get(this.mProvider);
Mike Lockwood3a76fd62009-09-01 07:26:56 -0400738 if (records != null) {
739 records.remove(this);
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 @Override
744 public String toString() {
745 return "UpdateRecord{"
746 + Integer.toHexString(System.identityHashCode(this))
747 + " " + mProvider + " " + mReceiver + "}";
748 }
749
750 void dump(PrintWriter pw, String prefix) {
751 pw.println(prefix + this);
752 pw.println(prefix + "mProvider=" + mProvider + " mReceiver=" + mReceiver);
753 pw.println(prefix + "mMinTime=" + mMinTime + " mMinDistance=" + mMinDistance);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400754 pw.println(prefix + "mUid=" + mUid);
755 pw.println(prefix + "mLastFixBroadcast:");
756 mLastFixBroadcast.dump(new PrintWriterPrinter(pw), prefix + " ");
757 pw.println(prefix + "mLastStatusBroadcast=" + mLastStatusBroadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400761 private Receiver getReceiver(ILocationListener listener) {
762 IBinder binder = listener.asBinder();
763 Receiver receiver = mReceivers.get(binder);
764 if (receiver == null) {
765 receiver = new Receiver(listener);
766 mReceivers.put(binder, receiver);
767
768 try {
769 if (receiver.isListener()) {
770 receiver.getListener().asBinder().linkToDeath(receiver, 0);
771 }
772 } catch (RemoteException e) {
773 Log.e(TAG, "linkToDeath failed:", e);
774 return null;
775 }
776 }
777 return receiver;
778 }
779
780 private Receiver getReceiver(PendingIntent intent) {
781 Receiver receiver = mReceivers.get(intent);
782 if (receiver == null) {
783 receiver = new Receiver(intent);
784 mReceivers.put(intent, receiver);
785 }
786 return receiver;
787 }
788
789 private boolean providerHasListener(String provider, int uid, Receiver excludedReceiver) {
790 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
791 if (records != null) {
792 for (int i = records.size() - 1; i >= 0; i--) {
793 UpdateRecord record = records.get(i);
794 if (record.mUid == uid && record.mReceiver != excludedReceiver) {
795 return true;
796 }
797 }
798 }
Mike Lockwood95427cd2009-05-07 13:27:54 -0400799 for (ProximityAlert alert : mProximityAlerts.values()) {
800 if (alert.mUid == uid) {
801 return true;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400802 }
803 }
804 return false;
805 }
806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 public void requestLocationUpdates(String provider,
808 long minTime, float minDistance, ILocationListener listener) {
809
810 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400811 synchronized (mLock) {
812 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814 } catch (SecurityException se) {
815 throw se;
816 } catch (Exception e) {
817 Log.e(TAG, "requestUpdates got exception:", e);
818 }
819 }
820
821 public void requestLocationUpdatesPI(String provider,
822 long minTime, float minDistance, PendingIntent intent) {
823 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400824 synchronized (mLock) {
825 requestLocationUpdatesLocked(provider, minTime, minDistance, getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827 } catch (SecurityException se) {
828 throw se;
829 } catch (Exception e) {
830 Log.e(TAG, "requestUpdates got exception:", e);
831 }
832 }
833
834 private void requestLocationUpdatesLocked(String provider,
835 long minTime, float minDistance, Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700836 if (LOCAL_LOGV) {
837 Log.v(TAG, "_requestLocationUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 }
839
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400840 LocationProviderProxy proxy = mProvidersByName.get(provider);
841 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 throw new IllegalArgumentException("provider=" + provider);
843 }
844
845 checkPermissionsSafe(provider);
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 // so wakelock calls will succeed
848 final int callingUid = Binder.getCallingUid();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400849 boolean newUid = !providerHasListener(provider, callingUid, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 long identity = Binder.clearCallingIdentity();
851 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400852 UpdateRecord r = new UpdateRecord(provider, minTime, minDistance, receiver, callingUid);
853 UpdateRecord oldRecord = receiver.mUpdateRecords.put(provider, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 if (oldRecord != null) {
855 oldRecord.disposeLocked();
856 }
857
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400858 if (newUid) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400859 proxy.addListener(callingUid);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400860 }
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 boolean isProviderEnabled = isAllowedBySettingsLocked(provider);
863 if (isProviderEnabled) {
864 long minTimeForProvider = getMinTimeLocked(provider);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400865 proxy.setMinTime(minTimeForProvider);
866 proxy.enableLocationTracking(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 } else {
Mike Lockwood48f17512009-04-23 09:12:08 -0700868 // Notify the listener that updates are currently disabled
869 receiver.callProviderEnabledLocked(provider, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871 } finally {
872 Binder.restoreCallingIdentity(identity);
873 }
874 }
875
876 public void removeUpdates(ILocationListener listener) {
877 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400878 synchronized (mLock) {
879 removeUpdatesLocked(getReceiver(listener));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 } catch (SecurityException se) {
882 throw se;
883 } catch (Exception e) {
884 Log.e(TAG, "removeUpdates got exception:", e);
885 }
886 }
887
888 public void removeUpdatesPI(PendingIntent intent) {
889 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400890 synchronized (mLock) {
891 removeUpdatesLocked(getReceiver(intent));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 }
893 } catch (SecurityException se) {
894 throw se;
895 } catch (Exception e) {
896 Log.e(TAG, "removeUpdates got exception:", e);
897 }
898 }
899
900 private void removeUpdatesLocked(Receiver receiver) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700901 if (LOCAL_LOGV) {
902 Log.v(TAG, "_removeUpdates: listener = " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
905 // so wakelock calls will succeed
906 final int callingUid = Binder.getCallingUid();
907 long identity = Binder.clearCallingIdentity();
908 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400909 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
910 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
Joshua Bartel080b61b2009-10-05 12:44:46 -0400911 synchronized(receiver) {
912 if(receiver.mPendingBroadcasts > 0) {
913 decrementPendingBroadcasts();
914 receiver.mPendingBroadcasts = 0;
915 }
916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918
919 // Record which providers were associated with this listener
920 HashSet<String> providers = new HashSet<String>();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400921 HashMap<String,UpdateRecord> oldRecords = receiver.mUpdateRecords;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 if (oldRecords != null) {
923 // Call dispose() on the obsolete update records.
924 for (UpdateRecord record : oldRecords.values()) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400925 if (!providerHasListener(record.mProvider, callingUid, receiver)) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400926 LocationProviderProxy proxy = mProvidersByName.get(record.mProvider);
927 if (proxy != null) {
928 proxy.removeListener(callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 }
930 }
931 record.disposeLocked();
932 }
933 // Accumulate providers
934 providers.addAll(oldRecords.keySet());
935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 // See if the providers associated with this listener have any
938 // other listeners; if one does, inform it of the new smallest minTime
939 // value; if one does not, disable location tracking for it
940 for (String provider : providers) {
941 // If provider is already disabled, don't need to do anything
942 if (!isAllowedBySettingsLocked(provider)) {
943 continue;
944 }
945
946 boolean hasOtherListener = false;
947 ArrayList<UpdateRecord> recordsForProvider = mRecordsByProvider.get(provider);
948 if (recordsForProvider != null && recordsForProvider.size() > 0) {
949 hasOtherListener = true;
950 }
951
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400952 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 if (p != null) {
954 if (hasOtherListener) {
955 p.setMinTime(getMinTimeLocked(provider));
956 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 p.enableLocationTracking(false);
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 } finally {
962 Binder.restoreCallingIdentity(identity);
963 }
964 }
965
966 public boolean addGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400967 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 return false;
969 }
Mike Lockwoodb7e99222009-07-07 13:18:21 -0400970 if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 PackageManager.PERMISSION_GRANTED) {
972 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
973 }
974
975 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400976 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 } catch (RemoteException e) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400978 Log.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 return false;
980 }
981 return true;
982 }
983
984 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400985 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400986 try {
987 mGpsStatusProvider.removeGpsStatusListener(listener);
988 } catch (Exception e) {
989 Log.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992 }
993
994 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -0400995 if (provider == null) {
996 // throw NullPointerException to remain compatible with previous implementation
997 throw new NullPointerException();
998 }
999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 // first check for permission to the provider
1001 checkPermissionsSafe(provider);
1002 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001003 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 != PackageManager.PERMISSION_GRANTED)) {
1005 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1006 }
1007
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001008 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001009 LocationProviderProxy proxy = mProvidersByName.get(provider);
Mike Lockwood6ba7ae12009-08-17 08:39:12 -04001010 if (proxy == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 return false;
1012 }
1013
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001014 return proxy.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
1016 }
1017
Danke Xie22d1f9f2009-08-18 18:28:45 -04001018 public boolean sendNiResponse(int notifId, int userResponse)
1019 {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001020 if (Binder.getCallingUid() != Process.myUid()) {
1021 throw new SecurityException(
1022 "calling sendNiResponse from outside of the system is not allowed");
1023 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001024 try {
1025 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
1026 }
1027 catch (RemoteException e)
1028 {
1029 Log.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
1030 return false;
1031 }
1032 }
1033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 class ProximityAlert {
1035 final int mUid;
1036 final double mLatitude;
1037 final double mLongitude;
1038 final float mRadius;
1039 final long mExpiration;
1040 final PendingIntent mIntent;
1041 final Location mLocation;
1042
1043 public ProximityAlert(int uid, double latitude, double longitude,
1044 float radius, long expiration, PendingIntent intent) {
1045 mUid = uid;
1046 mLatitude = latitude;
1047 mLongitude = longitude;
1048 mRadius = radius;
1049 mExpiration = expiration;
1050 mIntent = intent;
1051
1052 mLocation = new Location("");
1053 mLocation.setLatitude(latitude);
1054 mLocation.setLongitude(longitude);
1055 }
1056
1057 long getExpiration() {
1058 return mExpiration;
1059 }
1060
1061 PendingIntent getIntent() {
1062 return mIntent;
1063 }
1064
Yusuf T. Mobile2d2078a2009-07-14 15:46:26 -07001065 boolean isInProximity(double latitude, double longitude, float accuracy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 Location loc = new Location("");
1067 loc.setLatitude(latitude);
1068 loc.setLongitude(longitude);
1069
1070 double radius = loc.distanceTo(mLocation);
Yusuf T. Mobile2d2078a2009-07-14 15:46:26 -07001071 return radius <= Math.max(mRadius,accuracy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073
1074 @Override
1075 public String toString() {
1076 return "ProximityAlert{"
1077 + Integer.toHexString(System.identityHashCode(this))
1078 + " uid " + mUid + mIntent + "}";
1079 }
1080
1081 void dump(PrintWriter pw, String prefix) {
1082 pw.println(prefix + this);
1083 pw.println(prefix + "mLatitude=" + mLatitude + " mLongitude=" + mLongitude);
1084 pw.println(prefix + "mRadius=" + mRadius + " mExpiration=" + mExpiration);
1085 pw.println(prefix + "mIntent=" + mIntent);
1086 pw.println(prefix + "mLocation:");
1087 mLocation.dump(new PrintWriterPrinter(pw), prefix + " ");
1088 }
1089 }
1090
1091 // Listener for receiving locations to trigger proximity alerts
Mike Lockwood48f17512009-04-23 09:12:08 -07001092 class ProximityListener extends ILocationListener.Stub implements PendingIntent.OnFinished {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093
1094 boolean isGpsAvailable = false;
1095
1096 // Note: this is called with the lock held.
1097 public void onLocationChanged(Location loc) {
1098
1099 // If Gps is available, then ignore updates from NetworkLocationProvider
1100 if (loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
1101 isGpsAvailable = true;
1102 }
1103 if (isGpsAvailable && loc.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
1104 return;
1105 }
1106
1107 // Process proximity alerts
1108 long now = System.currentTimeMillis();
1109 double latitude = loc.getLatitude();
1110 double longitude = loc.getLongitude();
Yusuf T. Mobile2d2078a2009-07-14 15:46:26 -07001111 float accuracy = loc.getAccuracy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 ArrayList<PendingIntent> intentsToRemove = null;
1113
1114 for (ProximityAlert alert : mProximityAlerts.values()) {
1115 PendingIntent intent = alert.getIntent();
1116 long expiration = alert.getExpiration();
1117
1118 if ((expiration == -1) || (now <= expiration)) {
1119 boolean entered = mProximitiesEntered.contains(alert);
1120 boolean inProximity =
Yusuf T. Mobile2d2078a2009-07-14 15:46:26 -07001121 alert.isInProximity(latitude, longitude, accuracy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (!entered && inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001123 if (LOCAL_LOGV) {
1124 Log.v(TAG, "Entered alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
1126 mProximitiesEntered.add(alert);
1127 Intent enteredIntent = new Intent();
1128 enteredIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
1129 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001130 synchronized (this) {
1131 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001132 // is called before decrementPendingBroadcasts()
1133 intent.send(mContext, 0, enteredIntent, this, mLocationHandler);
1134 // call this after broadcasting so we do not increment
1135 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001136 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001139 if (LOCAL_LOGV) {
1140 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142 if (intentsToRemove == null) {
1143 intentsToRemove = new ArrayList<PendingIntent>();
1144 }
1145 intentsToRemove.add(intent);
1146 }
1147 } else if (entered && !inProximity) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001148 if (LOCAL_LOGV) {
1149 Log.v(TAG, "Exited alert");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151 mProximitiesEntered.remove(alert);
1152 Intent exitedIntent = new Intent();
1153 exitedIntent.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
1154 try {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001155 synchronized (this) {
1156 // synchronize to ensure incrementPendingBroadcasts()
Mike Lockwood48f17512009-04-23 09:12:08 -07001157 // is called before decrementPendingBroadcasts()
1158 intent.send(mContext, 0, exitedIntent, this, mLocationHandler);
1159 // call this after broadcasting so we do not increment
1160 // if we throw an exeption.
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001161 incrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -07001162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 } catch (PendingIntent.CanceledException e) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001164 if (LOCAL_LOGV) {
1165 Log.v(TAG, "Canceled proximity alert: " + alert, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167 if (intentsToRemove == null) {
1168 intentsToRemove = new ArrayList<PendingIntent>();
1169 }
1170 intentsToRemove.add(intent);
1171 }
1172 }
1173 } else {
1174 // Mark alert for expiration
The Android Open Source Project10592532009-03-18 17:39:46 -07001175 if (LOCAL_LOGV) {
1176 Log.v(TAG, "Expiring proximity alert: " + alert);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178 if (intentsToRemove == null) {
1179 intentsToRemove = new ArrayList<PendingIntent>();
1180 }
1181 intentsToRemove.add(alert.getIntent());
1182 }
1183 }
1184
1185 // Remove expired alerts
1186 if (intentsToRemove != null) {
1187 for (PendingIntent i : intentsToRemove) {
Mike Lockwood0dac8c02010-01-19 11:48:14 -05001188 ProximityAlert alert = mProximityAlerts.remove(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 mProximitiesEntered.remove(alert);
1190 }
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193
1194 // Note: this is called with the lock held.
1195 public void onProviderDisabled(String provider) {
1196 if (provider.equals(LocationManager.GPS_PROVIDER)) {
1197 isGpsAvailable = false;
1198 }
1199 }
1200
1201 // Note: this is called with the lock held.
1202 public void onProviderEnabled(String provider) {
1203 // ignore
1204 }
1205
1206 // Note: this is called with the lock held.
1207 public void onStatusChanged(String provider, int status, Bundle extras) {
1208 if ((provider.equals(LocationManager.GPS_PROVIDER)) &&
1209 (status != LocationProvider.AVAILABLE)) {
1210 isGpsAvailable = false;
1211 }
1212 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001213
1214 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1215 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001216 // synchronize to ensure incrementPendingBroadcasts()
1217 // is called before decrementPendingBroadcasts()
1218 synchronized (this) {
1219 decrementPendingBroadcasts();
1220 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223
1224 public void addProximityAlert(double latitude, double longitude,
1225 float radius, long expiration, PendingIntent intent) {
1226 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001227 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 addProximityAlertLocked(latitude, longitude, radius, expiration, intent);
1229 }
1230 } catch (SecurityException se) {
1231 throw se;
1232 } catch (Exception e) {
1233 Log.e(TAG, "addProximityAlert got exception:", e);
1234 }
1235 }
1236
1237 private void addProximityAlertLocked(double latitude, double longitude,
1238 float radius, long expiration, PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001239 if (LOCAL_LOGV) {
1240 Log.v(TAG, "addProximityAlert: latitude = " + latitude +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 ", longitude = " + longitude +
1242 ", expiration = " + expiration +
1243 ", intent = " + intent);
1244 }
1245
1246 // Require ability to access all providers for now
1247 if (!isAllowedProviderSafe(LocationManager.GPS_PROVIDER) ||
1248 !isAllowedProviderSafe(LocationManager.NETWORK_PROVIDER)) {
1249 throw new SecurityException("Requires ACCESS_FINE_LOCATION permission");
1250 }
1251
1252 if (expiration != -1) {
1253 expiration += System.currentTimeMillis();
1254 }
1255 ProximityAlert alert = new ProximityAlert(Binder.getCallingUid(),
1256 latitude, longitude, radius, expiration, intent);
1257 mProximityAlerts.put(intent, alert);
1258
Mike Lockwood48f17512009-04-23 09:12:08 -07001259 if (mProximityReceiver == null) {
1260 mProximityListener = new ProximityListener();
1261 mProximityReceiver = new Receiver(mProximityListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262
Mike Lockwood95427cd2009-05-07 13:27:54 -04001263 for (int i = mProviders.size() - 1; i >= 0; i--) {
1264 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood48f17512009-04-23 09:12:08 -07001265 requestLocationUpdatesLocked(provider.getName(), 1000L, 1.0f, mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268 }
1269
1270 public void removeProximityAlert(PendingIntent intent) {
1271 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001272 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 removeProximityAlertLocked(intent);
1274 }
1275 } catch (SecurityException se) {
1276 throw se;
1277 } catch (Exception e) {
1278 Log.e(TAG, "removeProximityAlert got exception:", e);
1279 }
1280 }
1281
1282 private void removeProximityAlertLocked(PendingIntent intent) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001283 if (LOCAL_LOGV) {
1284 Log.v(TAG, "removeProximityAlert: intent = " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286
1287 mProximityAlerts.remove(intent);
1288 if (mProximityAlerts.size() == 0) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001289 removeUpdatesLocked(mProximityReceiver);
1290 mProximityReceiver = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 mProximityListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293 }
1294
1295 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001296 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001297 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 * accessed by the caller
1299 */
1300 public Bundle getProviderInfo(String provider) {
1301 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001302 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 return _getProviderInfoLocked(provider);
1304 }
1305 } catch (SecurityException se) {
1306 throw se;
1307 } catch (Exception e) {
1308 Log.e(TAG, "_getProviderInfo got exception:", e);
1309 return null;
1310 }
1311 }
1312
1313 private Bundle _getProviderInfoLocked(String provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001314 LocationProviderProxy p = mProvidersByName.get(provider);
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001315 if (p == null || !p.isEnabled()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 return null;
1317 }
1318
1319 checkPermissionsSafe(provider);
1320
1321 Bundle b = new Bundle();
1322 b.putBoolean("network", p.requiresNetwork());
1323 b.putBoolean("satellite", p.requiresSatellite());
1324 b.putBoolean("cell", p.requiresCell());
1325 b.putBoolean("cost", p.hasMonetaryCost());
1326 b.putBoolean("altitude", p.supportsAltitude());
1327 b.putBoolean("speed", p.supportsSpeed());
1328 b.putBoolean("bearing", p.supportsBearing());
1329 b.putInt("power", p.getPowerRequirement());
1330 b.putInt("accuracy", p.getAccuracy());
1331
1332 return b;
1333 }
1334
1335 public boolean isProviderEnabled(String provider) {
1336 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001337 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 return _isProviderEnabledLocked(provider);
1339 }
1340 } catch (SecurityException se) {
1341 throw se;
1342 } catch (Exception e) {
1343 Log.e(TAG, "isProviderEnabled got exception:", e);
1344 return false;
1345 }
1346 }
1347
Mike Lockwood275555c2009-05-01 11:30:34 -04001348 public void reportLocation(Location location) {
1349 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1350 != PackageManager.PERMISSION_GRANTED) {
1351 throw new SecurityException("Requires INSTALL_LOCATION_PROVIDER permission");
1352 }
1353
Mike Lockwood4e50b782009-04-03 08:24:43 -07001354 mLocationHandler.removeMessages(MESSAGE_LOCATION_CHANGED, location);
1355 Message m = Message.obtain(mLocationHandler, MESSAGE_LOCATION_CHANGED, location);
1356 mLocationHandler.sendMessageAtFrontOfQueue(m);
1357 }
1358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 private boolean _isProviderEnabledLocked(String provider) {
1360 checkPermissionsSafe(provider);
1361
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001362 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 if (p == null) {
1364 throw new IllegalArgumentException("provider=" + provider);
1365 }
1366 return isAllowedBySettingsLocked(provider);
1367 }
1368
1369 public Location getLastKnownLocation(String provider) {
1370 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001371 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 return _getLastKnownLocationLocked(provider);
1373 }
1374 } catch (SecurityException se) {
1375 throw se;
1376 } catch (Exception e) {
1377 Log.e(TAG, "getLastKnownLocation got exception:", e);
1378 return null;
1379 }
1380 }
1381
1382 private Location _getLastKnownLocationLocked(String provider) {
1383 checkPermissionsSafe(provider);
1384
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001385 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 if (p == null) {
1387 throw new IllegalArgumentException("provider=" + provider);
1388 }
1389
1390 if (!isAllowedBySettingsLocked(provider)) {
1391 return null;
1392 }
1393
Mike Lockwood9aa1fa22009-09-01 07:51:15 -04001394 return mLastKnownLocation.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396
1397 private static boolean shouldBroadcastSafe(Location loc, Location lastLoc, UpdateRecord record) {
1398 // Always broadcast the first update
1399 if (lastLoc == null) {
1400 return true;
1401 }
1402
1403 // Don't broadcast same location again regardless of condition
1404 // TODO - we should probably still rebroadcast if user explicitly sets a minTime > 0
1405 if (loc.getTime() == lastLoc.getTime()) {
1406 return false;
1407 }
1408
1409 // Check whether sufficient distance has been traveled
1410 double minDistance = record.mMinDistance;
1411 if (minDistance > 0.0) {
1412 if (loc.distanceTo(lastLoc) <= minDistance) {
1413 return false;
1414 }
1415 }
1416
1417 return true;
1418 }
1419
Mike Lockwood4e50b782009-04-03 08:24:43 -07001420 private void handleLocationChangedLocked(Location location) {
1421 String provider = location.getProvider();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1423 if (records == null || records.size() == 0) {
1424 return;
1425 }
1426
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001427 LocationProviderProxy p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 if (p == null) {
1429 return;
1430 }
1431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 // Update last known location for provider
Mike Lockwood4e50b782009-04-03 08:24:43 -07001433 Location lastLocation = mLastKnownLocation.get(provider);
1434 if (lastLocation == null) {
1435 mLastKnownLocation.put(provider, new Location(location));
1436 } else {
1437 lastLocation.set(location);
1438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 // Fetch latest status update time
1441 long newStatusUpdateTime = p.getStatusUpdateTime();
1442
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001443 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 Bundle extras = new Bundle();
1445 int status = p.getStatus(extras);
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 ArrayList<Receiver> deadReceivers = null;
1448
1449 // Broadcast location or status to all listeners
1450 final int N = records.size();
1451 for (int i=0; i<N; i++) {
1452 UpdateRecord r = records.get(i);
1453 Receiver receiver = r.mReceiver;
1454
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001455 Location lastLoc = r.mLastFixBroadcast;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001456 if ((lastLoc == null) || shouldBroadcastSafe(location, lastLoc, r)) {
1457 if (lastLoc == null) {
1458 lastLoc = new Location(location);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001459 r.mLastFixBroadcast = lastLoc;
Mike Lockwood4e50b782009-04-03 08:24:43 -07001460 } else {
1461 lastLoc.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001463 if (!receiver.callLocationChangedLocked(location)) {
1464 Log.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1465 if (deadReceivers == null) {
1466 deadReceivers = new ArrayList<Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001468 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
1470 }
1471
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001472 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
1474 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
1475
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001476 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
1478 Log.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
1479 if (deadReceivers == null) {
1480 deadReceivers = new ArrayList<Receiver>();
1481 }
1482 if (!deadReceivers.contains(receiver)) {
1483 deadReceivers.add(receiver);
1484 }
1485 }
1486 }
1487 }
1488
1489 if (deadReceivers != null) {
1490 for (int i=deadReceivers.size()-1; i>=0; i--) {
1491 removeUpdatesLocked(deadReceivers.get(i));
1492 }
1493 }
1494 }
1495
1496 private class LocationWorkerHandler extends Handler {
1497
1498 @Override
1499 public void handleMessage(Message msg) {
1500 try {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001501 if (msg.what == MESSAGE_LOCATION_CHANGED) {
1502 // log("LocationWorkerHandler: MESSAGE_LOCATION_CHANGED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001504 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001505 Location location = (Location) msg.obj;
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001506 String provider = location.getProvider();
Mike Lockwood98cb6672009-04-17 18:03:44 -04001507
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001508 // notify other providers of the new location
1509 for (int i = mProviders.size() - 1; i >= 0; i--) {
1510 LocationProviderProxy proxy = mProviders.get(i);
1511 if (!provider.equals(proxy.getName())) {
1512 proxy.updateLocation(location);
Mike Lockwood98cb6672009-04-17 18:03:44 -04001513 }
1514 }
1515
Mike Lockwoodfd6e5f02009-05-21 11:28:20 -04001516 if (isAllowedBySettingsLocked(provider)) {
1517 handleLocationChangedLocked(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
1521 } catch (Exception e) {
1522 // Log, don't crash!
1523 Log.e(TAG, "Exception in LocationWorkerHandler.handleMessage:", e);
1524 }
1525 }
1526 }
1527
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001528 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1529 @Override
1530 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 String action = intent.getAction();
1532
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001533 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001534 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001535 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001536 synchronized (mLock) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001537 int uidList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001538 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001539 uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST);
1540 } else {
1541 uidList = new int[]{intent.getIntExtra(Intent.EXTRA_UID, -1)};
1542 }
1543 if (uidList == null || uidList.length == 0) {
1544 return;
1545 }
1546 for (int uid : uidList) {
1547 if (uid >= 0) {
1548 ArrayList<Receiver> removedRecs = null;
1549 for (ArrayList<UpdateRecord> i : mRecordsByProvider.values()) {
1550 for (int j=i.size()-1; j>=0; j--) {
1551 UpdateRecord ur = i.get(j);
1552 if (ur.mReceiver.isPendingIntent() && ur.mUid == uid) {
1553 if (removedRecs == null) {
1554 removedRecs = new ArrayList<Receiver>();
1555 }
1556 if (!removedRecs.contains(ur.mReceiver)) {
1557 removedRecs.add(ur.mReceiver);
1558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
1560 }
1561 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001562 ArrayList<ProximityAlert> removedAlerts = null;
1563 for (ProximityAlert i : mProximityAlerts.values()) {
1564 if (i.mUid == uid) {
1565 if (removedAlerts == null) {
1566 removedAlerts = new ArrayList<ProximityAlert>();
1567 }
1568 if (!removedAlerts.contains(i)) {
1569 removedAlerts.add(i);
1570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
1572 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001573 if (removedRecs != null) {
1574 for (int i=removedRecs.size()-1; i>=0; i--) {
1575 removeUpdatesLocked(removedRecs.get(i));
1576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001578 if (removedAlerts != null) {
1579 for (int i=removedAlerts.size()-1; i>=0; i--) {
1580 removeProximityAlertLocked(removedAlerts.get(i).mIntent);
1581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 }
1584 }
1585 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001586 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 boolean noConnectivity =
1588 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
1589 if (!noConnectivity) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001590 mNetworkState = LocationProvider.AVAILABLE;
1591 } else {
1592 mNetworkState = LocationProvider.TEMPORARILY_UNAVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
Mike Lockwood03d24672009-10-08 15:45:03 -04001594 NetworkInfo info =
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001595 (NetworkInfo)intent.getExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
1597 // Notify location providers of current network state
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001598 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001599 for (int i = mProviders.size() - 1; i >= 0; i--) {
1600 LocationProviderProxy provider = mProviders.get(i);
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001601 if (provider.isEnabled() && provider.requiresNetwork()) {
Mike Lockwood03d24672009-10-08 15:45:03 -04001602 provider.updateNetworkState(mNetworkState, info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 }
1605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 }
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001608 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
1610 // Wake locks
1611
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001612 private void incrementPendingBroadcasts() {
1613 synchronized (mWakeLock) {
1614 if (mPendingBroadcasts++ == 0) {
1615 try {
1616 mWakeLock.acquire();
1617 log("Acquired wakelock");
1618 } catch (Exception e) {
1619 // This is to catch a runtime exception thrown when we try to release an
1620 // already released lock.
1621 Log.e(TAG, "exception in acquireWakeLock()", e);
1622 }
1623 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001624 }
1625 }
1626
1627 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001628 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001629 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001630 try {
1631 // Release wake lock
1632 if (mWakeLock.isHeld()) {
1633 mWakeLock.release();
1634 log("Released wakelock");
1635 } else {
1636 log("Can't release wakelock again!");
1637 }
1638 } catch (Exception e) {
1639 // This is to catch a runtime exception thrown when we try to release an
1640 // already released lock.
1641 Log.e(TAG, "exception in releaseWakeLock()", e);
1642 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001643 }
1644 }
1645 }
1646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 // Geocoder
1648
1649 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001650 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001651 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001652 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
1653 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001655 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
1657
Mike Lockwooda55c3212009-04-15 11:10:11 -04001658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001660 double lowerLeftLatitude, double lowerLeftLongitude,
1661 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001662 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001663
1664 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001665 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1666 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1667 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001669 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
1671
1672 // Mock Providers
1673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 private void checkMockPermissionsSafe() {
1675 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1676 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1677 if (!allowMocks) {
1678 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1679 }
1680
1681 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1682 PackageManager.PERMISSION_GRANTED) {
1683 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
1684 }
1685 }
1686
1687 public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
1688 boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
1689 boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
1690 checkMockPermissionsSafe();
1691
Mike Lockwood86328a92009-10-23 08:38:25 -04001692 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001693 synchronized (mLock) {
Mike Lockwood4e50b782009-04-03 08:24:43 -07001694 MockProvider provider = new MockProvider(name, this,
1695 requiresNetwork, requiresSatellite,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 requiresCell, hasMonetaryCost, supportsAltitude,
1697 supportsSpeed, supportsBearing, powerRequirement, accuracy);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001698 // remove the real provider if we are replacing GPS or network provider
1699 if (LocationManager.GPS_PROVIDER.equals(name)
1700 || LocationManager.NETWORK_PROVIDER.equals(name)) {
1701 LocationProviderProxy proxy = mProvidersByName.get(name);
1702 if (proxy != null) {
1703 proxy.enableLocationTracking(false);
1704 removeProvider(proxy);
1705 }
1706 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001707 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
1709 }
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001710 addProvider(new LocationProviderProxy(mContext, name, provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001711 mMockProviders.put(name, provider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001712 mLastKnownLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 updateProvidersLocked();
1714 }
Mike Lockwood86328a92009-10-23 08:38:25 -04001715 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717
1718 public void removeTestProvider(String provider) {
1719 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001720 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001721 MockProvider mockProvider = mMockProviders.get(provider);
1722 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1724 }
Mike Lockwood86328a92009-10-23 08:38:25 -04001725 long identity = Binder.clearCallingIdentity();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001726 removeProvider(mProvidersByName.get(provider));
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001727 mMockProviders.remove(mockProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001728 // reinstall real provider if we were mocking GPS or network provider
1729 if (LocationManager.GPS_PROVIDER.equals(provider) &&
1730 mGpsLocationProvider != null) {
1731 addProvider(mGpsLocationProvider);
1732 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) &&
1733 mNetworkLocationProvider != null) {
1734 addProvider(mNetworkLocationProvider);
1735 }
1736 mLastKnownLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04001738 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
1740 }
1741
1742 public void setTestProviderLocation(String provider, Location loc) {
1743 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001744 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001745 MockProvider mockProvider = mMockProviders.get(provider);
1746 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1748 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04001749 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
1750 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001751 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04001752 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754 }
1755
1756 public void clearTestProviderLocation(String provider) {
1757 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001758 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001759 MockProvider mockProvider = mMockProviders.get(provider);
1760 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1762 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001763 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765 }
1766
1767 public void setTestProviderEnabled(String provider, boolean enabled) {
1768 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001769 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001770 MockProvider mockProvider = mMockProviders.get(provider);
1771 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1773 }
Mike Lockwood86328a92009-10-23 08:38:25 -04001774 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001776 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 mEnabledProviders.add(provider);
1778 mDisabledProviders.remove(provider);
1779 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001780 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 mEnabledProviders.remove(provider);
1782 mDisabledProviders.add(provider);
1783 }
1784 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04001785 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 }
1788
1789 public void clearTestProviderEnabled(String provider) {
1790 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001791 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001792 MockProvider mockProvider = mMockProviders.get(provider);
1793 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1795 }
Mike Lockwood86328a92009-10-23 08:38:25 -04001796 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 mEnabledProviders.remove(provider);
1798 mDisabledProviders.remove(provider);
1799 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04001800 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802 }
1803
1804 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
1805 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001806 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001807 MockProvider mockProvider = mMockProviders.get(provider);
1808 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1810 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001811 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 }
1814
1815 public void clearTestProviderStatus(String provider) {
1816 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001817 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001818 MockProvider mockProvider = mMockProviders.get(provider);
1819 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
1821 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001822 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
1824 }
1825
1826 private void log(String log) {
1827 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1828 Log.d(TAG, log);
1829 }
1830 }
1831
1832 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1833 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1834 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001835 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 + Binder.getCallingPid()
1837 + ", uid=" + Binder.getCallingUid());
1838 return;
1839 }
1840
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001841 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 pw.println("Current Location Manager state:");
1843 pw.println(" sProvidersLoaded=" + sProvidersLoaded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 pw.println(" Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001845 int N = mReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 for (int i=0; i<N; i++) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001847 pw.println(" " + mReceivers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
1849 pw.println(" Location Listeners:");
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001850 for (Receiver i : mReceivers.values()) {
1851 pw.println(" " + i + ":");
1852 for (Map.Entry<String,UpdateRecord> j : i.mUpdateRecords.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 pw.println(" " + j.getKey() + ":");
1854 j.getValue().dump(pw, " ");
1855 }
1856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 pw.println(" Records by Provider:");
1858 for (Map.Entry<String, ArrayList<UpdateRecord>> i
1859 : mRecordsByProvider.entrySet()) {
1860 pw.println(" " + i.getKey() + ":");
1861 for (UpdateRecord j : i.getValue()) {
1862 pw.println(" " + j + ":");
1863 j.dump(pw, " ");
1864 }
1865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 pw.println(" Last Known Locations:");
1867 for (Map.Entry<String, Location> i
1868 : mLastKnownLocation.entrySet()) {
1869 pw.println(" " + i.getKey() + ":");
1870 i.getValue().dump(new PrintWriterPrinter(pw), " ");
1871 }
1872 if (mProximityAlerts.size() > 0) {
1873 pw.println(" Proximity Alerts:");
1874 for (Map.Entry<PendingIntent, ProximityAlert> i
1875 : mProximityAlerts.entrySet()) {
1876 pw.println(" " + i.getKey() + ":");
1877 i.getValue().dump(pw, " ");
1878 }
1879 }
1880 if (mProximitiesEntered.size() > 0) {
1881 pw.println(" Proximities Entered:");
1882 for (ProximityAlert i : mProximitiesEntered) {
1883 pw.println(" " + i + ":");
1884 i.dump(pw, " ");
1885 }
1886 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001887 pw.println(" mProximityReceiver=" + mProximityReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 pw.println(" mProximityListener=" + mProximityListener);
1889 if (mEnabledProviders.size() > 0) {
1890 pw.println(" Enabled Providers:");
1891 for (String i : mEnabledProviders) {
1892 pw.println(" " + i);
1893 }
1894
1895 }
1896 if (mDisabledProviders.size() > 0) {
1897 pw.println(" Disabled Providers:");
1898 for (String i : mDisabledProviders) {
1899 pw.println(" " + i);
1900 }
1901
1902 }
1903 if (mMockProviders.size() > 0) {
1904 pw.println(" Mock Providers:");
1905 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001906 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910 }
1911}