blob: 3dcb4886114ec49c0c2186159f2fdf150e2731be [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;
David Christie2ff96af2014-01-30 16:09:37 -080076import com.android.server.location.LocationRequestStatistics;
77import com.android.server.location.LocationRequestStatistics.PackageProviderKey;
78import com.android.server.location.LocationRequestStatistics.PackageStatistics;
Mike Lockwood43e33f22010-03-26 10:41:48 -040079import com.android.server.location.MockProvider;
80import com.android.server.location.PassiveProvider;
81
82import java.io.FileDescriptor;
83import java.io.PrintWriter;
84import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070085import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040086import java.util.HashMap;
87import java.util.HashSet;
88import java.util.List;
89import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040090import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * The service class that manages LocationProviders and issues location
94 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080096public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080098 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070099
100 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Victoria Lease37425c32012-10-16 16:08:48 -0700102 // Location resolution level: no location data whatsoever
103 private static final int RESOLUTION_LEVEL_NONE = 0;
104 // Location resolution level: coarse location data only
105 private static final int RESOLUTION_LEVEL_COARSE = 1;
106 // Location resolution level: fine location data
107 private static final int RESOLUTION_LEVEL_FINE = 2;
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700110 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700112 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400113 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700114 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
115
116 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700117 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700118 private static final String FUSED_LOCATION_SERVICE_ACTION =
119 "com.android.location.service.FusedLocationProvider";
120
121 private static final int MSG_LOCATION_CHANGED = 1;
122
David Christie1b9b7b12013-04-15 15:31:11 -0700123 private static final long NANOS_PER_MILLI = 1000000L;
124
David Christie0b837452013-07-29 16:02:13 -0700125 // The maximum interval a location request can have and still be considered "high power".
126 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
127
Nick Pellyf1be6862012-05-15 10:53:42 -0700128 // Location Providers may sometimes deliver location updates
129 // slightly faster that requested - provide grace period so
130 // we don't unnecessarily filter events that are otherwise on
131 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700132 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700133
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700134 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
135
136 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800137 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700138
139 // used internally for synchronization
140 private final Object mLock = new Object();
141
Victoria Lease5cd731a2012-12-19 15:04:21 -0800142 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700143 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700144 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700145 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700146 private PowerManager mPowerManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700147 private GeocoderProxy mGeocodeProvider;
148 private IGpsStatusProvider mGpsStatusProvider;
149 private INetInitiatedListener mNetInitiatedListener;
150 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700151 private PassiveProvider mPassiveProvider; // track passive provider for special cases
152 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700153
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700154 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 // Set of providers that are explicitly enabled
156 private final Set<String> mEnabledProviders = new HashSet<String>();
157
158 // Set of providers that are explicitly disabled
159 private final Set<String> mDisabledProviders = new HashSet<String>();
160
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700161 // Mock (test) providers
162 private final HashMap<String, MockProvider> mMockProviders =
163 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400166 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700168 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500169 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700170 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400171
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700172 // real providers, saved here when mocked out
173 private final HashMap<String, LocationProviderInterface> mRealProviders =
174 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700176 // mapping from provider name to provider
177 private final HashMap<String, LocationProviderInterface> mProvidersByName =
178 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700180 // mapping from provider name to all its UpdateRecords
181 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
182 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700183
David Christie2ff96af2014-01-30 16:09:37 -0800184 private final LocationRequestStatistics mRequestStatistics = new LocationRequestStatistics();
185
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700186 // mapping from provider name to last known location
187 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
David Christie1b9b7b12013-04-15 15:31:11 -0700189 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
190 // locations stored here are not fudged for coarse permissions.
191 private final HashMap<String, Location> mLastLocationCoarseInterval =
192 new HashMap<String, Location>();
193
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700194 // all providers that operate over proxy, for authorizing incoming location
195 private final ArrayList<LocationProviderProxy> mProxyProviders =
196 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Victoria Lease38389b62012-09-30 11:44:22 -0700198 // current active user on the device - other users are denied location data
199 private int mCurrentUserId = UserHandle.USER_OWNER;
200
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700201 public LocationManagerService(Context context) {
202 super();
203 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800204 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800205
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700206 if (D) Log.d(TAG, "Constructed");
207
208 // most startup is deferred until systemReady()
209 }
210
Svetoslav Ganova0027152013-06-25 14:59:53 -0700211 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700212 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800213 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700214
Victoria Lease5cd731a2012-12-19 15:04:21 -0800215 // fetch package manager
216 mPackageManager = mContext.getPackageManager();
217
Victoria Lease0aa28602013-05-29 15:28:26 -0700218 // fetch power manager
219 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800220
221 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700222 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800223
224 // prepare mLocationHandler's dependents
225 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
226 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
227 mBlacklist.init();
228 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
229
Dianne Hackbornc2293022013-02-06 23:14:49 -0800230 // Monitor for app ops mode changes.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700231 AppOpsManager.OnOpChangedListener callback
232 = new AppOpsManager.OnOpChangedInternalListener() {
233 public void onOpChanged(int op, String packageName) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800234 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700235 for (Receiver receiver : mReceivers.values()) {
236 receiver.updateMonitoring(true);
237 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800238 applyAllProviderRequirementsLocked();
239 }
240 }
241 };
242 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
243
Victoria Lease5cd731a2012-12-19 15:04:21 -0800244 // prepare providers
245 loadProvidersLocked();
246 updateProvidersLocked();
247 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700248
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700249 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700250 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700251 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700252 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800253 @Override
254 public void onChange(boolean selfChange) {
255 synchronized (mLock) {
256 updateProvidersLocked();
257 }
258 }
259 }, UserHandle.USER_ALL);
260 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700261
Victoria Lease38389b62012-09-30 11:44:22 -0700262 // listen for user change
263 IntentFilter intentFilter = new IntentFilter();
264 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
265
266 mContext.registerReceiverAsUser(new BroadcastReceiver() {
267 @Override
268 public void onReceive(Context context, Intent intent) {
269 String action = intent.getAction();
270 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
271 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
272 }
273 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800274 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700275 }
276
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500277 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
278 PackageManager pm = mContext.getPackageManager();
279 String systemPackageName = mContext.getPackageName();
280 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
281
282 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
283 new Intent(FUSED_LOCATION_SERVICE_ACTION),
284 PackageManager.GET_META_DATA, mCurrentUserId);
285 for (ResolveInfo rInfo : rInfos) {
286 String packageName = rInfo.serviceInfo.packageName;
287
288 // Check that the signature is in the list of supported sigs. If it's not in
289 // this list the standard provider binding logic won't bind to it.
290 try {
291 PackageInfo pInfo;
292 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
293 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
294 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
295 ", but has wrong signature, ignoring");
296 continue;
297 }
298 } catch (NameNotFoundException e) {
299 Log.e(TAG, "missing package: " + packageName);
300 continue;
301 }
302
303 // Get the version info
304 if (rInfo.serviceInfo.metaData == null) {
305 Log.w(TAG, "Found fused provider without metadata: " + packageName);
306 continue;
307 }
308
309 int version = rInfo.serviceInfo.metaData.getInt(
310 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
311 if (version == 0) {
312 // This should be the fallback fused location provider.
313
314 // Make sure it's in the system partition.
315 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
316 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
317 continue;
318 }
319
320 // Check that the fallback is signed the same as the OS
321 // as a proxy for coreApp="true"
322 if (pm.checkSignatures(systemPackageName, packageName)
323 != PackageManager.SIGNATURE_MATCH) {
324 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
325 + packageName);
326 continue;
327 }
328
329 // Found a valid fallback.
330 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
331 return;
332 } else {
333 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
334 }
335 }
336
337 throw new IllegalStateException("Unable to find a fused location provider that is in the "
338 + "system partition with version 0 and signed with the platform certificate. "
339 + "Such a package is needed to provide a default fused location provider in the "
340 + "event that no other fused location provider has been installed or is currently "
341 + "available. For example, coreOnly boot mode when decrypting the data "
342 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
343 }
344
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700345 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700346 // create a passive location provider, which is always enabled
347 PassiveProvider passiveProvider = new PassiveProvider(this);
348 addProviderLocked(passiveProvider);
349 mEnabledProviders.add(passiveProvider.getName());
350 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700351 // Create a gps location provider
352 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
353 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700354
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700355 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700356 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
357 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
358 addProviderLocked(gpsProvider);
359 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
360 }
361
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700362 /*
363 Load package name(s) containing location provider support.
364 These packages can contain services implementing location providers:
365 Geocoder Provider, Network Location Provider, and
366 Fused Location Provider. They will each be searched for
367 service components implementing these providers.
368 The location framework also has support for installation
369 of new location providers at run-time. The new package does not
370 have to be explicitly listed here, however it must have a signature
371 that matches the signature of at least one package on this list.
372 */
373 Resources resources = mContext.getResources();
374 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500375 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700376 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500377 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
378 Arrays.toString(pkgs));
379 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
380
381 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700382
383 // bind to network provider
384 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
385 mContext,
386 LocationManager.NETWORK_PROVIDER,
387 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700388 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
389 com.android.internal.R.string.config_networkLocationProviderPackageName,
390 com.android.internal.R.array.config_locationProviderPackageNames,
391 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700392 if (networkProvider != null) {
393 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
394 mProxyProviders.add(networkProvider);
395 addProviderLocked(networkProvider);
396 } else {
397 Slog.w(TAG, "no network location provider found");
398 }
399
400 // bind to fused provider
401 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
402 mContext,
403 LocationManager.FUSED_PROVIDER,
404 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700405 com.android.internal.R.bool.config_enableFusedLocationOverlay,
406 com.android.internal.R.string.config_fusedLocationProviderPackageName,
407 com.android.internal.R.array.config_locationProviderPackageNames,
408 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700409 if (fusedLocationProvider != null) {
410 addProviderLocked(fusedLocationProvider);
411 mProxyProviders.add(fusedLocationProvider);
412 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700413 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700414 } else {
415 Slog.e(TAG, "no fused location provider found",
416 new IllegalStateException("Location service needs a fused location provider"));
417 }
418
419 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700420 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
421 com.android.internal.R.bool.config_enableGeocoderOverlay,
422 com.android.internal.R.string.config_geocoderProviderPackageName,
423 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800424 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700425 if (mGeocodeProvider == null) {
426 Slog.e(TAG, "no geocoder provider found");
427 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700428
destradaa1af4b022013-07-12 15:43:36 -0700429 // bind to fused provider
destradaa1af4b022013-07-12 15:43:36 -0700430 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
431 FusedProxy fusedProxy = FusedProxy.createAndBind(
432 mContext,
433 mLocationHandler,
destradaa64be0c62013-08-09 15:01:49 -0700434 flpHardwareProvider.getLocationHardware(),
435 com.android.internal.R.bool.config_enableFusedLocationOverlay,
436 com.android.internal.R.string.config_fusedLocationProviderPackageName,
437 com.android.internal.R.array.config_locationProviderPackageNames);
destradaa1af4b022013-07-12 15:43:36 -0700438 if(fusedProxy == null) {
439 Slog.e(TAG, "No FusedProvider found.");
440 }
destradaa0682809a2013-08-12 18:50:30 -0700441
442 // bind to geofence provider
443 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
444 com.android.internal.R.bool.config_enableGeofenceOverlay,
445 com.android.internal.R.string.config_geofenceProviderPackageName,
446 com.android.internal.R.array.config_locationProviderPackageNames,
447 mLocationHandler,
448 gpsProvider.getGpsGeofenceProxy(),
449 flpHardwareProvider.getGeofenceHardware());
450 if (provider == null) {
451 Slog.e(TAG, "no geofence provider found");
452 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700453 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700456 * Called when the device's active user changes.
457 * @param userId the new active user's UserId
458 */
459 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700460 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800461 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700462 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700463 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700464 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700465 for (LocationProviderInterface p : mProviders) {
466 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700467 }
Victoria Lease38389b62012-09-30 11:44:22 -0700468 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700469 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700470 }
471 }
472
473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
475 * location updates.
476 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700477 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700478 final int mUid; // uid of receiver
479 final int mPid; // pid of receiver
480 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700481 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 final ILocationListener mListener;
484 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700485 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700486 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700488
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400489 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700490
David Christie0b837452013-07-29 16:02:13 -0700491 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700492 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700493 // True if app ops has started monitoring this receiver for high power (gps) locations.
494 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700495 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700496 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700498 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700499 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700502 if (listener != null) {
503 mKey = listener.asBinder();
504 } else {
505 mKey = intent;
506 }
Victoria Lease37425c32012-10-16 16:08:48 -0700507 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700508 mUid = uid;
509 mPid = pid;
510 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700511 if (workSource != null && workSource.size() <= 0) {
512 workSource = null;
513 }
514 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700515 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700516
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700517 updateMonitoring(true);
518
Victoria Lease0aa28602013-05-29 15:28:26 -0700519 // construct/configure wakelock
520 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700521 if (workSource == null) {
522 workSource = new WorkSource(mUid, mPackageName);
523 }
524 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526
527 @Override
528 public boolean equals(Object otherObj) {
529 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700530 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
532 return false;
533 }
534
535 @Override
536 public int hashCode() {
537 return mKey.hashCode();
538 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 @Override
541 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700542 StringBuilder s = new StringBuilder();
543 s.append("Reciever[");
544 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700546 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700548 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700550 for (String p : mUpdateRecords.keySet()) {
551 s.append(" ").append(mUpdateRecords.get(p).toString());
552 }
553 s.append("]");
554 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556
David Christie15b31912013-08-13 15:54:32 -0700557 /**
558 * Update AppOp monitoring for this receiver.
559 *
560 * @param allow If true receiver is currently active, if false it's been removed.
561 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700562 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700563 if (mHideFromAppOps) {
564 return;
565 }
566
David Christie15b31912013-08-13 15:54:32 -0700567 boolean requestingLocation = false;
568 boolean requestingHighPowerLocation = false;
569 if (allow) {
570 // See if receiver has any enabled update records. Also note if any update records
571 // are high power (has a high power provider with an interval under a threshold).
572 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
573 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
574 requestingLocation = true;
575 LocationProviderInterface locationProvider
David Christie2ff96af2014-01-30 16:09:37 -0800576 = mProvidersByName.get(updateRecord.mProvider);
David Christie15b31912013-08-13 15:54:32 -0700577 ProviderProperties properties = locationProvider != null
578 ? locationProvider.getProperties() : null;
579 if (properties != null
580 && properties.mPowerRequirement == Criteria.POWER_HIGH
581 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
582 requestingHighPowerLocation = true;
583 break;
584 }
585 }
586 }
587 }
588
David Christie0b837452013-07-29 16:02:13 -0700589 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700590 mOpMonitoring = updateMonitoring(
591 requestingLocation,
592 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700593 AppOpsManager.OP_MONITOR_LOCATION);
594
595 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700596 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700597 mOpHighPowerMonitoring = updateMonitoring(
598 requestingHighPowerLocation,
599 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700600 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700601 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700602 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700603 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
604 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
605 }
David Christie0b837452013-07-29 16:02:13 -0700606 }
607
608 /**
609 * Update AppOps monitoring for a single location request and op type.
610 *
611 * @param allowMonitoring True if monitoring is allowed for this request/op.
612 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
613 * @param op AppOps code for the op to update.
614 * @return True if monitoring is on for this request/op after updating.
615 */
616 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
617 int op) {
618 if (!currentlyMonitoring) {
619 if (allowMonitoring) {
620 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
621 == AppOpsManager.MODE_ALLOWED;
622 }
623 } else {
624 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
625 != AppOpsManager.MODE_ALLOWED) {
626 mAppOps.finishOp(op, mUid, mPackageName);
627 return false;
628 }
629 }
630
631 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public boolean isListener() {
635 return mListener != null;
636 }
637
638 public boolean isPendingIntent() {
639 return mPendingIntent != null;
640 }
641
642 public ILocationListener getListener() {
643 if (mListener != null) {
644 return mListener;
645 }
646 throw new IllegalStateException("Request for non-existent listener");
647 }
648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
650 if (mListener != null) {
651 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700652 synchronized (this) {
653 // synchronize to ensure incrementPendingBroadcastsLocked()
654 // is called before decrementPendingBroadcasts()
655 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700656 // call this after broadcasting so we do not increment
657 // if we throw an exeption.
658 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 } catch (RemoteException e) {
661 return false;
662 }
663 } else {
664 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800665 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
667 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700668 synchronized (this) {
669 // synchronize to ensure incrementPendingBroadcastsLocked()
670 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700671 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700672 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700673 // call this after broadcasting so we do not increment
674 // if we throw an exeption.
675 incrementPendingBroadcastsLocked();
676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 } catch (PendingIntent.CanceledException e) {
678 return false;
679 }
680 }
681 return true;
682 }
683
684 public boolean callLocationChangedLocked(Location location) {
685 if (mListener != null) {
686 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700687 synchronized (this) {
688 // synchronize to ensure incrementPendingBroadcastsLocked()
689 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800690 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700691 // call this after broadcasting so we do not increment
692 // if we throw an exeption.
693 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 } catch (RemoteException e) {
696 return false;
697 }
698 } else {
699 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800700 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700702 synchronized (this) {
703 // synchronize to ensure incrementPendingBroadcastsLocked()
704 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700705 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700706 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700707 // call this after broadcasting so we do not increment
708 // if we throw an exeption.
709 incrementPendingBroadcastsLocked();
710 }
711 } catch (PendingIntent.CanceledException e) {
712 return false;
713 }
714 }
715 return true;
716 }
717
718 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -0700719 // First update AppOp monitoring.
720 // An app may get/lose location access as providers are enabled/disabled.
721 updateMonitoring(true);
722
Mike Lockwood48f17512009-04-23 09:12:08 -0700723 if (mListener != null) {
724 try {
725 synchronized (this) {
726 // synchronize to ensure incrementPendingBroadcastsLocked()
727 // is called before decrementPendingBroadcasts()
728 if (enabled) {
729 mListener.onProviderEnabled(provider);
730 } else {
731 mListener.onProviderDisabled(provider);
732 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700733 // call this after broadcasting so we do not increment
734 // if we throw an exeption.
735 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700736 }
737 } catch (RemoteException e) {
738 return false;
739 }
740 } else {
741 Intent providerIntent = new Intent();
742 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
743 try {
744 synchronized (this) {
745 // synchronize to ensure incrementPendingBroadcastsLocked()
746 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700747 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700748 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700749 // call this after broadcasting so we do not increment
750 // if we throw an exeption.
751 incrementPendingBroadcastsLocked();
752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 } catch (PendingIntent.CanceledException e) {
754 return false;
755 }
756 }
757 return true;
758 }
759
Nick Pellyf1be6862012-05-15 10:53:42 -0700760 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700762 if (D) Log.d(TAG, "Location listener died");
763
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400764 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 removeUpdatesLocked(this);
766 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700767 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700768 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700769 }
770 }
771
Nick Pellye0fd6932012-07-11 10:26:13 -0700772 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700773 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
774 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400775 synchronized (this) {
776 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700777 }
778 }
779
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400780 // this must be called while synchronized by caller in a synchronized block
781 // containing the sending of the broadcaset
782 private void incrementPendingBroadcastsLocked() {
783 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700784 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400785 }
786 }
787
788 private void decrementPendingBroadcastsLocked() {
789 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700790 if (mWakeLock.isHeld()) {
791 mWakeLock.release();
792 }
793 }
794 }
795
796 public void clearPendingBroadcastsLocked() {
797 if (mPendingBroadcasts > 0) {
798 mPendingBroadcasts = 0;
799 if (mWakeLock.isHeld()) {
800 mWakeLock.release();
801 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700802 }
803 }
804 }
805
Nick Pellye0fd6932012-07-11 10:26:13 -0700806 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700807 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700808 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400809 //the receiver list if it is not found. If it is not found then the
810 //LocationListener was removed when it had a pending broadcast and should
811 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700812 synchronized (mLock) {
813 IBinder binder = listener.asBinder();
814 Receiver receiver = mReceivers.get(binder);
815 if (receiver != null) {
816 synchronized (receiver) {
817 // so wakelock calls will succeed
818 long identity = Binder.clearCallingIdentity();
819 receiver.decrementPendingBroadcastsLocked();
820 Binder.restoreCallingIdentity(identity);
David Christie2ff96af2014-01-30 16:09:37 -0800821 }
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 }
824 }
825
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700826 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400827 mProviders.add(provider);
828 mProvidersByName.put(provider.getName(), provider);
829 }
830
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700831 private void removeProviderLocked(LocationProviderInterface provider) {
832 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400833 mProviders.remove(provider);
834 mProvidersByName.remove(provider.getName());
835 }
836
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800837 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800838 * Returns "true" if access to the specified location provider is allowed by the current
839 * user's settings. Access to all location providers is forbidden to non-location-provider
840 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800841 *
842 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800843 * @return
844 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800845 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 if (mEnabledProviders.contains(provider)) {
847 return true;
848 }
849 if (mDisabledProviders.contains(provider)) {
850 return false;
851 }
852 // Use system settings
853 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854
Victoria Leaseb711d572012-10-02 13:14:11 -0700855 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700858 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800859 * Returns "true" if access to the specified location provider is allowed by the specified
860 * user's settings. Access to all location providers is forbidden to non-location-provider
861 * processes belonging to background users.
862 *
863 * @param provider the name of the location provider
864 * @param uid the requestor's UID
865 * @return
866 */
867 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
868 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
869 return false;
870 }
871 return isAllowedByCurrentUserSettingsLocked(provider);
872 }
873
874 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700875 * Returns the permission string associated with the specified resolution level.
876 *
877 * @param resolutionLevel the resolution level
878 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700879 */
Victoria Lease37425c32012-10-16 16:08:48 -0700880 private String getResolutionPermission(int resolutionLevel) {
881 switch (resolutionLevel) {
882 case RESOLUTION_LEVEL_FINE:
883 return android.Manifest.permission.ACCESS_FINE_LOCATION;
884 case RESOLUTION_LEVEL_COARSE:
885 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
886 default:
887 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700889 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700890
Victoria Leaseda479c52012-10-15 15:24:16 -0700891 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700892 * Returns the resolution level allowed to the given PID/UID pair.
893 *
894 * @param pid the PID
895 * @param uid the UID
896 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700897 */
Victoria Lease37425c32012-10-16 16:08:48 -0700898 private int getAllowedResolutionLevel(int pid, int uid) {
899 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
900 pid, uid) == PackageManager.PERMISSION_GRANTED) {
901 return RESOLUTION_LEVEL_FINE;
902 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
903 pid, uid) == PackageManager.PERMISSION_GRANTED) {
904 return RESOLUTION_LEVEL_COARSE;
905 } else {
906 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700907 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700908 }
909
910 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700911 * Returns the resolution level allowed to the caller
912 *
913 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700914 */
Victoria Lease37425c32012-10-16 16:08:48 -0700915 private int getCallerAllowedResolutionLevel() {
916 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
917 }
918
919 /**
920 * Throw SecurityException if specified resolution level is insufficient to use geofences.
921 *
922 * @param allowedResolutionLevel resolution level allowed to caller
923 */
924 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
925 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700926 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
Victoria Lease37425c32012-10-16 16:08:48 -0700930 /**
931 * Return the minimum resolution level required to use the specified location provider.
932 *
933 * @param provider the name of the location provider
934 * @return minimum resolution level required for provider
935 */
936 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700937 if (LocationManager.GPS_PROVIDER.equals(provider) ||
938 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
939 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700940 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700941 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
942 LocationManager.FUSED_PROVIDER.equals(provider)) {
943 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700944 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700945 } else {
946 // mock providers
947 LocationProviderInterface lp = mMockProviders.get(provider);
948 if (lp != null) {
949 ProviderProperties properties = lp.getProperties();
950 if (properties != null) {
951 if (properties.mRequiresSatellite) {
952 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700953 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700954 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
955 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700956 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700957 }
958 }
959 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700960 }
Victoria Lease37425c32012-10-16 16:08:48 -0700961 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700962 }
963
Victoria Lease37425c32012-10-16 16:08:48 -0700964 /**
965 * Throw SecurityException if specified resolution level is insufficient to use the named
966 * location provider.
967 *
968 * @param allowedResolutionLevel resolution level allowed to caller
969 * @param providerName the name of the location provider
970 */
971 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
972 String providerName) {
973 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
974 if (allowedResolutionLevel < requiredResolutionLevel) {
975 switch (requiredResolutionLevel) {
976 case RESOLUTION_LEVEL_FINE:
977 throw new SecurityException("\"" + providerName + "\" location provider " +
978 "requires ACCESS_FINE_LOCATION permission.");
979 case RESOLUTION_LEVEL_COARSE:
980 throw new SecurityException("\"" + providerName + "\" location provider " +
981 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
982 default:
983 throw new SecurityException("Insufficient permission for \"" + providerName +
984 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700985 }
986 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700987 }
988
David Christie82edc9b2013-07-19 11:31:42 -0700989 /**
990 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
991 * for battery).
992 */
David Christie40e57822013-07-30 11:36:48 -0700993 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -0700994 mContext.enforceCallingOrSelfPermission(
995 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
996 }
997
David Christie40e57822013-07-30 11:36:48 -0700998 private void checkUpdateAppOpsAllowed() {
999 mContext.enforceCallingOrSelfPermission(
1000 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
1001 }
1002
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001003 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001004 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1005 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001006 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001007 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001008 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001009 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001010 }
1011 return -1;
1012 }
1013
1014 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
1015 int op = resolutionLevelToOp(allowedResolutionLevel);
1016 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001017 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1018 return false;
1019 }
1020 }
1021 return true;
1022 }
1023
1024 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001025 int op = resolutionLevelToOp(allowedResolutionLevel);
1026 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001027 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1028 return false;
1029 }
1030 }
1031 return true;
1032 }
1033
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001034 /**
1035 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001036 * fused, also including ones that are not permitted to
1037 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001038 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001039 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001041 ArrayList<String> out;
1042 synchronized (mLock) {
1043 out = new ArrayList<String>(mProviders.size());
1044 for (LocationProviderInterface provider : mProviders) {
1045 String name = provider.getName();
1046 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001047 continue;
1048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 out.add(name);
1050 }
1051 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001052
1053 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 return out;
1055 }
1056
Mike Lockwood03ca2162010-04-01 08:10:09 -07001057 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001058 * Return all providers by name, that match criteria and are optionally
1059 * enabled.
1060 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001061 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001062 @Override
1063 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001064 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001065 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001066 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001067 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001068 try {
1069 synchronized (mLock) {
1070 out = new ArrayList<String>(mProviders.size());
1071 for (LocationProviderInterface provider : mProviders) {
1072 String name = provider.getName();
1073 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001074 continue;
1075 }
Victoria Lease37425c32012-10-16 16:08:48 -07001076 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001077 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001078 continue;
1079 }
1080 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1081 name, provider.getProperties(), criteria)) {
1082 continue;
1083 }
1084 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001085 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001086 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001087 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001088 } finally {
1089 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001090 }
1091
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001092 if (D) Log.d(TAG, "getProviders()=" + out);
1093 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001094 }
1095
1096 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001097 * Return the name of the best provider given a Criteria object.
1098 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001099 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001100 * has been deprecated as well. So this method now uses
1101 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001102 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001103 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001104 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001105 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001106
1107 List<String> providers = getProviders(criteria, 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;
1112 }
1113 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001114 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001115 result = pickBest(providers);
1116 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1117 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001118 }
1119
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001120 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001121 return null;
1122 }
1123
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001124 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001125 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001126 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001127 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1128 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001129 } else {
1130 return providers.get(0);
1131 }
1132 }
1133
Nick Pellye0fd6932012-07-11 10:26:13 -07001134 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001135 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1136 LocationProviderInterface p = mProvidersByName.get(provider);
1137 if (p == null) {
1138 throw new IllegalArgumentException("provider=" + provider);
1139 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001140
1141 boolean result = LocationProvider.propertiesMeetCriteria(
1142 p.getName(), p.getProperties(), criteria);
1143 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1144 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001145 }
1146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001148 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001149 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001150 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 boolean isEnabled = p.isEnabled();
1152 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001153 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001155 updateProviderListenersLocked(name, false, mCurrentUserId);
David Christieb084fef2013-12-18 14:33:57 -08001156 // If any provider has been disabled, clear all last locations for all providers.
1157 // This is to be on the safe side in case a provider has location derived from
1158 // this disabled provider.
1159 mLastLocation.clear();
1160 mLastLocationCoarseInterval.clear();
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001161 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001163 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001164 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001166 }
1167 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001168 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1169 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001170 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1171 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173 }
1174
Victoria Leaseb711d572012-10-02 13:14:11 -07001175 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 int listeners = 0;
1177
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001178 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001179 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180
1181 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1184 if (records != null) {
1185 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001186 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001188 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001189 // Sends a notification message to the receiver
1190 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1191 if (deadReceivers == null) {
1192 deadReceivers = new ArrayList<Receiver>();
1193 }
1194 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001196 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 }
1200
1201 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001202 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 removeUpdatesLocked(deadReceivers.get(i));
1204 }
1205 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 if (enabled) {
1208 p.enable();
1209 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001210 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001217 private void applyRequirementsLocked(String provider) {
1218 LocationProviderInterface p = mProvidersByName.get(provider);
1219 if (p == null) return;
1220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001222 WorkSource worksource = new WorkSource();
1223 ProviderRequest providerRequest = new ProviderRequest();
1224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001226 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001227 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001228 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1229 record.mReceiver.mAllowedResolutionLevel)) {
1230 LocationRequest locationRequest = record.mRequest;
1231 providerRequest.locationRequests.add(locationRequest);
1232 if (locationRequest.getInterval() < providerRequest.interval) {
1233 providerRequest.reportLocation = true;
1234 providerRequest.interval = locationRequest.getInterval();
1235 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001236 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001237 }
1238 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001239
1240 if (providerRequest.reportLocation) {
1241 // calculate who to blame for power
1242 // This is somewhat arbitrary. We pick a threshold interval
1243 // that is slightly higher that the minimum interval, and
1244 // spread the blame across all applications with a request
1245 // under that threshold.
1246 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1247 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001248 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001249 LocationRequest locationRequest = record.mRequest;
1250 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001251 if (record.mReceiver.mWorkSource != null
1252 && record.mReceiver.mWorkSource.size() > 0
1253 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001254 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001255 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001256 worksource.add(record.mReceiver.mWorkSource);
1257 } else {
1258 // Assign blame to caller.
1259 worksource.add(
1260 record.mReceiver.mUid,
1261 record.mReceiver.mPackageName);
1262 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001263 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001264 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001268
1269 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1270 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272
1273 private class UpdateRecord {
1274 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001275 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001277 Location mLastFixBroadcast;
1278 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279
1280 /**
1281 * Note: must be constructed with lock held.
1282 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001283 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001285 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287
1288 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1289 if (records == null) {
1290 records = new ArrayList<UpdateRecord>();
1291 mRecordsByProvider.put(provider, records);
1292 }
1293 if (!records.contains(this)) {
1294 records.add(this);
1295 }
David Christie2ff96af2014-01-30 16:09:37 -08001296
1297 // Update statistics for historical location requests by package/provider
1298 mRequestStatistics.startRequesting(
1299 mReceiver.mPackageName, provider, request.getInterval());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
1302 /**
David Christie2ff96af2014-01-30 16:09:37 -08001303 * Method to be called when a record will no longer be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001305 void disposeLocked(boolean removeReceiver) {
David Christie2ff96af2014-01-30 16:09:37 -08001306 mRequestStatistics.stopRequesting(mReceiver.mPackageName, mProvider);
1307
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001308 // remove from mRecordsByProvider
1309 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1310 if (globalRecords != null) {
1311 globalRecords.remove(this);
1312 }
1313
1314 if (!removeReceiver) return; // the caller will handle the rest
1315
1316 // remove from Receiver#mUpdateRecords
1317 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1318 if (receiverRecords != null) {
1319 receiverRecords.remove(this.mProvider);
1320
1321 // and also remove the Receiver if it has no more update records
1322 if (removeReceiver && receiverRecords.size() == 0) {
1323 removeUpdatesLocked(mReceiver);
1324 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327
1328 @Override
1329 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001330 StringBuilder s = new StringBuilder();
1331 s.append("UpdateRecord[");
1332 s.append(mProvider);
1333 s.append(' ').append(mReceiver.mPackageName).append('(');
1334 s.append(mReceiver.mUid).append(')');
1335 s.append(' ').append(mRequest);
1336 s.append(']');
1337 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001341 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001342 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001343 IBinder binder = listener.asBinder();
1344 Receiver receiver = mReceivers.get(binder);
1345 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001346 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1347 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001348 mReceivers.put(binder, receiver);
1349
1350 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001351 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001352 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001353 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001354 return null;
1355 }
1356 }
1357 return receiver;
1358 }
1359
David Christie82edc9b2013-07-19 11:31:42 -07001360 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001361 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001362 Receiver receiver = mReceivers.get(intent);
1363 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001364 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1365 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001366 mReceivers.put(intent, receiver);
1367 }
1368 return receiver;
1369 }
1370
Victoria Lease37425c32012-10-16 16:08:48 -07001371 /**
1372 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1373 * and consistency requirements.
1374 *
1375 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001376 * @return a version of request that meets the given resolution and consistency requirements
1377 * @hide
1378 */
1379 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1380 LocationRequest sanitizedRequest = new LocationRequest(request);
1381 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1382 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001383 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001384 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001385 break;
1386 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001387 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001388 break;
1389 }
1390 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001391 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1392 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001393 }
Victoria Lease37425c32012-10-16 16:08:48 -07001394 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1395 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001396 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001397 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001398 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001399 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001400 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001401 }
Victoria Lease37425c32012-10-16 16:08:48 -07001402 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001403 }
1404
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001405 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001406 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001407 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001408 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001409 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001410 String[] packages = mPackageManager.getPackagesForUid(uid);
1411 if (packages == null) {
1412 throw new SecurityException("invalid UID " + uid);
1413 }
1414 for (String pkg : packages) {
1415 if (packageName.equals(pkg)) return;
1416 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001417 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001418 }
1419
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001420 private void checkPendingIntent(PendingIntent intent) {
1421 if (intent == null) {
1422 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001423 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001424 }
1425
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001426 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001427 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001428 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001429 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001430 } else if (intent != null && listener != null) {
1431 throw new IllegalArgumentException("cannot register both listener and intent");
1432 } else if (intent != null) {
1433 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001434 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001435 } else {
David Christie40e57822013-07-30 11:36:48 -07001436 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001437 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001438 }
1439
Nick Pellye0fd6932012-07-11 10:26:13 -07001440 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001441 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1442 PendingIntent intent, String packageName) {
1443 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1444 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001445 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1446 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1447 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001448 WorkSource workSource = request.getWorkSource();
1449 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001450 checkDeviceStatsAllowed();
1451 }
1452 boolean hideFromAppOps = request.getHideFromAppOps();
1453 if (hideFromAppOps) {
1454 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001455 }
Victoria Lease37425c32012-10-16 16:08:48 -07001456 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001458 final int pid = Binder.getCallingPid();
1459 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001460 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 long identity = Binder.clearCallingIdentity();
1462 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001463 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1464 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001465 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001466
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001467 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001468 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001469 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001470 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 } finally {
1473 Binder.restoreCallingIdentity(identity);
1474 }
1475 }
1476
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001477 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1478 int pid, int uid, String packageName) {
1479 // Figure out the provider. Either its explicitly request (legacy use cases), or
1480 // use the fused provider
1481 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1482 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001483 if (name == null) {
1484 throw new IllegalArgumentException("provider name must not be null");
1485 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001486
1487 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1488 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001489 LocationProviderInterface provider = mProvidersByName.get(name);
1490 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07001491 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001492 }
1493
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001494 UpdateRecord record = new UpdateRecord(name, request, receiver);
1495 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1496 if (oldRecord != null) {
1497 oldRecord.disposeLocked(false);
1498 }
1499
Victoria Lease09eeaec2013-02-05 11:34:13 -08001500 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001501 if (isProviderEnabled) {
1502 applyRequirementsLocked(name);
1503 } else {
1504 // Notify the listener that updates are currently disabled
1505 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
David Christie0b837452013-07-29 16:02:13 -07001507 // Update the monitoring here just in case multiple location requests were added to the
1508 // same receiver (this request may be high power and the initial might not have been).
1509 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511
Nick Pellye0fd6932012-07-11 10:26:13 -07001512 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001513 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1514 String packageName) {
1515 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001516
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001517 final int pid = Binder.getCallingPid();
1518 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001519
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001520 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001521 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001522 boolean hideFromAppOps = false;
1523 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1524 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001525
1526 // providers may use public location API's, need to clear identity
1527 long identity = Binder.clearCallingIdentity();
1528 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001529 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001530 } finally {
1531 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534 }
1535
1536 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001537 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001538
1539 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1540 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1541 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001542 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001546 receiver.updateMonitoring(false);
1547
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001548 // Record which providers were associated with this listener
1549 HashSet<String> providers = new HashSet<String>();
1550 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1551 if (oldRecords != null) {
1552 // Call dispose() on the obsolete update records.
1553 for (UpdateRecord record : oldRecords.values()) {
David Christie2ff96af2014-01-30 16:09:37 -08001554 // Update statistics for historical location requests by package/provider
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001555 record.disposeLocked(false);
1556 }
1557 // Accumulate providers
1558 providers.addAll(oldRecords.keySet());
1559 }
1560
1561 // update provider
1562 for (String provider : providers) {
1563 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001564 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001565 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
1567
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001568 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
1570 }
1571
Dianne Hackbornc2293022013-02-06 23:14:49 -08001572 private void applyAllProviderRequirementsLocked() {
1573 for (LocationProviderInterface p : mProviders) {
1574 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001575 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001576 continue;
1577 }
1578
1579 applyRequirementsLocked(p.getName());
1580 }
1581 }
1582
Nick Pellye0fd6932012-07-11 10:26:13 -07001583 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001584 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001585 if (D) Log.d(TAG, "getLastLocation: " + request);
1586 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001587 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001588 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001589 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1590 request.getProvider());
1591 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001592
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001593 final int uid = Binder.getCallingUid();
1594 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001595 try {
1596 if (mBlacklist.isBlacklisted(packageName)) {
1597 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1598 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001599 return null;
1600 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001601
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001602 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1603 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1604 packageName);
1605 return null;
1606 }
1607
Victoria Leaseb711d572012-10-02 13:14:11 -07001608 synchronized (mLock) {
1609 // Figure out the provider. Either its explicitly request (deprecated API's),
1610 // or use the fused provider
1611 String name = request.getProvider();
1612 if (name == null) name = LocationManager.FUSED_PROVIDER;
1613 LocationProviderInterface provider = mProvidersByName.get(name);
1614 if (provider == null) return null;
1615
Victoria Lease09eeaec2013-02-05 11:34:13 -08001616 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001617
David Christie1b9b7b12013-04-15 15:31:11 -07001618 Location location;
1619 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1620 // Make sure that an app with coarse permissions can't get frequent location
1621 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1622 location = mLastLocationCoarseInterval.get(name);
1623 } else {
1624 location = mLastLocation.get(name);
1625 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001626 if (location == null) {
1627 return null;
1628 }
Victoria Lease37425c32012-10-16 16:08:48 -07001629 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001630 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1631 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001632 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001633 }
Victoria Lease37425c32012-10-16 16:08:48 -07001634 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001635 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001636 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001637 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001638 return null;
1639 } finally {
1640 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001641 }
1642 }
1643
1644 @Override
1645 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1646 String packageName) {
1647 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001648 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1649 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001650 checkPendingIntent(intent);
1651 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001652 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1653 request.getProvider());
1654 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001655
Victoria Lease37425c32012-10-16 16:08:48 -07001656 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001657
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001658 // geo-fence manager uses the public location API, need to clear identity
1659 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001660 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1661 // temporary measure until geofences work for secondary users
1662 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1663 return;
1664 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001665 long identity = Binder.clearCallingIdentity();
1666 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001667 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1668 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001669 } finally {
1670 Binder.restoreCallingIdentity(identity);
1671 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001672 }
1673
1674 @Override
1675 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001676 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001677 checkPendingIntent(intent);
1678 checkPackageName(packageName);
1679
1680 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1681
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001682 // geo-fence manager uses the public location API, need to clear identity
1683 long identity = Binder.clearCallingIdentity();
1684 try {
1685 mGeofenceManager.removeFence(geofence, intent);
1686 } finally {
1687 Binder.restoreCallingIdentity(identity);
1688 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001689 }
1690
1691
1692 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001693 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001694 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 return false;
1696 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001697 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1698 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001699 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001701 final int uid = Binder.getCallingUid();
1702 final long ident = Binder.clearCallingIdentity();
1703 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001704 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001705 return false;
1706 }
1707 } finally {
1708 Binder.restoreCallingIdentity(ident);
1709 }
1710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001712 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001714 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 return false;
1716 }
1717 return true;
1718 }
1719
Nick Pellye0fd6932012-07-11 10:26:13 -07001720 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001722 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001723 try {
1724 mGpsStatusProvider.removeGpsStatusListener(listener);
1725 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001726 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729 }
1730
Nick Pellye0fd6932012-07-11 10:26:13 -07001731 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001733 if (provider == null) {
1734 // throw NullPointerException to remain compatible with previous implementation
1735 throw new NullPointerException();
1736 }
Victoria Lease37425c32012-10-16 16:08:48 -07001737 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1738 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001741 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 != PackageManager.PERMISSION_GRANTED)) {
1743 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1744 }
1745
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001746 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001747 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001748 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001749
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001750 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752 }
1753
Nick Pellye0fd6932012-07-11 10:26:13 -07001754 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001755 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001756 if (Binder.getCallingUid() != Process.myUid()) {
1757 throw new SecurityException(
1758 "calling sendNiResponse from outside of the system is not allowed");
1759 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001760 try {
1761 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001762 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001763 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001764 return false;
1765 }
1766 }
1767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001769 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001770 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 * accessed by the caller
1772 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001773 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001774 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001775 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08001776 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07001777 }
1778
Victoria Lease37425c32012-10-16 16:08:48 -07001779 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1780 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001782 LocationProviderInterface p;
1783 synchronized (mLock) {
1784 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001787 if (p == null) return null;
1788 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790
Nick Pellye0fd6932012-07-11 10:26:13 -07001791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07001793 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
1794 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001795 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1796
Victoria Lease09eeaec2013-02-05 11:34:13 -08001797 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001798 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001799 try {
1800 synchronized (mLock) {
1801 LocationProviderInterface p = mProvidersByName.get(provider);
1802 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001803
Victoria Lease09eeaec2013-02-05 11:34:13 -08001804 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001805 }
1806 } finally {
1807 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001808 }
1809 }
1810
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001811 /**
1812 * Returns "true" if the UID belongs to a bound location provider.
1813 *
1814 * @param uid the uid
1815 * @return true if uid belongs to a bound location provider
1816 */
1817 private boolean isUidALocationProvider(int uid) {
1818 if (uid == Process.SYSTEM_UID) {
1819 return true;
1820 }
1821 if (mGeocodeProvider != null) {
1822 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1823 }
1824 for (LocationProviderProxy proxy : mProxyProviders) {
1825 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1826 }
1827 return false;
1828 }
1829
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001830 private void checkCallerIsProvider() {
1831 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1832 == PackageManager.PERMISSION_GRANTED) {
1833 return;
1834 }
1835
1836 // Previously we only used the INSTALL_LOCATION_PROVIDER
1837 // check. But that is system or signature
1838 // protection level which is not flexible enough for
1839 // providers installed oustide the system image. So
1840 // also allow providers with a UID matching the
1841 // currently bound package name
1842
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001843 if (isUidALocationProvider(Binder.getCallingUid())) {
1844 return;
1845 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001846
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001847 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1848 "or UID of a currently bound location provider");
1849 }
1850
1851 private boolean doesPackageHaveUid(int uid, String packageName) {
1852 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 return false;
1854 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001855 try {
1856 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1857 if (appInfo.uid != uid) {
1858 return false;
1859 }
1860 } catch (NameNotFoundException e) {
1861 return false;
1862 }
1863 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
1865
Nick Pellye0fd6932012-07-11 10:26:13 -07001866 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001867 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001868 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001869
Nick Pelly2eeeec22012-07-18 13:13:37 -07001870 if (!location.isComplete()) {
1871 Log.w(TAG, "Dropping incomplete location: " + location);
1872 return;
1873 }
1874
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001875 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1876 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001877 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001878 mLocationHandler.sendMessageAtFrontOfQueue(m);
1879 }
1880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881
Laurent Tu75defb62012-11-01 16:21:52 -07001882 private static boolean shouldBroadcastSafe(
1883 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 // Always broadcast the first update
1885 if (lastLoc == null) {
1886 return true;
1887 }
1888
Nick Pellyf1be6862012-05-15 10:53:42 -07001889 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001890 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001891 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1892 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001893 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 return false;
1895 }
1896
1897 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001898 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 if (minDistance > 0.0) {
1900 if (loc.distanceTo(lastLoc) <= minDistance) {
1901 return false;
1902 }
1903 }
1904
Laurent Tu75defb62012-11-01 16:21:52 -07001905 // Check whether sufficient number of udpates is left
1906 if (record.mRequest.getNumUpdates() <= 0) {
1907 return false;
1908 }
1909
1910 // Check whether the expiry date has passed
1911 if (record.mRequest.getExpireAt() < now) {
1912 return false;
1913 }
1914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 return true;
1916 }
1917
Mike Lockwooda4903f22010-02-17 06:42:23 -05001918 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001919 if (D) Log.d(TAG, "incoming location: " + location);
1920
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001921 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001922 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923
Laurent Tu60ec50a2012-10-04 17:00:10 -07001924 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001925 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001926 if (p == null) return;
1927
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001928 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001929 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1930 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001931 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001932 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001933 lastLocation = new Location(provider);
1934 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001935 } else {
1936 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1937 if (noGPSLocation == null && lastNoGPSLocation != null) {
1938 // New location has no no-GPS location: adopt last no-GPS location. This is set
1939 // directly into location because we do not want to notify COARSE clients.
1940 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1941 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001942 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001943 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944
David Christie1b9b7b12013-04-15 15:31:11 -07001945 // Update last known coarse interval location if enough time has passed.
1946 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1947 if (lastLocationCoarseInterval == null) {
1948 lastLocationCoarseInterval = new Location(location);
1949 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1950 }
1951 long timeDiffNanos = location.getElapsedRealtimeNanos()
1952 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1953 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1954 lastLocationCoarseInterval.set(location);
1955 }
1956 // Don't ever return a coarse location that is more recent than the allowed update
1957 // interval (i.e. don't allow an app to keep registering and unregistering for
1958 // location updates to overcome the minimum interval).
1959 noGPSLocation =
1960 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1961
Laurent Tu60ec50a2012-10-04 17:00:10 -07001962 // Skip if there are no UpdateRecords for this provider.
1963 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1964 if (records == null || records.size() == 0) return;
1965
Victoria Lease09016ab2012-09-16 12:33:15 -07001966 // Fetch coarse location
1967 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001968 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001969 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1970 }
1971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 // Fetch latest status update time
1973 long newStatusUpdateTime = p.getStatusUpdateTime();
1974
David Christie2ff96af2014-01-30 16:09:37 -08001975 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 Bundle extras = new Bundle();
1977 int status = p.getStatus(extras);
1978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001980 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001983 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001985 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001986
Victoria Lease269518e2012-10-29 08:25:39 -07001987 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001988 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001989 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001990 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001991 " (current user: " + mCurrentUserId + ", app: " +
1992 receiver.mPackageName + ")");
1993 }
1994 continue;
1995 }
1996
Nick Pelly4035f5a2012-08-17 14:43:49 -07001997 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1998 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1999 receiver.mPackageName);
2000 continue;
2001 }
2002
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002003 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
2004 receiver.mAllowedResolutionLevel)) {
2005 if (D) Log.d(TAG, "skipping loc update for no op app: " +
2006 receiver.mPackageName);
2007 continue;
2008 }
2009
Victoria Lease09016ab2012-09-16 12:33:15 -07002010 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07002011 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2012 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002013 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002014 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002015 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002016 if (notifyLocation != null) {
2017 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002018 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002019 if (lastLoc == null) {
2020 lastLoc = new Location(notifyLocation);
2021 r.mLastFixBroadcast = lastLoc;
2022 } else {
2023 lastLoc.set(notifyLocation);
2024 }
2025 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2026 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2027 receiverDead = true;
2028 }
Laurent Tu75defb62012-11-01 16:21:52 -07002029 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
2031 }
2032
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002033 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002035 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002037 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002039 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002040 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002041 }
2042 }
2043
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002044 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002045 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002046 if (deadUpdateRecords == null) {
2047 deadUpdateRecords = new ArrayList<UpdateRecord>();
2048 }
2049 deadUpdateRecords.add(r);
2050 }
2051 // track dead receivers
2052 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002053 if (deadReceivers == null) {
2054 deadReceivers = new ArrayList<Receiver>();
2055 }
2056 if (!deadReceivers.contains(receiver)) {
2057 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
2059 }
2060 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002061
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002062 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002064 for (Receiver receiver : deadReceivers) {
2065 removeUpdatesLocked(receiver);
2066 }
2067 }
2068 if (deadUpdateRecords != null) {
2069 for (UpdateRecord r : deadUpdateRecords) {
2070 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002072 applyRequirementsLocked(provider);
2073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
2075
2076 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002077 public LocationWorkerHandler(Looper looper) {
2078 super(looper, null, true);
2079 }
2080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 @Override
2082 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002083 switch (msg.what) {
2084 case MSG_LOCATION_CHANGED:
2085 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2086 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088 }
2089 }
2090
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002091 private boolean isMockProvider(String provider) {
2092 synchronized (mLock) {
2093 return mMockProviders.containsKey(provider);
2094 }
2095 }
2096
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002097 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002098 // create a working copy of the incoming Location so that the service can modify it without
2099 // disturbing the caller's copy
2100 Location myLocation = new Location(location);
2101 String provider = myLocation.getProvider();
2102
2103 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2104 // bit if location did not come from a mock provider because passive/fused providers can
2105 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2106 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2107 myLocation.setIsFromMockProvider(true);
2108 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002109
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002110 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002111 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2112 if (!passive) {
2113 // notify passive provider of the new location
2114 mPassiveProvider.updateLocation(myLocation);
2115 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002116 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120
Mike Lockwoode97ae402010-09-29 15:23:46 -04002121 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2122 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002123 public void onPackageDisappeared(String packageName, int reason) {
2124 // remove all receivers associated with this package name
2125 synchronized (mLock) {
2126 ArrayList<Receiver> deadReceivers = null;
2127
2128 for (Receiver receiver : mReceivers.values()) {
2129 if (receiver.mPackageName.equals(packageName)) {
2130 if (deadReceivers == null) {
2131 deadReceivers = new ArrayList<Receiver>();
2132 }
2133 deadReceivers.add(receiver);
2134 }
2135 }
2136
2137 // perform removal outside of mReceivers loop
2138 if (deadReceivers != null) {
2139 for (Receiver receiver : deadReceivers) {
2140 removeUpdatesLocked(receiver);
2141 }
2142 }
2143 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002144 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002145 };
2146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 // Geocoder
2148
Nick Pellye0fd6932012-07-11 10:26:13 -07002149 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002150 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002151 return mGeocodeProvider != null;
2152 }
2153
Nick Pellye0fd6932012-07-11 10:26:13 -07002154 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002156 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002157 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002158 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2159 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002161 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163
Mike Lockwooda55c3212009-04-15 11:10:11 -04002164
Nick Pellye0fd6932012-07-11 10:26:13 -07002165 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002167 double lowerLeftLatitude, double lowerLeftLongitude,
2168 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002169 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002170
2171 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002172 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2173 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2174 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002176 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178
2179 // Mock Providers
2180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 private void checkMockPermissionsSafe() {
2182 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2183 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2184 if (!allowMocks) {
2185 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2186 }
2187
2188 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
David Christie2ff96af2014-01-30 16:09:37 -08002189 PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
2193
Nick Pellye0fd6932012-07-11 10:26:13 -07002194 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002195 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 checkMockPermissionsSafe();
2197
Mike Lockwooda4903f22010-02-17 06:42:23 -05002198 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2199 throw new IllegalArgumentException("Cannot mock the passive location provider");
2200 }
2201
Mike Lockwood86328a92009-10-23 08:38:25 -04002202 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002203 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002204 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002205 // remove the real provider if we are replacing GPS or network provider
2206 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002207 || LocationManager.NETWORK_PROVIDER.equals(name)
2208 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002209 LocationProviderInterface p = mProvidersByName.get(name);
2210 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002211 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002212 }
2213 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002214 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2216 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002217 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002218 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002219 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002220 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 updateProvidersLocked();
2222 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002223 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225
Nick Pellye0fd6932012-07-11 10:26:13 -07002226 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 public void removeTestProvider(String provider) {
2228 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002229 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002230
2231 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002232 // we don't leave anything dangling.
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002233 clearTestProviderEnabled(provider);
2234 clearTestProviderLocation(provider);
2235 clearTestProviderStatus(provider);
2236
You Kima6d0b6f2012-10-28 03:58:44 +09002237 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002238 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2240 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002241 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002242 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002243
2244 // reinstate real provider if available
2245 LocationProviderInterface realProvider = mRealProviders.get(provider);
2246 if (realProvider != null) {
2247 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002248 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002249 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002250 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002252 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
2254 }
2255
Nick Pellye0fd6932012-07-11 10:26:13 -07002256 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 public void setTestProviderLocation(String provider, Location loc) {
2258 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002259 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002260 MockProvider mockProvider = mMockProviders.get(provider);
2261 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2263 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002264 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2265 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002266 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002267 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
2269 }
2270
Nick Pellye0fd6932012-07-11 10:26:13 -07002271 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 public void clearTestProviderLocation(String provider) {
2273 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002274 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002275 MockProvider mockProvider = mMockProviders.get(provider);
2276 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2278 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002279 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
2281 }
2282
Nick Pellye0fd6932012-07-11 10:26:13 -07002283 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 public void setTestProviderEnabled(String provider, boolean enabled) {
2285 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002286 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002287 MockProvider mockProvider = mMockProviders.get(provider);
2288 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2290 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002291 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002293 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 mEnabledProviders.add(provider);
2295 mDisabledProviders.remove(provider);
2296 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002297 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 mEnabledProviders.remove(provider);
2299 mDisabledProviders.add(provider);
2300 }
2301 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002302 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304 }
2305
Nick Pellye0fd6932012-07-11 10:26:13 -07002306 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 public void clearTestProviderEnabled(String provider) {
2308 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002309 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002310 MockProvider mockProvider = mMockProviders.get(provider);
2311 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2313 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002314 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 mEnabledProviders.remove(provider);
2316 mDisabledProviders.remove(provider);
2317 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002318 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
2320 }
2321
Nick Pellye0fd6932012-07-11 10:26:13 -07002322 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2324 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002325 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002326 MockProvider mockProvider = mMockProviders.get(provider);
2327 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2329 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002330 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 }
2332 }
2333
Nick Pellye0fd6932012-07-11 10:26:13 -07002334 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 public void clearTestProviderStatus(String provider) {
2336 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002337 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002338 MockProvider mockProvider = mMockProviders.get(provider);
2339 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2341 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002342 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
2344 }
2345
2346 private void log(String log) {
2347 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002348 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
2350 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002351
2352 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2354 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2355 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002356 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 + Binder.getCallingPid()
2358 + ", uid=" + Binder.getCallingUid());
2359 return;
2360 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002361
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002362 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002365 for (Receiver receiver : mReceivers.values()) {
2366 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
David Christie2ff96af2014-01-30 16:09:37 -08002368 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002369 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2370 pw.println(" " + entry.getKey() + ":");
2371 for (UpdateRecord record : entry.getValue()) {
2372 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
2374 }
David Christie2ff96af2014-01-30 16:09:37 -08002375 pw.println(" Historical Records by Provider:");
2376 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
2377 : mRequestStatistics.statistics.entrySet()) {
2378 PackageProviderKey key = entry.getKey();
2379 PackageStatistics stats = entry.getValue();
2380 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
2381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002383 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2384 String provider = entry.getKey();
2385 Location location = entry.getValue();
2386 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002388
David Christie1b9b7b12013-04-15 15:31:11 -07002389 pw.println(" Last Known Locations Coarse Intervals:");
2390 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2391 String provider = entry.getKey();
2392 Location location = entry.getValue();
2393 pw.println(" " + provider + ": " + location);
2394 }
2395
Nick Pellye0fd6932012-07-11 10:26:13 -07002396 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 if (mEnabledProviders.size() > 0) {
2399 pw.println(" Enabled Providers:");
2400 for (String i : mEnabledProviders) {
2401 pw.println(" " + i);
2402 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405 if (mDisabledProviders.size() > 0) {
2406 pw.println(" Disabled Providers:");
2407 for (String i : mDisabledProviders) {
2408 pw.println(" " + i);
2409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002411 pw.append(" ");
2412 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 if (mMockProviders.size() > 0) {
2414 pw.println(" Mock Providers:");
2415 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002416 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 }
2418 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002419
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002420 pw.append(" fudger: ");
2421 mLocationFudger.dump(fd, pw, args);
2422
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002423 if (args.length > 0 && "short".equals(args[0])) {
2424 return;
2425 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002426 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002427 pw.print(provider.getName() + " Internal State");
2428 if (provider instanceof LocationProviderProxy) {
2429 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2430 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002431 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002432 pw.println(":");
2433 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436 }
2437}