blob: 9761441dc1551a98e2d4098bca0b101e565fd63c [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.
226 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
227 public void opChanged(int op, String packageName) {
228 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700229 for (Receiver receiver : mReceivers.values()) {
230 receiver.updateMonitoring(true);
231 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800232 applyAllProviderRequirementsLocked();
233 }
234 }
235 };
236 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
237
Victoria Lease5cd731a2012-12-19 15:04:21 -0800238 // prepare providers
239 loadProvidersLocked();
240 updateProvidersLocked();
241 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700242
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700243 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700244 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700245 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700246 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800247 @Override
248 public void onChange(boolean selfChange) {
249 synchronized (mLock) {
250 updateProvidersLocked();
251 }
252 }
253 }, UserHandle.USER_ALL);
254 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700255
Victoria Lease38389b62012-09-30 11:44:22 -0700256 // listen for user change
257 IntentFilter intentFilter = new IntentFilter();
258 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
259
260 mContext.registerReceiverAsUser(new BroadcastReceiver() {
261 @Override
262 public void onReceive(Context context, Intent intent) {
263 String action = intent.getAction();
264 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
265 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
266 }
267 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800268 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700269 }
270
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500271 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
272 PackageManager pm = mContext.getPackageManager();
273 String systemPackageName = mContext.getPackageName();
274 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
275
276 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
277 new Intent(FUSED_LOCATION_SERVICE_ACTION),
278 PackageManager.GET_META_DATA, mCurrentUserId);
279 for (ResolveInfo rInfo : rInfos) {
280 String packageName = rInfo.serviceInfo.packageName;
281
282 // Check that the signature is in the list of supported sigs. If it's not in
283 // this list the standard provider binding logic won't bind to it.
284 try {
285 PackageInfo pInfo;
286 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
287 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
288 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
289 ", but has wrong signature, ignoring");
290 continue;
291 }
292 } catch (NameNotFoundException e) {
293 Log.e(TAG, "missing package: " + packageName);
294 continue;
295 }
296
297 // Get the version info
298 if (rInfo.serviceInfo.metaData == null) {
299 Log.w(TAG, "Found fused provider without metadata: " + packageName);
300 continue;
301 }
302
303 int version = rInfo.serviceInfo.metaData.getInt(
304 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
305 if (version == 0) {
306 // This should be the fallback fused location provider.
307
308 // Make sure it's in the system partition.
309 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
310 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
311 continue;
312 }
313
314 // Check that the fallback is signed the same as the OS
315 // as a proxy for coreApp="true"
316 if (pm.checkSignatures(systemPackageName, packageName)
317 != PackageManager.SIGNATURE_MATCH) {
318 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
319 + packageName);
320 continue;
321 }
322
323 // Found a valid fallback.
324 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
325 return;
326 } else {
327 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
328 }
329 }
330
331 throw new IllegalStateException("Unable to find a fused location provider that is in the "
332 + "system partition with version 0 and signed with the platform certificate. "
333 + "Such a package is needed to provide a default fused location provider in the "
334 + "event that no other fused location provider has been installed or is currently "
335 + "available. For example, coreOnly boot mode when decrypting the data "
336 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
337 }
338
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700339 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700340 // create a passive location provider, which is always enabled
341 PassiveProvider passiveProvider = new PassiveProvider(this);
342 addProviderLocked(passiveProvider);
343 mEnabledProviders.add(passiveProvider.getName());
344 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700345 // Create a gps location provider
346 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
347 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700348
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700349 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700350 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
351 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
352 addProviderLocked(gpsProvider);
353 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
354 }
355
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700356 /*
357 Load package name(s) containing location provider support.
358 These packages can contain services implementing location providers:
359 Geocoder Provider, Network Location Provider, and
360 Fused Location Provider. They will each be searched for
361 service components implementing these providers.
362 The location framework also has support for installation
363 of new location providers at run-time. The new package does not
364 have to be explicitly listed here, however it must have a signature
365 that matches the signature of at least one package on this list.
366 */
367 Resources resources = mContext.getResources();
368 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500369 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700370 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500371 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
372 Arrays.toString(pkgs));
373 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
374
375 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700376
377 // bind to network provider
378 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
379 mContext,
380 LocationManager.NETWORK_PROVIDER,
381 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700382 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
383 com.android.internal.R.string.config_networkLocationProviderPackageName,
384 com.android.internal.R.array.config_locationProviderPackageNames,
385 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700386 if (networkProvider != null) {
387 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
388 mProxyProviders.add(networkProvider);
389 addProviderLocked(networkProvider);
390 } else {
391 Slog.w(TAG, "no network location provider found");
392 }
393
394 // bind to fused provider
395 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
396 mContext,
397 LocationManager.FUSED_PROVIDER,
398 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700399 com.android.internal.R.bool.config_enableFusedLocationOverlay,
400 com.android.internal.R.string.config_fusedLocationProviderPackageName,
401 com.android.internal.R.array.config_locationProviderPackageNames,
402 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700403 if (fusedLocationProvider != null) {
404 addProviderLocked(fusedLocationProvider);
405 mProxyProviders.add(fusedLocationProvider);
406 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700407 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700408 } else {
409 Slog.e(TAG, "no fused location provider found",
410 new IllegalStateException("Location service needs a fused location provider"));
411 }
412
413 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700414 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
415 com.android.internal.R.bool.config_enableGeocoderOverlay,
416 com.android.internal.R.string.config_geocoderProviderPackageName,
417 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800418 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700419 if (mGeocodeProvider == null) {
420 Slog.e(TAG, "no geocoder provider found");
421 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700422
destradaa1af4b022013-07-12 15:43:36 -0700423 // bind to fused provider
destradaa1af4b022013-07-12 15:43:36 -0700424 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
425 FusedProxy fusedProxy = FusedProxy.createAndBind(
426 mContext,
427 mLocationHandler,
destradaa64be0c62013-08-09 15:01:49 -0700428 flpHardwareProvider.getLocationHardware(),
429 com.android.internal.R.bool.config_enableFusedLocationOverlay,
430 com.android.internal.R.string.config_fusedLocationProviderPackageName,
431 com.android.internal.R.array.config_locationProviderPackageNames);
destradaa1af4b022013-07-12 15:43:36 -0700432 if(fusedProxy == null) {
433 Slog.e(TAG, "No FusedProvider found.");
434 }
destradaa0682809a2013-08-12 18:50:30 -0700435
436 // bind to geofence provider
437 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
438 com.android.internal.R.bool.config_enableGeofenceOverlay,
439 com.android.internal.R.string.config_geofenceProviderPackageName,
440 com.android.internal.R.array.config_locationProviderPackageNames,
441 mLocationHandler,
442 gpsProvider.getGpsGeofenceProxy(),
443 flpHardwareProvider.getGeofenceHardware());
444 if (provider == null) {
445 Slog.e(TAG, "no geofence provider found");
446 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700447 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700450 * Called when the device's active user changes.
451 * @param userId the new active user's UserId
452 */
453 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700454 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800455 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700456 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700457 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700458 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700459 for (LocationProviderInterface p : mProviders) {
460 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700461 }
Victoria Lease38389b62012-09-30 11:44:22 -0700462 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700463 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700464 }
465 }
466
467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
469 * location updates.
470 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700471 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700472 final int mUid; // uid of receiver
473 final int mPid; // pid of receiver
474 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700475 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 final ILocationListener mListener;
478 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700479 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700480 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700482
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400483 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700484
David Christie0b837452013-07-29 16:02:13 -0700485 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700486 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700487 // True if app ops has started monitoring this receiver for high power (gps) locations.
488 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700489 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700490 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700492 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700493 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700496 if (listener != null) {
497 mKey = listener.asBinder();
498 } else {
499 mKey = intent;
500 }
Victoria Lease37425c32012-10-16 16:08:48 -0700501 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700502 mUid = uid;
503 mPid = pid;
504 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700505 if (workSource != null && workSource.size() <= 0) {
506 workSource = null;
507 }
508 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700509 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700510
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700511 updateMonitoring(true);
512
Victoria Lease0aa28602013-05-29 15:28:26 -0700513 // construct/configure wakelock
514 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700515 if (workSource == null) {
516 workSource = new WorkSource(mUid, mPackageName);
517 }
518 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520
521 @Override
522 public boolean equals(Object otherObj) {
523 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700524 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526 return false;
527 }
528
529 @Override
530 public int hashCode() {
531 return mKey.hashCode();
532 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 @Override
535 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700536 StringBuilder s = new StringBuilder();
537 s.append("Reciever[");
538 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700540 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700542 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700544 for (String p : mUpdateRecords.keySet()) {
545 s.append(" ").append(mUpdateRecords.get(p).toString());
546 }
547 s.append("]");
548 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
550
David Christie15b3191f2013-08-13 15:54:32 -0700551 /**
552 * Update AppOp monitoring for this receiver.
553 *
554 * @param allow If true receiver is currently active, if false it's been removed.
555 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700556 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700557 if (mHideFromAppOps) {
558 return;
559 }
560
David Christie15b3191f2013-08-13 15:54:32 -0700561 boolean requestingLocation = false;
562 boolean requestingHighPowerLocation = false;
563 if (allow) {
564 // See if receiver has any enabled update records. Also note if any update records
565 // are high power (has a high power provider with an interval under a threshold).
566 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
567 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
568 requestingLocation = true;
569 LocationProviderInterface locationProvider
570 = mProvidersByName.get(updateRecord.mProvider);
571 ProviderProperties properties = locationProvider != null
572 ? locationProvider.getProperties() : null;
573 if (properties != null
574 && properties.mPowerRequirement == Criteria.POWER_HIGH
575 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
576 requestingHighPowerLocation = true;
577 break;
578 }
579 }
580 }
581 }
582
David Christie0b837452013-07-29 16:02:13 -0700583 // First update monitoring of any location request (including high power).
David Christie15b3191f2013-08-13 15:54:32 -0700584 mOpMonitoring = updateMonitoring(
585 requestingLocation,
586 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700587 AppOpsManager.OP_MONITOR_LOCATION);
588
589 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700590 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b3191f2013-08-13 15:54:32 -0700591 mOpHighPowerMonitoring = updateMonitoring(
592 requestingHighPowerLocation,
593 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700594 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700595 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b3191f2013-08-13 15:54:32 -0700596 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700597 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
598 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
599 }
David Christie0b837452013-07-29 16:02:13 -0700600 }
601
602 /**
603 * Update AppOps monitoring for a single location request and op type.
604 *
605 * @param allowMonitoring True if monitoring is allowed for this request/op.
606 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
607 * @param op AppOps code for the op to update.
608 * @return True if monitoring is on for this request/op after updating.
609 */
610 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
611 int op) {
612 if (!currentlyMonitoring) {
613 if (allowMonitoring) {
614 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
615 == AppOpsManager.MODE_ALLOWED;
616 }
617 } else {
618 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
619 != AppOpsManager.MODE_ALLOWED) {
620 mAppOps.finishOp(op, mUid, mPackageName);
621 return false;
622 }
623 }
624
625 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700626 }
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 public boolean isListener() {
629 return mListener != null;
630 }
631
632 public boolean isPendingIntent() {
633 return mPendingIntent != null;
634 }
635
636 public ILocationListener getListener() {
637 if (mListener != null) {
638 return mListener;
639 }
640 throw new IllegalStateException("Request for non-existent listener");
641 }
642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
644 if (mListener != null) {
645 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700646 synchronized (this) {
647 // synchronize to ensure incrementPendingBroadcastsLocked()
648 // is called before decrementPendingBroadcasts()
649 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700650 // call this after broadcasting so we do not increment
651 // if we throw an exeption.
652 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 } catch (RemoteException e) {
655 return false;
656 }
657 } else {
658 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800659 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
661 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700662 synchronized (this) {
663 // synchronize to ensure incrementPendingBroadcastsLocked()
664 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700665 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700666 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700667 // call this after broadcasting so we do not increment
668 // if we throw an exeption.
669 incrementPendingBroadcastsLocked();
670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 } catch (PendingIntent.CanceledException e) {
672 return false;
673 }
674 }
675 return true;
676 }
677
678 public boolean callLocationChangedLocked(Location location) {
679 if (mListener != null) {
680 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700681 synchronized (this) {
682 // synchronize to ensure incrementPendingBroadcastsLocked()
683 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800684 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700685 // call this after broadcasting so we do not increment
686 // if we throw an exeption.
687 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 } catch (RemoteException e) {
690 return false;
691 }
692 } else {
693 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800694 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700696 synchronized (this) {
697 // synchronize to ensure incrementPendingBroadcastsLocked()
698 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700699 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700700 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700701 // call this after broadcasting so we do not increment
702 // if we throw an exeption.
703 incrementPendingBroadcastsLocked();
704 }
705 } catch (PendingIntent.CanceledException e) {
706 return false;
707 }
708 }
709 return true;
710 }
711
712 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b3191f2013-08-13 15:54:32 -0700713 // First update AppOp monitoring.
714 // An app may get/lose location access as providers are enabled/disabled.
715 updateMonitoring(true);
716
Mike Lockwood48f17512009-04-23 09:12:08 -0700717 if (mListener != null) {
718 try {
719 synchronized (this) {
720 // synchronize to ensure incrementPendingBroadcastsLocked()
721 // is called before decrementPendingBroadcasts()
722 if (enabled) {
723 mListener.onProviderEnabled(provider);
724 } else {
725 mListener.onProviderDisabled(provider);
726 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700727 // call this after broadcasting so we do not increment
728 // if we throw an exeption.
729 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700730 }
731 } catch (RemoteException e) {
732 return false;
733 }
734 } else {
735 Intent providerIntent = new Intent();
736 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
737 try {
738 synchronized (this) {
739 // synchronize to ensure incrementPendingBroadcastsLocked()
740 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700741 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700742 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700743 // call this after broadcasting so we do not increment
744 // if we throw an exeption.
745 incrementPendingBroadcastsLocked();
746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 } catch (PendingIntent.CanceledException e) {
748 return false;
749 }
750 }
751 return true;
752 }
753
Nick Pellyf1be6862012-05-15 10:53:42 -0700754 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700756 if (D) Log.d(TAG, "Location listener died");
757
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400758 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 removeUpdatesLocked(this);
760 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700761 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700762 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700763 }
764 }
765
Nick Pellye0fd6932012-07-11 10:26:13 -0700766 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700767 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
768 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400769 synchronized (this) {
770 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700771 }
772 }
773
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400774 // this must be called while synchronized by caller in a synchronized block
775 // containing the sending of the broadcaset
776 private void incrementPendingBroadcastsLocked() {
777 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700778 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400779 }
780 }
781
782 private void decrementPendingBroadcastsLocked() {
783 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700784 if (mWakeLock.isHeld()) {
785 mWakeLock.release();
786 }
787 }
788 }
789
790 public void clearPendingBroadcastsLocked() {
791 if (mPendingBroadcasts > 0) {
792 mPendingBroadcasts = 0;
793 if (mWakeLock.isHeld()) {
794 mWakeLock.release();
795 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700796 }
797 }
798 }
799
Nick Pellye0fd6932012-07-11 10:26:13 -0700800 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700801 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700802 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400803 //the receiver list if it is not found. If it is not found then the
804 //LocationListener was removed when it had a pending broadcast and should
805 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700806 synchronized (mLock) {
807 IBinder binder = listener.asBinder();
808 Receiver receiver = mReceivers.get(binder);
809 if (receiver != null) {
810 synchronized (receiver) {
811 // so wakelock calls will succeed
812 long identity = Binder.clearCallingIdentity();
813 receiver.decrementPendingBroadcastsLocked();
814 Binder.restoreCallingIdentity(identity);
815 }
816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818 }
819
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700820 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400821 mProviders.add(provider);
822 mProvidersByName.put(provider.getName(), provider);
823 }
824
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700825 private void removeProviderLocked(LocationProviderInterface provider) {
826 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400827 mProviders.remove(provider);
828 mProvidersByName.remove(provider.getName());
829 }
830
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800831 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800832 * Returns "true" if access to the specified location provider is allowed by the current
833 * user's settings. Access to all location providers is forbidden to non-location-provider
834 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800835 *
836 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800837 * @return
838 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800839 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 if (mEnabledProviders.contains(provider)) {
841 return true;
842 }
843 if (mDisabledProviders.contains(provider)) {
844 return false;
845 }
846 // Use system settings
847 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848
Victoria Leaseb711d572012-10-02 13:14:11 -0700849 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700852 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800853 * Returns "true" if access to the specified location provider is allowed by the specified
854 * user's settings. Access to all location providers is forbidden to non-location-provider
855 * processes belonging to background users.
856 *
857 * @param provider the name of the location provider
858 * @param uid the requestor's UID
859 * @return
860 */
861 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
862 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
863 return false;
864 }
865 return isAllowedByCurrentUserSettingsLocked(provider);
866 }
867
868 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700869 * Returns the permission string associated with the specified resolution level.
870 *
871 * @param resolutionLevel the resolution level
872 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700873 */
Victoria Lease37425c32012-10-16 16:08:48 -0700874 private String getResolutionPermission(int resolutionLevel) {
875 switch (resolutionLevel) {
876 case RESOLUTION_LEVEL_FINE:
877 return android.Manifest.permission.ACCESS_FINE_LOCATION;
878 case RESOLUTION_LEVEL_COARSE:
879 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
880 default:
881 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700883 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700884
Victoria Leaseda479c52012-10-15 15:24:16 -0700885 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700886 * Returns the resolution level allowed to the given PID/UID pair.
887 *
888 * @param pid the PID
889 * @param uid the UID
890 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700891 */
Victoria Lease37425c32012-10-16 16:08:48 -0700892 private int getAllowedResolutionLevel(int pid, int uid) {
893 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
894 pid, uid) == PackageManager.PERMISSION_GRANTED) {
895 return RESOLUTION_LEVEL_FINE;
896 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
897 pid, uid) == PackageManager.PERMISSION_GRANTED) {
898 return RESOLUTION_LEVEL_COARSE;
899 } else {
900 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700901 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700902 }
903
904 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700905 * Returns the resolution level allowed to the caller
906 *
907 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700908 */
Victoria Lease37425c32012-10-16 16:08:48 -0700909 private int getCallerAllowedResolutionLevel() {
910 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
911 }
912
913 /**
914 * Throw SecurityException if specified resolution level is insufficient to use geofences.
915 *
916 * @param allowedResolutionLevel resolution level allowed to caller
917 */
918 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
919 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700920 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
923
Victoria Lease37425c32012-10-16 16:08:48 -0700924 /**
925 * Return the minimum resolution level required to use the specified location provider.
926 *
927 * @param provider the name of the location provider
928 * @return minimum resolution level required for provider
929 */
930 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700931 if (LocationManager.GPS_PROVIDER.equals(provider) ||
932 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
933 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700934 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700935 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
936 LocationManager.FUSED_PROVIDER.equals(provider)) {
937 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700938 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700939 } else {
940 // mock providers
941 LocationProviderInterface lp = mMockProviders.get(provider);
942 if (lp != null) {
943 ProviderProperties properties = lp.getProperties();
944 if (properties != null) {
945 if (properties.mRequiresSatellite) {
946 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700947 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700948 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
949 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700950 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700951 }
952 }
953 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700954 }
Victoria Lease37425c32012-10-16 16:08:48 -0700955 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700956 }
957
Victoria Lease37425c32012-10-16 16:08:48 -0700958 /**
959 * Throw SecurityException if specified resolution level is insufficient to use the named
960 * location provider.
961 *
962 * @param allowedResolutionLevel resolution level allowed to caller
963 * @param providerName the name of the location provider
964 */
965 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
966 String providerName) {
967 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
968 if (allowedResolutionLevel < requiredResolutionLevel) {
969 switch (requiredResolutionLevel) {
970 case RESOLUTION_LEVEL_FINE:
971 throw new SecurityException("\"" + providerName + "\" location provider " +
972 "requires ACCESS_FINE_LOCATION permission.");
973 case RESOLUTION_LEVEL_COARSE:
974 throw new SecurityException("\"" + providerName + "\" location provider " +
975 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
976 default:
977 throw new SecurityException("Insufficient permission for \"" + providerName +
978 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700979 }
980 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700981 }
982
David Christie82edc9b2013-07-19 11:31:42 -0700983 /**
984 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
985 * for battery).
986 */
David Christie40e57822013-07-30 11:36:48 -0700987 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -0700988 mContext.enforceCallingOrSelfPermission(
989 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
990 }
991
David Christie40e57822013-07-30 11:36:48 -0700992 private void checkUpdateAppOpsAllowed() {
993 mContext.enforceCallingOrSelfPermission(
994 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
995 }
996
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800997 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800998 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
999 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001000 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001001 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001002 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001003 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001004 }
1005 return -1;
1006 }
1007
1008 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
1009 int op = resolutionLevelToOp(allowedResolutionLevel);
1010 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001011 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1012 return false;
1013 }
1014 }
1015 return true;
1016 }
1017
1018 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001019 int op = resolutionLevelToOp(allowedResolutionLevel);
1020 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001021 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1022 return false;
1023 }
1024 }
1025 return true;
1026 }
1027
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001028 /**
1029 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001030 * fused, also including ones that are not permitted to
1031 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001032 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001033 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001035 ArrayList<String> out;
1036 synchronized (mLock) {
1037 out = new ArrayList<String>(mProviders.size());
1038 for (LocationProviderInterface provider : mProviders) {
1039 String name = provider.getName();
1040 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001041 continue;
1042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 out.add(name);
1044 }
1045 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001046
1047 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 return out;
1049 }
1050
Mike Lockwood03ca2162010-04-01 08:10:09 -07001051 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001052 * Return all providers by name, that match criteria and are optionally
1053 * enabled.
1054 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001055 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001056 @Override
1057 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001058 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001059 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001060 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001061 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001062 try {
1063 synchronized (mLock) {
1064 out = new ArrayList<String>(mProviders.size());
1065 for (LocationProviderInterface provider : mProviders) {
1066 String name = provider.getName();
1067 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001068 continue;
1069 }
Victoria Lease37425c32012-10-16 16:08:48 -07001070 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001071 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001072 continue;
1073 }
1074 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1075 name, provider.getProperties(), criteria)) {
1076 continue;
1077 }
1078 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001079 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001080 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001081 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001082 } finally {
1083 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001084 }
1085
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001086 if (D) Log.d(TAG, "getProviders()=" + out);
1087 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001088 }
1089
1090 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001091 * Return the name of the best provider given a Criteria object.
1092 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001093 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001094 * has been deprecated as well. So this method now uses
1095 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001096 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001097 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001098 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001099 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001100
1101 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001102 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001103 result = pickBest(providers);
1104 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1105 return result;
1106 }
1107 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001108 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001109 result = pickBest(providers);
1110 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1111 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001112 }
1113
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001114 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001115 return null;
1116 }
1117
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001118 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001119 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001120 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001121 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1122 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001123 } else {
1124 return providers.get(0);
1125 }
1126 }
1127
Nick Pellye0fd6932012-07-11 10:26:13 -07001128 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001129 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1130 LocationProviderInterface p = mProvidersByName.get(provider);
1131 if (p == null) {
1132 throw new IllegalArgumentException("provider=" + provider);
1133 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001134
1135 boolean result = LocationProvider.propertiesMeetCriteria(
1136 p.getName(), p.getProperties(), criteria);
1137 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1138 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001139 }
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001142 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001143 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001144 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 boolean isEnabled = p.isEnabled();
1146 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001147 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001149 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001150 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001152 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001153 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001155 }
1156 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001157 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1158 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160 }
1161
Victoria Leaseb711d572012-10-02 13:14:11 -07001162 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 int listeners = 0;
1164
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001165 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001166 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167
1168 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1171 if (records != null) {
1172 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001173 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001175 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001176 // Sends a notification message to the receiver
1177 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1178 if (deadReceivers == null) {
1179 deadReceivers = new ArrayList<Receiver>();
1180 }
1181 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001183 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
1187
1188 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001189 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 removeUpdatesLocked(deadReceivers.get(i));
1191 }
1192 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 if (enabled) {
1195 p.enable();
1196 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001197 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001204 private void applyRequirementsLocked(String provider) {
1205 LocationProviderInterface p = mProvidersByName.get(provider);
1206 if (p == null) return;
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001209 WorkSource worksource = new WorkSource();
1210 ProviderRequest providerRequest = new ProviderRequest();
1211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001213 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001214 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001215 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1216 record.mReceiver.mAllowedResolutionLevel)) {
1217 LocationRequest locationRequest = record.mRequest;
1218 providerRequest.locationRequests.add(locationRequest);
1219 if (locationRequest.getInterval() < providerRequest.interval) {
1220 providerRequest.reportLocation = true;
1221 providerRequest.interval = locationRequest.getInterval();
1222 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001223 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001224 }
1225 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001226
1227 if (providerRequest.reportLocation) {
1228 // calculate who to blame for power
1229 // This is somewhat arbitrary. We pick a threshold interval
1230 // that is slightly higher that the minimum interval, and
1231 // spread the blame across all applications with a request
1232 // under that threshold.
1233 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1234 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001235 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001236 LocationRequest locationRequest = record.mRequest;
1237 if (locationRequest.getInterval() <= thresholdInterval) {
David Christie82edc9b2013-07-19 11:31:42 -07001238 if (record.mReceiver.mWorkSource != null) {
1239 // Assign blame to another work source.
1240 worksource.add(record.mReceiver.mWorkSource);
1241 } else {
1242 // Assign blame to caller.
1243 worksource.add(
1244 record.mReceiver.mUid,
1245 record.mReceiver.mPackageName);
1246 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001247 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001248 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 }
1251 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001252
1253 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1254 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256
1257 private class UpdateRecord {
1258 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001259 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001261 Location mLastFixBroadcast;
1262 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263
1264 /**
1265 * Note: must be constructed with lock held.
1266 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001267 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001269 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
1272 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1273 if (records == null) {
1274 records = new ArrayList<UpdateRecord>();
1275 mRecordsByProvider.put(provider, records);
1276 }
1277 if (!records.contains(this)) {
1278 records.add(this);
1279 }
1280 }
1281
1282 /**
1283 * Method to be called when a record will no longer be used. Calling this multiple times
1284 * must have the same effect as calling it once.
1285 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001286 void disposeLocked(boolean removeReceiver) {
1287 // remove from mRecordsByProvider
1288 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1289 if (globalRecords != null) {
1290 globalRecords.remove(this);
1291 }
1292
1293 if (!removeReceiver) return; // the caller will handle the rest
1294
1295 // remove from Receiver#mUpdateRecords
1296 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1297 if (receiverRecords != null) {
1298 receiverRecords.remove(this.mProvider);
1299
1300 // and also remove the Receiver if it has no more update records
1301 if (removeReceiver && receiverRecords.size() == 0) {
1302 removeUpdatesLocked(mReceiver);
1303 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306
1307 @Override
1308 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001309 StringBuilder s = new StringBuilder();
1310 s.append("UpdateRecord[");
1311 s.append(mProvider);
1312 s.append(' ').append(mReceiver.mPackageName).append('(');
1313 s.append(mReceiver.mUid).append(')');
1314 s.append(' ').append(mRequest);
1315 s.append(']');
1316 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001320 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001321 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001322 IBinder binder = listener.asBinder();
1323 Receiver receiver = mReceivers.get(binder);
1324 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001325 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1326 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001327 mReceivers.put(binder, receiver);
1328
1329 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001330 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001331 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001332 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001333 return null;
1334 }
1335 }
1336 return receiver;
1337 }
1338
David Christie82edc9b2013-07-19 11:31:42 -07001339 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001340 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001341 Receiver receiver = mReceivers.get(intent);
1342 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001343 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1344 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001345 mReceivers.put(intent, receiver);
1346 }
1347 return receiver;
1348 }
1349
Victoria Lease37425c32012-10-16 16:08:48 -07001350 /**
1351 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1352 * and consistency requirements.
1353 *
1354 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001355 * @return a version of request that meets the given resolution and consistency requirements
1356 * @hide
1357 */
1358 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1359 LocationRequest sanitizedRequest = new LocationRequest(request);
1360 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1361 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001362 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001363 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001364 break;
1365 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001366 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001367 break;
1368 }
1369 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001370 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1371 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001372 }
Victoria Lease37425c32012-10-16 16:08:48 -07001373 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1374 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001375 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001376 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001377 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001378 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001379 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001380 }
Victoria Lease37425c32012-10-16 16:08:48 -07001381 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001382 }
1383
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001384 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001385 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001386 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001387 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001388 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001389 String[] packages = mPackageManager.getPackagesForUid(uid);
1390 if (packages == null) {
1391 throw new SecurityException("invalid UID " + uid);
1392 }
1393 for (String pkg : packages) {
1394 if (packageName.equals(pkg)) return;
1395 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001396 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001397 }
1398
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001399 private void checkPendingIntent(PendingIntent intent) {
1400 if (intent == null) {
1401 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001402 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001403 }
1404
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001405 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001406 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001407 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001408 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001409 } else if (intent != null && listener != null) {
1410 throw new IllegalArgumentException("cannot register both listener and intent");
1411 } else if (intent != null) {
1412 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001413 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001414 } else {
David Christie40e57822013-07-30 11:36:48 -07001415 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001416 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001417 }
1418
Nick Pellye0fd6932012-07-11 10:26:13 -07001419 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001420 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1421 PendingIntent intent, String packageName) {
1422 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1423 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001424 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1425 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1426 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001427 WorkSource workSource = request.getWorkSource();
1428 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001429 checkDeviceStatsAllowed();
1430 }
1431 boolean hideFromAppOps = request.getHideFromAppOps();
1432 if (hideFromAppOps) {
1433 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001434 }
Victoria Lease37425c32012-10-16 16:08:48 -07001435 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001437 final int pid = Binder.getCallingPid();
1438 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001439 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 long identity = Binder.clearCallingIdentity();
1441 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001442 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1443 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001444 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001445
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001446 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001447 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001448 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001449 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 } finally {
1452 Binder.restoreCallingIdentity(identity);
1453 }
1454 }
1455
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001456 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1457 int pid, int uid, String packageName) {
1458 // Figure out the provider. Either its explicitly request (legacy use cases), or
1459 // use the fused provider
1460 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1461 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001462 if (name == null) {
1463 throw new IllegalArgumentException("provider name must not be null");
1464 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001465
1466 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1467 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001468 LocationProviderInterface provider = mProvidersByName.get(name);
1469 if (provider == null) {
1470 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1471 }
1472
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001473 UpdateRecord record = new UpdateRecord(name, request, receiver);
1474 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1475 if (oldRecord != null) {
1476 oldRecord.disposeLocked(false);
1477 }
1478
Victoria Lease09eeaec2013-02-05 11:34:13 -08001479 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001480 if (isProviderEnabled) {
1481 applyRequirementsLocked(name);
1482 } else {
1483 // Notify the listener that updates are currently disabled
1484 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
David Christie0b837452013-07-29 16:02:13 -07001486 // Update the monitoring here just in case multiple location requests were added to the
1487 // same receiver (this request may be high power and the initial might not have been).
1488 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490
Nick Pellye0fd6932012-07-11 10:26:13 -07001491 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001492 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1493 String packageName) {
1494 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001495
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001496 final int pid = Binder.getCallingPid();
1497 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001498
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001499 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001500 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001501 boolean hideFromAppOps = false;
1502 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1503 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001504
1505 // providers may use public location API's, need to clear identity
1506 long identity = Binder.clearCallingIdentity();
1507 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001508 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001509 } finally {
1510 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513 }
1514
1515 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001516 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001517
1518 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1519 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1520 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001521 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001525 receiver.updateMonitoring(false);
1526
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001527 // Record which providers were associated with this listener
1528 HashSet<String> providers = new HashSet<String>();
1529 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1530 if (oldRecords != null) {
1531 // Call dispose() on the obsolete update records.
1532 for (UpdateRecord record : oldRecords.values()) {
1533 record.disposeLocked(false);
1534 }
1535 // Accumulate providers
1536 providers.addAll(oldRecords.keySet());
1537 }
1538
1539 // update provider
1540 for (String provider : providers) {
1541 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001542 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001543 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001546 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
1549
Dianne Hackbornc2293022013-02-06 23:14:49 -08001550 private void applyAllProviderRequirementsLocked() {
1551 for (LocationProviderInterface p : mProviders) {
1552 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001553 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001554 continue;
1555 }
1556
1557 applyRequirementsLocked(p.getName());
1558 }
1559 }
1560
Nick Pellye0fd6932012-07-11 10:26:13 -07001561 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001562 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001563 if (D) Log.d(TAG, "getLastLocation: " + request);
1564 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001565 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001566 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001567 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1568 request.getProvider());
1569 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001570
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001571 final int uid = Binder.getCallingUid();
1572 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001573 try {
1574 if (mBlacklist.isBlacklisted(packageName)) {
1575 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1576 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001577 return null;
1578 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001579
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001580 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1581 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1582 packageName);
1583 return null;
1584 }
1585
Victoria Leaseb711d572012-10-02 13:14:11 -07001586 synchronized (mLock) {
1587 // Figure out the provider. Either its explicitly request (deprecated API's),
1588 // or use the fused provider
1589 String name = request.getProvider();
1590 if (name == null) name = LocationManager.FUSED_PROVIDER;
1591 LocationProviderInterface provider = mProvidersByName.get(name);
1592 if (provider == null) return null;
1593
Victoria Lease09eeaec2013-02-05 11:34:13 -08001594 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001595
David Christie1b9b7b12013-04-15 15:31:11 -07001596 Location location;
1597 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1598 // Make sure that an app with coarse permissions can't get frequent location
1599 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1600 location = mLastLocationCoarseInterval.get(name);
1601 } else {
1602 location = mLastLocation.get(name);
1603 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001604 if (location == null) {
1605 return null;
1606 }
Victoria Lease37425c32012-10-16 16:08:48 -07001607 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001608 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1609 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001610 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001611 }
Victoria Lease37425c32012-10-16 16:08:48 -07001612 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001613 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001614 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001615 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001616 return null;
1617 } finally {
1618 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001619 }
1620 }
1621
1622 @Override
1623 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1624 String packageName) {
1625 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001626 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1627 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001628 checkPendingIntent(intent);
1629 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001630 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1631 request.getProvider());
1632 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001633
Victoria Lease37425c32012-10-16 16:08:48 -07001634 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001635
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001636 // geo-fence manager uses the public location API, need to clear identity
1637 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001638 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1639 // temporary measure until geofences work for secondary users
1640 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1641 return;
1642 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001643 long identity = Binder.clearCallingIdentity();
1644 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001645 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1646 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001647 } finally {
1648 Binder.restoreCallingIdentity(identity);
1649 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001650 }
1651
1652 @Override
1653 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001654 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001655 checkPendingIntent(intent);
1656 checkPackageName(packageName);
1657
1658 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1659
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001660 // geo-fence manager uses the public location API, need to clear identity
1661 long identity = Binder.clearCallingIdentity();
1662 try {
1663 mGeofenceManager.removeFence(geofence, intent);
1664 } finally {
1665 Binder.restoreCallingIdentity(identity);
1666 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001667 }
1668
1669
1670 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001671 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001672 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 return false;
1674 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001675 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1676 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001677 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001679 final int uid = Binder.getCallingUid();
1680 final long ident = Binder.clearCallingIdentity();
1681 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001682 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001683 return false;
1684 }
1685 } finally {
1686 Binder.restoreCallingIdentity(ident);
1687 }
1688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001690 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 return false;
1694 }
1695 return true;
1696 }
1697
Nick Pellye0fd6932012-07-11 10:26:13 -07001698 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001700 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001701 try {
1702 mGpsStatusProvider.removeGpsStatusListener(listener);
1703 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001704 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
1707 }
1708
Nick Pellye0fd6932012-07-11 10:26:13 -07001709 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001711 if (provider == null) {
1712 // throw NullPointerException to remain compatible with previous implementation
1713 throw new NullPointerException();
1714 }
Victoria Lease37425c32012-10-16 16:08:48 -07001715 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1716 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001719 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 != PackageManager.PERMISSION_GRANTED)) {
1721 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1722 }
1723
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001724 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001725 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001726 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001727
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001728 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730 }
1731
Nick Pellye0fd6932012-07-11 10:26:13 -07001732 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001733 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001734 if (Binder.getCallingUid() != Process.myUid()) {
1735 throw new SecurityException(
1736 "calling sendNiResponse from outside of the system is not allowed");
1737 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001738 try {
1739 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001740 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001741 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001742 return false;
1743 }
1744 }
1745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001747 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001748 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 * accessed by the caller
1750 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001751 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001752 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001753 if (mProvidersByName.get(provider) == null) {
1754 return null;
1755 }
1756
Victoria Lease37425c32012-10-16 16:08:48 -07001757 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1758 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001760 LocationProviderInterface p;
1761 synchronized (mLock) {
1762 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
1764
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001765 if (p == null) return null;
1766 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
1768
Nick Pellye0fd6932012-07-11 10:26:13 -07001769 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001771 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1772 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001773 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1774
Victoria Lease09eeaec2013-02-05 11:34:13 -08001775 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001776 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001777 try {
1778 synchronized (mLock) {
1779 LocationProviderInterface p = mProvidersByName.get(provider);
1780 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001781
Victoria Lease09eeaec2013-02-05 11:34:13 -08001782 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001783 }
1784 } finally {
1785 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001786 }
1787 }
1788
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001789 /**
1790 * Returns "true" if the UID belongs to a bound location provider.
1791 *
1792 * @param uid the uid
1793 * @return true if uid belongs to a bound location provider
1794 */
1795 private boolean isUidALocationProvider(int uid) {
1796 if (uid == Process.SYSTEM_UID) {
1797 return true;
1798 }
1799 if (mGeocodeProvider != null) {
1800 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1801 }
1802 for (LocationProviderProxy proxy : mProxyProviders) {
1803 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1804 }
1805 return false;
1806 }
1807
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001808 private void checkCallerIsProvider() {
1809 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1810 == PackageManager.PERMISSION_GRANTED) {
1811 return;
1812 }
1813
1814 // Previously we only used the INSTALL_LOCATION_PROVIDER
1815 // check. But that is system or signature
1816 // protection level which is not flexible enough for
1817 // providers installed oustide the system image. So
1818 // also allow providers with a UID matching the
1819 // currently bound package name
1820
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001821 if (isUidALocationProvider(Binder.getCallingUid())) {
1822 return;
1823 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001824
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001825 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1826 "or UID of a currently bound location provider");
1827 }
1828
1829 private boolean doesPackageHaveUid(int uid, String packageName) {
1830 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 return false;
1832 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001833 try {
1834 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1835 if (appInfo.uid != uid) {
1836 return false;
1837 }
1838 } catch (NameNotFoundException e) {
1839 return false;
1840 }
1841 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843
Nick Pellye0fd6932012-07-11 10:26:13 -07001844 @Override
Mike Lockwooda4903f252010-02-17 06:42:23 -05001845 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001846 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001847
Nick Pelly2eeeec22012-07-18 13:13:37 -07001848 if (!location.isComplete()) {
1849 Log.w(TAG, "Dropping incomplete location: " + location);
1850 return;
1851 }
1852
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001853 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1854 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f252010-02-17 06:42:23 -05001855 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001856 mLocationHandler.sendMessageAtFrontOfQueue(m);
1857 }
1858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859
Laurent Tu75defb62012-11-01 16:21:52 -07001860 private static boolean shouldBroadcastSafe(
1861 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 // Always broadcast the first update
1863 if (lastLoc == null) {
1864 return true;
1865 }
1866
Nick Pellyf1be6862012-05-15 10:53:42 -07001867 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001868 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001869 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1870 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001871 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 return false;
1873 }
1874
1875 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001876 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 if (minDistance > 0.0) {
1878 if (loc.distanceTo(lastLoc) <= minDistance) {
1879 return false;
1880 }
1881 }
1882
Laurent Tu75defb62012-11-01 16:21:52 -07001883 // Check whether sufficient number of udpates is left
1884 if (record.mRequest.getNumUpdates() <= 0) {
1885 return false;
1886 }
1887
1888 // Check whether the expiry date has passed
1889 if (record.mRequest.getExpireAt() < now) {
1890 return false;
1891 }
1892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 return true;
1894 }
1895
Mike Lockwooda4903f252010-02-17 06:42:23 -05001896 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001897 if (D) Log.d(TAG, "incoming location: " + location);
1898
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001899 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f252010-02-17 06:42:23 -05001900 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901
Laurent Tu60ec50a2012-10-04 17:00:10 -07001902 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001903 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001904 if (p == null) return;
1905
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001906 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001907 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1908 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001909 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001910 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001911 lastLocation = new Location(provider);
1912 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001913 } else {
1914 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1915 if (noGPSLocation == null && lastNoGPSLocation != null) {
1916 // New location has no no-GPS location: adopt last no-GPS location. This is set
1917 // directly into location because we do not want to notify COARSE clients.
1918 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1919 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001920 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001921 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922
David Christie1b9b7b12013-04-15 15:31:11 -07001923 // Update last known coarse interval location if enough time has passed.
1924 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1925 if (lastLocationCoarseInterval == null) {
1926 lastLocationCoarseInterval = new Location(location);
1927 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1928 }
1929 long timeDiffNanos = location.getElapsedRealtimeNanos()
1930 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1931 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1932 lastLocationCoarseInterval.set(location);
1933 }
1934 // Don't ever return a coarse location that is more recent than the allowed update
1935 // interval (i.e. don't allow an app to keep registering and unregistering for
1936 // location updates to overcome the minimum interval).
1937 noGPSLocation =
1938 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1939
Laurent Tu60ec50a2012-10-04 17:00:10 -07001940 // Skip if there are no UpdateRecords for this provider.
1941 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1942 if (records == null || records.size() == 0) return;
1943
Victoria Lease09016ab2012-09-16 12:33:15 -07001944 // Fetch coarse location
1945 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001946 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001947 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1948 }
1949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 // Fetch latest status update time
1951 long newStatusUpdateTime = p.getStatusUpdateTime();
1952
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001953 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 Bundle extras = new Bundle();
1955 int status = p.getStatus(extras);
1956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001958 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001961 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001963 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001964
Victoria Lease269518e2012-10-29 08:25:39 -07001965 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001966 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001967 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001968 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001969 " (current user: " + mCurrentUserId + ", app: " +
1970 receiver.mPackageName + ")");
1971 }
1972 continue;
1973 }
1974
Nick Pelly4035f5a2012-08-17 14:43:49 -07001975 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1976 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1977 receiver.mPackageName);
1978 continue;
1979 }
1980
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001981 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1982 receiver.mAllowedResolutionLevel)) {
1983 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1984 receiver.mPackageName);
1985 continue;
1986 }
1987
Victoria Lease09016ab2012-09-16 12:33:15 -07001988 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001989 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1990 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001991 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001992 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001993 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001994 if (notifyLocation != null) {
1995 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001996 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001997 if (lastLoc == null) {
1998 lastLoc = new Location(notifyLocation);
1999 r.mLastFixBroadcast = lastLoc;
2000 } else {
2001 lastLoc.set(notifyLocation);
2002 }
2003 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2004 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2005 receiverDead = true;
2006 }
Laurent Tu75defb62012-11-01 16:21:52 -07002007 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
2009 }
2010
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002011 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002013 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002015 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002017 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002018 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002019 }
2020 }
2021
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002022 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002023 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002024 if (deadUpdateRecords == null) {
2025 deadUpdateRecords = new ArrayList<UpdateRecord>();
2026 }
2027 deadUpdateRecords.add(r);
2028 }
2029 // track dead receivers
2030 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002031 if (deadReceivers == null) {
2032 deadReceivers = new ArrayList<Receiver>();
2033 }
2034 if (!deadReceivers.contains(receiver)) {
2035 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
2037 }
2038 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002039
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002040 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002042 for (Receiver receiver : deadReceivers) {
2043 removeUpdatesLocked(receiver);
2044 }
2045 }
2046 if (deadUpdateRecords != null) {
2047 for (UpdateRecord r : deadUpdateRecords) {
2048 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002050 applyRequirementsLocked(provider);
2051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053
2054 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002055 public LocationWorkerHandler(Looper looper) {
2056 super(looper, null, true);
2057 }
2058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 @Override
2060 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002061 switch (msg.what) {
2062 case MSG_LOCATION_CHANGED:
2063 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2064 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
2066 }
2067 }
2068
Victoria Lease54ca7aef22013-01-08 09:39:50 -08002069 private boolean isMockProvider(String provider) {
2070 synchronized (mLock) {
2071 return mMockProviders.containsKey(provider);
2072 }
2073 }
2074
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002075 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7aef22013-01-08 09:39:50 -08002076 // create a working copy of the incoming Location so that the service can modify it without
2077 // disturbing the caller's copy
2078 Location myLocation = new Location(location);
2079 String provider = myLocation.getProvider();
2080
2081 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2082 // bit if location did not come from a mock provider because passive/fused providers can
2083 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2084 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2085 myLocation.setIsFromMockProvider(true);
2086 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002087
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002088 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002089 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2090 if (!passive) {
2091 // notify passive provider of the new location
2092 mPassiveProvider.updateLocation(myLocation);
2093 }
Victoria Lease54ca7aef22013-01-08 09:39:50 -08002094 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098
Mike Lockwoode97ae402010-09-29 15:23:46 -04002099 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2100 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002101 public void onPackageDisappeared(String packageName, int reason) {
2102 // remove all receivers associated with this package name
2103 synchronized (mLock) {
2104 ArrayList<Receiver> deadReceivers = null;
2105
2106 for (Receiver receiver : mReceivers.values()) {
2107 if (receiver.mPackageName.equals(packageName)) {
2108 if (deadReceivers == null) {
2109 deadReceivers = new ArrayList<Receiver>();
2110 }
2111 deadReceivers.add(receiver);
2112 }
2113 }
2114
2115 // perform removal outside of mReceivers loop
2116 if (deadReceivers != null) {
2117 for (Receiver receiver : deadReceivers) {
2118 removeUpdatesLocked(receiver);
2119 }
2120 }
2121 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002122 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002123 };
2124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 // Geocoder
2126
Nick Pellye0fd6932012-07-11 10:26:13 -07002127 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002128 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002129 return mGeocodeProvider != null;
2130 }
2131
Nick Pellye0fd6932012-07-11 10:26:13 -07002132 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002134 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002135 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002136 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2137 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002139 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141
Mike Lockwooda55c3212009-04-15 11:10:11 -04002142
Nick Pellye0fd6932012-07-11 10:26:13 -07002143 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002145 double lowerLeftLatitude, double lowerLeftLongitude,
2146 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002147 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002148
2149 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002150 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2151 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2152 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002154 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
2156
2157 // Mock Providers
2158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 private void checkMockPermissionsSafe() {
2160 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2161 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2162 if (!allowMocks) {
2163 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2164 }
2165
2166 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2167 PackageManager.PERMISSION_GRANTED) {
2168 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171
Nick Pellye0fd6932012-07-11 10:26:13 -07002172 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002173 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 checkMockPermissionsSafe();
2175
Mike Lockwooda4903f252010-02-17 06:42:23 -05002176 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2177 throw new IllegalArgumentException("Cannot mock the passive location provider");
2178 }
2179
Mike Lockwood86328a92009-10-23 08:38:25 -04002180 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002181 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002182 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002183 // remove the real provider if we are replacing GPS or network provider
2184 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002185 || LocationManager.NETWORK_PROVIDER.equals(name)
2186 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002187 LocationProviderInterface p = mProvidersByName.get(name);
2188 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002189 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002190 }
2191 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002192 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2194 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002195 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002196 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002197 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002198 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 updateProvidersLocked();
2200 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002201 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
2203
Nick Pellye0fd6932012-07-11 10:26:13 -07002204 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 public void removeTestProvider(String provider) {
2206 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002207 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002208 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002209 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2211 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002212 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002213 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002214
2215 // reinstate real provider if available
2216 LocationProviderInterface realProvider = mRealProviders.get(provider);
2217 if (realProvider != null) {
2218 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002219 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002220 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002221 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002223 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225 }
2226
Nick Pellye0fd6932012-07-11 10:26:13 -07002227 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 public void setTestProviderLocation(String provider, Location loc) {
2229 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002230 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002231 MockProvider mockProvider = mMockProviders.get(provider);
2232 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2234 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002235 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2236 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002237 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002238 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
2240 }
2241
Nick Pellye0fd6932012-07-11 10:26:13 -07002242 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 public void clearTestProviderLocation(String provider) {
2244 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002245 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002246 MockProvider mockProvider = mMockProviders.get(provider);
2247 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2249 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002250 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 }
2252 }
2253
Nick Pellye0fd6932012-07-11 10:26:13 -07002254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 public void setTestProviderEnabled(String provider, boolean enabled) {
2256 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002257 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002258 MockProvider mockProvider = mMockProviders.get(provider);
2259 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2261 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002262 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002264 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 mEnabledProviders.add(provider);
2266 mDisabledProviders.remove(provider);
2267 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002268 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 mEnabledProviders.remove(provider);
2270 mDisabledProviders.add(provider);
2271 }
2272 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002273 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 }
2275 }
2276
Nick Pellye0fd6932012-07-11 10:26:13 -07002277 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 public void clearTestProviderEnabled(String provider) {
2279 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002280 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002281 MockProvider mockProvider = mMockProviders.get(provider);
2282 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2284 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002285 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 mEnabledProviders.remove(provider);
2287 mDisabledProviders.remove(provider);
2288 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002289 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 }
2291 }
2292
Nick Pellye0fd6932012-07-11 10:26:13 -07002293 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2295 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002296 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002297 MockProvider mockProvider = mMockProviders.get(provider);
2298 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2300 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002301 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 }
2303 }
2304
Nick Pellye0fd6932012-07-11 10:26:13 -07002305 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 public void clearTestProviderStatus(String provider) {
2307 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002308 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002309 MockProvider mockProvider = mMockProviders.get(provider);
2310 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2312 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002313 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
2315 }
2316
2317 private void log(String log) {
2318 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002319 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 }
2321 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002322
2323 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2325 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2326 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002327 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 + Binder.getCallingPid()
2329 + ", uid=" + Binder.getCallingUid());
2330 return;
2331 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002332
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002333 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002336 for (Receiver receiver : mReceivers.values()) {
2337 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002340 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2341 pw.println(" " + entry.getKey() + ":");
2342 for (UpdateRecord record : entry.getValue()) {
2343 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 }
2345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002347 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2348 String provider = entry.getKey();
2349 Location location = entry.getValue();
2350 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002352
David Christie1b9b7b12013-04-15 15:31:11 -07002353 pw.println(" Last Known Locations Coarse Intervals:");
2354 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2355 String provider = entry.getKey();
2356 Location location = entry.getValue();
2357 pw.println(" " + provider + ": " + location);
2358 }
2359
Nick Pellye0fd6932012-07-11 10:26:13 -07002360 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 if (mEnabledProviders.size() > 0) {
2363 pw.println(" Enabled Providers:");
2364 for (String i : mEnabledProviders) {
2365 pw.println(" " + i);
2366 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369 if (mDisabledProviders.size() > 0) {
2370 pw.println(" Disabled Providers:");
2371 for (String i : mDisabledProviders) {
2372 pw.println(" " + i);
2373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002375 pw.append(" ");
2376 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 if (mMockProviders.size() > 0) {
2378 pw.println(" Mock Providers:");
2379 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002380 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 }
2382 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002383
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002384 pw.append(" fudger: ");
2385 mLocationFudger.dump(fd, pw, args);
2386
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002387 if (args.length > 0 && "short".equals(args[0])) {
2388 return;
2389 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002390 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002391 pw.print(provider.getName() + " Internal State");
2392 if (provider instanceof LocationProviderProxy) {
2393 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2394 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002395 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002396 pw.println(":");
2397 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 }
2400 }
2401}