blob: 8f480ddbf514c2c4ea6790f1f0bcc7152a02453f [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;
destradaa1af4b022013-07-12 15:43:36 -070066import com.android.server.location.FlpHardwareProvider;
67import com.android.server.location.FusedProxy;
Mike Lockwood43e33f22010-03-26 10:41:48 -040068import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070069import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070070import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040071import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070072import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070073import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040074import com.android.server.location.LocationProviderInterface;
75import com.android.server.location.LocationProviderProxy;
76import com.android.server.location.MockProvider;
77import com.android.server.location.PassiveProvider;
78
79import java.io.FileDescriptor;
80import java.io.PrintWriter;
81import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070082import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040083import java.util.HashMap;
84import java.util.HashSet;
85import java.util.List;
86import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040087import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/**
90 * The service class that manages LocationProviders and issues location
91 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080093public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080095 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070096
97 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Victoria Lease37425c32012-10-16 16:08:48 -070099 // Location resolution level: no location data whatsoever
100 private static final int RESOLUTION_LEVEL_NONE = 0;
101 // Location resolution level: coarse location data only
102 private static final int RESOLUTION_LEVEL_COARSE = 1;
103 // Location resolution level: fine location data
104 private static final int RESOLUTION_LEVEL_FINE = 2;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700107 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700109 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400110 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700111 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
112
113 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700114 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700115 private static final String FUSED_LOCATION_SERVICE_ACTION =
116 "com.android.location.service.FusedLocationProvider";
117
118 private static final int MSG_LOCATION_CHANGED = 1;
119
David Christie1b9b7b12013-04-15 15:31:11 -0700120 private static final long NANOS_PER_MILLI = 1000000L;
121
David Christie0b837452013-07-29 16:02:13 -0700122 // The maximum interval a location request can have and still be considered "high power".
123 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
124
Nick Pellyf1be6862012-05-15 10:53:42 -0700125 // Location Providers may sometimes deliver location updates
126 // slightly faster that requested - provide grace period so
127 // we don't unnecessarily filter events that are otherwise on
128 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700129 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700130
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700131 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
132
133 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800134 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700135
136 // used internally for synchronization
137 private final Object mLock = new Object();
138
Victoria Lease5cd731a2012-12-19 15:04:21 -0800139 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700140 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700141 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700142 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700143 private PowerManager mPowerManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700144 private GeocoderProxy mGeocodeProvider;
145 private IGpsStatusProvider mGpsStatusProvider;
146 private INetInitiatedListener mNetInitiatedListener;
147 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700148 private PassiveProvider mPassiveProvider; // track passive provider for special cases
149 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700150
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700151 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 // Set of providers that are explicitly enabled
153 private final Set<String> mEnabledProviders = new HashSet<String>();
154
155 // Set of providers that are explicitly disabled
156 private final Set<String> mDisabledProviders = new HashSet<String>();
157
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700158 // Mock (test) providers
159 private final HashMap<String, MockProvider> mMockProviders =
160 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700162 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400163 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500166 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400168
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700169 // real providers, saved here when mocked out
170 private final HashMap<String, LocationProviderInterface> mRealProviders =
171 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700173 // mapping from provider name to provider
174 private final HashMap<String, LocationProviderInterface> mProvidersByName =
175 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700177 // mapping from provider name to all its UpdateRecords
178 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
179 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700180
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700181 // mapping from provider name to last known location
182 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
David Christie1b9b7b12013-04-15 15:31:11 -0700184 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
185 // locations stored here are not fudged for coarse permissions.
186 private final HashMap<String, Location> mLastLocationCoarseInterval =
187 new HashMap<String, Location>();
188
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700189 // all providers that operate over proxy, for authorizing incoming location
190 private final ArrayList<LocationProviderProxy> mProxyProviders =
191 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Victoria Lease38389b62012-09-30 11:44:22 -0700193 // current active user on the device - other users are denied location data
194 private int mCurrentUserId = UserHandle.USER_OWNER;
195
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700196 public LocationManagerService(Context context) {
197 super();
198 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800199 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800200
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700201 if (D) Log.d(TAG, "Constructed");
202
203 // most startup is deferred until systemReady()
204 }
205
Svetoslav Ganova0027152013-06-25 14:59:53 -0700206 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700207 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800208 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700209
Victoria Lease5cd731a2012-12-19 15:04:21 -0800210 // fetch package manager
211 mPackageManager = mContext.getPackageManager();
212
Victoria Lease0aa28602013-05-29 15:28:26 -0700213 // fetch power manager
214 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800215
216 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700217 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800218
219 // prepare mLocationHandler's dependents
220 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
221 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
222 mBlacklist.init();
223 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
224
Dianne Hackbornc2293022013-02-06 23:14:49 -0800225 // Monitor for app ops mode changes.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700226 AppOpsManager.OnOpChangedListener callback
227 = new AppOpsManager.OnOpChangedInternalListener() {
228 public void onOpChanged(int op, String packageName) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800229 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700230 for (Receiver receiver : mReceivers.values()) {
231 receiver.updateMonitoring(true);
232 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800233 applyAllProviderRequirementsLocked();
234 }
235 }
236 };
237 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
238
Victoria Lease5cd731a2012-12-19 15:04:21 -0800239 // prepare providers
240 loadProvidersLocked();
241 updateProvidersLocked();
242 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700243
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700244 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700245 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700246 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700247 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800248 @Override
249 public void onChange(boolean selfChange) {
250 synchronized (mLock) {
251 updateProvidersLocked();
252 }
253 }
254 }, UserHandle.USER_ALL);
255 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700256
Victoria Lease38389b62012-09-30 11:44:22 -0700257 // listen for user change
258 IntentFilter intentFilter = new IntentFilter();
259 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
260
261 mContext.registerReceiverAsUser(new BroadcastReceiver() {
262 @Override
263 public void onReceive(Context context, Intent intent) {
264 String action = intent.getAction();
265 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
266 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
267 }
268 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800269 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700270 }
271
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500272 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
273 PackageManager pm = mContext.getPackageManager();
274 String systemPackageName = mContext.getPackageName();
275 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
276
277 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
278 new Intent(FUSED_LOCATION_SERVICE_ACTION),
279 PackageManager.GET_META_DATA, mCurrentUserId);
280 for (ResolveInfo rInfo : rInfos) {
281 String packageName = rInfo.serviceInfo.packageName;
282
283 // Check that the signature is in the list of supported sigs. If it's not in
284 // this list the standard provider binding logic won't bind to it.
285 try {
286 PackageInfo pInfo;
287 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
288 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
289 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
290 ", but has wrong signature, ignoring");
291 continue;
292 }
293 } catch (NameNotFoundException e) {
294 Log.e(TAG, "missing package: " + packageName);
295 continue;
296 }
297
298 // Get the version info
299 if (rInfo.serviceInfo.metaData == null) {
300 Log.w(TAG, "Found fused provider without metadata: " + packageName);
301 continue;
302 }
303
304 int version = rInfo.serviceInfo.metaData.getInt(
305 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
306 if (version == 0) {
307 // This should be the fallback fused location provider.
308
309 // Make sure it's in the system partition.
310 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
311 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
312 continue;
313 }
314
315 // Check that the fallback is signed the same as the OS
316 // as a proxy for coreApp="true"
317 if (pm.checkSignatures(systemPackageName, packageName)
318 != PackageManager.SIGNATURE_MATCH) {
319 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
320 + packageName);
321 continue;
322 }
323
324 // Found a valid fallback.
325 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
326 return;
327 } else {
328 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
329 }
330 }
331
332 throw new IllegalStateException("Unable to find a fused location provider that is in the "
333 + "system partition with version 0 and signed with the platform certificate. "
334 + "Such a package is needed to provide a default fused location provider in the "
335 + "event that no other fused location provider has been installed or is currently "
336 + "available. For example, coreOnly boot mode when decrypting the data "
337 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
338 }
339
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700340 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700341 // create a passive location provider, which is always enabled
342 PassiveProvider passiveProvider = new PassiveProvider(this);
343 addProviderLocked(passiveProvider);
344 mEnabledProviders.add(passiveProvider.getName());
345 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700346 // Create a gps location provider
347 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
348 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700349
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700350 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700351 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
352 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
353 addProviderLocked(gpsProvider);
354 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
355 }
356
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700357 /*
358 Load package name(s) containing location provider support.
359 These packages can contain services implementing location providers:
360 Geocoder Provider, Network Location Provider, and
361 Fused Location Provider. They will each be searched for
362 service components implementing these providers.
363 The location framework also has support for installation
364 of new location providers at run-time. The new package does not
365 have to be explicitly listed here, however it must have a signature
366 that matches the signature of at least one package on this list.
367 */
368 Resources resources = mContext.getResources();
369 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500370 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700371 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500372 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
373 Arrays.toString(pkgs));
374 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
375
376 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700377
378 // bind to network provider
379 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
380 mContext,
381 LocationManager.NETWORK_PROVIDER,
382 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700383 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
384 com.android.internal.R.string.config_networkLocationProviderPackageName,
385 com.android.internal.R.array.config_locationProviderPackageNames,
386 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700387 if (networkProvider != null) {
388 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
389 mProxyProviders.add(networkProvider);
390 addProviderLocked(networkProvider);
391 } else {
392 Slog.w(TAG, "no network location provider found");
393 }
394
395 // bind to fused provider
396 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
397 mContext,
398 LocationManager.FUSED_PROVIDER,
399 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700400 com.android.internal.R.bool.config_enableFusedLocationOverlay,
401 com.android.internal.R.string.config_fusedLocationProviderPackageName,
402 com.android.internal.R.array.config_locationProviderPackageNames,
403 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700404 if (fusedLocationProvider != null) {
405 addProviderLocked(fusedLocationProvider);
406 mProxyProviders.add(fusedLocationProvider);
407 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700408 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700409 } else {
410 Slog.e(TAG, "no fused location provider found",
411 new IllegalStateException("Location service needs a fused location provider"));
412 }
413
414 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700415 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
416 com.android.internal.R.bool.config_enableGeocoderOverlay,
417 com.android.internal.R.string.config_geocoderProviderPackageName,
418 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800419 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700420 if (mGeocodeProvider == null) {
421 Slog.e(TAG, "no geocoder provider found");
422 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700423
destradaa1af4b022013-07-12 15:43:36 -0700424 // bind to fused provider
destradaa1af4b022013-07-12 15:43:36 -0700425 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
426 FusedProxy fusedProxy = FusedProxy.createAndBind(
427 mContext,
428 mLocationHandler,
destradaa64be0c62013-08-09 15:01:49 -0700429 flpHardwareProvider.getLocationHardware(),
430 com.android.internal.R.bool.config_enableFusedLocationOverlay,
431 com.android.internal.R.string.config_fusedLocationProviderPackageName,
432 com.android.internal.R.array.config_locationProviderPackageNames);
destradaa1af4b022013-07-12 15:43:36 -0700433 if(fusedProxy == null) {
434 Slog.e(TAG, "No FusedProvider found.");
435 }
destradaa0682809a2013-08-12 18:50:30 -0700436
437 // bind to geofence provider
438 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
439 com.android.internal.R.bool.config_enableGeofenceOverlay,
440 com.android.internal.R.string.config_geofenceProviderPackageName,
441 com.android.internal.R.array.config_locationProviderPackageNames,
442 mLocationHandler,
443 gpsProvider.getGpsGeofenceProxy(),
444 flpHardwareProvider.getGeofenceHardware());
445 if (provider == null) {
446 Slog.e(TAG, "no geofence provider found");
447 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700448 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700451 * Called when the device's active user changes.
452 * @param userId the new active user's UserId
453 */
454 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700455 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800456 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700457 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700458 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700459 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700460 for (LocationProviderInterface p : mProviders) {
461 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700462 }
Victoria Lease38389b62012-09-30 11:44:22 -0700463 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700464 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700465 }
466 }
467
468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
470 * location updates.
471 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700472 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700473 final int mUid; // uid of receiver
474 final int mPid; // pid of receiver
475 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700476 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 final ILocationListener mListener;
479 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700480 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700481 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700483
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400484 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700485
David Christie0b837452013-07-29 16:02:13 -0700486 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700487 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700488 // True if app ops has started monitoring this receiver for high power (gps) locations.
489 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700490 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700491 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700493 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700494 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700497 if (listener != null) {
498 mKey = listener.asBinder();
499 } else {
500 mKey = intent;
501 }
Victoria Lease37425c32012-10-16 16:08:48 -0700502 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700503 mUid = uid;
504 mPid = pid;
505 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700506 if (workSource != null && workSource.size() <= 0) {
507 workSource = null;
508 }
509 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700510 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700511
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700512 updateMonitoring(true);
513
Victoria Lease0aa28602013-05-29 15:28:26 -0700514 // construct/configure wakelock
515 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700516 if (workSource == null) {
517 workSource = new WorkSource(mUid, mPackageName);
518 }
519 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521
522 @Override
523 public boolean equals(Object otherObj) {
524 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700525 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 return false;
528 }
529
530 @Override
531 public int hashCode() {
532 return mKey.hashCode();
533 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 @Override
536 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700537 StringBuilder s = new StringBuilder();
538 s.append("Reciever[");
539 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700541 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700543 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700545 for (String p : mUpdateRecords.keySet()) {
546 s.append(" ").append(mUpdateRecords.get(p).toString());
547 }
548 s.append("]");
549 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551
David Christie15b31912013-08-13 15:54:32 -0700552 /**
553 * Update AppOp monitoring for this receiver.
554 *
555 * @param allow If true receiver is currently active, if false it's been removed.
556 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700557 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700558 if (mHideFromAppOps) {
559 return;
560 }
561
David Christie15b31912013-08-13 15:54:32 -0700562 boolean requestingLocation = false;
563 boolean requestingHighPowerLocation = false;
564 if (allow) {
565 // See if receiver has any enabled update records. Also note if any update records
566 // are high power (has a high power provider with an interval under a threshold).
567 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
568 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
569 requestingLocation = true;
570 LocationProviderInterface locationProvider
571 = mProvidersByName.get(updateRecord.mProvider);
572 ProviderProperties properties = locationProvider != null
573 ? locationProvider.getProperties() : null;
574 if (properties != null
575 && properties.mPowerRequirement == Criteria.POWER_HIGH
576 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
577 requestingHighPowerLocation = true;
578 break;
579 }
580 }
581 }
582 }
583
David Christie0b837452013-07-29 16:02:13 -0700584 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700585 mOpMonitoring = updateMonitoring(
586 requestingLocation,
587 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700588 AppOpsManager.OP_MONITOR_LOCATION);
589
590 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700591 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700592 mOpHighPowerMonitoring = updateMonitoring(
593 requestingHighPowerLocation,
594 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700595 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700596 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700597 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700598 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
599 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
600 }
David Christie0b837452013-07-29 16:02:13 -0700601 }
602
603 /**
604 * Update AppOps monitoring for a single location request and op type.
605 *
606 * @param allowMonitoring True if monitoring is allowed for this request/op.
607 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
608 * @param op AppOps code for the op to update.
609 * @return True if monitoring is on for this request/op after updating.
610 */
611 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
612 int op) {
613 if (!currentlyMonitoring) {
614 if (allowMonitoring) {
615 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
616 == AppOpsManager.MODE_ALLOWED;
617 }
618 } else {
619 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
620 != AppOpsManager.MODE_ALLOWED) {
621 mAppOps.finishOp(op, mUid, mPackageName);
622 return false;
623 }
624 }
625
626 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700627 }
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public boolean isListener() {
630 return mListener != null;
631 }
632
633 public boolean isPendingIntent() {
634 return mPendingIntent != null;
635 }
636
637 public ILocationListener getListener() {
638 if (mListener != null) {
639 return mListener;
640 }
641 throw new IllegalStateException("Request for non-existent listener");
642 }
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
645 if (mListener != null) {
646 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700647 synchronized (this) {
648 // synchronize to ensure incrementPendingBroadcastsLocked()
649 // is called before decrementPendingBroadcasts()
650 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700651 // call this after broadcasting so we do not increment
652 // if we throw an exeption.
653 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 } catch (RemoteException e) {
656 return false;
657 }
658 } else {
659 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800660 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
662 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700663 synchronized (this) {
664 // synchronize to ensure incrementPendingBroadcastsLocked()
665 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700666 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700667 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700668 // call this after broadcasting so we do not increment
669 // if we throw an exeption.
670 incrementPendingBroadcastsLocked();
671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 } catch (PendingIntent.CanceledException e) {
673 return false;
674 }
675 }
676 return true;
677 }
678
679 public boolean callLocationChangedLocked(Location location) {
680 if (mListener != null) {
681 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700682 synchronized (this) {
683 // synchronize to ensure incrementPendingBroadcastsLocked()
684 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800685 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700686 // call this after broadcasting so we do not increment
687 // if we throw an exeption.
688 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 } catch (RemoteException e) {
691 return false;
692 }
693 } else {
694 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800695 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700697 synchronized (this) {
698 // synchronize to ensure incrementPendingBroadcastsLocked()
699 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700700 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700701 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700702 // call this after broadcasting so we do not increment
703 // if we throw an exeption.
704 incrementPendingBroadcastsLocked();
705 }
706 } catch (PendingIntent.CanceledException e) {
707 return false;
708 }
709 }
710 return true;
711 }
712
713 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -0700714 // First update AppOp monitoring.
715 // An app may get/lose location access as providers are enabled/disabled.
716 updateMonitoring(true);
717
Mike Lockwood48f17512009-04-23 09:12:08 -0700718 if (mListener != null) {
719 try {
720 synchronized (this) {
721 // synchronize to ensure incrementPendingBroadcastsLocked()
722 // is called before decrementPendingBroadcasts()
723 if (enabled) {
724 mListener.onProviderEnabled(provider);
725 } else {
726 mListener.onProviderDisabled(provider);
727 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700728 // call this after broadcasting so we do not increment
729 // if we throw an exeption.
730 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700731 }
732 } catch (RemoteException e) {
733 return false;
734 }
735 } else {
736 Intent providerIntent = new Intent();
737 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
738 try {
739 synchronized (this) {
740 // synchronize to ensure incrementPendingBroadcastsLocked()
741 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700742 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700743 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700744 // call this after broadcasting so we do not increment
745 // if we throw an exeption.
746 incrementPendingBroadcastsLocked();
747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 } catch (PendingIntent.CanceledException e) {
749 return false;
750 }
751 }
752 return true;
753 }
754
Nick Pellyf1be6862012-05-15 10:53:42 -0700755 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700757 if (D) Log.d(TAG, "Location listener died");
758
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400759 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 removeUpdatesLocked(this);
761 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700762 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700763 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700764 }
765 }
766
Nick Pellye0fd6932012-07-11 10:26:13 -0700767 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700768 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
769 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400770 synchronized (this) {
771 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700772 }
773 }
774
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400775 // this must be called while synchronized by caller in a synchronized block
776 // containing the sending of the broadcaset
777 private void incrementPendingBroadcastsLocked() {
778 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700779 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400780 }
781 }
782
783 private void decrementPendingBroadcastsLocked() {
784 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700785 if (mWakeLock.isHeld()) {
786 mWakeLock.release();
787 }
788 }
789 }
790
791 public void clearPendingBroadcastsLocked() {
792 if (mPendingBroadcasts > 0) {
793 mPendingBroadcasts = 0;
794 if (mWakeLock.isHeld()) {
795 mWakeLock.release();
796 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700797 }
798 }
799 }
800
Nick Pellye0fd6932012-07-11 10:26:13 -0700801 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700802 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700803 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400804 //the receiver list if it is not found. If it is not found then the
805 //LocationListener was removed when it had a pending broadcast and should
806 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700807 synchronized (mLock) {
808 IBinder binder = listener.asBinder();
809 Receiver receiver = mReceivers.get(binder);
810 if (receiver != null) {
811 synchronized (receiver) {
812 // so wakelock calls will succeed
813 long identity = Binder.clearCallingIdentity();
814 receiver.decrementPendingBroadcastsLocked();
815 Binder.restoreCallingIdentity(identity);
816 }
817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819 }
820
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700821 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400822 mProviders.add(provider);
823 mProvidersByName.put(provider.getName(), provider);
824 }
825
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700826 private void removeProviderLocked(LocationProviderInterface provider) {
827 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400828 mProviders.remove(provider);
829 mProvidersByName.remove(provider.getName());
830 }
831
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800832 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800833 * Returns "true" if access to the specified location provider is allowed by the current
834 * user's settings. Access to all location providers is forbidden to non-location-provider
835 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800836 *
837 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800838 * @return
839 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800840 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 if (mEnabledProviders.contains(provider)) {
842 return true;
843 }
844 if (mDisabledProviders.contains(provider)) {
845 return false;
846 }
847 // Use system settings
848 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849
Victoria Leaseb711d572012-10-02 13:14:11 -0700850 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700853 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800854 * Returns "true" if access to the specified location provider is allowed by the specified
855 * user's settings. Access to all location providers is forbidden to non-location-provider
856 * processes belonging to background users.
857 *
858 * @param provider the name of the location provider
859 * @param uid the requestor's UID
860 * @return
861 */
862 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
863 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
864 return false;
865 }
866 return isAllowedByCurrentUserSettingsLocked(provider);
867 }
868
869 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700870 * Returns the permission string associated with the specified resolution level.
871 *
872 * @param resolutionLevel the resolution level
873 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700874 */
Victoria Lease37425c32012-10-16 16:08:48 -0700875 private String getResolutionPermission(int resolutionLevel) {
876 switch (resolutionLevel) {
877 case RESOLUTION_LEVEL_FINE:
878 return android.Manifest.permission.ACCESS_FINE_LOCATION;
879 case RESOLUTION_LEVEL_COARSE:
880 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
881 default:
882 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700884 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700885
Victoria Leaseda479c52012-10-15 15:24:16 -0700886 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700887 * Returns the resolution level allowed to the given PID/UID pair.
888 *
889 * @param pid the PID
890 * @param uid the UID
891 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700892 */
Victoria Lease37425c32012-10-16 16:08:48 -0700893 private int getAllowedResolutionLevel(int pid, int uid) {
894 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
895 pid, uid) == PackageManager.PERMISSION_GRANTED) {
896 return RESOLUTION_LEVEL_FINE;
897 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
898 pid, uid) == PackageManager.PERMISSION_GRANTED) {
899 return RESOLUTION_LEVEL_COARSE;
900 } else {
901 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700902 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700903 }
904
905 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700906 * Returns the resolution level allowed to the caller
907 *
908 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700909 */
Victoria Lease37425c32012-10-16 16:08:48 -0700910 private int getCallerAllowedResolutionLevel() {
911 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
912 }
913
914 /**
915 * Throw SecurityException if specified resolution level is insufficient to use geofences.
916 *
917 * @param allowedResolutionLevel resolution level allowed to caller
918 */
919 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
920 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700921 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924
Victoria Lease37425c32012-10-16 16:08:48 -0700925 /**
926 * Return the minimum resolution level required to use the specified location provider.
927 *
928 * @param provider the name of the location provider
929 * @return minimum resolution level required for provider
930 */
931 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700932 if (LocationManager.GPS_PROVIDER.equals(provider) ||
933 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
934 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700935 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700936 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
937 LocationManager.FUSED_PROVIDER.equals(provider)) {
938 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700939 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700940 } else {
941 // mock providers
942 LocationProviderInterface lp = mMockProviders.get(provider);
943 if (lp != null) {
944 ProviderProperties properties = lp.getProperties();
945 if (properties != null) {
946 if (properties.mRequiresSatellite) {
947 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700948 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700949 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
950 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700951 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700952 }
953 }
954 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700955 }
Victoria Lease37425c32012-10-16 16:08:48 -0700956 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700957 }
958
Victoria Lease37425c32012-10-16 16:08:48 -0700959 /**
960 * Throw SecurityException if specified resolution level is insufficient to use the named
961 * location provider.
962 *
963 * @param allowedResolutionLevel resolution level allowed to caller
964 * @param providerName the name of the location provider
965 */
966 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
967 String providerName) {
968 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
969 if (allowedResolutionLevel < requiredResolutionLevel) {
970 switch (requiredResolutionLevel) {
971 case RESOLUTION_LEVEL_FINE:
972 throw new SecurityException("\"" + providerName + "\" location provider " +
973 "requires ACCESS_FINE_LOCATION permission.");
974 case RESOLUTION_LEVEL_COARSE:
975 throw new SecurityException("\"" + providerName + "\" location provider " +
976 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
977 default:
978 throw new SecurityException("Insufficient permission for \"" + providerName +
979 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700980 }
981 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700982 }
983
David Christie82edc9b2013-07-19 11:31:42 -0700984 /**
985 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
986 * for battery).
987 */
David Christie40e57822013-07-30 11:36:48 -0700988 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -0700989 mContext.enforceCallingOrSelfPermission(
990 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
991 }
992
David Christie40e57822013-07-30 11:36:48 -0700993 private void checkUpdateAppOpsAllowed() {
994 mContext.enforceCallingOrSelfPermission(
995 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
996 }
997
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800998 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800999 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1000 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001001 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001002 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001003 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001004 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001005 }
1006 return -1;
1007 }
1008
1009 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
1010 int op = resolutionLevelToOp(allowedResolutionLevel);
1011 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001012 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1013 return false;
1014 }
1015 }
1016 return true;
1017 }
1018
1019 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001020 int op = resolutionLevelToOp(allowedResolutionLevel);
1021 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001022 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1023 return false;
1024 }
1025 }
1026 return true;
1027 }
1028
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001029 /**
1030 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001031 * fused, also including ones that are not permitted to
1032 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001033 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001034 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001036 ArrayList<String> out;
1037 synchronized (mLock) {
1038 out = new ArrayList<String>(mProviders.size());
1039 for (LocationProviderInterface provider : mProviders) {
1040 String name = provider.getName();
1041 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001042 continue;
1043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 out.add(name);
1045 }
1046 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001047
1048 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 return out;
1050 }
1051
Mike Lockwood03ca2162010-04-01 08:10:09 -07001052 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001053 * Return all providers by name, that match criteria and are optionally
1054 * enabled.
1055 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001056 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001057 @Override
1058 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001059 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001060 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001061 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001062 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001063 try {
1064 synchronized (mLock) {
1065 out = new ArrayList<String>(mProviders.size());
1066 for (LocationProviderInterface provider : mProviders) {
1067 String name = provider.getName();
1068 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001069 continue;
1070 }
Victoria Lease37425c32012-10-16 16:08:48 -07001071 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001072 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001073 continue;
1074 }
1075 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1076 name, provider.getProperties(), criteria)) {
1077 continue;
1078 }
1079 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001080 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001081 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001082 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001083 } finally {
1084 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001085 }
1086
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001087 if (D) Log.d(TAG, "getProviders()=" + out);
1088 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001089 }
1090
1091 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001092 * Return the name of the best provider given a Criteria object.
1093 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001094 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001095 * has been deprecated as well. So this method now uses
1096 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001097 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001098 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001099 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001100 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001101
1102 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001103 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001104 result = pickBest(providers);
1105 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1106 return result;
1107 }
1108 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001109 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001110 result = pickBest(providers);
1111 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1112 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001113 }
1114
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001115 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001116 return null;
1117 }
1118
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001119 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001120 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001121 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001122 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1123 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001124 } else {
1125 return providers.get(0);
1126 }
1127 }
1128
Nick Pellye0fd6932012-07-11 10:26:13 -07001129 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001130 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1131 LocationProviderInterface p = mProvidersByName.get(provider);
1132 if (p == null) {
1133 throw new IllegalArgumentException("provider=" + provider);
1134 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001135
1136 boolean result = LocationProvider.propertiesMeetCriteria(
1137 p.getName(), p.getProperties(), criteria);
1138 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1139 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001140 }
1141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001143 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001144 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001145 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 boolean isEnabled = p.isEnabled();
1147 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001148 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001150 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001151 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001153 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001154 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001156 }
1157 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001158 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1159 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001160 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1161 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 }
1164
Victoria Leaseb711d572012-10-02 13:14:11 -07001165 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 int listeners = 0;
1167
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001168 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001169 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170
1171 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1174 if (records != null) {
1175 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001176 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001178 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001179 // Sends a notification message to the receiver
1180 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1181 if (deadReceivers == null) {
1182 deadReceivers = new ArrayList<Receiver>();
1183 }
1184 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001186 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 }
1190
1191 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001192 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 removeUpdatesLocked(deadReceivers.get(i));
1194 }
1195 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 if (enabled) {
1198 p.enable();
1199 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001200 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001207 private void applyRequirementsLocked(String provider) {
1208 LocationProviderInterface p = mProvidersByName.get(provider);
1209 if (p == null) return;
1210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001212 WorkSource worksource = new WorkSource();
1213 ProviderRequest providerRequest = new ProviderRequest();
1214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001216 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001217 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001218 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1219 record.mReceiver.mAllowedResolutionLevel)) {
1220 LocationRequest locationRequest = record.mRequest;
1221 providerRequest.locationRequests.add(locationRequest);
1222 if (locationRequest.getInterval() < providerRequest.interval) {
1223 providerRequest.reportLocation = true;
1224 providerRequest.interval = locationRequest.getInterval();
1225 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001226 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001227 }
1228 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001229
1230 if (providerRequest.reportLocation) {
1231 // calculate who to blame for power
1232 // This is somewhat arbitrary. We pick a threshold interval
1233 // that is slightly higher that the minimum interval, and
1234 // spread the blame across all applications with a request
1235 // under that threshold.
1236 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1237 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001238 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001239 LocationRequest locationRequest = record.mRequest;
1240 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001241 if (record.mReceiver.mWorkSource != null
1242 && record.mReceiver.mWorkSource.size() > 0
1243 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001244 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001245 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001246 worksource.add(record.mReceiver.mWorkSource);
1247 } else {
1248 // Assign blame to caller.
1249 worksource.add(
1250 record.mReceiver.mUid,
1251 record.mReceiver.mPackageName);
1252 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001253 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001254 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001258
1259 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1260 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262
1263 private class UpdateRecord {
1264 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001265 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001267 Location mLastFixBroadcast;
1268 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269
1270 /**
1271 * Note: must be constructed with lock held.
1272 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001273 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001275 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277
1278 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1279 if (records == null) {
1280 records = new ArrayList<UpdateRecord>();
1281 mRecordsByProvider.put(provider, records);
1282 }
1283 if (!records.contains(this)) {
1284 records.add(this);
1285 }
1286 }
1287
1288 /**
1289 * Method to be called when a record will no longer be used. Calling this multiple times
1290 * must have the same effect as calling it once.
1291 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001292 void disposeLocked(boolean removeReceiver) {
1293 // remove from mRecordsByProvider
1294 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1295 if (globalRecords != null) {
1296 globalRecords.remove(this);
1297 }
1298
1299 if (!removeReceiver) return; // the caller will handle the rest
1300
1301 // remove from Receiver#mUpdateRecords
1302 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1303 if (receiverRecords != null) {
1304 receiverRecords.remove(this.mProvider);
1305
1306 // and also remove the Receiver if it has no more update records
1307 if (removeReceiver && receiverRecords.size() == 0) {
1308 removeUpdatesLocked(mReceiver);
1309 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312
1313 @Override
1314 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001315 StringBuilder s = new StringBuilder();
1316 s.append("UpdateRecord[");
1317 s.append(mProvider);
1318 s.append(' ').append(mReceiver.mPackageName).append('(');
1319 s.append(mReceiver.mUid).append(')');
1320 s.append(' ').append(mRequest);
1321 s.append(']');
1322 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001326 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001327 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001328 IBinder binder = listener.asBinder();
1329 Receiver receiver = mReceivers.get(binder);
1330 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001331 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1332 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001333 mReceivers.put(binder, receiver);
1334
1335 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001336 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001337 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001338 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001339 return null;
1340 }
1341 }
1342 return receiver;
1343 }
1344
David Christie82edc9b2013-07-19 11:31:42 -07001345 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001346 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001347 Receiver receiver = mReceivers.get(intent);
1348 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001349 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1350 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001351 mReceivers.put(intent, receiver);
1352 }
1353 return receiver;
1354 }
1355
Victoria Lease37425c32012-10-16 16:08:48 -07001356 /**
1357 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1358 * and consistency requirements.
1359 *
1360 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001361 * @return a version of request that meets the given resolution and consistency requirements
1362 * @hide
1363 */
1364 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1365 LocationRequest sanitizedRequest = new LocationRequest(request);
1366 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1367 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001368 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001369 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001370 break;
1371 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001372 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001373 break;
1374 }
1375 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001376 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1377 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001378 }
Victoria Lease37425c32012-10-16 16:08:48 -07001379 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1380 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001381 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001382 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001383 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001384 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001385 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001386 }
Victoria Lease37425c32012-10-16 16:08:48 -07001387 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001388 }
1389
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001390 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001391 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001392 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001393 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001394 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001395 String[] packages = mPackageManager.getPackagesForUid(uid);
1396 if (packages == null) {
1397 throw new SecurityException("invalid UID " + uid);
1398 }
1399 for (String pkg : packages) {
1400 if (packageName.equals(pkg)) return;
1401 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001402 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001403 }
1404
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001405 private void checkPendingIntent(PendingIntent intent) {
1406 if (intent == null) {
1407 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001408 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001409 }
1410
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001411 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001412 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001413 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001414 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001415 } else if (intent != null && listener != null) {
1416 throw new IllegalArgumentException("cannot register both listener and intent");
1417 } else if (intent != null) {
1418 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001419 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001420 } else {
David Christie40e57822013-07-30 11:36:48 -07001421 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001422 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001423 }
1424
Nick Pellye0fd6932012-07-11 10:26:13 -07001425 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001426 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1427 PendingIntent intent, String packageName) {
1428 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1429 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001430 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1431 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1432 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001433 WorkSource workSource = request.getWorkSource();
1434 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001435 checkDeviceStatsAllowed();
1436 }
1437 boolean hideFromAppOps = request.getHideFromAppOps();
1438 if (hideFromAppOps) {
1439 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001440 }
Victoria Lease37425c32012-10-16 16:08:48 -07001441 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001443 final int pid = Binder.getCallingPid();
1444 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001445 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 long identity = Binder.clearCallingIdentity();
1447 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001448 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1449 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001450 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001451
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001452 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001453 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001454 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001455 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 } finally {
1458 Binder.restoreCallingIdentity(identity);
1459 }
1460 }
1461
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001462 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1463 int pid, int uid, String packageName) {
1464 // Figure out the provider. Either its explicitly request (legacy use cases), or
1465 // use the fused provider
1466 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1467 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001468 if (name == null) {
1469 throw new IllegalArgumentException("provider name must not be null");
1470 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001471
1472 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1473 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001474 LocationProviderInterface provider = mProvidersByName.get(name);
1475 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07001476 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001477 }
1478
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001479 UpdateRecord record = new UpdateRecord(name, request, receiver);
1480 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1481 if (oldRecord != null) {
1482 oldRecord.disposeLocked(false);
1483 }
1484
Victoria Lease09eeaec2013-02-05 11:34:13 -08001485 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001486 if (isProviderEnabled) {
1487 applyRequirementsLocked(name);
1488 } else {
1489 // Notify the listener that updates are currently disabled
1490 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
David Christie0b837452013-07-29 16:02:13 -07001492 // Update the monitoring here just in case multiple location requests were added to the
1493 // same receiver (this request may be high power and the initial might not have been).
1494 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
1496
Nick Pellye0fd6932012-07-11 10:26:13 -07001497 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001498 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1499 String packageName) {
1500 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001501
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001502 final int pid = Binder.getCallingPid();
1503 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001504
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001505 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001506 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001507 boolean hideFromAppOps = false;
1508 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1509 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001510
1511 // providers may use public location API's, need to clear identity
1512 long identity = Binder.clearCallingIdentity();
1513 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001514 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001515 } finally {
1516 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
1519 }
1520
1521 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001522 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001523
1524 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1525 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1526 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001527 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001531 receiver.updateMonitoring(false);
1532
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001533 // Record which providers were associated with this listener
1534 HashSet<String> providers = new HashSet<String>();
1535 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1536 if (oldRecords != null) {
1537 // Call dispose() on the obsolete update records.
1538 for (UpdateRecord record : oldRecords.values()) {
1539 record.disposeLocked(false);
1540 }
1541 // Accumulate providers
1542 providers.addAll(oldRecords.keySet());
1543 }
1544
1545 // update provider
1546 for (String provider : providers) {
1547 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001548 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001549 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001552 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554 }
1555
Dianne Hackbornc2293022013-02-06 23:14:49 -08001556 private void applyAllProviderRequirementsLocked() {
1557 for (LocationProviderInterface p : mProviders) {
1558 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001559 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001560 continue;
1561 }
1562
1563 applyRequirementsLocked(p.getName());
1564 }
1565 }
1566
Nick Pellye0fd6932012-07-11 10:26:13 -07001567 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001568 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001569 if (D) Log.d(TAG, "getLastLocation: " + request);
1570 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001571 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001572 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001573 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1574 request.getProvider());
1575 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001576
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001577 final int uid = Binder.getCallingUid();
1578 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001579 try {
1580 if (mBlacklist.isBlacklisted(packageName)) {
1581 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1582 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001583 return null;
1584 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001585
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001586 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1587 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1588 packageName);
1589 return null;
1590 }
1591
Victoria Leaseb711d572012-10-02 13:14:11 -07001592 synchronized (mLock) {
1593 // Figure out the provider. Either its explicitly request (deprecated API's),
1594 // or use the fused provider
1595 String name = request.getProvider();
1596 if (name == null) name = LocationManager.FUSED_PROVIDER;
1597 LocationProviderInterface provider = mProvidersByName.get(name);
1598 if (provider == null) return null;
1599
Victoria Lease09eeaec2013-02-05 11:34:13 -08001600 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001601
David Christie1b9b7b12013-04-15 15:31:11 -07001602 Location location;
1603 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1604 // Make sure that an app with coarse permissions can't get frequent location
1605 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1606 location = mLastLocationCoarseInterval.get(name);
1607 } else {
1608 location = mLastLocation.get(name);
1609 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001610 if (location == null) {
1611 return null;
1612 }
Victoria Lease37425c32012-10-16 16:08:48 -07001613 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001614 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1615 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001616 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001617 }
Victoria Lease37425c32012-10-16 16:08:48 -07001618 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001619 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001620 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001621 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001622 return null;
1623 } finally {
1624 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001625 }
1626 }
1627
1628 @Override
1629 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1630 String packageName) {
1631 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001632 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1633 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001634 checkPendingIntent(intent);
1635 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001636 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1637 request.getProvider());
1638 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001639
Victoria Lease37425c32012-10-16 16:08:48 -07001640 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001641
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001642 // geo-fence manager uses the public location API, need to clear identity
1643 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001644 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1645 // temporary measure until geofences work for secondary users
1646 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1647 return;
1648 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001649 long identity = Binder.clearCallingIdentity();
1650 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001651 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1652 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001653 } finally {
1654 Binder.restoreCallingIdentity(identity);
1655 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001656 }
1657
1658 @Override
1659 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001660 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001661 checkPendingIntent(intent);
1662 checkPackageName(packageName);
1663
1664 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1665
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001666 // geo-fence manager uses the public location API, need to clear identity
1667 long identity = Binder.clearCallingIdentity();
1668 try {
1669 mGeofenceManager.removeFence(geofence, intent);
1670 } finally {
1671 Binder.restoreCallingIdentity(identity);
1672 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001673 }
1674
1675
1676 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001677 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001678 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 return false;
1680 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001681 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1682 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001683 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001685 final int uid = Binder.getCallingUid();
1686 final long ident = Binder.clearCallingIdentity();
1687 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001688 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001689 return false;
1690 }
1691 } finally {
1692 Binder.restoreCallingIdentity(ident);
1693 }
1694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001696 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001698 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 return false;
1700 }
1701 return true;
1702 }
1703
Nick Pellye0fd6932012-07-11 10:26:13 -07001704 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001706 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001707 try {
1708 mGpsStatusProvider.removeGpsStatusListener(listener);
1709 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001710 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
1713 }
1714
Nick Pellye0fd6932012-07-11 10:26:13 -07001715 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001717 if (provider == null) {
1718 // throw NullPointerException to remain compatible with previous implementation
1719 throw new NullPointerException();
1720 }
Victoria Lease37425c32012-10-16 16:08:48 -07001721 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1722 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001725 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 != PackageManager.PERMISSION_GRANTED)) {
1727 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1728 }
1729
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001730 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001731 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001732 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001733
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001734 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736 }
1737
Nick Pellye0fd6932012-07-11 10:26:13 -07001738 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001739 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001740 if (Binder.getCallingUid() != Process.myUid()) {
1741 throw new SecurityException(
1742 "calling sendNiResponse from outside of the system is not allowed");
1743 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001744 try {
1745 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001746 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001747 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001748 return false;
1749 }
1750 }
1751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001753 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001754 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 * accessed by the caller
1756 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001757 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001758 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001759 if (mProvidersByName.get(provider) == null) {
1760 return null;
1761 }
1762
Victoria Lease37425c32012-10-16 16:08:48 -07001763 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1764 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001766 LocationProviderInterface p;
1767 synchronized (mLock) {
1768 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001771 if (p == null) return null;
1772 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774
Nick Pellye0fd6932012-07-11 10:26:13 -07001775 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07001777 // TODO: remove this check in next release, see b/10696351
Victoria Lease37425c32012-10-16 16:08:48 -07001778 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1779 provider);
Tom O'Neilld5759432013-09-11 11:03:03 -07001780
1781 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
1782 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001783 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1784
Victoria Lease09eeaec2013-02-05 11:34:13 -08001785 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001786 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001787 try {
1788 synchronized (mLock) {
1789 LocationProviderInterface p = mProvidersByName.get(provider);
1790 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001791
Victoria Lease09eeaec2013-02-05 11:34:13 -08001792 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001793 }
1794 } finally {
1795 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001796 }
1797 }
1798
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001799 /**
1800 * Returns "true" if the UID belongs to a bound location provider.
1801 *
1802 * @param uid the uid
1803 * @return true if uid belongs to a bound location provider
1804 */
1805 private boolean isUidALocationProvider(int uid) {
1806 if (uid == Process.SYSTEM_UID) {
1807 return true;
1808 }
1809 if (mGeocodeProvider != null) {
1810 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1811 }
1812 for (LocationProviderProxy proxy : mProxyProviders) {
1813 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1814 }
1815 return false;
1816 }
1817
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001818 private void checkCallerIsProvider() {
1819 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1820 == PackageManager.PERMISSION_GRANTED) {
1821 return;
1822 }
1823
1824 // Previously we only used the INSTALL_LOCATION_PROVIDER
1825 // check. But that is system or signature
1826 // protection level which is not flexible enough for
1827 // providers installed oustide the system image. So
1828 // also allow providers with a UID matching the
1829 // currently bound package name
1830
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001831 if (isUidALocationProvider(Binder.getCallingUid())) {
1832 return;
1833 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001834
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001835 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1836 "or UID of a currently bound location provider");
1837 }
1838
1839 private boolean doesPackageHaveUid(int uid, String packageName) {
1840 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 return false;
1842 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001843 try {
1844 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1845 if (appInfo.uid != uid) {
1846 return false;
1847 }
1848 } catch (NameNotFoundException e) {
1849 return false;
1850 }
1851 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853
Nick Pellye0fd6932012-07-11 10:26:13 -07001854 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001855 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001856 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001857
Nick Pelly2eeeec22012-07-18 13:13:37 -07001858 if (!location.isComplete()) {
1859 Log.w(TAG, "Dropping incomplete location: " + location);
1860 return;
1861 }
1862
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001863 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1864 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001865 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001866 mLocationHandler.sendMessageAtFrontOfQueue(m);
1867 }
1868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869
Laurent Tu75defb62012-11-01 16:21:52 -07001870 private static boolean shouldBroadcastSafe(
1871 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 // Always broadcast the first update
1873 if (lastLoc == null) {
1874 return true;
1875 }
1876
Nick Pellyf1be6862012-05-15 10:53:42 -07001877 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001878 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001879 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1880 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001881 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 return false;
1883 }
1884
1885 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001886 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (minDistance > 0.0) {
1888 if (loc.distanceTo(lastLoc) <= minDistance) {
1889 return false;
1890 }
1891 }
1892
Laurent Tu75defb62012-11-01 16:21:52 -07001893 // Check whether sufficient number of udpates is left
1894 if (record.mRequest.getNumUpdates() <= 0) {
1895 return false;
1896 }
1897
1898 // Check whether the expiry date has passed
1899 if (record.mRequest.getExpireAt() < now) {
1900 return false;
1901 }
1902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 return true;
1904 }
1905
Mike Lockwooda4903f22010-02-17 06:42:23 -05001906 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001907 if (D) Log.d(TAG, "incoming location: " + location);
1908
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001909 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001910 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911
Laurent Tu60ec50a2012-10-04 17:00:10 -07001912 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001913 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001914 if (p == null) return;
1915
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001916 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001917 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1918 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001919 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001920 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001921 lastLocation = new Location(provider);
1922 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001923 } else {
1924 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1925 if (noGPSLocation == null && lastNoGPSLocation != null) {
1926 // New location has no no-GPS location: adopt last no-GPS location. This is set
1927 // directly into location because we do not want to notify COARSE clients.
1928 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1929 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001930 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001931 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932
David Christie1b9b7b12013-04-15 15:31:11 -07001933 // Update last known coarse interval location if enough time has passed.
1934 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1935 if (lastLocationCoarseInterval == null) {
1936 lastLocationCoarseInterval = new Location(location);
1937 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1938 }
1939 long timeDiffNanos = location.getElapsedRealtimeNanos()
1940 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1941 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1942 lastLocationCoarseInterval.set(location);
1943 }
1944 // Don't ever return a coarse location that is more recent than the allowed update
1945 // interval (i.e. don't allow an app to keep registering and unregistering for
1946 // location updates to overcome the minimum interval).
1947 noGPSLocation =
1948 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1949
Laurent Tu60ec50a2012-10-04 17:00:10 -07001950 // Skip if there are no UpdateRecords for this provider.
1951 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1952 if (records == null || records.size() == 0) return;
1953
Victoria Lease09016ab2012-09-16 12:33:15 -07001954 // Fetch coarse location
1955 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001956 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001957 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1958 }
1959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 // Fetch latest status update time
1961 long newStatusUpdateTime = p.getStatusUpdateTime();
1962
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001963 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 Bundle extras = new Bundle();
1965 int status = p.getStatus(extras);
1966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001968 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001971 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001973 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001974
Victoria Lease269518e2012-10-29 08:25:39 -07001975 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001976 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001977 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001978 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001979 " (current user: " + mCurrentUserId + ", app: " +
1980 receiver.mPackageName + ")");
1981 }
1982 continue;
1983 }
1984
Nick Pelly4035f5a2012-08-17 14:43:49 -07001985 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1986 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1987 receiver.mPackageName);
1988 continue;
1989 }
1990
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001991 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1992 receiver.mAllowedResolutionLevel)) {
1993 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1994 receiver.mPackageName);
1995 continue;
1996 }
1997
Victoria Lease09016ab2012-09-16 12:33:15 -07001998 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001999 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2000 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002001 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002002 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002003 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002004 if (notifyLocation != null) {
2005 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002006 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002007 if (lastLoc == null) {
2008 lastLoc = new Location(notifyLocation);
2009 r.mLastFixBroadcast = lastLoc;
2010 } else {
2011 lastLoc.set(notifyLocation);
2012 }
2013 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2014 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2015 receiverDead = true;
2016 }
Laurent Tu75defb62012-11-01 16:21:52 -07002017 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
2019 }
2020
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002021 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002023 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002025 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002027 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002028 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002029 }
2030 }
2031
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002032 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002033 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002034 if (deadUpdateRecords == null) {
2035 deadUpdateRecords = new ArrayList<UpdateRecord>();
2036 }
2037 deadUpdateRecords.add(r);
2038 }
2039 // track dead receivers
2040 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002041 if (deadReceivers == null) {
2042 deadReceivers = new ArrayList<Receiver>();
2043 }
2044 if (!deadReceivers.contains(receiver)) {
2045 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
2047 }
2048 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002049
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002050 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002052 for (Receiver receiver : deadReceivers) {
2053 removeUpdatesLocked(receiver);
2054 }
2055 }
2056 if (deadUpdateRecords != null) {
2057 for (UpdateRecord r : deadUpdateRecords) {
2058 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002060 applyRequirementsLocked(provider);
2061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063
2064 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002065 public LocationWorkerHandler(Looper looper) {
2066 super(looper, null, true);
2067 }
2068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 @Override
2070 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002071 switch (msg.what) {
2072 case MSG_LOCATION_CHANGED:
2073 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2074 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076 }
2077 }
2078
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002079 private boolean isMockProvider(String provider) {
2080 synchronized (mLock) {
2081 return mMockProviders.containsKey(provider);
2082 }
2083 }
2084
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002085 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002086 // create a working copy of the incoming Location so that the service can modify it without
2087 // disturbing the caller's copy
2088 Location myLocation = new Location(location);
2089 String provider = myLocation.getProvider();
2090
2091 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2092 // bit if location did not come from a mock provider because passive/fused providers can
2093 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2094 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2095 myLocation.setIsFromMockProvider(true);
2096 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002097
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002098 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002099 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2100 if (!passive) {
2101 // notify passive provider of the new location
2102 mPassiveProvider.updateLocation(myLocation);
2103 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002104 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108
Mike Lockwoode97ae402010-09-29 15:23:46 -04002109 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2110 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002111 public void onPackageDisappeared(String packageName, int reason) {
2112 // remove all receivers associated with this package name
2113 synchronized (mLock) {
2114 ArrayList<Receiver> deadReceivers = null;
2115
2116 for (Receiver receiver : mReceivers.values()) {
2117 if (receiver.mPackageName.equals(packageName)) {
2118 if (deadReceivers == null) {
2119 deadReceivers = new ArrayList<Receiver>();
2120 }
2121 deadReceivers.add(receiver);
2122 }
2123 }
2124
2125 // perform removal outside of mReceivers loop
2126 if (deadReceivers != null) {
2127 for (Receiver receiver : deadReceivers) {
2128 removeUpdatesLocked(receiver);
2129 }
2130 }
2131 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002132 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002133 };
2134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 // Geocoder
2136
Nick Pellye0fd6932012-07-11 10:26:13 -07002137 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002138 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002139 return mGeocodeProvider != null;
2140 }
2141
Nick Pellye0fd6932012-07-11 10:26:13 -07002142 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002144 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002145 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002146 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2147 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002149 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151
Mike Lockwooda55c3212009-04-15 11:10:11 -04002152
Nick Pellye0fd6932012-07-11 10:26:13 -07002153 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002155 double lowerLeftLatitude, double lowerLeftLongitude,
2156 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002157 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002158
2159 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002160 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2161 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2162 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002164 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166
2167 // Mock Providers
2168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 private void checkMockPermissionsSafe() {
2170 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2171 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2172 if (!allowMocks) {
2173 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2174 }
2175
2176 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2177 PackageManager.PERMISSION_GRANTED) {
2178 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181
Nick Pellye0fd6932012-07-11 10:26:13 -07002182 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002183 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 checkMockPermissionsSafe();
2185
Mike Lockwooda4903f22010-02-17 06:42:23 -05002186 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2187 throw new IllegalArgumentException("Cannot mock the passive location provider");
2188 }
2189
Mike Lockwood86328a92009-10-23 08:38:25 -04002190 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002191 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002192 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002193 // remove the real provider if we are replacing GPS or network provider
2194 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002195 || LocationManager.NETWORK_PROVIDER.equals(name)
2196 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002197 LocationProviderInterface p = mProvidersByName.get(name);
2198 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002199 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002200 }
2201 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002202 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2204 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002205 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002206 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002207 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002208 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 updateProvidersLocked();
2210 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002211 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 }
2213
Nick Pellye0fd6932012-07-11 10:26:13 -07002214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 public void removeTestProvider(String provider) {
2216 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002217 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002218 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002219 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2221 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002222 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002223 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002224
2225 // reinstate real provider if available
2226 LocationProviderInterface realProvider = mRealProviders.get(provider);
2227 if (realProvider != null) {
2228 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002229 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002230 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002231 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002233 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
2235 }
2236
Nick Pellye0fd6932012-07-11 10:26:13 -07002237 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 public void setTestProviderLocation(String provider, Location loc) {
2239 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002240 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002241 MockProvider mockProvider = mMockProviders.get(provider);
2242 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2244 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002245 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2246 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002247 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002248 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 }
2251
Nick Pellye0fd6932012-07-11 10:26:13 -07002252 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 public void clearTestProviderLocation(String provider) {
2254 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002255 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002256 MockProvider mockProvider = mMockProviders.get(provider);
2257 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2259 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002260 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 }
2262 }
2263
Nick Pellye0fd6932012-07-11 10:26:13 -07002264 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 public void setTestProviderEnabled(String provider, boolean enabled) {
2266 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002267 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002268 MockProvider mockProvider = mMockProviders.get(provider);
2269 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2271 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002272 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002274 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 mEnabledProviders.add(provider);
2276 mDisabledProviders.remove(provider);
2277 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002278 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 mEnabledProviders.remove(provider);
2280 mDisabledProviders.add(provider);
2281 }
2282 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002283 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
2285 }
2286
Nick Pellye0fd6932012-07-11 10:26:13 -07002287 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 public void clearTestProviderEnabled(String provider) {
2289 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002290 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002291 MockProvider mockProvider = mMockProviders.get(provider);
2292 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2294 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002295 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 mEnabledProviders.remove(provider);
2297 mDisabledProviders.remove(provider);
2298 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002299 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 }
2301 }
2302
Nick Pellye0fd6932012-07-11 10:26:13 -07002303 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2305 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002306 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002307 MockProvider mockProvider = mMockProviders.get(provider);
2308 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2310 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002311 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
2313 }
2314
Nick Pellye0fd6932012-07-11 10:26:13 -07002315 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 public void clearTestProviderStatus(String provider) {
2317 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002318 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002319 MockProvider mockProvider = mMockProviders.get(provider);
2320 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2322 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002323 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
2325 }
2326
2327 private void log(String log) {
2328 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002329 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 }
2331 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002332
2333 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2335 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2336 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002337 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 + Binder.getCallingPid()
2339 + ", uid=" + Binder.getCallingUid());
2340 return;
2341 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002342
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002343 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002346 for (Receiver receiver : mReceivers.values()) {
2347 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002350 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2351 pw.println(" " + entry.getKey() + ":");
2352 for (UpdateRecord record : entry.getValue()) {
2353 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 }
2355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002357 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2358 String provider = entry.getKey();
2359 Location location = entry.getValue();
2360 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002362
David Christie1b9b7b12013-04-15 15:31:11 -07002363 pw.println(" Last Known Locations Coarse Intervals:");
2364 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2365 String provider = entry.getKey();
2366 Location location = entry.getValue();
2367 pw.println(" " + provider + ": " + location);
2368 }
2369
Nick Pellye0fd6932012-07-11 10:26:13 -07002370 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 if (mEnabledProviders.size() > 0) {
2373 pw.println(" Enabled Providers:");
2374 for (String i : mEnabledProviders) {
2375 pw.println(" " + i);
2376 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379 if (mDisabledProviders.size() > 0) {
2380 pw.println(" Disabled Providers:");
2381 for (String i : mDisabledProviders) {
2382 pw.println(" " + i);
2383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002385 pw.append(" ");
2386 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 if (mMockProviders.size() > 0) {
2388 pw.println(" Mock Providers:");
2389 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002390 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 }
2392 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002393
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002394 pw.append(" fudger: ");
2395 mLocationFudger.dump(fd, pw, args);
2396
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002397 if (args.length > 0 && "short".equals(args[0])) {
2398 return;
2399 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002400 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002401 pw.print(provider.getName() + " Internal State");
2402 if (provider instanceof LocationProviderProxy) {
2403 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2404 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002405 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002406 pw.println(":");
2407 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 }
2410 }
2411}