blob: cde84dc0c3e39b10ca49ebf0144b47247c0f91c7 [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.
David Christie40e57822013-07-30 11:36:48 -0700465 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700467
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400468 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700469
David Christie0b837452013-07-29 16:02:13 -0700470 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700471 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700472 // True if app ops has started monitoring this receiver for high power (gps) locations.
473 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700474 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700475 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700477 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700478 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700481 if (listener != null) {
482 mKey = listener.asBinder();
483 } else {
484 mKey = intent;
485 }
Victoria Lease37425c32012-10-16 16:08:48 -0700486 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700487 mUid = uid;
488 mPid = pid;
489 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700490 if (workSource != null && workSource.size() <= 0) {
491 workSource = null;
492 }
493 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700494 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700495
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700496 updateMonitoring(true);
497
Victoria Lease0aa28602013-05-29 15:28:26 -0700498 // construct/configure wakelock
499 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700500 if (workSource == null) {
501 workSource = new WorkSource(mUid, mPackageName);
502 }
503 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
506 @Override
507 public boolean equals(Object otherObj) {
508 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700509 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 return false;
512 }
513
514 @Override
515 public int hashCode() {
516 return mKey.hashCode();
517 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 @Override
520 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700521 StringBuilder s = new StringBuilder();
522 s.append("Reciever[");
523 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700525 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700527 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700529 for (String p : mUpdateRecords.keySet()) {
530 s.append(" ").append(mUpdateRecords.get(p).toString());
531 }
532 s.append("]");
533 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700536 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700537 if (mHideFromAppOps) {
538 return;
539 }
540
David Christie0b837452013-07-29 16:02:13 -0700541 // First update monitoring of any location request (including high power).
542 mOpMonitoring = updateMonitoring(allow, mOpMonitoring,
543 AppOpsManager.OP_MONITOR_LOCATION);
544
545 // Now update monitoring of high power requests only.
546 // A high power request is any gps request with interval under a threshold.
547 boolean allowHighPower = allow;
548 if (allowHighPower) {
549 UpdateRecord gpsRecord = mUpdateRecords.get(LocationManager.GPS_PROVIDER);
550 if (gpsRecord == null
551 || gpsRecord.mRequest.getInterval() > HIGH_POWER_INTERVAL_MS) {
552 allowHighPower = false;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700553 }
554 }
David Christie0b837452013-07-29 16:02:13 -0700555 mOpHighPowerMonitoring = updateMonitoring(allowHighPower, mOpHighPowerMonitoring,
556 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
557 }
558
559 /**
560 * Update AppOps monitoring for a single location request and op type.
561 *
562 * @param allowMonitoring True if monitoring is allowed for this request/op.
563 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
564 * @param op AppOps code for the op to update.
565 * @return True if monitoring is on for this request/op after updating.
566 */
567 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
568 int op) {
569 if (!currentlyMonitoring) {
570 if (allowMonitoring) {
571 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
572 == AppOpsManager.MODE_ALLOWED;
573 }
574 } else {
575 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
576 != AppOpsManager.MODE_ALLOWED) {
577 mAppOps.finishOp(op, mUid, mPackageName);
578 return false;
579 }
580 }
581
582 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700583 }
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 public boolean isListener() {
586 return mListener != null;
587 }
588
589 public boolean isPendingIntent() {
590 return mPendingIntent != null;
591 }
592
593 public ILocationListener getListener() {
594 if (mListener != null) {
595 return mListener;
596 }
597 throw new IllegalStateException("Request for non-existent listener");
598 }
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
601 if (mListener != null) {
602 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700603 synchronized (this) {
604 // synchronize to ensure incrementPendingBroadcastsLocked()
605 // is called before decrementPendingBroadcasts()
606 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700607 // call this after broadcasting so we do not increment
608 // if we throw an exeption.
609 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 } catch (RemoteException e) {
612 return false;
613 }
614 } else {
615 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800616 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
618 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700619 synchronized (this) {
620 // synchronize to ensure incrementPendingBroadcastsLocked()
621 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700622 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700623 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700624 // call this after broadcasting so we do not increment
625 // if we throw an exeption.
626 incrementPendingBroadcastsLocked();
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 } catch (PendingIntent.CanceledException e) {
629 return false;
630 }
631 }
632 return true;
633 }
634
635 public boolean callLocationChangedLocked(Location location) {
636 if (mListener != null) {
637 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700638 synchronized (this) {
639 // synchronize to ensure incrementPendingBroadcastsLocked()
640 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800641 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700642 // call this after broadcasting so we do not increment
643 // if we throw an exeption.
644 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 } catch (RemoteException e) {
647 return false;
648 }
649 } else {
650 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800651 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700653 synchronized (this) {
654 // synchronize to ensure incrementPendingBroadcastsLocked()
655 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700656 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700657 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700658 // call this after broadcasting so we do not increment
659 // if we throw an exeption.
660 incrementPendingBroadcastsLocked();
661 }
662 } catch (PendingIntent.CanceledException e) {
663 return false;
664 }
665 }
666 return true;
667 }
668
669 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
670 if (mListener != null) {
671 try {
672 synchronized (this) {
673 // synchronize to ensure incrementPendingBroadcastsLocked()
674 // is called before decrementPendingBroadcasts()
675 if (enabled) {
676 mListener.onProviderEnabled(provider);
677 } else {
678 mListener.onProviderDisabled(provider);
679 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700680 // call this after broadcasting so we do not increment
681 // if we throw an exeption.
682 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700683 }
684 } catch (RemoteException e) {
685 return false;
686 }
687 } else {
688 Intent providerIntent = new Intent();
689 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
690 try {
691 synchronized (this) {
692 // synchronize to ensure incrementPendingBroadcastsLocked()
693 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700694 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700695 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700696 // call this after broadcasting so we do not increment
697 // if we throw an exeption.
698 incrementPendingBroadcastsLocked();
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 } catch (PendingIntent.CanceledException e) {
701 return false;
702 }
703 }
704 return true;
705 }
706
Nick Pellyf1be6862012-05-15 10:53:42 -0700707 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700709 if (D) Log.d(TAG, "Location listener died");
710
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400711 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 removeUpdatesLocked(this);
713 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700714 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700715 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700716 }
717 }
718
Nick Pellye0fd6932012-07-11 10:26:13 -0700719 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700720 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
721 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400722 synchronized (this) {
723 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700724 }
725 }
726
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400727 // this must be called while synchronized by caller in a synchronized block
728 // containing the sending of the broadcaset
729 private void incrementPendingBroadcastsLocked() {
730 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700731 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400732 }
733 }
734
735 private void decrementPendingBroadcastsLocked() {
736 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700737 if (mWakeLock.isHeld()) {
738 mWakeLock.release();
739 }
740 }
741 }
742
743 public void clearPendingBroadcastsLocked() {
744 if (mPendingBroadcasts > 0) {
745 mPendingBroadcasts = 0;
746 if (mWakeLock.isHeld()) {
747 mWakeLock.release();
748 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700749 }
750 }
751 }
752
Nick Pellye0fd6932012-07-11 10:26:13 -0700753 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700754 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700755 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400756 //the receiver list if it is not found. If it is not found then the
757 //LocationListener was removed when it had a pending broadcast and should
758 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700759 synchronized (mLock) {
760 IBinder binder = listener.asBinder();
761 Receiver receiver = mReceivers.get(binder);
762 if (receiver != null) {
763 synchronized (receiver) {
764 // so wakelock calls will succeed
765 long identity = Binder.clearCallingIdentity();
766 receiver.decrementPendingBroadcastsLocked();
767 Binder.restoreCallingIdentity(identity);
768 }
769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 }
772
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700773 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400774 mProviders.add(provider);
775 mProvidersByName.put(provider.getName(), provider);
776 }
777
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700778 private void removeProviderLocked(LocationProviderInterface provider) {
779 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400780 mProviders.remove(provider);
781 mProvidersByName.remove(provider.getName());
782 }
783
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800784 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800785 * Returns "true" if access to the specified location provider is allowed by the current
786 * user's settings. Access to all location providers is forbidden to non-location-provider
787 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800788 *
789 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800790 * @return
791 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800792 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 if (mEnabledProviders.contains(provider)) {
794 return true;
795 }
796 if (mDisabledProviders.contains(provider)) {
797 return false;
798 }
799 // Use system settings
800 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801
Victoria Leaseb711d572012-10-02 13:14:11 -0700802 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700805 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800806 * Returns "true" if access to the specified location provider is allowed by the specified
807 * user's settings. Access to all location providers is forbidden to non-location-provider
808 * processes belonging to background users.
809 *
810 * @param provider the name of the location provider
811 * @param uid the requestor's UID
812 * @return
813 */
814 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
815 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
816 return false;
817 }
818 return isAllowedByCurrentUserSettingsLocked(provider);
819 }
820
821 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700822 * Returns the permission string associated with the specified resolution level.
823 *
824 * @param resolutionLevel the resolution level
825 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700826 */
Victoria Lease37425c32012-10-16 16:08:48 -0700827 private String getResolutionPermission(int resolutionLevel) {
828 switch (resolutionLevel) {
829 case RESOLUTION_LEVEL_FINE:
830 return android.Manifest.permission.ACCESS_FINE_LOCATION;
831 case RESOLUTION_LEVEL_COARSE:
832 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
833 default:
834 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700836 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700837
Victoria Leaseda479c52012-10-15 15:24:16 -0700838 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700839 * Returns the resolution level allowed to the given PID/UID pair.
840 *
841 * @param pid the PID
842 * @param uid the UID
843 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700844 */
Victoria Lease37425c32012-10-16 16:08:48 -0700845 private int getAllowedResolutionLevel(int pid, int uid) {
846 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
847 pid, uid) == PackageManager.PERMISSION_GRANTED) {
848 return RESOLUTION_LEVEL_FINE;
849 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
850 pid, uid) == PackageManager.PERMISSION_GRANTED) {
851 return RESOLUTION_LEVEL_COARSE;
852 } else {
853 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700854 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700855 }
856
857 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700858 * Returns the resolution level allowed to the caller
859 *
860 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700861 */
Victoria Lease37425c32012-10-16 16:08:48 -0700862 private int getCallerAllowedResolutionLevel() {
863 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
864 }
865
866 /**
867 * Throw SecurityException if specified resolution level is insufficient to use geofences.
868 *
869 * @param allowedResolutionLevel resolution level allowed to caller
870 */
871 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
872 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700873 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876
Victoria Lease37425c32012-10-16 16:08:48 -0700877 /**
878 * Return the minimum resolution level required to use the specified location provider.
879 *
880 * @param provider the name of the location provider
881 * @return minimum resolution level required for provider
882 */
883 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700884 if (LocationManager.GPS_PROVIDER.equals(provider) ||
885 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
886 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700887 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700888 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
889 LocationManager.FUSED_PROVIDER.equals(provider)) {
890 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700891 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700892 } else {
893 // mock providers
894 LocationProviderInterface lp = mMockProviders.get(provider);
895 if (lp != null) {
896 ProviderProperties properties = lp.getProperties();
897 if (properties != null) {
898 if (properties.mRequiresSatellite) {
899 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700900 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700901 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
902 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700903 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700904 }
905 }
906 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700907 }
Victoria Lease37425c32012-10-16 16:08:48 -0700908 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700909 }
910
Victoria Lease37425c32012-10-16 16:08:48 -0700911 /**
912 * Throw SecurityException if specified resolution level is insufficient to use the named
913 * location provider.
914 *
915 * @param allowedResolutionLevel resolution level allowed to caller
916 * @param providerName the name of the location provider
917 */
918 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
919 String providerName) {
920 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
921 if (allowedResolutionLevel < requiredResolutionLevel) {
922 switch (requiredResolutionLevel) {
923 case RESOLUTION_LEVEL_FINE:
924 throw new SecurityException("\"" + providerName + "\" location provider " +
925 "requires ACCESS_FINE_LOCATION permission.");
926 case RESOLUTION_LEVEL_COARSE:
927 throw new SecurityException("\"" + providerName + "\" location provider " +
928 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
929 default:
930 throw new SecurityException("Insufficient permission for \"" + providerName +
931 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700932 }
933 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700934 }
935
David Christie82edc9b2013-07-19 11:31:42 -0700936 /**
937 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
938 * for battery).
939 */
David Christie40e57822013-07-30 11:36:48 -0700940 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -0700941 mContext.enforceCallingOrSelfPermission(
942 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
943 }
944
David Christie40e57822013-07-30 11:36:48 -0700945 private void checkUpdateAppOpsAllowed() {
946 mContext.enforceCallingOrSelfPermission(
947 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
948 }
949
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800950 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800951 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
952 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800953 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800954 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800955 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800956 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800957 }
958 return -1;
959 }
960
961 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
962 int op = resolutionLevelToOp(allowedResolutionLevel);
963 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800964 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
965 return false;
966 }
967 }
968 return true;
969 }
970
971 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800972 int op = resolutionLevelToOp(allowedResolutionLevel);
973 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800974 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
975 return false;
976 }
977 }
978 return true;
979 }
980
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700981 /**
982 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700983 * fused, also including ones that are not permitted to
984 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700985 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700986 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700988 ArrayList<String> out;
989 synchronized (mLock) {
990 out = new ArrayList<String>(mProviders.size());
991 for (LocationProviderInterface provider : mProviders) {
992 String name = provider.getName();
993 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700994 continue;
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 out.add(name);
997 }
998 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700999
1000 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 return out;
1002 }
1003
Mike Lockwood03ca2162010-04-01 08:10:09 -07001004 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001005 * Return all providers by name, that match criteria and are optionally
1006 * enabled.
1007 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001008 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001009 @Override
1010 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001011 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001012 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001013 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001014 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001015 try {
1016 synchronized (mLock) {
1017 out = new ArrayList<String>(mProviders.size());
1018 for (LocationProviderInterface provider : mProviders) {
1019 String name = provider.getName();
1020 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001021 continue;
1022 }
Victoria Lease37425c32012-10-16 16:08:48 -07001023 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001024 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001025 continue;
1026 }
1027 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1028 name, provider.getProperties(), criteria)) {
1029 continue;
1030 }
1031 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001032 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001033 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001034 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001035 } finally {
1036 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001037 }
1038
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001039 if (D) Log.d(TAG, "getProviders()=" + out);
1040 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001041 }
1042
1043 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001044 * Return the name of the best provider given a Criteria object.
1045 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001046 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001047 * has been deprecated as well. So this method now uses
1048 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001049 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001050 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001051 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001052 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001053
1054 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001055 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001056 result = pickBest(providers);
1057 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1058 return result;
1059 }
1060 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001061 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001062 result = pickBest(providers);
1063 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1064 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001065 }
1066
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001067 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001068 return null;
1069 }
1070
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001071 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001072 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001073 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001074 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1075 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001076 } else {
1077 return providers.get(0);
1078 }
1079 }
1080
Nick Pellye0fd6932012-07-11 10:26:13 -07001081 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001082 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1083 LocationProviderInterface p = mProvidersByName.get(provider);
1084 if (p == null) {
1085 throw new IllegalArgumentException("provider=" + provider);
1086 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001087
1088 boolean result = LocationProvider.propertiesMeetCriteria(
1089 p.getName(), p.getProperties(), criteria);
1090 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1091 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001092 }
1093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001095 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001096 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001097 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 boolean isEnabled = p.isEnabled();
1099 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001100 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001102 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001103 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001105 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001106 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001108 }
1109 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001110 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1111 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113 }
1114
Victoria Leaseb711d572012-10-02 13:14:11 -07001115 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 int listeners = 0;
1117
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001118 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001119 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120
1121 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1124 if (records != null) {
1125 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001126 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001128 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001129 // Sends a notification message to the receiver
1130 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1131 if (deadReceivers == null) {
1132 deadReceivers = new ArrayList<Receiver>();
1133 }
1134 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001136 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 }
1140
1141 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001142 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 removeUpdatesLocked(deadReceivers.get(i));
1144 }
1145 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 if (enabled) {
1148 p.enable();
1149 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001150 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001157 private void applyRequirementsLocked(String provider) {
1158 LocationProviderInterface p = mProvidersByName.get(provider);
1159 if (p == null) return;
1160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001162 WorkSource worksource = new WorkSource();
1163 ProviderRequest providerRequest = new ProviderRequest();
1164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001166 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001167 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001168 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1169 record.mReceiver.mAllowedResolutionLevel)) {
1170 LocationRequest locationRequest = record.mRequest;
1171 providerRequest.locationRequests.add(locationRequest);
1172 if (locationRequest.getInterval() < providerRequest.interval) {
1173 providerRequest.reportLocation = true;
1174 providerRequest.interval = locationRequest.getInterval();
1175 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001176 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001177 }
1178 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001179
1180 if (providerRequest.reportLocation) {
1181 // calculate who to blame for power
1182 // This is somewhat arbitrary. We pick a threshold interval
1183 // that is slightly higher that the minimum interval, and
1184 // spread the blame across all applications with a request
1185 // under that threshold.
1186 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1187 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001188 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001189 LocationRequest locationRequest = record.mRequest;
1190 if (locationRequest.getInterval() <= thresholdInterval) {
David Christie82edc9b2013-07-19 11:31:42 -07001191 if (record.mReceiver.mWorkSource != null) {
1192 // Assign blame to another work source.
1193 worksource.add(record.mReceiver.mWorkSource);
1194 } else {
1195 // Assign blame to caller.
1196 worksource.add(
1197 record.mReceiver.mUid,
1198 record.mReceiver.mPackageName);
1199 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001200 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001201 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001205
1206 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1207 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
1209
1210 private class UpdateRecord {
1211 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001212 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001214 Location mLastFixBroadcast;
1215 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216
1217 /**
1218 * Note: must be constructed with lock held.
1219 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001220 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001222 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224
1225 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1226 if (records == null) {
1227 records = new ArrayList<UpdateRecord>();
1228 mRecordsByProvider.put(provider, records);
1229 }
1230 if (!records.contains(this)) {
1231 records.add(this);
1232 }
1233 }
1234
1235 /**
1236 * Method to be called when a record will no longer be used. Calling this multiple times
1237 * must have the same effect as calling it once.
1238 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001239 void disposeLocked(boolean removeReceiver) {
1240 // remove from mRecordsByProvider
1241 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1242 if (globalRecords != null) {
1243 globalRecords.remove(this);
1244 }
1245
1246 if (!removeReceiver) return; // the caller will handle the rest
1247
1248 // remove from Receiver#mUpdateRecords
1249 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1250 if (receiverRecords != null) {
1251 receiverRecords.remove(this.mProvider);
1252
1253 // and also remove the Receiver if it has no more update records
1254 if (removeReceiver && receiverRecords.size() == 0) {
1255 removeUpdatesLocked(mReceiver);
1256 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
1259
1260 @Override
1261 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001262 StringBuilder s = new StringBuilder();
1263 s.append("UpdateRecord[");
1264 s.append(mProvider);
1265 s.append(' ').append(mReceiver.mPackageName).append('(');
1266 s.append(mReceiver.mUid).append(')');
1267 s.append(' ').append(mRequest);
1268 s.append(']');
1269 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001273 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001274 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001275 IBinder binder = listener.asBinder();
1276 Receiver receiver = mReceivers.get(binder);
1277 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001278 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1279 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001280 mReceivers.put(binder, receiver);
1281
1282 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001283 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001284 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001286 return null;
1287 }
1288 }
1289 return receiver;
1290 }
1291
David Christie82edc9b2013-07-19 11:31:42 -07001292 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001293 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001294 Receiver receiver = mReceivers.get(intent);
1295 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001296 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1297 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001298 mReceivers.put(intent, receiver);
1299 }
1300 return receiver;
1301 }
1302
Victoria Lease37425c32012-10-16 16:08:48 -07001303 /**
1304 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1305 * and consistency requirements.
1306 *
1307 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001308 * @return a version of request that meets the given resolution and consistency requirements
1309 * @hide
1310 */
1311 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1312 LocationRequest sanitizedRequest = new LocationRequest(request);
1313 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1314 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001315 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001316 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001317 break;
1318 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001319 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001320 break;
1321 }
1322 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001323 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1324 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001325 }
Victoria Lease37425c32012-10-16 16:08:48 -07001326 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1327 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001328 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001329 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001330 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001331 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001332 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001333 }
Victoria Lease37425c32012-10-16 16:08:48 -07001334 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001335 }
1336
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001337 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001338 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001339 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001340 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001341 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001342 String[] packages = mPackageManager.getPackagesForUid(uid);
1343 if (packages == null) {
1344 throw new SecurityException("invalid UID " + uid);
1345 }
1346 for (String pkg : packages) {
1347 if (packageName.equals(pkg)) return;
1348 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001349 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001350 }
1351
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001352 private void checkPendingIntent(PendingIntent intent) {
1353 if (intent == null) {
1354 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001355 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001356 }
1357
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001358 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001359 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001360 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001361 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001362 } else if (intent != null && listener != null) {
1363 throw new IllegalArgumentException("cannot register both listener and intent");
1364 } else if (intent != null) {
1365 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001366 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001367 } else {
David Christie40e57822013-07-30 11:36:48 -07001368 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001369 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001370 }
1371
Nick Pellye0fd6932012-07-11 10:26:13 -07001372 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001373 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1374 PendingIntent intent, String packageName) {
1375 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1376 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001377 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1378 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1379 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001380 WorkSource workSource = request.getWorkSource();
1381 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001382 checkDeviceStatsAllowed();
1383 }
1384 boolean hideFromAppOps = request.getHideFromAppOps();
1385 if (hideFromAppOps) {
1386 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001387 }
Victoria Lease37425c32012-10-16 16:08:48 -07001388 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001390 final int pid = Binder.getCallingPid();
1391 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001392 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 long identity = Binder.clearCallingIdentity();
1394 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001395 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1396 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001397 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001398
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001399 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001400 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001401 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001402 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 } finally {
1405 Binder.restoreCallingIdentity(identity);
1406 }
1407 }
1408
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001409 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1410 int pid, int uid, String packageName) {
1411 // Figure out the provider. Either its explicitly request (legacy use cases), or
1412 // use the fused provider
1413 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1414 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001415 if (name == null) {
1416 throw new IllegalArgumentException("provider name must not be null");
1417 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001418
1419 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1420 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001421 LocationProviderInterface provider = mProvidersByName.get(name);
1422 if (provider == null) {
1423 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1424 }
1425
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001426 UpdateRecord record = new UpdateRecord(name, request, receiver);
1427 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1428 if (oldRecord != null) {
1429 oldRecord.disposeLocked(false);
1430 }
1431
Victoria Lease09eeaec2013-02-05 11:34:13 -08001432 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001433 if (isProviderEnabled) {
1434 applyRequirementsLocked(name);
1435 } else {
1436 // Notify the listener that updates are currently disabled
1437 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
David Christie0b837452013-07-29 16:02:13 -07001439 // Update the monitoring here just in case multiple location requests were added to the
1440 // same receiver (this request may be high power and the initial might not have been).
1441 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
1443
Nick Pellye0fd6932012-07-11 10:26:13 -07001444 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001445 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1446 String packageName) {
1447 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001448
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001449 final int pid = Binder.getCallingPid();
1450 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001451
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001452 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001453 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001454 boolean hideFromAppOps = false;
1455 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1456 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001457
1458 // providers may use public location API's, need to clear identity
1459 long identity = Binder.clearCallingIdentity();
1460 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001461 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001462 } finally {
1463 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
1467
1468 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001469 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001470
1471 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1472 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1473 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001474 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
1477
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001478 receiver.updateMonitoring(false);
1479
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001480 // Record which providers were associated with this listener
1481 HashSet<String> providers = new HashSet<String>();
1482 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1483 if (oldRecords != null) {
1484 // Call dispose() on the obsolete update records.
1485 for (UpdateRecord record : oldRecords.values()) {
1486 record.disposeLocked(false);
1487 }
1488 // Accumulate providers
1489 providers.addAll(oldRecords.keySet());
1490 }
1491
1492 // update provider
1493 for (String provider : providers) {
1494 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001495 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001496 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
1498
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001499 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
1501 }
1502
Dianne Hackbornc2293022013-02-06 23:14:49 -08001503 private void applyAllProviderRequirementsLocked() {
1504 for (LocationProviderInterface p : mProviders) {
1505 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001506 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001507 continue;
1508 }
1509
1510 applyRequirementsLocked(p.getName());
1511 }
1512 }
1513
Nick Pellye0fd6932012-07-11 10:26:13 -07001514 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001515 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001516 if (D) Log.d(TAG, "getLastLocation: " + request);
1517 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001518 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001519 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001520 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1521 request.getProvider());
1522 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001523
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001524 final int uid = Binder.getCallingUid();
1525 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001526 try {
1527 if (mBlacklist.isBlacklisted(packageName)) {
1528 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1529 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001530 return null;
1531 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001532
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001533 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1534 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1535 packageName);
1536 return null;
1537 }
1538
Victoria Leaseb711d572012-10-02 13:14:11 -07001539 synchronized (mLock) {
1540 // Figure out the provider. Either its explicitly request (deprecated API's),
1541 // or use the fused provider
1542 String name = request.getProvider();
1543 if (name == null) name = LocationManager.FUSED_PROVIDER;
1544 LocationProviderInterface provider = mProvidersByName.get(name);
1545 if (provider == null) return null;
1546
Victoria Lease09eeaec2013-02-05 11:34:13 -08001547 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001548
David Christie1b9b7b12013-04-15 15:31:11 -07001549 Location location;
1550 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1551 // Make sure that an app with coarse permissions can't get frequent location
1552 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1553 location = mLastLocationCoarseInterval.get(name);
1554 } else {
1555 location = mLastLocation.get(name);
1556 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001557 if (location == null) {
1558 return null;
1559 }
Victoria Lease37425c32012-10-16 16:08:48 -07001560 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001561 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1562 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001563 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001564 }
Victoria Lease37425c32012-10-16 16:08:48 -07001565 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001566 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001567 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001568 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001569 return null;
1570 } finally {
1571 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001572 }
1573 }
1574
1575 @Override
1576 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1577 String packageName) {
1578 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001579 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1580 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001581 checkPendingIntent(intent);
1582 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001583 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1584 request.getProvider());
1585 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001586
Victoria Lease37425c32012-10-16 16:08:48 -07001587 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001588
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001589 // geo-fence manager uses the public location API, need to clear identity
1590 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001591 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1592 // temporary measure until geofences work for secondary users
1593 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1594 return;
1595 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001596 long identity = Binder.clearCallingIdentity();
1597 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001598 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1599 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001600 } finally {
1601 Binder.restoreCallingIdentity(identity);
1602 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001603 }
1604
1605 @Override
1606 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001607 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001608 checkPendingIntent(intent);
1609 checkPackageName(packageName);
1610
1611 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1612
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001613 // geo-fence manager uses the public location API, need to clear identity
1614 long identity = Binder.clearCallingIdentity();
1615 try {
1616 mGeofenceManager.removeFence(geofence, intent);
1617 } finally {
1618 Binder.restoreCallingIdentity(identity);
1619 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001620 }
1621
1622
1623 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001624 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001625 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 return false;
1627 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001628 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1629 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001630 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001632 final int uid = Binder.getCallingUid();
1633 final long ident = Binder.clearCallingIdentity();
1634 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001635 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001636 return false;
1637 }
1638 } finally {
1639 Binder.restoreCallingIdentity(ident);
1640 }
1641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001643 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001645 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 return false;
1647 }
1648 return true;
1649 }
1650
Nick Pellye0fd6932012-07-11 10:26:13 -07001651 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001653 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001654 try {
1655 mGpsStatusProvider.removeGpsStatusListener(listener);
1656 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001657 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 }
1661
Nick Pellye0fd6932012-07-11 10:26:13 -07001662 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001664 if (provider == null) {
1665 // throw NullPointerException to remain compatible with previous implementation
1666 throw new NullPointerException();
1667 }
Victoria Lease37425c32012-10-16 16:08:48 -07001668 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1669 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001672 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 != PackageManager.PERMISSION_GRANTED)) {
1674 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1675 }
1676
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001677 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001678 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001679 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001680
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001681 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 }
1684
Nick Pellye0fd6932012-07-11 10:26:13 -07001685 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001686 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001687 if (Binder.getCallingUid() != Process.myUid()) {
1688 throw new SecurityException(
1689 "calling sendNiResponse from outside of the system is not allowed");
1690 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001691 try {
1692 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001693 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001694 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001695 return false;
1696 }
1697 }
1698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001700 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001701 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 * accessed by the caller
1703 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001704 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001705 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001706 if (mProvidersByName.get(provider) == null) {
1707 return null;
1708 }
1709
Victoria Lease37425c32012-10-16 16:08:48 -07001710 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1711 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001713 LocationProviderInterface p;
1714 synchronized (mLock) {
1715 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001718 if (p == null) return null;
1719 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
1721
Nick Pellye0fd6932012-07-11 10:26:13 -07001722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001724 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1725 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001726 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1727
Victoria Lease09eeaec2013-02-05 11:34:13 -08001728 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001729 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001730 try {
1731 synchronized (mLock) {
1732 LocationProviderInterface p = mProvidersByName.get(provider);
1733 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001734
Victoria Lease09eeaec2013-02-05 11:34:13 -08001735 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001736 }
1737 } finally {
1738 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001739 }
1740 }
1741
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001742 /**
1743 * Returns "true" if the UID belongs to a bound location provider.
1744 *
1745 * @param uid the uid
1746 * @return true if uid belongs to a bound location provider
1747 */
1748 private boolean isUidALocationProvider(int uid) {
1749 if (uid == Process.SYSTEM_UID) {
1750 return true;
1751 }
1752 if (mGeocodeProvider != null) {
1753 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1754 }
1755 for (LocationProviderProxy proxy : mProxyProviders) {
1756 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1757 }
1758 return false;
1759 }
1760
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001761 private void checkCallerIsProvider() {
1762 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1763 == PackageManager.PERMISSION_GRANTED) {
1764 return;
1765 }
1766
1767 // Previously we only used the INSTALL_LOCATION_PROVIDER
1768 // check. But that is system or signature
1769 // protection level which is not flexible enough for
1770 // providers installed oustide the system image. So
1771 // also allow providers with a UID matching the
1772 // currently bound package name
1773
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001774 if (isUidALocationProvider(Binder.getCallingUid())) {
1775 return;
1776 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001777
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001778 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1779 "or UID of a currently bound location provider");
1780 }
1781
1782 private boolean doesPackageHaveUid(int uid, String packageName) {
1783 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 return false;
1785 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001786 try {
1787 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1788 if (appInfo.uid != uid) {
1789 return false;
1790 }
1791 } catch (NameNotFoundException e) {
1792 return false;
1793 }
1794 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796
Nick Pellye0fd6932012-07-11 10:26:13 -07001797 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001798 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001799 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001800
Nick Pelly2eeeec22012-07-18 13:13:37 -07001801 if (!location.isComplete()) {
1802 Log.w(TAG, "Dropping incomplete location: " + location);
1803 return;
1804 }
1805
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001806 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1807 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001808 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001809 mLocationHandler.sendMessageAtFrontOfQueue(m);
1810 }
1811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812
Laurent Tu75defb62012-11-01 16:21:52 -07001813 private static boolean shouldBroadcastSafe(
1814 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 // Always broadcast the first update
1816 if (lastLoc == null) {
1817 return true;
1818 }
1819
Nick Pellyf1be6862012-05-15 10:53:42 -07001820 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001821 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001822 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1823 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001824 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 return false;
1826 }
1827
1828 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001829 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 if (minDistance > 0.0) {
1831 if (loc.distanceTo(lastLoc) <= minDistance) {
1832 return false;
1833 }
1834 }
1835
Laurent Tu75defb62012-11-01 16:21:52 -07001836 // Check whether sufficient number of udpates is left
1837 if (record.mRequest.getNumUpdates() <= 0) {
1838 return false;
1839 }
1840
1841 // Check whether the expiry date has passed
1842 if (record.mRequest.getExpireAt() < now) {
1843 return false;
1844 }
1845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 return true;
1847 }
1848
Mike Lockwooda4903f22010-02-17 06:42:23 -05001849 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001850 if (D) Log.d(TAG, "incoming location: " + location);
1851
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001852 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001853 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854
Laurent Tu60ec50a2012-10-04 17:00:10 -07001855 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001856 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001857 if (p == null) return;
1858
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001859 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001860 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1861 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001862 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001863 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001864 lastLocation = new Location(provider);
1865 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001866 } else {
1867 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1868 if (noGPSLocation == null && lastNoGPSLocation != null) {
1869 // New location has no no-GPS location: adopt last no-GPS location. This is set
1870 // directly into location because we do not want to notify COARSE clients.
1871 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1872 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001873 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001874 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875
David Christie1b9b7b12013-04-15 15:31:11 -07001876 // Update last known coarse interval location if enough time has passed.
1877 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1878 if (lastLocationCoarseInterval == null) {
1879 lastLocationCoarseInterval = new Location(location);
1880 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1881 }
1882 long timeDiffNanos = location.getElapsedRealtimeNanos()
1883 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1884 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1885 lastLocationCoarseInterval.set(location);
1886 }
1887 // Don't ever return a coarse location that is more recent than the allowed update
1888 // interval (i.e. don't allow an app to keep registering and unregistering for
1889 // location updates to overcome the minimum interval).
1890 noGPSLocation =
1891 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1892
Laurent Tu60ec50a2012-10-04 17:00:10 -07001893 // Skip if there are no UpdateRecords for this provider.
1894 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1895 if (records == null || records.size() == 0) return;
1896
Victoria Lease09016ab2012-09-16 12:33:15 -07001897 // Fetch coarse location
1898 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001899 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001900 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1901 }
1902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 // Fetch latest status update time
1904 long newStatusUpdateTime = p.getStatusUpdateTime();
1905
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001906 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 Bundle extras = new Bundle();
1908 int status = p.getStatus(extras);
1909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001911 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001914 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001916 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001917
Victoria Lease269518e2012-10-29 08:25:39 -07001918 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001919 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001920 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001921 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001922 " (current user: " + mCurrentUserId + ", app: " +
1923 receiver.mPackageName + ")");
1924 }
1925 continue;
1926 }
1927
Nick Pelly4035f5a2012-08-17 14:43:49 -07001928 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1929 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1930 receiver.mPackageName);
1931 continue;
1932 }
1933
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001934 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1935 receiver.mAllowedResolutionLevel)) {
1936 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1937 receiver.mPackageName);
1938 continue;
1939 }
1940
Victoria Lease09016ab2012-09-16 12:33:15 -07001941 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001942 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1943 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001944 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001945 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001946 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001947 if (notifyLocation != null) {
1948 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001949 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001950 if (lastLoc == null) {
1951 lastLoc = new Location(notifyLocation);
1952 r.mLastFixBroadcast = lastLoc;
1953 } else {
1954 lastLoc.set(notifyLocation);
1955 }
1956 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1957 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1958 receiverDead = true;
1959 }
Laurent Tu75defb62012-11-01 16:21:52 -07001960 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 }
1963
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001964 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001966 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001968 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001970 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001971 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001972 }
1973 }
1974
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001975 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001976 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001977 if (deadUpdateRecords == null) {
1978 deadUpdateRecords = new ArrayList<UpdateRecord>();
1979 }
1980 deadUpdateRecords.add(r);
1981 }
1982 // track dead receivers
1983 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001984 if (deadReceivers == null) {
1985 deadReceivers = new ArrayList<Receiver>();
1986 }
1987 if (!deadReceivers.contains(receiver)) {
1988 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
1990 }
1991 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001992
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001993 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001995 for (Receiver receiver : deadReceivers) {
1996 removeUpdatesLocked(receiver);
1997 }
1998 }
1999 if (deadUpdateRecords != null) {
2000 for (UpdateRecord r : deadUpdateRecords) {
2001 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002003 applyRequirementsLocked(provider);
2004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
2006
2007 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002008 public LocationWorkerHandler(Looper looper) {
2009 super(looper, null, true);
2010 }
2011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 @Override
2013 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002014 switch (msg.what) {
2015 case MSG_LOCATION_CHANGED:
2016 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2017 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
2019 }
2020 }
2021
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002022 private boolean isMockProvider(String provider) {
2023 synchronized (mLock) {
2024 return mMockProviders.containsKey(provider);
2025 }
2026 }
2027
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002028 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002029 // create a working copy of the incoming Location so that the service can modify it without
2030 // disturbing the caller's copy
2031 Location myLocation = new Location(location);
2032 String provider = myLocation.getProvider();
2033
2034 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2035 // bit if location did not come from a mock provider because passive/fused providers can
2036 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2037 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2038 myLocation.setIsFromMockProvider(true);
2039 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002040
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002041 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002042 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2043 if (!passive) {
2044 // notify passive provider of the new location
2045 mPassiveProvider.updateLocation(myLocation);
2046 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002047 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051
Mike Lockwoode97ae402010-09-29 15:23:46 -04002052 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2053 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002054 public void onPackageDisappeared(String packageName, int reason) {
2055 // remove all receivers associated with this package name
2056 synchronized (mLock) {
2057 ArrayList<Receiver> deadReceivers = null;
2058
2059 for (Receiver receiver : mReceivers.values()) {
2060 if (receiver.mPackageName.equals(packageName)) {
2061 if (deadReceivers == null) {
2062 deadReceivers = new ArrayList<Receiver>();
2063 }
2064 deadReceivers.add(receiver);
2065 }
2066 }
2067
2068 // perform removal outside of mReceivers loop
2069 if (deadReceivers != null) {
2070 for (Receiver receiver : deadReceivers) {
2071 removeUpdatesLocked(receiver);
2072 }
2073 }
2074 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002075 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002076 };
2077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 // Geocoder
2079
Nick Pellye0fd6932012-07-11 10:26:13 -07002080 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002081 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002082 return mGeocodeProvider != null;
2083 }
2084
Nick Pellye0fd6932012-07-11 10:26:13 -07002085 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002087 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002088 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002089 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2090 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002092 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094
Mike Lockwooda55c3212009-04-15 11:10:11 -04002095
Nick Pellye0fd6932012-07-11 10:26:13 -07002096 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002098 double lowerLeftLatitude, double lowerLeftLongitude,
2099 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002100 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002101
2102 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002103 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2104 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2105 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002107 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109
2110 // Mock Providers
2111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 private void checkMockPermissionsSafe() {
2113 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2114 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2115 if (!allowMocks) {
2116 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2117 }
2118
2119 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2120 PackageManager.PERMISSION_GRANTED) {
2121 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124
Nick Pellye0fd6932012-07-11 10:26:13 -07002125 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002126 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 checkMockPermissionsSafe();
2128
Mike Lockwooda4903f22010-02-17 06:42:23 -05002129 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2130 throw new IllegalArgumentException("Cannot mock the passive location provider");
2131 }
2132
Mike Lockwood86328a92009-10-23 08:38:25 -04002133 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002134 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002135 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002136 // remove the real provider if we are replacing GPS or network provider
2137 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002138 || LocationManager.NETWORK_PROVIDER.equals(name)
2139 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002140 LocationProviderInterface p = mProvidersByName.get(name);
2141 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002142 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002143 }
2144 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002145 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2147 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002148 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002149 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002150 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002151 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 updateProvidersLocked();
2153 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002154 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
2156
Nick Pellye0fd6932012-07-11 10:26:13 -07002157 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 public void removeTestProvider(String provider) {
2159 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002160 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002161 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002162 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2164 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002165 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002166 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002167
2168 // reinstate real provider if available
2169 LocationProviderInterface realProvider = mRealProviders.get(provider);
2170 if (realProvider != null) {
2171 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002172 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002173 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002174 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002176 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178 }
2179
Nick Pellye0fd6932012-07-11 10:26:13 -07002180 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 public void setTestProviderLocation(String provider, Location loc) {
2182 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002183 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002184 MockProvider mockProvider = mMockProviders.get(provider);
2185 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2187 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002188 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2189 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002190 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002191 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
2193 }
2194
Nick Pellye0fd6932012-07-11 10:26:13 -07002195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 public void clearTestProviderLocation(String provider) {
2197 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002198 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002199 MockProvider mockProvider = mMockProviders.get(provider);
2200 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2202 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002203 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205 }
2206
Nick Pellye0fd6932012-07-11 10:26:13 -07002207 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 public void setTestProviderEnabled(String provider, boolean enabled) {
2209 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002210 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002211 MockProvider mockProvider = mMockProviders.get(provider);
2212 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2214 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002215 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002217 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 mEnabledProviders.add(provider);
2219 mDisabledProviders.remove(provider);
2220 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002221 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 mEnabledProviders.remove(provider);
2223 mDisabledProviders.add(provider);
2224 }
2225 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002226 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
2228 }
2229
Nick Pellye0fd6932012-07-11 10:26:13 -07002230 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 public void clearTestProviderEnabled(String provider) {
2232 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002233 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002234 MockProvider mockProvider = mMockProviders.get(provider);
2235 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2237 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002238 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 mEnabledProviders.remove(provider);
2240 mDisabledProviders.remove(provider);
2241 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002242 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 }
2244 }
2245
Nick Pellye0fd6932012-07-11 10:26:13 -07002246 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2248 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002249 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002250 MockProvider mockProvider = mMockProviders.get(provider);
2251 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2253 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002254 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
2256 }
2257
Nick Pellye0fd6932012-07-11 10:26:13 -07002258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 public void clearTestProviderStatus(String provider) {
2260 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002261 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002262 MockProvider mockProvider = mMockProviders.get(provider);
2263 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2265 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002266 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
2268 }
2269
2270 private void log(String log) {
2271 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002272 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 }
2274 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002275
2276 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2278 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2279 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002280 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 + Binder.getCallingPid()
2282 + ", uid=" + Binder.getCallingUid());
2283 return;
2284 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002285
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002286 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002289 for (Receiver receiver : mReceivers.values()) {
2290 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002293 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2294 pw.println(" " + entry.getKey() + ":");
2295 for (UpdateRecord record : entry.getValue()) {
2296 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 }
2298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002300 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2301 String provider = entry.getKey();
2302 Location location = entry.getValue();
2303 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002305
David Christie1b9b7b12013-04-15 15:31:11 -07002306 pw.println(" Last Known Locations Coarse Intervals:");
2307 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2308 String provider = entry.getKey();
2309 Location location = entry.getValue();
2310 pw.println(" " + provider + ": " + location);
2311 }
2312
Nick Pellye0fd6932012-07-11 10:26:13 -07002313 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 if (mEnabledProviders.size() > 0) {
2316 pw.println(" Enabled Providers:");
2317 for (String i : mEnabledProviders) {
2318 pw.println(" " + i);
2319 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 }
2322 if (mDisabledProviders.size() > 0) {
2323 pw.println(" Disabled Providers:");
2324 for (String i : mDisabledProviders) {
2325 pw.println(" " + i);
2326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002328 pw.append(" ");
2329 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 if (mMockProviders.size() > 0) {
2331 pw.println(" Mock Providers:");
2332 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002333 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
2335 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002336
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002337 pw.append(" fudger: ");
2338 mLocationFudger.dump(fd, pw, args);
2339
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002340 if (args.length > 0 && "short".equals(args[0])) {
2341 return;
2342 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002343 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002344 pw.print(provider.getName() + " Internal State");
2345 if (provider instanceof LocationProviderProxy) {
2346 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2347 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002348 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002349 pw.println(":");
2350 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 }
2353 }
2354}