blob: 11fc941f2096a87814f983325215a4e247d6df92 [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
destradaacfbdcd22014-04-30 11:29:11 -0700429 // bind to fused provider if supported
Zhentao Suna4dfc2c2014-05-12 16:14:47 -0700430 if (FlpHardwareProvider.getInstance(mContext).isSupported()) {
destradaacfbdcd22014-04-30 11:29:11 -0700431 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
432 FusedProxy fusedProxy = FusedProxy.createAndBind(
433 mContext,
434 mLocationHandler,
435 flpHardwareProvider.getLocationHardware(),
436 com.android.internal.R.bool.config_enableFusedLocationOverlay,
437 com.android.internal.R.string.config_fusedLocationProviderPackageName,
438 com.android.internal.R.array.config_locationProviderPackageNames);
439 if(fusedProxy == null) {
440 Slog.e(TAG, "Unable to bind FusedProxy.");
441 }
destradaa0682809a2013-08-12 18:50:30 -0700442
destradaacfbdcd22014-04-30 11:29:11 -0700443 // bind to geofence provider
444 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
445 com.android.internal.R.bool.config_enableGeofenceOverlay,
446 com.android.internal.R.string.config_geofenceProviderPackageName,
447 com.android.internal.R.array.config_locationProviderPackageNames,
448 mLocationHandler,
449 gpsProvider.getGpsGeofenceProxy(),
450 flpHardwareProvider.getGeofenceHardware());
451 if (provider == null) {
452 Slog.e(TAG, "Unable to bind FLP Geofence proxy.");
453 }
454 } else {
455 Slog.e(TAG, "FLP HAL not supported.");
destradaa0682809a2013-08-12 18:50:30 -0700456 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900457
458 String[] testProviderStrings = resources.getStringArray(
459 com.android.internal.R.array.config_testLocationProviders);
460 for (String testProviderString : testProviderStrings) {
461 String fragments[] = testProviderString.split(",");
462 String name = fragments[0].trim();
463 if (mProvidersByName.get(name) != null) {
464 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
465 }
466 ProviderProperties properties = new ProviderProperties(
467 Boolean.parseBoolean(fragments[1]) /* requiresNetwork */,
468 Boolean.parseBoolean(fragments[2]) /* requiresSatellite */,
469 Boolean.parseBoolean(fragments[3]) /* requiresCell */,
470 Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */,
471 Boolean.parseBoolean(fragments[5]) /* supportsAltitude */,
472 Boolean.parseBoolean(fragments[6]) /* supportsSpeed */,
473 Boolean.parseBoolean(fragments[7]) /* supportsBearing */,
474 Integer.parseInt(fragments[8]) /* powerRequirement */,
475 Integer.parseInt(fragments[9]) /* accuracy */);
476 addTestProviderLocked(name, properties);
477 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700478 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700481 * Called when the device's active user changes.
482 * @param userId the new active user's UserId
483 */
484 private void switchUser(int userId) {
Jianzheng Zhoud5c69462013-10-10 14:02:09 +0800485 if (mCurrentUserId == userId) {
486 return;
487 }
Victoria Lease83762d22012-10-03 13:51:17 -0700488 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800489 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700490 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700491 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700492 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700493 for (LocationProviderInterface p : mProviders) {
494 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700495 }
Victoria Lease38389b62012-09-30 11:44:22 -0700496 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700497 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700498 }
499 }
500
501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
503 * location updates.
504 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700505 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700506 final int mUid; // uid of receiver
507 final int mPid; // pid of receiver
508 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700509 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 final ILocationListener mListener;
512 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700513 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700514 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700516
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400517 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700518
David Christie0b837452013-07-29 16:02:13 -0700519 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700520 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700521 // True if app ops has started monitoring this receiver for high power (gps) locations.
522 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700523 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700524 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700526 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700527 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700530 if (listener != null) {
531 mKey = listener.asBinder();
532 } else {
533 mKey = intent;
534 }
Victoria Lease37425c32012-10-16 16:08:48 -0700535 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700536 mUid = uid;
537 mPid = pid;
538 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700539 if (workSource != null && workSource.size() <= 0) {
540 workSource = null;
541 }
542 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700543 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700544
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700545 updateMonitoring(true);
546
Victoria Lease0aa28602013-05-29 15:28:26 -0700547 // construct/configure wakelock
548 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700549 if (workSource == null) {
550 workSource = new WorkSource(mUid, mPackageName);
551 }
552 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
555 @Override
556 public boolean equals(Object otherObj) {
557 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700558 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 return false;
561 }
562
563 @Override
564 public int hashCode() {
565 return mKey.hashCode();
566 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 @Override
569 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700570 StringBuilder s = new StringBuilder();
571 s.append("Reciever[");
572 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700574 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700576 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700578 for (String p : mUpdateRecords.keySet()) {
579 s.append(" ").append(mUpdateRecords.get(p).toString());
580 }
581 s.append("]");
582 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
584
David Christie15b31912013-08-13 15:54:32 -0700585 /**
586 * Update AppOp monitoring for this receiver.
587 *
588 * @param allow If true receiver is currently active, if false it's been removed.
589 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700590 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700591 if (mHideFromAppOps) {
592 return;
593 }
594
David Christie15b31912013-08-13 15:54:32 -0700595 boolean requestingLocation = false;
596 boolean requestingHighPowerLocation = false;
597 if (allow) {
598 // See if receiver has any enabled update records. Also note if any update records
599 // are high power (has a high power provider with an interval under a threshold).
600 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
601 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
602 requestingLocation = true;
603 LocationProviderInterface locationProvider
David Christie2ff96af2014-01-30 16:09:37 -0800604 = mProvidersByName.get(updateRecord.mProvider);
David Christie15b31912013-08-13 15:54:32 -0700605 ProviderProperties properties = locationProvider != null
606 ? locationProvider.getProperties() : null;
607 if (properties != null
608 && properties.mPowerRequirement == Criteria.POWER_HIGH
609 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
610 requestingHighPowerLocation = true;
611 break;
612 }
613 }
614 }
615 }
616
David Christie0b837452013-07-29 16:02:13 -0700617 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700618 mOpMonitoring = updateMonitoring(
619 requestingLocation,
620 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700621 AppOpsManager.OP_MONITOR_LOCATION);
622
623 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700624 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700625 mOpHighPowerMonitoring = updateMonitoring(
626 requestingHighPowerLocation,
627 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700628 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700629 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700630 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700631 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
632 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
633 }
David Christie0b837452013-07-29 16:02:13 -0700634 }
635
636 /**
637 * Update AppOps monitoring for a single location request and op type.
638 *
639 * @param allowMonitoring True if monitoring is allowed for this request/op.
640 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
641 * @param op AppOps code for the op to update.
642 * @return True if monitoring is on for this request/op after updating.
643 */
644 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
645 int op) {
646 if (!currentlyMonitoring) {
647 if (allowMonitoring) {
648 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
649 == AppOpsManager.MODE_ALLOWED;
650 }
651 } else {
652 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
653 != AppOpsManager.MODE_ALLOWED) {
654 mAppOps.finishOp(op, mUid, mPackageName);
655 return false;
656 }
657 }
658
659 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700660 }
661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 public boolean isListener() {
663 return mListener != null;
664 }
665
666 public boolean isPendingIntent() {
667 return mPendingIntent != null;
668 }
669
670 public ILocationListener getListener() {
671 if (mListener != null) {
672 return mListener;
673 }
674 throw new IllegalStateException("Request for non-existent listener");
675 }
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
678 if (mListener != null) {
679 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700680 synchronized (this) {
681 // synchronize to ensure incrementPendingBroadcastsLocked()
682 // is called before decrementPendingBroadcasts()
683 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700684 // call this after broadcasting so we do not increment
685 // if we throw an exeption.
686 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 } catch (RemoteException e) {
689 return false;
690 }
691 } else {
692 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800693 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
695 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700696 synchronized (this) {
697 // synchronize to ensure incrementPendingBroadcastsLocked()
698 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700699 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700700 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700701 // call this after broadcasting so we do not increment
702 // if we throw an exeption.
703 incrementPendingBroadcastsLocked();
704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 } catch (PendingIntent.CanceledException e) {
706 return false;
707 }
708 }
709 return true;
710 }
711
712 public boolean callLocationChangedLocked(Location location) {
713 if (mListener != null) {
714 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700715 synchronized (this) {
716 // synchronize to ensure incrementPendingBroadcastsLocked()
717 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800718 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700719 // call this after broadcasting so we do not increment
720 // if we throw an exeption.
721 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 } catch (RemoteException e) {
724 return false;
725 }
726 } else {
727 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800728 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700730 synchronized (this) {
731 // synchronize to ensure incrementPendingBroadcastsLocked()
732 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700733 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700734 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700735 // call this after broadcasting so we do not increment
736 // if we throw an exeption.
737 incrementPendingBroadcastsLocked();
738 }
739 } catch (PendingIntent.CanceledException e) {
740 return false;
741 }
742 }
743 return true;
744 }
745
746 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -0700747 // First update AppOp monitoring.
748 // An app may get/lose location access as providers are enabled/disabled.
749 updateMonitoring(true);
750
Mike Lockwood48f17512009-04-23 09:12:08 -0700751 if (mListener != null) {
752 try {
753 synchronized (this) {
754 // synchronize to ensure incrementPendingBroadcastsLocked()
755 // is called before decrementPendingBroadcasts()
756 if (enabled) {
757 mListener.onProviderEnabled(provider);
758 } else {
759 mListener.onProviderDisabled(provider);
760 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700761 // call this after broadcasting so we do not increment
762 // if we throw an exeption.
763 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700764 }
765 } catch (RemoteException e) {
766 return false;
767 }
768 } else {
769 Intent providerIntent = new Intent();
770 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
771 try {
772 synchronized (this) {
773 // synchronize to ensure incrementPendingBroadcastsLocked()
774 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700775 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700776 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700777 // call this after broadcasting so we do not increment
778 // if we throw an exeption.
779 incrementPendingBroadcastsLocked();
780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 } catch (PendingIntent.CanceledException e) {
782 return false;
783 }
784 }
785 return true;
786 }
787
Nick Pellyf1be6862012-05-15 10:53:42 -0700788 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700790 if (D) Log.d(TAG, "Location listener died");
791
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400792 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 removeUpdatesLocked(this);
794 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700795 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700796 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700797 }
798 }
799
Nick Pellye0fd6932012-07-11 10:26:13 -0700800 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700801 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
802 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400803 synchronized (this) {
804 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700805 }
806 }
807
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400808 // this must be called while synchronized by caller in a synchronized block
809 // containing the sending of the broadcaset
810 private void incrementPendingBroadcastsLocked() {
811 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700812 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400813 }
814 }
815
816 private void decrementPendingBroadcastsLocked() {
817 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700818 if (mWakeLock.isHeld()) {
819 mWakeLock.release();
820 }
821 }
822 }
823
824 public void clearPendingBroadcastsLocked() {
825 if (mPendingBroadcasts > 0) {
826 mPendingBroadcasts = 0;
827 if (mWakeLock.isHeld()) {
828 mWakeLock.release();
829 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700830 }
831 }
832 }
833
Nick Pellye0fd6932012-07-11 10:26:13 -0700834 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700835 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700836 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400837 //the receiver list if it is not found. If it is not found then the
838 //LocationListener was removed when it had a pending broadcast and should
839 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700840 synchronized (mLock) {
841 IBinder binder = listener.asBinder();
842 Receiver receiver = mReceivers.get(binder);
843 if (receiver != null) {
844 synchronized (receiver) {
845 // so wakelock calls will succeed
846 long identity = Binder.clearCallingIdentity();
847 receiver.decrementPendingBroadcastsLocked();
848 Binder.restoreCallingIdentity(identity);
David Christie2ff96af2014-01-30 16:09:37 -0800849 }
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852 }
853
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700854 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400855 mProviders.add(provider);
856 mProvidersByName.put(provider.getName(), provider);
857 }
858
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700859 private void removeProviderLocked(LocationProviderInterface provider) {
860 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400861 mProviders.remove(provider);
862 mProvidersByName.remove(provider.getName());
863 }
864
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800865 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800866 * Returns "true" if access to the specified location provider is allowed by the current
867 * user's settings. Access to all location providers is forbidden to non-location-provider
868 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800869 *
870 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800871 * @return
872 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800873 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 if (mEnabledProviders.contains(provider)) {
875 return true;
876 }
877 if (mDisabledProviders.contains(provider)) {
878 return false;
879 }
880 // Use system settings
881 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882
Victoria Leaseb711d572012-10-02 13:14:11 -0700883 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
885
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700886 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800887 * Returns "true" if access to the specified location provider is allowed by the specified
888 * user's settings. Access to all location providers is forbidden to non-location-provider
889 * processes belonging to background users.
890 *
891 * @param provider the name of the location provider
892 * @param uid the requestor's UID
893 * @return
894 */
895 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
896 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
897 return false;
898 }
899 return isAllowedByCurrentUserSettingsLocked(provider);
900 }
901
902 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700903 * Returns the permission string associated with the specified resolution level.
904 *
905 * @param resolutionLevel the resolution level
906 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700907 */
Victoria Lease37425c32012-10-16 16:08:48 -0700908 private String getResolutionPermission(int resolutionLevel) {
909 switch (resolutionLevel) {
910 case RESOLUTION_LEVEL_FINE:
911 return android.Manifest.permission.ACCESS_FINE_LOCATION;
912 case RESOLUTION_LEVEL_COARSE:
913 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
914 default:
915 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700917 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700918
Victoria Leaseda479c52012-10-15 15:24:16 -0700919 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700920 * Returns the resolution level allowed to the given PID/UID pair.
921 *
922 * @param pid the PID
923 * @param uid the UID
924 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700925 */
Victoria Lease37425c32012-10-16 16:08:48 -0700926 private int getAllowedResolutionLevel(int pid, int uid) {
927 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
928 pid, uid) == PackageManager.PERMISSION_GRANTED) {
929 return RESOLUTION_LEVEL_FINE;
930 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
931 pid, uid) == PackageManager.PERMISSION_GRANTED) {
932 return RESOLUTION_LEVEL_COARSE;
933 } else {
934 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700935 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700936 }
937
938 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700939 * Returns the resolution level allowed to the caller
940 *
941 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700942 */
Victoria Lease37425c32012-10-16 16:08:48 -0700943 private int getCallerAllowedResolutionLevel() {
944 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
945 }
946
947 /**
948 * Throw SecurityException if specified resolution level is insufficient to use geofences.
949 *
950 * @param allowedResolutionLevel resolution level allowed to caller
951 */
952 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
953 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700954 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957
Victoria Lease37425c32012-10-16 16:08:48 -0700958 /**
959 * Return the minimum resolution level required to use the specified location provider.
960 *
961 * @param provider the name of the location provider
962 * @return minimum resolution level required for provider
963 */
964 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700965 if (LocationManager.GPS_PROVIDER.equals(provider) ||
966 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
967 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700968 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700969 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
970 LocationManager.FUSED_PROVIDER.equals(provider)) {
971 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700972 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700973 } else {
974 // mock providers
975 LocationProviderInterface lp = mMockProviders.get(provider);
976 if (lp != null) {
977 ProviderProperties properties = lp.getProperties();
978 if (properties != null) {
979 if (properties.mRequiresSatellite) {
980 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700981 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700982 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
983 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700984 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700985 }
986 }
987 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700988 }
Victoria Lease37425c32012-10-16 16:08:48 -0700989 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700990 }
991
Victoria Lease37425c32012-10-16 16:08:48 -0700992 /**
993 * Throw SecurityException if specified resolution level is insufficient to use the named
994 * location provider.
995 *
996 * @param allowedResolutionLevel resolution level allowed to caller
997 * @param providerName the name of the location provider
998 */
999 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
1000 String providerName) {
1001 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
1002 if (allowedResolutionLevel < requiredResolutionLevel) {
1003 switch (requiredResolutionLevel) {
1004 case RESOLUTION_LEVEL_FINE:
1005 throw new SecurityException("\"" + providerName + "\" location provider " +
1006 "requires ACCESS_FINE_LOCATION permission.");
1007 case RESOLUTION_LEVEL_COARSE:
1008 throw new SecurityException("\"" + providerName + "\" location provider " +
1009 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
1010 default:
1011 throw new SecurityException("Insufficient permission for \"" + providerName +
1012 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -07001013 }
1014 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001015 }
1016
David Christie82edc9b2013-07-19 11:31:42 -07001017 /**
1018 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
1019 * for battery).
1020 */
David Christie40e57822013-07-30 11:36:48 -07001021 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -07001022 mContext.enforceCallingOrSelfPermission(
1023 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
1024 }
1025
David Christie40e57822013-07-30 11:36:48 -07001026 private void checkUpdateAppOpsAllowed() {
1027 mContext.enforceCallingOrSelfPermission(
1028 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
1029 }
1030
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001031 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001032 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1033 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001034 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001035 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001036 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001037 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001038 }
1039 return -1;
1040 }
1041
1042 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
1043 int op = resolutionLevelToOp(allowedResolutionLevel);
1044 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001045 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1046 return false;
1047 }
1048 }
1049 return true;
1050 }
1051
1052 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001053 int op = resolutionLevelToOp(allowedResolutionLevel);
1054 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001055 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1056 return false;
1057 }
1058 }
1059 return true;
1060 }
1061
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001062 /**
1063 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001064 * fused, also including ones that are not permitted to
1065 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001066 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001067 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001069 ArrayList<String> out;
1070 synchronized (mLock) {
1071 out = new ArrayList<String>(mProviders.size());
1072 for (LocationProviderInterface provider : mProviders) {
1073 String name = provider.getName();
1074 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001075 continue;
1076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 out.add(name);
1078 }
1079 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001080
1081 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 return out;
1083 }
1084
Mike Lockwood03ca2162010-04-01 08:10:09 -07001085 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001086 * Return all providers by name, that match criteria and are optionally
1087 * enabled.
1088 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001089 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001090 @Override
1091 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001092 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001093 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001094 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001095 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001096 try {
1097 synchronized (mLock) {
1098 out = new ArrayList<String>(mProviders.size());
1099 for (LocationProviderInterface provider : mProviders) {
1100 String name = provider.getName();
1101 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001102 continue;
1103 }
Victoria Lease37425c32012-10-16 16:08:48 -07001104 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001105 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001106 continue;
1107 }
1108 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1109 name, provider.getProperties(), criteria)) {
1110 continue;
1111 }
1112 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001113 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001114 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001115 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001116 } finally {
1117 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001118 }
1119
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001120 if (D) Log.d(TAG, "getProviders()=" + out);
1121 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001122 }
1123
1124 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001125 * Return the name of the best provider given a Criteria object.
1126 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001127 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001128 * has been deprecated as well. So this method now uses
1129 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001130 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001131 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001132 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001133 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001134
1135 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001136 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001137 result = pickBest(providers);
1138 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1139 return result;
1140 }
1141 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001142 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001143 result = pickBest(providers);
1144 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1145 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001146 }
1147
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001148 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001149 return null;
1150 }
1151
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001152 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001153 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001154 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001155 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1156 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001157 } else {
1158 return providers.get(0);
1159 }
1160 }
1161
Nick Pellye0fd6932012-07-11 10:26:13 -07001162 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001163 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1164 LocationProviderInterface p = mProvidersByName.get(provider);
1165 if (p == null) {
1166 throw new IllegalArgumentException("provider=" + provider);
1167 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001168
1169 boolean result = LocationProvider.propertiesMeetCriteria(
1170 p.getName(), p.getProperties(), criteria);
1171 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1172 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001173 }
1174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001176 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001177 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001178 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 boolean isEnabled = p.isEnabled();
1180 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001181 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001183 updateProviderListenersLocked(name, false, mCurrentUserId);
David Christieb084fef2013-12-18 14:33:57 -08001184 // If any provider has been disabled, clear all last locations for all providers.
1185 // This is to be on the safe side in case a provider has location derived from
1186 // this disabled provider.
1187 mLastLocation.clear();
1188 mLastLocationCoarseInterval.clear();
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001189 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001191 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001192 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001194 }
1195 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001196 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1197 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001198 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1199 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 }
1202
Victoria Leaseb711d572012-10-02 13:14:11 -07001203 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 int listeners = 0;
1205
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001206 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001207 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208
1209 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1212 if (records != null) {
1213 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001214 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001216 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001217 // Sends a notification message to the receiver
1218 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1219 if (deadReceivers == null) {
1220 deadReceivers = new ArrayList<Receiver>();
1221 }
1222 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001224 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
1228
1229 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001230 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 removeUpdatesLocked(deadReceivers.get(i));
1232 }
1233 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 if (enabled) {
1236 p.enable();
1237 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001238 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001245 private void applyRequirementsLocked(String provider) {
1246 LocationProviderInterface p = mProvidersByName.get(provider);
1247 if (p == null) return;
1248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001250 WorkSource worksource = new WorkSource();
1251 ProviderRequest providerRequest = new ProviderRequest();
1252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001254 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001255 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001256 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1257 record.mReceiver.mAllowedResolutionLevel)) {
1258 LocationRequest locationRequest = record.mRequest;
1259 providerRequest.locationRequests.add(locationRequest);
1260 if (locationRequest.getInterval() < providerRequest.interval) {
1261 providerRequest.reportLocation = true;
1262 providerRequest.interval = locationRequest.getInterval();
1263 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001264 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001265 }
1266 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001267
1268 if (providerRequest.reportLocation) {
1269 // calculate who to blame for power
1270 // This is somewhat arbitrary. We pick a threshold interval
1271 // that is slightly higher that the minimum interval, and
1272 // spread the blame across all applications with a request
1273 // under that threshold.
1274 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1275 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001276 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001277 LocationRequest locationRequest = record.mRequest;
1278 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001279 if (record.mReceiver.mWorkSource != null
1280 && record.mReceiver.mWorkSource.size() > 0
1281 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001282 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001283 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001284 worksource.add(record.mReceiver.mWorkSource);
1285 } else {
1286 // Assign blame to caller.
1287 worksource.add(
1288 record.mReceiver.mUid,
1289 record.mReceiver.mPackageName);
1290 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001291 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001292 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
1295 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001296
1297 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1298 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
1300
1301 private class UpdateRecord {
1302 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001303 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001305 Location mLastFixBroadcast;
1306 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307
1308 /**
1309 * Note: must be constructed with lock held.
1310 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001311 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001313 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315
1316 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1317 if (records == null) {
1318 records = new ArrayList<UpdateRecord>();
1319 mRecordsByProvider.put(provider, records);
1320 }
1321 if (!records.contains(this)) {
1322 records.add(this);
1323 }
David Christie2ff96af2014-01-30 16:09:37 -08001324
1325 // Update statistics for historical location requests by package/provider
1326 mRequestStatistics.startRequesting(
1327 mReceiver.mPackageName, provider, request.getInterval());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329
1330 /**
David Christie2ff96af2014-01-30 16:09:37 -08001331 * Method to be called when a record will no longer be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001333 void disposeLocked(boolean removeReceiver) {
David Christie2ff96af2014-01-30 16:09:37 -08001334 mRequestStatistics.stopRequesting(mReceiver.mPackageName, mProvider);
1335
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001336 // remove from mRecordsByProvider
1337 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1338 if (globalRecords != null) {
1339 globalRecords.remove(this);
1340 }
1341
1342 if (!removeReceiver) return; // the caller will handle the rest
1343
1344 // remove from Receiver#mUpdateRecords
1345 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1346 if (receiverRecords != null) {
1347 receiverRecords.remove(this.mProvider);
1348
1349 // and also remove the Receiver if it has no more update records
1350 if (removeReceiver && receiverRecords.size() == 0) {
1351 removeUpdatesLocked(mReceiver);
1352 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355
1356 @Override
1357 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001358 StringBuilder s = new StringBuilder();
1359 s.append("UpdateRecord[");
1360 s.append(mProvider);
1361 s.append(' ').append(mReceiver.mPackageName).append('(');
1362 s.append(mReceiver.mUid).append(')');
1363 s.append(' ').append(mRequest);
1364 s.append(']');
1365 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001369 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001370 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001371 IBinder binder = listener.asBinder();
1372 Receiver receiver = mReceivers.get(binder);
1373 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001374 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1375 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001376 mReceivers.put(binder, receiver);
1377
1378 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001379 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001380 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001381 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001382 return null;
1383 }
1384 }
1385 return receiver;
1386 }
1387
David Christie82edc9b2013-07-19 11:31:42 -07001388 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001389 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001390 Receiver receiver = mReceivers.get(intent);
1391 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001392 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1393 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001394 mReceivers.put(intent, receiver);
1395 }
1396 return receiver;
1397 }
1398
Victoria Lease37425c32012-10-16 16:08:48 -07001399 /**
1400 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1401 * and consistency requirements.
1402 *
1403 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001404 * @return a version of request that meets the given resolution and consistency requirements
1405 * @hide
1406 */
1407 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1408 LocationRequest sanitizedRequest = new LocationRequest(request);
1409 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1410 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001411 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001412 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001413 break;
1414 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001415 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001416 break;
1417 }
1418 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001419 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1420 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001421 }
Victoria Lease37425c32012-10-16 16:08:48 -07001422 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1423 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001424 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001425 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001426 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001427 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001428 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001429 }
Victoria Lease37425c32012-10-16 16:08:48 -07001430 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001431 }
1432
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001433 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001434 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001435 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001436 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001437 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001438 String[] packages = mPackageManager.getPackagesForUid(uid);
1439 if (packages == null) {
1440 throw new SecurityException("invalid UID " + uid);
1441 }
1442 for (String pkg : packages) {
1443 if (packageName.equals(pkg)) return;
1444 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001445 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001446 }
1447
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001448 private void checkPendingIntent(PendingIntent intent) {
1449 if (intent == null) {
1450 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001451 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001452 }
1453
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001454 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001455 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001456 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001457 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001458 } else if (intent != null && listener != null) {
1459 throw new IllegalArgumentException("cannot register both listener and intent");
1460 } else if (intent != null) {
1461 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001462 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001463 } else {
David Christie40e57822013-07-30 11:36:48 -07001464 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001465 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001466 }
1467
Nick Pellye0fd6932012-07-11 10:26:13 -07001468 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001469 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1470 PendingIntent intent, String packageName) {
1471 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1472 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001473 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1474 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1475 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001476 WorkSource workSource = request.getWorkSource();
1477 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001478 checkDeviceStatsAllowed();
1479 }
1480 boolean hideFromAppOps = request.getHideFromAppOps();
1481 if (hideFromAppOps) {
1482 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001483 }
Victoria Lease37425c32012-10-16 16:08:48 -07001484 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001486 final int pid = Binder.getCallingPid();
1487 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001488 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 long identity = Binder.clearCallingIdentity();
1490 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001491 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1492 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001493 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001494
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001495 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001496 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001497 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001498 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 } finally {
1501 Binder.restoreCallingIdentity(identity);
1502 }
1503 }
1504
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001505 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1506 int pid, int uid, String packageName) {
1507 // Figure out the provider. Either its explicitly request (legacy use cases), or
1508 // use the fused provider
1509 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1510 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001511 if (name == null) {
1512 throw new IllegalArgumentException("provider name must not be null");
1513 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001514
1515 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1516 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001517 LocationProviderInterface provider = mProvidersByName.get(name);
1518 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07001519 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001520 }
1521
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001522 UpdateRecord record = new UpdateRecord(name, request, receiver);
1523 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1524 if (oldRecord != null) {
1525 oldRecord.disposeLocked(false);
1526 }
1527
Victoria Lease09eeaec2013-02-05 11:34:13 -08001528 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001529 if (isProviderEnabled) {
1530 applyRequirementsLocked(name);
1531 } else {
1532 // Notify the listener that updates are currently disabled
1533 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
David Christie0b837452013-07-29 16:02:13 -07001535 // Update the monitoring here just in case multiple location requests were added to the
1536 // same receiver (this request may be high power and the initial might not have been).
1537 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539
Nick Pellye0fd6932012-07-11 10:26:13 -07001540 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001541 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1542 String packageName) {
1543 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001544
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001545 final int pid = Binder.getCallingPid();
1546 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001547
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001548 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001549 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001550 boolean hideFromAppOps = false;
1551 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1552 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001553
1554 // providers may use public location API's, need to clear identity
1555 long identity = Binder.clearCallingIdentity();
1556 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001557 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001558 } finally {
1559 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562 }
1563
1564 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001565 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001566
1567 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1568 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1569 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001570 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001574 receiver.updateMonitoring(false);
1575
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001576 // Record which providers were associated with this listener
1577 HashSet<String> providers = new HashSet<String>();
1578 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1579 if (oldRecords != null) {
1580 // Call dispose() on the obsolete update records.
1581 for (UpdateRecord record : oldRecords.values()) {
David Christie2ff96af2014-01-30 16:09:37 -08001582 // Update statistics for historical location requests by package/provider
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001583 record.disposeLocked(false);
1584 }
1585 // Accumulate providers
1586 providers.addAll(oldRecords.keySet());
1587 }
1588
1589 // update provider
1590 for (String provider : providers) {
1591 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001592 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001593 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
1595
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001596 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598 }
1599
Dianne Hackbornc2293022013-02-06 23:14:49 -08001600 private void applyAllProviderRequirementsLocked() {
1601 for (LocationProviderInterface p : mProviders) {
1602 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001603 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001604 continue;
1605 }
1606
1607 applyRequirementsLocked(p.getName());
1608 }
1609 }
1610
Nick Pellye0fd6932012-07-11 10:26:13 -07001611 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001612 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001613 if (D) Log.d(TAG, "getLastLocation: " + request);
1614 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001615 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001616 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001617 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1618 request.getProvider());
1619 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001620
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001621 final int uid = Binder.getCallingUid();
1622 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001623 try {
1624 if (mBlacklist.isBlacklisted(packageName)) {
1625 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1626 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001627 return null;
1628 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001629
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001630 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1631 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1632 packageName);
1633 return null;
1634 }
1635
Victoria Leaseb711d572012-10-02 13:14:11 -07001636 synchronized (mLock) {
1637 // Figure out the provider. Either its explicitly request (deprecated API's),
1638 // or use the fused provider
1639 String name = request.getProvider();
1640 if (name == null) name = LocationManager.FUSED_PROVIDER;
1641 LocationProviderInterface provider = mProvidersByName.get(name);
1642 if (provider == null) return null;
1643
Victoria Lease09eeaec2013-02-05 11:34:13 -08001644 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001645
David Christie1b9b7b12013-04-15 15:31:11 -07001646 Location location;
1647 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1648 // Make sure that an app with coarse permissions can't get frequent location
1649 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1650 location = mLastLocationCoarseInterval.get(name);
1651 } else {
1652 location = mLastLocation.get(name);
1653 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001654 if (location == null) {
1655 return null;
1656 }
Victoria Lease37425c32012-10-16 16:08:48 -07001657 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001658 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1659 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001660 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001661 }
Victoria Lease37425c32012-10-16 16:08:48 -07001662 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001663 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001664 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001665 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001666 return null;
1667 } finally {
1668 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001669 }
1670 }
1671
1672 @Override
1673 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1674 String packageName) {
1675 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001676 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1677 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001678 checkPendingIntent(intent);
1679 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001680 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1681 request.getProvider());
1682 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001683
Victoria Lease37425c32012-10-16 16:08:48 -07001684 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001685
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001686 // geo-fence manager uses the public location API, need to clear identity
1687 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001688 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1689 // temporary measure until geofences work for secondary users
1690 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1691 return;
1692 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001693 long identity = Binder.clearCallingIdentity();
1694 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001695 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1696 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001697 } finally {
1698 Binder.restoreCallingIdentity(identity);
1699 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001700 }
1701
1702 @Override
1703 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001704 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001705 checkPendingIntent(intent);
1706 checkPackageName(packageName);
1707
1708 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1709
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001710 // geo-fence manager uses the public location API, need to clear identity
1711 long identity = Binder.clearCallingIdentity();
1712 try {
1713 mGeofenceManager.removeFence(geofence, intent);
1714 } finally {
1715 Binder.restoreCallingIdentity(identity);
1716 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001717 }
1718
1719
1720 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001721 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001722 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 return false;
1724 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001725 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1726 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001727 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001729 final int uid = Binder.getCallingUid();
1730 final long ident = Binder.clearCallingIdentity();
1731 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001732 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001733 return false;
1734 }
1735 } finally {
1736 Binder.restoreCallingIdentity(ident);
1737 }
1738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001740 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001742 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 return false;
1744 }
1745 return true;
1746 }
1747
Nick Pellye0fd6932012-07-11 10:26:13 -07001748 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001750 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001751 try {
1752 mGpsStatusProvider.removeGpsStatusListener(listener);
1753 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001754 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757 }
1758
Nick Pellye0fd6932012-07-11 10:26:13 -07001759 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001761 if (provider == null) {
1762 // throw NullPointerException to remain compatible with previous implementation
1763 throw new NullPointerException();
1764 }
Victoria Lease37425c32012-10-16 16:08:48 -07001765 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1766 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001769 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 != PackageManager.PERMISSION_GRANTED)) {
1771 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1772 }
1773
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001774 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001775 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001776 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001777
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001778 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780 }
1781
Nick Pellye0fd6932012-07-11 10:26:13 -07001782 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001783 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001784 if (Binder.getCallingUid() != Process.myUid()) {
1785 throw new SecurityException(
1786 "calling sendNiResponse from outside of the system is not allowed");
1787 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001788 try {
1789 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001790 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001791 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001792 return false;
1793 }
1794 }
1795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001797 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001798 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 * accessed by the caller
1800 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001801 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001802 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001803 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08001804 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07001805 }
1806
Victoria Lease37425c32012-10-16 16:08:48 -07001807 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1808 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001810 LocationProviderInterface p;
1811 synchronized (mLock) {
1812 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001815 if (p == null) return null;
1816 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818
Nick Pellye0fd6932012-07-11 10:26:13 -07001819 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07001821 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
1822 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001823 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1824
Victoria Lease09eeaec2013-02-05 11:34:13 -08001825 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001826 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001827 try {
1828 synchronized (mLock) {
1829 LocationProviderInterface p = mProvidersByName.get(provider);
1830 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001831
Victoria Lease09eeaec2013-02-05 11:34:13 -08001832 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001833 }
1834 } finally {
1835 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001836 }
1837 }
1838
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001839 /**
1840 * Returns "true" if the UID belongs to a bound location provider.
1841 *
1842 * @param uid the uid
1843 * @return true if uid belongs to a bound location provider
1844 */
1845 private boolean isUidALocationProvider(int uid) {
1846 if (uid == Process.SYSTEM_UID) {
1847 return true;
1848 }
1849 if (mGeocodeProvider != null) {
David Christie1f141c12014-05-14 15:11:15 -07001850 if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001851 }
1852 for (LocationProviderProxy proxy : mProxyProviders) {
David Christie1f141c12014-05-14 15:11:15 -07001853 if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001854 }
1855 return false;
1856 }
1857
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001858 private void checkCallerIsProvider() {
1859 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1860 == PackageManager.PERMISSION_GRANTED) {
1861 return;
1862 }
1863
1864 // Previously we only used the INSTALL_LOCATION_PROVIDER
1865 // check. But that is system or signature
1866 // protection level which is not flexible enough for
1867 // providers installed oustide the system image. So
1868 // also allow providers with a UID matching the
1869 // currently bound package name
1870
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001871 if (isUidALocationProvider(Binder.getCallingUid())) {
1872 return;
1873 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001874
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001875 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1876 "or UID of a currently bound location provider");
1877 }
1878
David Christie1f141c12014-05-14 15:11:15 -07001879 /**
1880 * Returns true if the given package belongs to the given uid.
1881 */
1882 private boolean doesUidHavePackage(int uid, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001883 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 return false;
1885 }
David Christie1f141c12014-05-14 15:11:15 -07001886 String[] packageNames = mPackageManager.getPackagesForUid(uid);
1887 if (packageNames == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001888 return false;
1889 }
David Christie1f141c12014-05-14 15:11:15 -07001890 for (String name : packageNames) {
1891 if (packageName.equals(name)) {
1892 return true;
1893 }
1894 }
1895 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897
Nick Pellye0fd6932012-07-11 10:26:13 -07001898 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001899 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001900 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001901
Nick Pelly2eeeec22012-07-18 13:13:37 -07001902 if (!location.isComplete()) {
1903 Log.w(TAG, "Dropping incomplete location: " + location);
1904 return;
1905 }
1906
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001907 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1908 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001909 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001910 mLocationHandler.sendMessageAtFrontOfQueue(m);
1911 }
1912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913
Laurent Tu75defb62012-11-01 16:21:52 -07001914 private static boolean shouldBroadcastSafe(
1915 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 // Always broadcast the first update
1917 if (lastLoc == null) {
1918 return true;
1919 }
1920
Nick Pellyf1be6862012-05-15 10:53:42 -07001921 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001922 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001923 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1924 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001925 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 return false;
1927 }
1928
1929 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001930 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (minDistance > 0.0) {
1932 if (loc.distanceTo(lastLoc) <= minDistance) {
1933 return false;
1934 }
1935 }
1936
Laurent Tu75defb62012-11-01 16:21:52 -07001937 // Check whether sufficient number of udpates is left
1938 if (record.mRequest.getNumUpdates() <= 0) {
1939 return false;
1940 }
1941
1942 // Check whether the expiry date has passed
1943 if (record.mRequest.getExpireAt() < now) {
1944 return false;
1945 }
1946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 return true;
1948 }
1949
Mike Lockwooda4903f22010-02-17 06:42:23 -05001950 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001951 if (D) Log.d(TAG, "incoming location: " + location);
1952
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001953 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001954 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955
Laurent Tu60ec50a2012-10-04 17:00:10 -07001956 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001957 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001958 if (p == null) return;
1959
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001960 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001961 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1962 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001963 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001964 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001965 lastLocation = new Location(provider);
1966 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001967 } else {
1968 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1969 if (noGPSLocation == null && lastNoGPSLocation != null) {
1970 // New location has no no-GPS location: adopt last no-GPS location. This is set
1971 // directly into location because we do not want to notify COARSE clients.
1972 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1973 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001974 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001975 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976
David Christie1b9b7b12013-04-15 15:31:11 -07001977 // Update last known coarse interval location if enough time has passed.
1978 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1979 if (lastLocationCoarseInterval == null) {
1980 lastLocationCoarseInterval = new Location(location);
1981 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1982 }
1983 long timeDiffNanos = location.getElapsedRealtimeNanos()
1984 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1985 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1986 lastLocationCoarseInterval.set(location);
1987 }
1988 // Don't ever return a coarse location that is more recent than the allowed update
1989 // interval (i.e. don't allow an app to keep registering and unregistering for
1990 // location updates to overcome the minimum interval).
1991 noGPSLocation =
1992 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1993
Laurent Tu60ec50a2012-10-04 17:00:10 -07001994 // Skip if there are no UpdateRecords for this provider.
1995 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1996 if (records == null || records.size() == 0) return;
1997
Victoria Lease09016ab2012-09-16 12:33:15 -07001998 // Fetch coarse location
1999 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07002000 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002001 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
2002 }
2003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 // Fetch latest status update time
2005 long newStatusUpdateTime = p.getStatusUpdateTime();
2006
David Christie2ff96af2014-01-30 16:09:37 -08002007 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 Bundle extras = new Bundle();
2009 int status = p.getStatus(extras);
2010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002012 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002015 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07002017 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07002018
Victoria Lease269518e2012-10-29 08:25:39 -07002019 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07002020 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002021 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07002022 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07002023 " (current user: " + mCurrentUserId + ", app: " +
2024 receiver.mPackageName + ")");
2025 }
2026 continue;
2027 }
2028
Nick Pelly4035f5a2012-08-17 14:43:49 -07002029 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
2030 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
2031 receiver.mPackageName);
2032 continue;
2033 }
2034
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002035 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
2036 receiver.mAllowedResolutionLevel)) {
2037 if (D) Log.d(TAG, "skipping loc update for no op app: " +
2038 receiver.mPackageName);
2039 continue;
2040 }
2041
Victoria Lease09016ab2012-09-16 12:33:15 -07002042 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07002043 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2044 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002045 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002046 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002047 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002048 if (notifyLocation != null) {
2049 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002050 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002051 if (lastLoc == null) {
2052 lastLoc = new Location(notifyLocation);
2053 r.mLastFixBroadcast = lastLoc;
2054 } else {
2055 lastLoc.set(notifyLocation);
2056 }
2057 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2058 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2059 receiverDead = true;
2060 }
Laurent Tu75defb62012-11-01 16:21:52 -07002061 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063 }
2064
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002065 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002067 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002069 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002071 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002072 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002073 }
2074 }
2075
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002076 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002077 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002078 if (deadUpdateRecords == null) {
2079 deadUpdateRecords = new ArrayList<UpdateRecord>();
2080 }
2081 deadUpdateRecords.add(r);
2082 }
2083 // track dead receivers
2084 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002085 if (deadReceivers == null) {
2086 deadReceivers = new ArrayList<Receiver>();
2087 }
2088 if (!deadReceivers.contains(receiver)) {
2089 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091 }
2092 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002093
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002094 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002096 for (Receiver receiver : deadReceivers) {
2097 removeUpdatesLocked(receiver);
2098 }
2099 }
2100 if (deadUpdateRecords != null) {
2101 for (UpdateRecord r : deadUpdateRecords) {
2102 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002104 applyRequirementsLocked(provider);
2105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107
2108 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002109 public LocationWorkerHandler(Looper looper) {
2110 super(looper, null, true);
2111 }
2112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 @Override
2114 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002115 switch (msg.what) {
2116 case MSG_LOCATION_CHANGED:
2117 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2118 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 }
2120 }
2121 }
2122
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002123 private boolean isMockProvider(String provider) {
2124 synchronized (mLock) {
2125 return mMockProviders.containsKey(provider);
2126 }
2127 }
2128
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002129 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002130 // create a working copy of the incoming Location so that the service can modify it without
2131 // disturbing the caller's copy
2132 Location myLocation = new Location(location);
2133 String provider = myLocation.getProvider();
2134
2135 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2136 // bit if location did not come from a mock provider because passive/fused providers can
2137 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2138 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2139 myLocation.setIsFromMockProvider(true);
2140 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002141
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002142 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002143 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2144 if (!passive) {
2145 // notify passive provider of the new location
2146 mPassiveProvider.updateLocation(myLocation);
2147 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002148 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152
Mike Lockwoode97ae402010-09-29 15:23:46 -04002153 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2154 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002155 public void onPackageDisappeared(String packageName, int reason) {
2156 // remove all receivers associated with this package name
2157 synchronized (mLock) {
2158 ArrayList<Receiver> deadReceivers = null;
2159
2160 for (Receiver receiver : mReceivers.values()) {
2161 if (receiver.mPackageName.equals(packageName)) {
2162 if (deadReceivers == null) {
2163 deadReceivers = new ArrayList<Receiver>();
2164 }
2165 deadReceivers.add(receiver);
2166 }
2167 }
2168
2169 // perform removal outside of mReceivers loop
2170 if (deadReceivers != null) {
2171 for (Receiver receiver : deadReceivers) {
2172 removeUpdatesLocked(receiver);
2173 }
2174 }
2175 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002176 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002177 };
2178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 // Geocoder
2180
Nick Pellye0fd6932012-07-11 10:26:13 -07002181 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002182 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002183 return mGeocodeProvider != null;
2184 }
2185
Nick Pellye0fd6932012-07-11 10:26:13 -07002186 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002188 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002189 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002190 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2191 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002193 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195
Mike Lockwooda55c3212009-04-15 11:10:11 -04002196
Nick Pellye0fd6932012-07-11 10:26:13 -07002197 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002199 double lowerLeftLatitude, double lowerLeftLongitude,
2200 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002201 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002202
2203 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002204 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2205 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2206 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002208 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
2210
2211 // Mock Providers
2212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 private void checkMockPermissionsSafe() {
2214 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2215 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2216 if (!allowMocks) {
2217 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2218 }
2219
2220 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
David Christie2ff96af2014-01-30 16:09:37 -08002221 PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225
Nick Pellye0fd6932012-07-11 10:26:13 -07002226 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002227 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 checkMockPermissionsSafe();
2229
Mike Lockwooda4903f22010-02-17 06:42:23 -05002230 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2231 throw new IllegalArgumentException("Cannot mock the passive location provider");
2232 }
2233
Mike Lockwood86328a92009-10-23 08:38:25 -04002234 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002235 synchronized (mLock) {
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002236 // remove the real provider if we are replacing GPS or network provider
2237 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002238 || LocationManager.NETWORK_PROVIDER.equals(name)
2239 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002240 LocationProviderInterface p = mProvidersByName.get(name);
2241 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002242 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002243 }
2244 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002245 addTestProviderLocked(name, properties);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 updateProvidersLocked();
2247 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002248 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002251 private void addTestProviderLocked(String name, ProviderProperties properties) {
2252 if (mProvidersByName.get(name) != null) {
2253 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2254 }
2255 MockProvider provider = new MockProvider(name, this, properties);
2256 addProviderLocked(provider);
2257 mMockProviders.put(name, provider);
2258 mLastLocation.put(name, null);
2259 mLastLocationCoarseInterval.put(name, null);
2260 }
2261
Nick Pellye0fd6932012-07-11 10:26:13 -07002262 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 public void removeTestProvider(String provider) {
2264 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002265 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002266
2267 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002268 // we don't leave anything dangling.
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002269 clearTestProviderEnabled(provider);
2270 clearTestProviderLocation(provider);
2271 clearTestProviderStatus(provider);
2272
You Kima6d0b6f2012-10-28 03:58:44 +09002273 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002274 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2276 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002277 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002278 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002279
2280 // reinstate real provider if available
2281 LocationProviderInterface realProvider = mRealProviders.get(provider);
2282 if (realProvider != null) {
2283 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002284 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002285 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002286 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002288 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
2290 }
2291
Nick Pellye0fd6932012-07-11 10:26:13 -07002292 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 public void setTestProviderLocation(String provider, Location loc) {
2294 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002295 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002296 MockProvider mockProvider = mMockProviders.get(provider);
2297 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2299 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002300 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2301 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002302 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002303 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 }
2305 }
2306
Nick Pellye0fd6932012-07-11 10:26:13 -07002307 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 public void clearTestProviderLocation(String provider) {
2309 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002310 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002311 MockProvider mockProvider = mMockProviders.get(provider);
2312 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2314 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002315 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
2317 }
2318
Nick Pellye0fd6932012-07-11 10:26:13 -07002319 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 public void setTestProviderEnabled(String provider, boolean enabled) {
2321 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002322 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002323 MockProvider mockProvider = mMockProviders.get(provider);
2324 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2326 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002327 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002329 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 mEnabledProviders.add(provider);
2331 mDisabledProviders.remove(provider);
2332 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002333 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 mEnabledProviders.remove(provider);
2335 mDisabledProviders.add(provider);
2336 }
2337 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002338 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 }
2340 }
2341
Nick Pellye0fd6932012-07-11 10:26:13 -07002342 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 public void clearTestProviderEnabled(String provider) {
2344 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002345 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002346 MockProvider mockProvider = mMockProviders.get(provider);
2347 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2349 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002350 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 mEnabledProviders.remove(provider);
2352 mDisabledProviders.remove(provider);
2353 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002354 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
2356 }
2357
Nick Pellye0fd6932012-07-11 10:26:13 -07002358 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2360 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002361 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002362 MockProvider mockProvider = mMockProviders.get(provider);
2363 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2365 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002366 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 }
2369
Nick Pellye0fd6932012-07-11 10:26:13 -07002370 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 public void clearTestProviderStatus(String provider) {
2372 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002373 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002374 MockProvider mockProvider = mMockProviders.get(provider);
2375 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2377 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002378 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380 }
2381
2382 private void log(String log) {
2383 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002384 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
2386 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002387
2388 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2390 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2391 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002392 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 + Binder.getCallingPid()
2394 + ", uid=" + Binder.getCallingUid());
2395 return;
2396 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002397
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002398 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002401 for (Receiver receiver : mReceivers.values()) {
2402 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
David Christie2ff96af2014-01-30 16:09:37 -08002404 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002405 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2406 pw.println(" " + entry.getKey() + ":");
2407 for (UpdateRecord record : entry.getValue()) {
2408 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 }
2410 }
David Christie2ff96af2014-01-30 16:09:37 -08002411 pw.println(" Historical Records by Provider:");
2412 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
2413 : mRequestStatistics.statistics.entrySet()) {
2414 PackageProviderKey key = entry.getKey();
2415 PackageStatistics stats = entry.getValue();
2416 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
2417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002419 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2420 String provider = entry.getKey();
2421 Location location = entry.getValue();
2422 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002424
David Christie1b9b7b12013-04-15 15:31:11 -07002425 pw.println(" Last Known Locations Coarse Intervals:");
2426 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2427 String provider = entry.getKey();
2428 Location location = entry.getValue();
2429 pw.println(" " + provider + ": " + location);
2430 }
2431
Nick Pellye0fd6932012-07-11 10:26:13 -07002432 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 if (mEnabledProviders.size() > 0) {
2435 pw.println(" Enabled Providers:");
2436 for (String i : mEnabledProviders) {
2437 pw.println(" " + i);
2438 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
2441 if (mDisabledProviders.size() > 0) {
2442 pw.println(" Disabled Providers:");
2443 for (String i : mDisabledProviders) {
2444 pw.println(" " + i);
2445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002447 pw.append(" ");
2448 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 if (mMockProviders.size() > 0) {
2450 pw.println(" Mock Providers:");
2451 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002452 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 }
2454 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002455
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002456 pw.append(" fudger: ");
2457 mLocationFudger.dump(fd, pw, args);
2458
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002459 if (args.length > 0 && "short".equals(args[0])) {
2460 return;
2461 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002462 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002463 pw.print(provider.getName() + " Internal State");
2464 if (provider instanceof LocationProviderProxy) {
2465 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2466 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002467 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002468 pw.println(":");
2469 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 }
2472 }
2473}