blob: 7f056c24f0ca8ef8584b1022dfd160915750ceb8 [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
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070021import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070025import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070026import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050027import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070029import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050030import android.content.pm.ResolveInfo;
31import android.content.pm.Signature;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050032import android.content.res.Resources;
Brian Muramatsubb95cb92012-08-29 10:43:21 -070033import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.location.Address;
Mike Lockwood03ca2162010-04-01 08:10:09 -070035import android.location.Criteria;
Mike Lockwood34901402010-01-04 12:14:21 -050036import android.location.GeocoderParams;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070037import android.location.Geofence;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040039import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.location.ILocationListener;
41import android.location.ILocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040042import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.location.Location;
44import android.location.LocationManager;
45import android.location.LocationProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070046import android.location.LocationRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
48import android.os.Bundle;
49import android.os.Handler;
50import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070051import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Message;
53import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070054import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070056import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070057import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070058import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080061import android.util.Slog;
Mike Lockwoode97ae402010-09-29 15:23:46 -040062import com.android.internal.content.PackageMonitor;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070063import com.android.internal.location.ProviderProperties;
64import com.android.internal.location.ProviderRequest;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070065import com.android.internal.os.BackgroundThread;
Mike Lockwood43e33f22010-03-26 10:41:48 -040066import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070067import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070068import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040069import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070070import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070071import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040072import com.android.server.location.LocationProviderInterface;
73import com.android.server.location.LocationProviderProxy;
74import com.android.server.location.MockProvider;
75import com.android.server.location.PassiveProvider;
76
77import java.io.FileDescriptor;
78import java.io.PrintWriter;
79import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070080import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040081import java.util.HashMap;
82import java.util.HashSet;
83import java.util.List;
84import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040085import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * The service class that manages LocationProviders and issues location
89 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080091public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080093 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070094
95 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Victoria Lease37425c32012-10-16 16:08:48 -070097 // Location resolution level: no location data whatsoever
98 private static final int RESOLUTION_LEVEL_NONE = 0;
99 // Location resolution level: coarse location data only
100 private static final int RESOLUTION_LEVEL_COARSE = 1;
101 // Location resolution level: fine location data
102 private static final int RESOLUTION_LEVEL_FINE = 2;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700105 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700107 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400108 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700109 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
110
111 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700112 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700113 private static final String FUSED_LOCATION_SERVICE_ACTION =
114 "com.android.location.service.FusedLocationProvider";
115
116 private static final int MSG_LOCATION_CHANGED = 1;
117
David Christie1b9b7b12013-04-15 15:31:11 -0700118 private static final long NANOS_PER_MILLI = 1000000L;
119
David Christie0b837452013-07-29 16:02:13 -0700120 // The maximum interval a location request can have and still be considered "high power".
121 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
122
Nick Pellyf1be6862012-05-15 10:53:42 -0700123 // Location Providers may sometimes deliver location updates
124 // slightly faster that requested - provide grace period so
125 // we don't unnecessarily filter events that are otherwise on
126 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700127 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700128
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700129 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
130
131 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800132 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700133
134 // used internally for synchronization
135 private final Object mLock = new Object();
136
Victoria Lease5cd731a2012-12-19 15:04:21 -0800137 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700138 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700139 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700140 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700141 private PowerManager mPowerManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700142 private GeocoderProxy mGeocodeProvider;
143 private IGpsStatusProvider mGpsStatusProvider;
144 private INetInitiatedListener mNetInitiatedListener;
145 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700146 private PassiveProvider mPassiveProvider; // track passive provider for special cases
147 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700148
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700149 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // Set of providers that are explicitly enabled
151 private final Set<String> mEnabledProviders = new HashSet<String>();
152
153 // Set of providers that are explicitly disabled
154 private final Set<String> mDisabledProviders = new HashSet<String>();
155
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700156 // Mock (test) providers
157 private final HashMap<String, MockProvider> mMockProviders =
158 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700160 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400161 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700163 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500164 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400166
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 // real providers, saved here when mocked out
168 private final HashMap<String, LocationProviderInterface> mRealProviders =
169 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700171 // mapping from provider name to provider
172 private final HashMap<String, LocationProviderInterface> mProvidersByName =
173 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700175 // mapping from provider name to all its UpdateRecords
176 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
177 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700178
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700179 // mapping from provider name to last known location
180 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
David Christie1b9b7b12013-04-15 15:31:11 -0700182 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
183 // locations stored here are not fudged for coarse permissions.
184 private final HashMap<String, Location> mLastLocationCoarseInterval =
185 new HashMap<String, Location>();
186
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700187 // all providers that operate over proxy, for authorizing incoming location
188 private final ArrayList<LocationProviderProxy> mProxyProviders =
189 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Victoria Lease38389b62012-09-30 11:44:22 -0700191 // current active user on the device - other users are denied location data
192 private int mCurrentUserId = UserHandle.USER_OWNER;
193
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700194 public LocationManagerService(Context context) {
195 super();
196 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800197 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800198
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700199 if (D) Log.d(TAG, "Constructed");
200
201 // most startup is deferred until systemReady()
202 }
203
Svetoslav Ganova0027152013-06-25 14:59:53 -0700204 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700205 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800206 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700207
Victoria Lease5cd731a2012-12-19 15:04:21 -0800208 // fetch package manager
209 mPackageManager = mContext.getPackageManager();
210
Victoria Lease0aa28602013-05-29 15:28:26 -0700211 // fetch power manager
212 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800213
214 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700215 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800216
217 // prepare mLocationHandler's dependents
218 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
219 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
220 mBlacklist.init();
221 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
222
Dianne Hackbornc2293022013-02-06 23:14:49 -0800223 // Monitor for app ops mode changes.
224 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
225 public void opChanged(int op, String packageName) {
226 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700227 for (Receiver receiver : mReceivers.values()) {
228 receiver.updateMonitoring(true);
229 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800230 applyAllProviderRequirementsLocked();
231 }
232 }
233 };
234 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
235
Victoria Lease5cd731a2012-12-19 15:04:21 -0800236 // prepare providers
237 loadProvidersLocked();
238 updateProvidersLocked();
239 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700240
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700241 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700242 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700243 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700244 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800245 @Override
246 public void onChange(boolean selfChange) {
247 synchronized (mLock) {
248 updateProvidersLocked();
249 }
250 }
251 }, UserHandle.USER_ALL);
252 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700253
Victoria Lease38389b62012-09-30 11:44:22 -0700254 // listen for user change
255 IntentFilter intentFilter = new IntentFilter();
256 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
257
258 mContext.registerReceiverAsUser(new BroadcastReceiver() {
259 @Override
260 public void onReceive(Context context, Intent intent) {
261 String action = intent.getAction();
262 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
263 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
264 }
265 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800266 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700267 }
268
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500269 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
270 PackageManager pm = mContext.getPackageManager();
271 String systemPackageName = mContext.getPackageName();
272 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
273
274 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
275 new Intent(FUSED_LOCATION_SERVICE_ACTION),
276 PackageManager.GET_META_DATA, mCurrentUserId);
277 for (ResolveInfo rInfo : rInfos) {
278 String packageName = rInfo.serviceInfo.packageName;
279
280 // Check that the signature is in the list of supported sigs. If it's not in
281 // this list the standard provider binding logic won't bind to it.
282 try {
283 PackageInfo pInfo;
284 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
285 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
286 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
287 ", but has wrong signature, ignoring");
288 continue;
289 }
290 } catch (NameNotFoundException e) {
291 Log.e(TAG, "missing package: " + packageName);
292 continue;
293 }
294
295 // Get the version info
296 if (rInfo.serviceInfo.metaData == null) {
297 Log.w(TAG, "Found fused provider without metadata: " + packageName);
298 continue;
299 }
300
301 int version = rInfo.serviceInfo.metaData.getInt(
302 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
303 if (version == 0) {
304 // This should be the fallback fused location provider.
305
306 // Make sure it's in the system partition.
307 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
308 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
309 continue;
310 }
311
312 // Check that the fallback is signed the same as the OS
313 // as a proxy for coreApp="true"
314 if (pm.checkSignatures(systemPackageName, packageName)
315 != PackageManager.SIGNATURE_MATCH) {
316 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
317 + packageName);
318 continue;
319 }
320
321 // Found a valid fallback.
322 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
323 return;
324 } else {
325 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
326 }
327 }
328
329 throw new IllegalStateException("Unable to find a fused location provider that is in the "
330 + "system partition with version 0 and signed with the platform certificate. "
331 + "Such a package is needed to provide a default fused location provider in the "
332 + "event that no other fused location provider has been installed or is currently "
333 + "available. For example, coreOnly boot mode when decrypting the data "
334 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
335 }
336
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700337 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700338 // create a passive location provider, which is always enabled
339 PassiveProvider passiveProvider = new PassiveProvider(this);
340 addProviderLocked(passiveProvider);
341 mEnabledProviders.add(passiveProvider.getName());
342 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700343 // Create a gps location provider
344 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
345 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700346
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700347 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700348 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
349 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
350 addProviderLocked(gpsProvider);
351 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
352 }
353
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700354 /*
355 Load package name(s) containing location provider support.
356 These packages can contain services implementing location providers:
357 Geocoder Provider, Network Location Provider, and
358 Fused Location Provider. They will each be searched for
359 service components implementing these providers.
360 The location framework also has support for installation
361 of new location providers at run-time. The new package does not
362 have to be explicitly listed here, however it must have a signature
363 that matches the signature of at least one package on this list.
364 */
365 Resources resources = mContext.getResources();
366 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500367 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700368 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500369 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
370 Arrays.toString(pkgs));
371 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
372
373 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700374
375 // bind to network provider
376 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
377 mContext,
378 LocationManager.NETWORK_PROVIDER,
379 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700380 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
381 com.android.internal.R.string.config_networkLocationProviderPackageName,
382 com.android.internal.R.array.config_locationProviderPackageNames,
383 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700384 if (networkProvider != null) {
385 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
386 mProxyProviders.add(networkProvider);
387 addProviderLocked(networkProvider);
388 } else {
389 Slog.w(TAG, "no network location provider found");
390 }
391
392 // bind to fused provider
393 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
394 mContext,
395 LocationManager.FUSED_PROVIDER,
396 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700397 com.android.internal.R.bool.config_enableFusedLocationOverlay,
398 com.android.internal.R.string.config_fusedLocationProviderPackageName,
399 com.android.internal.R.array.config_locationProviderPackageNames,
400 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700401 if (fusedLocationProvider != null) {
402 addProviderLocked(fusedLocationProvider);
403 mProxyProviders.add(fusedLocationProvider);
404 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700405 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700406 } else {
407 Slog.e(TAG, "no fused location provider found",
408 new IllegalStateException("Location service needs a fused location provider"));
409 }
410
411 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700412 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
413 com.android.internal.R.bool.config_enableGeocoderOverlay,
414 com.android.internal.R.string.config_geocoderProviderPackageName,
415 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800416 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700417 if (mGeocodeProvider == null) {
418 Slog.e(TAG, "no geocoder provider found");
419 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700420
421 // bind to geofence provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700422 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
423 com.android.internal.R.bool.config_enableGeofenceOverlay,
424 com.android.internal.R.string.config_geofenceProviderPackageName,
425 com.android.internal.R.array.config_locationProviderPackageNames,
426 mLocationHandler,
427 gpsProvider.getGpsGeofenceProxy());
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700428 if (provider == null) {
429 Slog.e(TAG, "no geofence provider found");
430 }
431
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700432 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700435 * Called when the device's active user changes.
436 * @param userId the new active user's UserId
437 */
438 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700439 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800440 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700441 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700442 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700443 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700444 for (LocationProviderInterface p : mProviders) {
445 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700446 }
Victoria Lease38389b62012-09-30 11:44:22 -0700447 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700448 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700449 }
450 }
451
452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
454 * location updates.
455 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700456 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700457 final int mUid; // uid of receiver
458 final int mPid; // pid of receiver
459 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700460 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final ILocationListener mListener;
463 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700464 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700466
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400467 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700468
David Christie0b837452013-07-29 16:02:13 -0700469 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700470 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700471 // True if app ops has started monitoring this receiver for high power (gps) locations.
472 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700473 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700474 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700476 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie82edc9b2013-07-19 11:31:42 -0700477 String packageName, WorkSource workSource) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700480 if (listener != null) {
481 mKey = listener.asBinder();
482 } else {
483 mKey = intent;
484 }
Victoria Lease37425c32012-10-16 16:08:48 -0700485 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700486 mUid = uid;
487 mPid = pid;
488 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700489 if (workSource != null && workSource.size() <= 0) {
490 workSource = null;
491 }
492 mWorkSource = workSource;
Victoria Lease0aa28602013-05-29 15:28:26 -0700493
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700494 updateMonitoring(true);
495
Victoria Lease0aa28602013-05-29 15:28:26 -0700496 // construct/configure wakelock
497 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700498 if (workSource == null) {
499 workSource = new WorkSource(mUid, mPackageName);
500 }
501 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
504 @Override
505 public boolean equals(Object otherObj) {
506 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700507 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509 return false;
510 }
511
512 @Override
513 public int hashCode() {
514 return mKey.hashCode();
515 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 @Override
518 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700519 StringBuilder s = new StringBuilder();
520 s.append("Reciever[");
521 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700523 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700525 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700527 for (String p : mUpdateRecords.keySet()) {
528 s.append(" ").append(mUpdateRecords.get(p).toString());
529 }
530 s.append("]");
531 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700534 public void updateMonitoring(boolean allow) {
David Christie0b837452013-07-29 16:02:13 -0700535 // First update monitoring of any location request (including high power).
536 mOpMonitoring = updateMonitoring(allow, mOpMonitoring,
537 AppOpsManager.OP_MONITOR_LOCATION);
538
539 // Now update monitoring of high power requests only.
540 // A high power request is any gps request with interval under a threshold.
541 boolean allowHighPower = allow;
542 if (allowHighPower) {
543 UpdateRecord gpsRecord = mUpdateRecords.get(LocationManager.GPS_PROVIDER);
544 if (gpsRecord == null
545 || gpsRecord.mRequest.getInterval() > HIGH_POWER_INTERVAL_MS) {
546 allowHighPower = false;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700547 }
548 }
David Christie0b837452013-07-29 16:02:13 -0700549 mOpHighPowerMonitoring = updateMonitoring(allowHighPower, mOpHighPowerMonitoring,
550 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
551 }
552
553 /**
554 * Update AppOps monitoring for a single location request and op type.
555 *
556 * @param allowMonitoring True if monitoring is allowed for this request/op.
557 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
558 * @param op AppOps code for the op to update.
559 * @return True if monitoring is on for this request/op after updating.
560 */
561 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
562 int op) {
563 if (!currentlyMonitoring) {
564 if (allowMonitoring) {
565 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
566 == AppOpsManager.MODE_ALLOWED;
567 }
568 } else {
569 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
570 != AppOpsManager.MODE_ALLOWED) {
571 mAppOps.finishOp(op, mUid, mPackageName);
572 return false;
573 }
574 }
575
576 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700577 }
578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 public boolean isListener() {
580 return mListener != null;
581 }
582
583 public boolean isPendingIntent() {
584 return mPendingIntent != null;
585 }
586
587 public ILocationListener getListener() {
588 if (mListener != null) {
589 return mListener;
590 }
591 throw new IllegalStateException("Request for non-existent listener");
592 }
593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
595 if (mListener != null) {
596 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700597 synchronized (this) {
598 // synchronize to ensure incrementPendingBroadcastsLocked()
599 // is called before decrementPendingBroadcasts()
600 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700601 // call this after broadcasting so we do not increment
602 // if we throw an exeption.
603 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 } catch (RemoteException e) {
606 return false;
607 }
608 } else {
609 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800610 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
612 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700613 synchronized (this) {
614 // synchronize to ensure incrementPendingBroadcastsLocked()
615 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700616 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700617 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700618 // call this after broadcasting so we do not increment
619 // if we throw an exeption.
620 incrementPendingBroadcastsLocked();
621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 } catch (PendingIntent.CanceledException e) {
623 return false;
624 }
625 }
626 return true;
627 }
628
629 public boolean callLocationChangedLocked(Location location) {
630 if (mListener != null) {
631 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700632 synchronized (this) {
633 // synchronize to ensure incrementPendingBroadcastsLocked()
634 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800635 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700636 // call this after broadcasting so we do not increment
637 // if we throw an exeption.
638 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 } catch (RemoteException e) {
641 return false;
642 }
643 } else {
644 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800645 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700647 synchronized (this) {
648 // synchronize to ensure incrementPendingBroadcastsLocked()
649 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700650 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700651 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700652 // call this after broadcasting so we do not increment
653 // if we throw an exeption.
654 incrementPendingBroadcastsLocked();
655 }
656 } catch (PendingIntent.CanceledException e) {
657 return false;
658 }
659 }
660 return true;
661 }
662
663 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
664 if (mListener != null) {
665 try {
666 synchronized (this) {
667 // synchronize to ensure incrementPendingBroadcastsLocked()
668 // is called before decrementPendingBroadcasts()
669 if (enabled) {
670 mListener.onProviderEnabled(provider);
671 } else {
672 mListener.onProviderDisabled(provider);
673 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700674 // call this after broadcasting so we do not increment
675 // if we throw an exeption.
676 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700677 }
678 } catch (RemoteException e) {
679 return false;
680 }
681 } else {
682 Intent providerIntent = new Intent();
683 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
684 try {
685 synchronized (this) {
686 // synchronize to ensure incrementPendingBroadcastsLocked()
687 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700688 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700689 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700690 // call this after broadcasting so we do not increment
691 // if we throw an exeption.
692 incrementPendingBroadcastsLocked();
693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 } catch (PendingIntent.CanceledException e) {
695 return false;
696 }
697 }
698 return true;
699 }
700
Nick Pellyf1be6862012-05-15 10:53:42 -0700701 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700703 if (D) Log.d(TAG, "Location listener died");
704
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400705 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 removeUpdatesLocked(this);
707 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700708 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700709 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700710 }
711 }
712
Nick Pellye0fd6932012-07-11 10:26:13 -0700713 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700714 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
715 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400716 synchronized (this) {
717 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700718 }
719 }
720
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400721 // this must be called while synchronized by caller in a synchronized block
722 // containing the sending of the broadcaset
723 private void incrementPendingBroadcastsLocked() {
724 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700725 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400726 }
727 }
728
729 private void decrementPendingBroadcastsLocked() {
730 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700731 if (mWakeLock.isHeld()) {
732 mWakeLock.release();
733 }
734 }
735 }
736
737 public void clearPendingBroadcastsLocked() {
738 if (mPendingBroadcasts > 0) {
739 mPendingBroadcasts = 0;
740 if (mWakeLock.isHeld()) {
741 mWakeLock.release();
742 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700743 }
744 }
745 }
746
Nick Pellye0fd6932012-07-11 10:26:13 -0700747 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700748 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700749 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400750 //the receiver list if it is not found. If it is not found then the
751 //LocationListener was removed when it had a pending broadcast and should
752 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700753 synchronized (mLock) {
754 IBinder binder = listener.asBinder();
755 Receiver receiver = mReceivers.get(binder);
756 if (receiver != null) {
757 synchronized (receiver) {
758 // so wakelock calls will succeed
759 long identity = Binder.clearCallingIdentity();
760 receiver.decrementPendingBroadcastsLocked();
761 Binder.restoreCallingIdentity(identity);
762 }
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
765 }
766
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700767 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400768 mProviders.add(provider);
769 mProvidersByName.put(provider.getName(), provider);
770 }
771
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700772 private void removeProviderLocked(LocationProviderInterface provider) {
773 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400774 mProviders.remove(provider);
775 mProvidersByName.remove(provider.getName());
776 }
777
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800778 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800779 * Returns "true" if access to the specified location provider is allowed by the current
780 * user's settings. Access to all location providers is forbidden to non-location-provider
781 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800782 *
783 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800784 * @return
785 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800786 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 if (mEnabledProviders.contains(provider)) {
788 return true;
789 }
790 if (mDisabledProviders.contains(provider)) {
791 return false;
792 }
793 // Use system settings
794 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795
Victoria Leaseb711d572012-10-02 13:14:11 -0700796 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700799 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800800 * Returns "true" if access to the specified location provider is allowed by the specified
801 * user's settings. Access to all location providers is forbidden to non-location-provider
802 * processes belonging to background users.
803 *
804 * @param provider the name of the location provider
805 * @param uid the requestor's UID
806 * @return
807 */
808 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
809 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
810 return false;
811 }
812 return isAllowedByCurrentUserSettingsLocked(provider);
813 }
814
815 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700816 * Returns the permission string associated with the specified resolution level.
817 *
818 * @param resolutionLevel the resolution level
819 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700820 */
Victoria Lease37425c32012-10-16 16:08:48 -0700821 private String getResolutionPermission(int resolutionLevel) {
822 switch (resolutionLevel) {
823 case RESOLUTION_LEVEL_FINE:
824 return android.Manifest.permission.ACCESS_FINE_LOCATION;
825 case RESOLUTION_LEVEL_COARSE:
826 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
827 default:
828 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700830 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700831
Victoria Leaseda479c52012-10-15 15:24:16 -0700832 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700833 * Returns the resolution level allowed to the given PID/UID pair.
834 *
835 * @param pid the PID
836 * @param uid the UID
837 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700838 */
Victoria Lease37425c32012-10-16 16:08:48 -0700839 private int getAllowedResolutionLevel(int pid, int uid) {
840 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
841 pid, uid) == PackageManager.PERMISSION_GRANTED) {
842 return RESOLUTION_LEVEL_FINE;
843 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
844 pid, uid) == PackageManager.PERMISSION_GRANTED) {
845 return RESOLUTION_LEVEL_COARSE;
846 } else {
847 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700848 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700849 }
850
851 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700852 * Returns the resolution level allowed to the caller
853 *
854 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700855 */
Victoria Lease37425c32012-10-16 16:08:48 -0700856 private int getCallerAllowedResolutionLevel() {
857 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
858 }
859
860 /**
861 * Throw SecurityException if specified resolution level is insufficient to use geofences.
862 *
863 * @param allowedResolutionLevel resolution level allowed to caller
864 */
865 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
866 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700867 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
Victoria Lease37425c32012-10-16 16:08:48 -0700871 /**
872 * Return the minimum resolution level required to use the specified location provider.
873 *
874 * @param provider the name of the location provider
875 * @return minimum resolution level required for provider
876 */
877 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700878 if (LocationManager.GPS_PROVIDER.equals(provider) ||
879 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
880 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700881 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700882 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
883 LocationManager.FUSED_PROVIDER.equals(provider)) {
884 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700885 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700886 } else {
887 // mock providers
888 LocationProviderInterface lp = mMockProviders.get(provider);
889 if (lp != null) {
890 ProviderProperties properties = lp.getProperties();
891 if (properties != null) {
892 if (properties.mRequiresSatellite) {
893 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700894 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700895 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
896 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700897 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700898 }
899 }
900 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700901 }
Victoria Lease37425c32012-10-16 16:08:48 -0700902 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700903 }
904
Victoria Lease37425c32012-10-16 16:08:48 -0700905 /**
906 * Throw SecurityException if specified resolution level is insufficient to use the named
907 * location provider.
908 *
909 * @param allowedResolutionLevel resolution level allowed to caller
910 * @param providerName the name of the location provider
911 */
912 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
913 String providerName) {
914 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
915 if (allowedResolutionLevel < requiredResolutionLevel) {
916 switch (requiredResolutionLevel) {
917 case RESOLUTION_LEVEL_FINE:
918 throw new SecurityException("\"" + providerName + "\" location provider " +
919 "requires ACCESS_FINE_LOCATION permission.");
920 case RESOLUTION_LEVEL_COARSE:
921 throw new SecurityException("\"" + providerName + "\" location provider " +
922 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
923 default:
924 throw new SecurityException("Insufficient permission for \"" + providerName +
925 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700926 }
927 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700928 }
929
David Christie82edc9b2013-07-19 11:31:42 -0700930 /**
931 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
932 * for battery).
933 */
934 private void checkWorkSourceAllowed() {
935 mContext.enforceCallingOrSelfPermission(
936 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
937 }
938
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800939 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800940 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
941 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800942 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800943 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800944 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800945 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800946 }
947 return -1;
948 }
949
950 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
951 int op = resolutionLevelToOp(allowedResolutionLevel);
952 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800953 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
954 return false;
955 }
956 }
957 return true;
958 }
959
960 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800961 int op = resolutionLevelToOp(allowedResolutionLevel);
962 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800963 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
964 return false;
965 }
966 }
967 return true;
968 }
969
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700970 /**
971 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700972 * fused, also including ones that are not permitted to
973 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700974 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700975 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700977 ArrayList<String> out;
978 synchronized (mLock) {
979 out = new ArrayList<String>(mProviders.size());
980 for (LocationProviderInterface provider : mProviders) {
981 String name = provider.getName();
982 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700983 continue;
984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 out.add(name);
986 }
987 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700988
989 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 return out;
991 }
992
Mike Lockwood03ca2162010-04-01 08:10:09 -0700993 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700994 * Return all providers by name, that match criteria and are optionally
995 * enabled.
996 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700997 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700998 @Override
999 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001000 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001001 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001002 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001003 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001004 try {
1005 synchronized (mLock) {
1006 out = new ArrayList<String>(mProviders.size());
1007 for (LocationProviderInterface provider : mProviders) {
1008 String name = provider.getName();
1009 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001010 continue;
1011 }
Victoria Lease37425c32012-10-16 16:08:48 -07001012 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001013 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001014 continue;
1015 }
1016 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1017 name, provider.getProperties(), criteria)) {
1018 continue;
1019 }
1020 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001021 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001022 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001023 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001024 } finally {
1025 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001026 }
1027
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001028 if (D) Log.d(TAG, "getProviders()=" + out);
1029 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001030 }
1031
1032 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001033 * Return the name of the best provider given a Criteria object.
1034 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001035 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001036 * has been deprecated as well. So this method now uses
1037 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001038 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001039 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001040 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001041 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001042
1043 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001044 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001045 result = pickBest(providers);
1046 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1047 return result;
1048 }
1049 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001050 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001051 result = pickBest(providers);
1052 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1053 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001054 }
1055
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001056 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001057 return null;
1058 }
1059
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001060 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001061 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001062 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001063 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1064 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001065 } else {
1066 return providers.get(0);
1067 }
1068 }
1069
Nick Pellye0fd6932012-07-11 10:26:13 -07001070 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001071 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1072 LocationProviderInterface p = mProvidersByName.get(provider);
1073 if (p == null) {
1074 throw new IllegalArgumentException("provider=" + provider);
1075 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001076
1077 boolean result = LocationProvider.propertiesMeetCriteria(
1078 p.getName(), p.getProperties(), criteria);
1079 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1080 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001081 }
1082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001084 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001085 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001086 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 boolean isEnabled = p.isEnabled();
1088 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001089 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001091 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001092 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001094 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001095 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001097 }
1098 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001099 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1100 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102 }
1103
Victoria Leaseb711d572012-10-02 13:14:11 -07001104 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 int listeners = 0;
1106
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001107 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001108 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109
1110 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1113 if (records != null) {
1114 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001115 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001117 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001118 // Sends a notification message to the receiver
1119 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1120 if (deadReceivers == null) {
1121 deadReceivers = new ArrayList<Receiver>();
1122 }
1123 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001125 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 }
1129
1130 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001131 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 removeUpdatesLocked(deadReceivers.get(i));
1133 }
1134 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 if (enabled) {
1137 p.enable();
1138 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001139 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001146 private void applyRequirementsLocked(String provider) {
1147 LocationProviderInterface p = mProvidersByName.get(provider);
1148 if (p == null) return;
1149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001151 WorkSource worksource = new WorkSource();
1152 ProviderRequest providerRequest = new ProviderRequest();
1153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001155 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001156 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001157 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1158 record.mReceiver.mAllowedResolutionLevel)) {
1159 LocationRequest locationRequest = record.mRequest;
1160 providerRequest.locationRequests.add(locationRequest);
1161 if (locationRequest.getInterval() < providerRequest.interval) {
1162 providerRequest.reportLocation = true;
1163 providerRequest.interval = locationRequest.getInterval();
1164 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001165 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001166 }
1167 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001168
1169 if (providerRequest.reportLocation) {
1170 // calculate who to blame for power
1171 // This is somewhat arbitrary. We pick a threshold interval
1172 // that is slightly higher that the minimum interval, and
1173 // spread the blame across all applications with a request
1174 // under that threshold.
1175 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1176 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001177 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001178 LocationRequest locationRequest = record.mRequest;
1179 if (locationRequest.getInterval() <= thresholdInterval) {
David Christie82edc9b2013-07-19 11:31:42 -07001180 if (record.mReceiver.mWorkSource != null) {
1181 // Assign blame to another work source.
1182 worksource.add(record.mReceiver.mWorkSource);
1183 } else {
1184 // Assign blame to caller.
1185 worksource.add(
1186 record.mReceiver.mUid,
1187 record.mReceiver.mPackageName);
1188 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001189 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001190 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001194
1195 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1196 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198
1199 private class UpdateRecord {
1200 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001201 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001203 Location mLastFixBroadcast;
1204 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205
1206 /**
1207 * Note: must be constructed with lock held.
1208 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001209 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001211 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213
1214 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1215 if (records == null) {
1216 records = new ArrayList<UpdateRecord>();
1217 mRecordsByProvider.put(provider, records);
1218 }
1219 if (!records.contains(this)) {
1220 records.add(this);
1221 }
1222 }
1223
1224 /**
1225 * Method to be called when a record will no longer be used. Calling this multiple times
1226 * must have the same effect as calling it once.
1227 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001228 void disposeLocked(boolean removeReceiver) {
1229 // remove from mRecordsByProvider
1230 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1231 if (globalRecords != null) {
1232 globalRecords.remove(this);
1233 }
1234
1235 if (!removeReceiver) return; // the caller will handle the rest
1236
1237 // remove from Receiver#mUpdateRecords
1238 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1239 if (receiverRecords != null) {
1240 receiverRecords.remove(this.mProvider);
1241
1242 // and also remove the Receiver if it has no more update records
1243 if (removeReceiver && receiverRecords.size() == 0) {
1244 removeUpdatesLocked(mReceiver);
1245 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248
1249 @Override
1250 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001251 StringBuilder s = new StringBuilder();
1252 s.append("UpdateRecord[");
1253 s.append(mProvider);
1254 s.append(' ').append(mReceiver.mPackageName).append('(');
1255 s.append(mReceiver.mUid).append(')');
1256 s.append(' ').append(mRequest);
1257 s.append(']');
1258 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001262 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie82edc9b2013-07-19 11:31:42 -07001263 String packageName, WorkSource workSource) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001264 IBinder binder = listener.asBinder();
1265 Receiver receiver = mReceivers.get(binder);
1266 if (receiver == null) {
David Christie82edc9b2013-07-19 11:31:42 -07001267 receiver = new Receiver(listener, null, pid, uid, packageName, workSource);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001268 mReceivers.put(binder, receiver);
1269
1270 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001271 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001272 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001273 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001274 return null;
1275 }
1276 }
1277 return receiver;
1278 }
1279
David Christie82edc9b2013-07-19 11:31:42 -07001280 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
1281 WorkSource workSource) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001282 Receiver receiver = mReceivers.get(intent);
1283 if (receiver == null) {
David Christie82edc9b2013-07-19 11:31:42 -07001284 receiver = new Receiver(null, intent, pid, uid, packageName, workSource);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001285 mReceivers.put(intent, receiver);
1286 }
1287 return receiver;
1288 }
1289
Victoria Lease37425c32012-10-16 16:08:48 -07001290 /**
1291 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1292 * and consistency requirements.
1293 *
1294 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001295 * @return a version of request that meets the given resolution and consistency requirements
1296 * @hide
1297 */
1298 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1299 LocationRequest sanitizedRequest = new LocationRequest(request);
1300 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1301 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001302 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001303 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001304 break;
1305 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001306 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001307 break;
1308 }
1309 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001310 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1311 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001312 }
Victoria Lease37425c32012-10-16 16:08:48 -07001313 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1314 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001315 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001316 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001317 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001318 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001319 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001320 }
Victoria Lease37425c32012-10-16 16:08:48 -07001321 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001322 }
1323
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001324 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001325 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001326 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001327 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001328 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001329 String[] packages = mPackageManager.getPackagesForUid(uid);
1330 if (packages == null) {
1331 throw new SecurityException("invalid UID " + uid);
1332 }
1333 for (String pkg : packages) {
1334 if (packageName.equals(pkg)) return;
1335 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001336 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001337 }
1338
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001339 private void checkPendingIntent(PendingIntent intent) {
1340 if (intent == null) {
1341 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001342 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001343 }
1344
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001345 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie82edc9b2013-07-19 11:31:42 -07001346 int pid, int uid, String packageName, WorkSource workSource) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001347 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001348 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001349 } else if (intent != null && listener != null) {
1350 throw new IllegalArgumentException("cannot register both listener and intent");
1351 } else if (intent != null) {
1352 checkPendingIntent(intent);
David Christie82edc9b2013-07-19 11:31:42 -07001353 return getReceiverLocked(intent, pid, uid, packageName, workSource);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001354 } else {
David Christie82edc9b2013-07-19 11:31:42 -07001355 return getReceiverLocked(listener, pid, uid, packageName, workSource);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001356 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001357 }
1358
Nick Pellye0fd6932012-07-11 10:26:13 -07001359 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001360 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1361 PendingIntent intent, String packageName) {
1362 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1363 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001364 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1365 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1366 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001367 WorkSource workSource = request.getWorkSource();
1368 if (workSource != null && workSource.size() > 0) {
1369 checkWorkSourceAllowed();
1370 }
Victoria Lease37425c32012-10-16 16:08:48 -07001371 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001373 final int pid = Binder.getCallingPid();
1374 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001375 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 long identity = Binder.clearCallingIdentity();
1377 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001378 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1379 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001380 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001381
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001382 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001383 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie82edc9b2013-07-19 11:31:42 -07001384 packageName, workSource);
Victoria Lease37425c32012-10-16 16:08:48 -07001385 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 } finally {
1388 Binder.restoreCallingIdentity(identity);
1389 }
1390 }
1391
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001392 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1393 int pid, int uid, String packageName) {
1394 // Figure out the provider. Either its explicitly request (legacy use cases), or
1395 // use the fused provider
1396 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1397 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001398 if (name == null) {
1399 throw new IllegalArgumentException("provider name must not be null");
1400 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001401
1402 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1403 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001404 LocationProviderInterface provider = mProvidersByName.get(name);
1405 if (provider == null) {
1406 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1407 }
1408
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001409 UpdateRecord record = new UpdateRecord(name, request, receiver);
1410 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1411 if (oldRecord != null) {
1412 oldRecord.disposeLocked(false);
1413 }
1414
Victoria Lease09eeaec2013-02-05 11:34:13 -08001415 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001416 if (isProviderEnabled) {
1417 applyRequirementsLocked(name);
1418 } else {
1419 // Notify the listener that updates are currently disabled
1420 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
David Christie0b837452013-07-29 16:02:13 -07001422 // Update the monitoring here just in case multiple location requests were added to the
1423 // same receiver (this request may be high power and the initial might not have been).
1424 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426
Nick Pellye0fd6932012-07-11 10:26:13 -07001427 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001428 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1429 String packageName) {
1430 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001431
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001432 final int pid = Binder.getCallingPid();
1433 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001434
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001435 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001436 WorkSource workSource = null;
1437 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid, packageName,
1438 workSource);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001439
1440 // providers may use public location API's, need to clear identity
1441 long identity = Binder.clearCallingIdentity();
1442 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001443 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001444 } finally {
1445 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448 }
1449
1450 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001451 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001452
1453 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1454 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1455 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001456 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001460 receiver.updateMonitoring(false);
1461
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001462 // Record which providers were associated with this listener
1463 HashSet<String> providers = new HashSet<String>();
1464 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1465 if (oldRecords != null) {
1466 // Call dispose() on the obsolete update records.
1467 for (UpdateRecord record : oldRecords.values()) {
1468 record.disposeLocked(false);
1469 }
1470 // Accumulate providers
1471 providers.addAll(oldRecords.keySet());
1472 }
1473
1474 // update provider
1475 for (String provider : providers) {
1476 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001477 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001478 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
1480
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001481 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
1483 }
1484
Dianne Hackbornc2293022013-02-06 23:14:49 -08001485 private void applyAllProviderRequirementsLocked() {
1486 for (LocationProviderInterface p : mProviders) {
1487 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001488 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001489 continue;
1490 }
1491
1492 applyRequirementsLocked(p.getName());
1493 }
1494 }
1495
Nick Pellye0fd6932012-07-11 10:26:13 -07001496 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001497 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001498 if (D) Log.d(TAG, "getLastLocation: " + request);
1499 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001500 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001501 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001502 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1503 request.getProvider());
1504 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001505
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001506 final int uid = Binder.getCallingUid();
1507 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001508 try {
1509 if (mBlacklist.isBlacklisted(packageName)) {
1510 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1511 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001512 return null;
1513 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001514
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001515 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1516 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1517 packageName);
1518 return null;
1519 }
1520
Victoria Leaseb711d572012-10-02 13:14:11 -07001521 synchronized (mLock) {
1522 // Figure out the provider. Either its explicitly request (deprecated API's),
1523 // or use the fused provider
1524 String name = request.getProvider();
1525 if (name == null) name = LocationManager.FUSED_PROVIDER;
1526 LocationProviderInterface provider = mProvidersByName.get(name);
1527 if (provider == null) return null;
1528
Victoria Lease09eeaec2013-02-05 11:34:13 -08001529 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001530
David Christie1b9b7b12013-04-15 15:31:11 -07001531 Location location;
1532 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1533 // Make sure that an app with coarse permissions can't get frequent location
1534 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1535 location = mLastLocationCoarseInterval.get(name);
1536 } else {
1537 location = mLastLocation.get(name);
1538 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001539 if (location == null) {
1540 return null;
1541 }
Victoria Lease37425c32012-10-16 16:08:48 -07001542 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001543 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1544 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001545 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001546 }
Victoria Lease37425c32012-10-16 16:08:48 -07001547 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001548 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001549 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001550 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001551 return null;
1552 } finally {
1553 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001554 }
1555 }
1556
1557 @Override
1558 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1559 String packageName) {
1560 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001561 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1562 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001563 checkPendingIntent(intent);
1564 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001565 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1566 request.getProvider());
1567 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001568
Victoria Lease37425c32012-10-16 16:08:48 -07001569 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001570
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001571 // geo-fence manager uses the public location API, need to clear identity
1572 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001573 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1574 // temporary measure until geofences work for secondary users
1575 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1576 return;
1577 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001578 long identity = Binder.clearCallingIdentity();
1579 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001580 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1581 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001582 } finally {
1583 Binder.restoreCallingIdentity(identity);
1584 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001585 }
1586
1587 @Override
1588 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001589 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001590 checkPendingIntent(intent);
1591 checkPackageName(packageName);
1592
1593 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1594
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001595 // geo-fence manager uses the public location API, need to clear identity
1596 long identity = Binder.clearCallingIdentity();
1597 try {
1598 mGeofenceManager.removeFence(geofence, intent);
1599 } finally {
1600 Binder.restoreCallingIdentity(identity);
1601 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001602 }
1603
1604
1605 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001606 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001607 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 return false;
1609 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001610 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1611 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001612 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001614 final int uid = Binder.getCallingUid();
1615 final long ident = Binder.clearCallingIdentity();
1616 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001617 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001618 return false;
1619 }
1620 } finally {
1621 Binder.restoreCallingIdentity(ident);
1622 }
1623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001625 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001627 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 return false;
1629 }
1630 return true;
1631 }
1632
Nick Pellye0fd6932012-07-11 10:26:13 -07001633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001635 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001636 try {
1637 mGpsStatusProvider.removeGpsStatusListener(listener);
1638 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001639 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
1642 }
1643
Nick Pellye0fd6932012-07-11 10:26:13 -07001644 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001646 if (provider == null) {
1647 // throw NullPointerException to remain compatible with previous implementation
1648 throw new NullPointerException();
1649 }
Victoria Lease37425c32012-10-16 16:08:48 -07001650 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1651 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001654 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 != PackageManager.PERMISSION_GRANTED)) {
1656 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1657 }
1658
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001659 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001660 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001661 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001662
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001663 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
1665 }
1666
Nick Pellye0fd6932012-07-11 10:26:13 -07001667 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001668 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001669 if (Binder.getCallingUid() != Process.myUid()) {
1670 throw new SecurityException(
1671 "calling sendNiResponse from outside of the system is not allowed");
1672 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001673 try {
1674 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001675 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001676 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001677 return false;
1678 }
1679 }
1680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001682 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001683 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 * accessed by the caller
1685 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001686 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001687 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001688 if (mProvidersByName.get(provider) == null) {
1689 return null;
1690 }
1691
Victoria Lease37425c32012-10-16 16:08:48 -07001692 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1693 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001695 LocationProviderInterface p;
1696 synchronized (mLock) {
1697 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001700 if (p == null) return null;
1701 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703
Nick Pellye0fd6932012-07-11 10:26:13 -07001704 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001706 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1707 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001708 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1709
Victoria Lease09eeaec2013-02-05 11:34:13 -08001710 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001711 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001712 try {
1713 synchronized (mLock) {
1714 LocationProviderInterface p = mProvidersByName.get(provider);
1715 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001716
Victoria Lease09eeaec2013-02-05 11:34:13 -08001717 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001718 }
1719 } finally {
1720 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001721 }
1722 }
1723
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001724 /**
1725 * Returns "true" if the UID belongs to a bound location provider.
1726 *
1727 * @param uid the uid
1728 * @return true if uid belongs to a bound location provider
1729 */
1730 private boolean isUidALocationProvider(int uid) {
1731 if (uid == Process.SYSTEM_UID) {
1732 return true;
1733 }
1734 if (mGeocodeProvider != null) {
1735 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1736 }
1737 for (LocationProviderProxy proxy : mProxyProviders) {
1738 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1739 }
1740 return false;
1741 }
1742
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001743 private void checkCallerIsProvider() {
1744 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1745 == PackageManager.PERMISSION_GRANTED) {
1746 return;
1747 }
1748
1749 // Previously we only used the INSTALL_LOCATION_PROVIDER
1750 // check. But that is system or signature
1751 // protection level which is not flexible enough for
1752 // providers installed oustide the system image. So
1753 // also allow providers with a UID matching the
1754 // currently bound package name
1755
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001756 if (isUidALocationProvider(Binder.getCallingUid())) {
1757 return;
1758 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001759
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001760 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1761 "or UID of a currently bound location provider");
1762 }
1763
1764 private boolean doesPackageHaveUid(int uid, String packageName) {
1765 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 return false;
1767 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001768 try {
1769 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1770 if (appInfo.uid != uid) {
1771 return false;
1772 }
1773 } catch (NameNotFoundException e) {
1774 return false;
1775 }
1776 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
1778
Nick Pellye0fd6932012-07-11 10:26:13 -07001779 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001780 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001781 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001782
Nick Pelly2eeeec22012-07-18 13:13:37 -07001783 if (!location.isComplete()) {
1784 Log.w(TAG, "Dropping incomplete location: " + location);
1785 return;
1786 }
1787
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001788 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1789 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001790 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001791 mLocationHandler.sendMessageAtFrontOfQueue(m);
1792 }
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794
Laurent Tu75defb62012-11-01 16:21:52 -07001795 private static boolean shouldBroadcastSafe(
1796 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 // Always broadcast the first update
1798 if (lastLoc == null) {
1799 return true;
1800 }
1801
Nick Pellyf1be6862012-05-15 10:53:42 -07001802 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001803 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001804 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1805 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001806 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 return false;
1808 }
1809
1810 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001811 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 if (minDistance > 0.0) {
1813 if (loc.distanceTo(lastLoc) <= minDistance) {
1814 return false;
1815 }
1816 }
1817
Laurent Tu75defb62012-11-01 16:21:52 -07001818 // Check whether sufficient number of udpates is left
1819 if (record.mRequest.getNumUpdates() <= 0) {
1820 return false;
1821 }
1822
1823 // Check whether the expiry date has passed
1824 if (record.mRequest.getExpireAt() < now) {
1825 return false;
1826 }
1827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 return true;
1829 }
1830
Mike Lockwooda4903f22010-02-17 06:42:23 -05001831 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001832 if (D) Log.d(TAG, "incoming location: " + location);
1833
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001834 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001835 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836
Laurent Tu60ec50a2012-10-04 17:00:10 -07001837 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001838 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001839 if (p == null) return;
1840
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001841 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001842 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1843 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001844 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001845 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001846 lastLocation = new Location(provider);
1847 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001848 } else {
1849 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1850 if (noGPSLocation == null && lastNoGPSLocation != null) {
1851 // New location has no no-GPS location: adopt last no-GPS location. This is set
1852 // directly into location because we do not want to notify COARSE clients.
1853 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1854 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001855 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001856 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857
David Christie1b9b7b12013-04-15 15:31:11 -07001858 // Update last known coarse interval location if enough time has passed.
1859 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1860 if (lastLocationCoarseInterval == null) {
1861 lastLocationCoarseInterval = new Location(location);
1862 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1863 }
1864 long timeDiffNanos = location.getElapsedRealtimeNanos()
1865 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1866 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1867 lastLocationCoarseInterval.set(location);
1868 }
1869 // Don't ever return a coarse location that is more recent than the allowed update
1870 // interval (i.e. don't allow an app to keep registering and unregistering for
1871 // location updates to overcome the minimum interval).
1872 noGPSLocation =
1873 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1874
Laurent Tu60ec50a2012-10-04 17:00:10 -07001875 // Skip if there are no UpdateRecords for this provider.
1876 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1877 if (records == null || records.size() == 0) return;
1878
Victoria Lease09016ab2012-09-16 12:33:15 -07001879 // Fetch coarse location
1880 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001881 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001882 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1883 }
1884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 // Fetch latest status update time
1886 long newStatusUpdateTime = p.getStatusUpdateTime();
1887
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001888 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 Bundle extras = new Bundle();
1890 int status = p.getStatus(extras);
1891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001893 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001896 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001898 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001899
Victoria Lease269518e2012-10-29 08:25:39 -07001900 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001901 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001902 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001903 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001904 " (current user: " + mCurrentUserId + ", app: " +
1905 receiver.mPackageName + ")");
1906 }
1907 continue;
1908 }
1909
Nick Pelly4035f5a2012-08-17 14:43:49 -07001910 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1911 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1912 receiver.mPackageName);
1913 continue;
1914 }
1915
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001916 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1917 receiver.mAllowedResolutionLevel)) {
1918 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1919 receiver.mPackageName);
1920 continue;
1921 }
1922
Victoria Lease09016ab2012-09-16 12:33:15 -07001923 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001924 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1925 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001926 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001927 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001928 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001929 if (notifyLocation != null) {
1930 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001931 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001932 if (lastLoc == null) {
1933 lastLoc = new Location(notifyLocation);
1934 r.mLastFixBroadcast = lastLoc;
1935 } else {
1936 lastLoc.set(notifyLocation);
1937 }
1938 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1939 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1940 receiverDead = true;
1941 }
Laurent Tu75defb62012-11-01 16:21:52 -07001942 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 }
1945
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001946 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001948 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001950 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001952 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001953 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001954 }
1955 }
1956
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001957 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001958 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001959 if (deadUpdateRecords == null) {
1960 deadUpdateRecords = new ArrayList<UpdateRecord>();
1961 }
1962 deadUpdateRecords.add(r);
1963 }
1964 // track dead receivers
1965 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001966 if (deadReceivers == null) {
1967 deadReceivers = new ArrayList<Receiver>();
1968 }
1969 if (!deadReceivers.contains(receiver)) {
1970 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972 }
1973 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001974
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001975 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001977 for (Receiver receiver : deadReceivers) {
1978 removeUpdatesLocked(receiver);
1979 }
1980 }
1981 if (deadUpdateRecords != null) {
1982 for (UpdateRecord r : deadUpdateRecords) {
1983 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
Victoria Lease8b38b292012-12-04 15:04:43 -08001985 applyRequirementsLocked(provider);
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988
1989 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08001990 public LocationWorkerHandler(Looper looper) {
1991 super(looper, null, true);
1992 }
1993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 @Override
1995 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001996 switch (msg.what) {
1997 case MSG_LOCATION_CHANGED:
1998 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
1999 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001 }
2002 }
2003
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002004 private boolean isMockProvider(String provider) {
2005 synchronized (mLock) {
2006 return mMockProviders.containsKey(provider);
2007 }
2008 }
2009
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002010 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002011 // create a working copy of the incoming Location so that the service can modify it without
2012 // disturbing the caller's copy
2013 Location myLocation = new Location(location);
2014 String provider = myLocation.getProvider();
2015
2016 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2017 // bit if location did not come from a mock provider because passive/fused providers can
2018 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2019 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2020 myLocation.setIsFromMockProvider(true);
2021 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002022
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002023 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002024 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2025 if (!passive) {
2026 // notify passive provider of the new location
2027 mPassiveProvider.updateLocation(myLocation);
2028 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002029 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033
Mike Lockwoode97ae402010-09-29 15:23:46 -04002034 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2035 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002036 public void onPackageDisappeared(String packageName, int reason) {
2037 // remove all receivers associated with this package name
2038 synchronized (mLock) {
2039 ArrayList<Receiver> deadReceivers = null;
2040
2041 for (Receiver receiver : mReceivers.values()) {
2042 if (receiver.mPackageName.equals(packageName)) {
2043 if (deadReceivers == null) {
2044 deadReceivers = new ArrayList<Receiver>();
2045 }
2046 deadReceivers.add(receiver);
2047 }
2048 }
2049
2050 // perform removal outside of mReceivers loop
2051 if (deadReceivers != null) {
2052 for (Receiver receiver : deadReceivers) {
2053 removeUpdatesLocked(receiver);
2054 }
2055 }
2056 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002057 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002058 };
2059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 // Geocoder
2061
Nick Pellye0fd6932012-07-11 10:26:13 -07002062 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002063 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002064 return mGeocodeProvider != null;
2065 }
2066
Nick Pellye0fd6932012-07-11 10:26:13 -07002067 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002069 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002070 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002071 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2072 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002074 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076
Mike Lockwooda55c3212009-04-15 11:10:11 -04002077
Nick Pellye0fd6932012-07-11 10:26:13 -07002078 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002080 double lowerLeftLatitude, double lowerLeftLongitude,
2081 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002082 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002083
2084 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002085 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2086 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2087 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002089 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091
2092 // Mock Providers
2093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 private void checkMockPermissionsSafe() {
2095 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2096 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2097 if (!allowMocks) {
2098 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2099 }
2100
2101 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2102 PackageManager.PERMISSION_GRANTED) {
2103 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
2106
Nick Pellye0fd6932012-07-11 10:26:13 -07002107 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002108 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 checkMockPermissionsSafe();
2110
Mike Lockwooda4903f22010-02-17 06:42:23 -05002111 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2112 throw new IllegalArgumentException("Cannot mock the passive location provider");
2113 }
2114
Mike Lockwood86328a92009-10-23 08:38:25 -04002115 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002116 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002117 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002118 // remove the real provider if we are replacing GPS or network provider
2119 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002120 || LocationManager.NETWORK_PROVIDER.equals(name)
2121 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002122 LocationProviderInterface p = mProvidersByName.get(name);
2123 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002124 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002125 }
2126 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002127 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2129 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002130 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002131 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002132 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002133 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 updateProvidersLocked();
2135 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002136 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
2138
Nick Pellye0fd6932012-07-11 10:26:13 -07002139 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 public void removeTestProvider(String provider) {
2141 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002142 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002143 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002144 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2146 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002147 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002148 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002149
2150 // reinstate real provider if available
2151 LocationProviderInterface realProvider = mRealProviders.get(provider);
2152 if (realProvider != null) {
2153 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002154 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002155 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002156 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002158 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
2160 }
2161
Nick Pellye0fd6932012-07-11 10:26:13 -07002162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 public void setTestProviderLocation(String provider, Location loc) {
2164 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002165 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002166 MockProvider mockProvider = mMockProviders.get(provider);
2167 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2169 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002170 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2171 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002172 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002173 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175 }
2176
Nick Pellye0fd6932012-07-11 10:26:13 -07002177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 public void clearTestProviderLocation(String provider) {
2179 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002180 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002181 MockProvider mockProvider = mMockProviders.get(provider);
2182 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2184 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002185 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
2187 }
2188
Nick Pellye0fd6932012-07-11 10:26:13 -07002189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 public void setTestProviderEnabled(String provider, boolean enabled) {
2191 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002192 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002193 MockProvider mockProvider = mMockProviders.get(provider);
2194 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2196 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002197 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002199 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 mEnabledProviders.add(provider);
2201 mDisabledProviders.remove(provider);
2202 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002203 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 mEnabledProviders.remove(provider);
2205 mDisabledProviders.add(provider);
2206 }
2207 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002208 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
2210 }
2211
Nick Pellye0fd6932012-07-11 10:26:13 -07002212 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 public void clearTestProviderEnabled(String provider) {
2214 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002215 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002216 MockProvider mockProvider = mMockProviders.get(provider);
2217 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2219 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002220 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 mEnabledProviders.remove(provider);
2222 mDisabledProviders.remove(provider);
2223 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002224 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 }
2226 }
2227
Nick Pellye0fd6932012-07-11 10:26:13 -07002228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2230 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002231 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002232 MockProvider mockProvider = mMockProviders.get(provider);
2233 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2235 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002236 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
2238 }
2239
Nick Pellye0fd6932012-07-11 10:26:13 -07002240 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 public void clearTestProviderStatus(String provider) {
2242 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002243 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002244 MockProvider mockProvider = mMockProviders.get(provider);
2245 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2247 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002248 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 }
2251
2252 private void log(String log) {
2253 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002254 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
2256 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002257
2258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2260 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2261 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002262 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 + Binder.getCallingPid()
2264 + ", uid=" + Binder.getCallingUid());
2265 return;
2266 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002267
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002268 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002271 for (Receiver receiver : mReceivers.values()) {
2272 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002275 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2276 pw.println(" " + entry.getKey() + ":");
2277 for (UpdateRecord record : entry.getValue()) {
2278 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
2280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002282 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2283 String provider = entry.getKey();
2284 Location location = entry.getValue();
2285 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002287
David Christie1b9b7b12013-04-15 15:31:11 -07002288 pw.println(" Last Known Locations Coarse Intervals:");
2289 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2290 String provider = entry.getKey();
2291 Location location = entry.getValue();
2292 pw.println(" " + provider + ": " + location);
2293 }
2294
Nick Pellye0fd6932012-07-11 10:26:13 -07002295 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 if (mEnabledProviders.size() > 0) {
2298 pw.println(" Enabled Providers:");
2299 for (String i : mEnabledProviders) {
2300 pw.println(" " + i);
2301 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304 if (mDisabledProviders.size() > 0) {
2305 pw.println(" Disabled Providers:");
2306 for (String i : mDisabledProviders) {
2307 pw.println(" " + i);
2308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002310 pw.append(" ");
2311 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 if (mMockProviders.size() > 0) {
2313 pw.println(" Mock Providers:");
2314 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002315 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
2317 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002318
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002319 pw.append(" fudger: ");
2320 mLocationFudger.dump(fd, pw, args);
2321
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002322 if (args.length > 0 && "short".equals(args[0])) {
2323 return;
2324 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002325 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002326 pw.print(provider.getName() + " Internal State");
2327 if (provider instanceof LocationProviderProxy) {
2328 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2329 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002330 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002331 pw.println(":");
2332 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
2335 }
2336}