blob: a32699a19df04a4c33ec0449c74178324f32fc9d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070021import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070025import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070026import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050027import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070029import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050030import android.content.pm.ResolveInfo;
31import android.content.pm.Signature;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050032import android.content.res.Resources;
Brian Muramatsubb95cb92012-08-29 10:43:21 -070033import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.location.Address;
Mike Lockwood03ca2162010-04-01 08:10:09 -070035import android.location.Criteria;
Mike Lockwood34901402010-01-04 12:14:21 -050036import android.location.GeocoderParams;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070037import android.location.Geofence;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040039import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.location.ILocationListener;
41import android.location.ILocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040042import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.location.Location;
44import android.location.LocationManager;
45import android.location.LocationProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070046import android.location.LocationRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
48import android.os.Bundle;
49import android.os.Handler;
50import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070051import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Message;
53import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070054import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070056import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070057import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070058import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080061import android.util.Slog;
Mike Lockwoode97ae402010-09-29 15:23:46 -040062import com.android.internal.content.PackageMonitor;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070063import com.android.internal.location.ProviderProperties;
64import com.android.internal.location.ProviderRequest;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070065import com.android.internal.os.BackgroundThread;
destradaa1af4b022013-07-12 15:43:36 -070066import com.android.server.location.FlpHardwareProvider;
67import com.android.server.location.FusedProxy;
Mike Lockwood43e33f22010-03-26 10:41:48 -040068import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070069import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070070import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040071import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070072import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070073import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040074import com.android.server.location.LocationProviderInterface;
75import com.android.server.location.LocationProviderProxy;
76import com.android.server.location.MockProvider;
77import com.android.server.location.PassiveProvider;
78
79import java.io.FileDescriptor;
80import java.io.PrintWriter;
81import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070082import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040083import java.util.HashMap;
84import java.util.HashSet;
85import java.util.List;
86import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040087import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/**
90 * The service class that manages LocationProviders and issues location
91 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080093public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080095 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070096
97 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Victoria Lease37425c32012-10-16 16:08:48 -070099 // Location resolution level: no location data whatsoever
100 private static final int RESOLUTION_LEVEL_NONE = 0;
101 // Location resolution level: coarse location data only
102 private static final int RESOLUTION_LEVEL_COARSE = 1;
103 // Location resolution level: fine location data
104 private static final int RESOLUTION_LEVEL_FINE = 2;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700107 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700109 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400110 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700111 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
112
113 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700114 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700115 private static final String FUSED_LOCATION_SERVICE_ACTION =
116 "com.android.location.service.FusedLocationProvider";
117
118 private static final int MSG_LOCATION_CHANGED = 1;
119
David Christie1b9b7b12013-04-15 15:31:11 -0700120 private static final long NANOS_PER_MILLI = 1000000L;
121
David Christie0b837452013-07-29 16:02:13 -0700122 // The maximum interval a location request can have and still be considered "high power".
123 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
124
Nick Pellyf1be6862012-05-15 10:53:42 -0700125 // Location Providers may sometimes deliver location updates
126 // slightly faster that requested - provide grace period so
127 // we don't unnecessarily filter events that are otherwise on
128 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700129 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700130
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700131 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
132
133 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800134 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700135
136 // used internally for synchronization
137 private final Object mLock = new Object();
138
Victoria Lease5cd731a2012-12-19 15:04:21 -0800139 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700140 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700141 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700142 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700143 private PowerManager mPowerManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700144 private GeocoderProxy mGeocodeProvider;
145 private IGpsStatusProvider mGpsStatusProvider;
146 private INetInitiatedListener mNetInitiatedListener;
147 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700148 private PassiveProvider mPassiveProvider; // track passive provider for special cases
149 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700150
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700151 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 // Set of providers that are explicitly enabled
153 private final Set<String> mEnabledProviders = new HashSet<String>();
154
155 // Set of providers that are explicitly disabled
156 private final Set<String> mDisabledProviders = new HashSet<String>();
157
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700158 // Mock (test) providers
159 private final HashMap<String, MockProvider> mMockProviders =
160 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700162 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400163 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500166 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400168
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700169 // real providers, saved here when mocked out
170 private final HashMap<String, LocationProviderInterface> mRealProviders =
171 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700173 // mapping from provider name to provider
174 private final HashMap<String, LocationProviderInterface> mProvidersByName =
175 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700177 // mapping from provider name to all its UpdateRecords
178 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
179 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700180
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700181 // mapping from provider name to last known location
182 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
David Christie1b9b7b12013-04-15 15:31:11 -0700184 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
185 // locations stored here are not fudged for coarse permissions.
186 private final HashMap<String, Location> mLastLocationCoarseInterval =
187 new HashMap<String, Location>();
188
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700189 // all providers that operate over proxy, for authorizing incoming location
190 private final ArrayList<LocationProviderProxy> mProxyProviders =
191 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Victoria Lease38389b62012-09-30 11:44:22 -0700193 // current active user on the device - other users are denied location data
194 private int mCurrentUserId = UserHandle.USER_OWNER;
195
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700196 public LocationManagerService(Context context) {
197 super();
198 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800199 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800200
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700201 if (D) Log.d(TAG, "Constructed");
202
203 // most startup is deferred until systemReady()
204 }
205
Svetoslav Ganova0027152013-06-25 14:59:53 -0700206 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700207 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800208 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700209
Victoria Lease5cd731a2012-12-19 15:04:21 -0800210 // fetch package manager
211 mPackageManager = mContext.getPackageManager();
212
Victoria Lease0aa28602013-05-29 15:28:26 -0700213 // fetch power manager
214 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800215
216 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700217 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800218
219 // prepare mLocationHandler's dependents
220 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
221 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
222 mBlacklist.init();
223 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
224
Dianne Hackbornc2293022013-02-06 23:14:49 -0800225 // Monitor for app ops mode changes.
226 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
227 public void opChanged(int op, String packageName) {
228 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700229 for (Receiver receiver : mReceivers.values()) {
230 receiver.updateMonitoring(true);
231 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800232 applyAllProviderRequirementsLocked();
233 }
234 }
235 };
236 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
237
Victoria Lease5cd731a2012-12-19 15:04:21 -0800238 // prepare providers
239 loadProvidersLocked();
240 updateProvidersLocked();
241 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700242
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700243 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700244 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700245 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700246 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800247 @Override
248 public void onChange(boolean selfChange) {
249 synchronized (mLock) {
250 updateProvidersLocked();
251 }
252 }
253 }, UserHandle.USER_ALL);
254 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700255
Victoria Lease38389b62012-09-30 11:44:22 -0700256 // listen for user change
257 IntentFilter intentFilter = new IntentFilter();
258 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
259
260 mContext.registerReceiverAsUser(new BroadcastReceiver() {
261 @Override
262 public void onReceive(Context context, Intent intent) {
263 String action = intent.getAction();
264 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
265 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
266 }
267 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800268 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700269 }
270
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500271 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
272 PackageManager pm = mContext.getPackageManager();
273 String systemPackageName = mContext.getPackageName();
274 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
275
276 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
277 new Intent(FUSED_LOCATION_SERVICE_ACTION),
278 PackageManager.GET_META_DATA, mCurrentUserId);
279 for (ResolveInfo rInfo : rInfos) {
280 String packageName = rInfo.serviceInfo.packageName;
281
282 // Check that the signature is in the list of supported sigs. If it's not in
283 // this list the standard provider binding logic won't bind to it.
284 try {
285 PackageInfo pInfo;
286 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
287 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
288 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
289 ", but has wrong signature, ignoring");
290 continue;
291 }
292 } catch (NameNotFoundException e) {
293 Log.e(TAG, "missing package: " + packageName);
294 continue;
295 }
296
297 // Get the version info
298 if (rInfo.serviceInfo.metaData == null) {
299 Log.w(TAG, "Found fused provider without metadata: " + packageName);
300 continue;
301 }
302
303 int version = rInfo.serviceInfo.metaData.getInt(
304 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
305 if (version == 0) {
306 // This should be the fallback fused location provider.
307
308 // Make sure it's in the system partition.
309 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
310 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
311 continue;
312 }
313
314 // Check that the fallback is signed the same as the OS
315 // as a proxy for coreApp="true"
316 if (pm.checkSignatures(systemPackageName, packageName)
317 != PackageManager.SIGNATURE_MATCH) {
318 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
319 + packageName);
320 continue;
321 }
322
323 // Found a valid fallback.
324 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
325 return;
326 } else {
327 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
328 }
329 }
330
331 throw new IllegalStateException("Unable to find a fused location provider that is in the "
332 + "system partition with version 0 and signed with the platform certificate. "
333 + "Such a package is needed to provide a default fused location provider in the "
334 + "event that no other fused location provider has been installed or is currently "
335 + "available. For example, coreOnly boot mode when decrypting the data "
336 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
337 }
338
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700339 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700340 // create a passive location provider, which is always enabled
341 PassiveProvider passiveProvider = new PassiveProvider(this);
342 addProviderLocked(passiveProvider);
343 mEnabledProviders.add(passiveProvider.getName());
344 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700345 // Create a gps location provider
346 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
347 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700348
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700349 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700350 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
351 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
352 addProviderLocked(gpsProvider);
353 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
354 }
355
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700356 /*
357 Load package name(s) containing location provider support.
358 These packages can contain services implementing location providers:
359 Geocoder Provider, Network Location Provider, and
360 Fused Location Provider. They will each be searched for
361 service components implementing these providers.
362 The location framework also has support for installation
363 of new location providers at run-time. The new package does not
364 have to be explicitly listed here, however it must have a signature
365 that matches the signature of at least one package on this list.
366 */
367 Resources resources = mContext.getResources();
368 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500369 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700370 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500371 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
372 Arrays.toString(pkgs));
373 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
374
375 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700376
377 // bind to network provider
378 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
379 mContext,
380 LocationManager.NETWORK_PROVIDER,
381 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700382 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
383 com.android.internal.R.string.config_networkLocationProviderPackageName,
384 com.android.internal.R.array.config_locationProviderPackageNames,
385 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700386 if (networkProvider != null) {
387 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
388 mProxyProviders.add(networkProvider);
389 addProviderLocked(networkProvider);
390 } else {
391 Slog.w(TAG, "no network location provider found");
392 }
393
394 // bind to fused provider
395 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
396 mContext,
397 LocationManager.FUSED_PROVIDER,
398 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700399 com.android.internal.R.bool.config_enableFusedLocationOverlay,
400 com.android.internal.R.string.config_fusedLocationProviderPackageName,
401 com.android.internal.R.array.config_locationProviderPackageNames,
402 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700403 if (fusedLocationProvider != null) {
404 addProviderLocked(fusedLocationProvider);
405 mProxyProviders.add(fusedLocationProvider);
406 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700407 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700408 } else {
409 Slog.e(TAG, "no fused location provider found",
410 new IllegalStateException("Location service needs a fused location provider"));
411 }
412
413 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700414 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
415 com.android.internal.R.bool.config_enableGeocoderOverlay,
416 com.android.internal.R.string.config_geocoderProviderPackageName,
417 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800418 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700419 if (mGeocodeProvider == null) {
420 Slog.e(TAG, "no geocoder provider found");
421 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700422
423 // bind to geofence provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700424 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
425 com.android.internal.R.bool.config_enableGeofenceOverlay,
426 com.android.internal.R.string.config_geofenceProviderPackageName,
427 com.android.internal.R.array.config_locationProviderPackageNames,
428 mLocationHandler,
429 gpsProvider.getGpsGeofenceProxy());
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700430 if (provider == null) {
431 Slog.e(TAG, "no geofence provider found");
432 }
433
destradaa1af4b022013-07-12 15:43:36 -0700434 // bind to fused provider
435 // TODO: [GeofenceIntegration] bind #getGeofenceHardware() with the GeofenceProxy
436 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
437 FusedProxy fusedProxy = FusedProxy.createAndBind(
438 mContext,
439 mLocationHandler,
destradaa64be0c62013-08-09 15:01:49 -0700440 flpHardwareProvider.getLocationHardware(),
441 com.android.internal.R.bool.config_enableFusedLocationOverlay,
442 com.android.internal.R.string.config_fusedLocationProviderPackageName,
443 com.android.internal.R.array.config_locationProviderPackageNames);
destradaa1af4b022013-07-12 15:43:36 -0700444
445 if(fusedProxy == null) {
446 Slog.e(TAG, "No FusedProvider found.");
447 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700448 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700451 * Called when the device's active user changes.
452 * @param userId the new active user's UserId
453 */
454 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700455 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800456 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700457 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700458 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700459 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700460 for (LocationProviderInterface p : mProviders) {
461 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700462 }
Victoria Lease38389b62012-09-30 11:44:22 -0700463 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700464 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700465 }
466 }
467
468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
470 * location updates.
471 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700472 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700473 final int mUid; // uid of receiver
474 final int mPid; // pid of receiver
475 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700476 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 final ILocationListener mListener;
479 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700480 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700481 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700483
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400484 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700485
David Christie0b837452013-07-29 16:02:13 -0700486 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700487 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700488 // True if app ops has started monitoring this receiver for high power (gps) locations.
489 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700490 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700491 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700493 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700494 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700497 if (listener != null) {
498 mKey = listener.asBinder();
499 } else {
500 mKey = intent;
501 }
Victoria Lease37425c32012-10-16 16:08:48 -0700502 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700503 mUid = uid;
504 mPid = pid;
505 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700506 if (workSource != null && workSource.size() <= 0) {
507 workSource = null;
508 }
509 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700510 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700511
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700512 updateMonitoring(true);
513
Victoria Lease0aa28602013-05-29 15:28:26 -0700514 // construct/configure wakelock
515 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700516 if (workSource == null) {
517 workSource = new WorkSource(mUid, mPackageName);
518 }
519 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521
522 @Override
523 public boolean equals(Object otherObj) {
524 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700525 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 return false;
528 }
529
530 @Override
531 public int hashCode() {
532 return mKey.hashCode();
533 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 @Override
536 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700537 StringBuilder s = new StringBuilder();
538 s.append("Reciever[");
539 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700541 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700543 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700545 for (String p : mUpdateRecords.keySet()) {
546 s.append(" ").append(mUpdateRecords.get(p).toString());
547 }
548 s.append("]");
549 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700552 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700553 if (mHideFromAppOps) {
554 return;
555 }
556
David Christie0b837452013-07-29 16:02:13 -0700557 // First update monitoring of any location request (including high power).
558 mOpMonitoring = updateMonitoring(allow, mOpMonitoring,
559 AppOpsManager.OP_MONITOR_LOCATION);
560
561 // Now update monitoring of high power requests only.
562 // A high power request is any gps request with interval under a threshold.
563 boolean allowHighPower = allow;
564 if (allowHighPower) {
565 UpdateRecord gpsRecord = mUpdateRecords.get(LocationManager.GPS_PROVIDER);
566 if (gpsRecord == null
567 || gpsRecord.mRequest.getInterval() > HIGH_POWER_INTERVAL_MS) {
568 allowHighPower = false;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700569 }
570 }
David Christiec750c1f2013-08-08 12:56:57 -0700571 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700572 mOpHighPowerMonitoring = updateMonitoring(allowHighPower, mOpHighPowerMonitoring,
573 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700574 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
575 // send an intent to notify that a high power request has been added/removed.
576 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
577 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
578 }
David Christie0b837452013-07-29 16:02:13 -0700579 }
580
581 /**
582 * Update AppOps monitoring for a single location request and op type.
583 *
584 * @param allowMonitoring True if monitoring is allowed for this request/op.
585 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
586 * @param op AppOps code for the op to update.
587 * @return True if monitoring is on for this request/op after updating.
588 */
589 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
590 int op) {
591 if (!currentlyMonitoring) {
592 if (allowMonitoring) {
593 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
594 == AppOpsManager.MODE_ALLOWED;
595 }
596 } else {
597 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
598 != AppOpsManager.MODE_ALLOWED) {
599 mAppOps.finishOp(op, mUid, mPackageName);
600 return false;
601 }
602 }
603
604 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700605 }
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public boolean isListener() {
608 return mListener != null;
609 }
610
611 public boolean isPendingIntent() {
612 return mPendingIntent != null;
613 }
614
615 public ILocationListener getListener() {
616 if (mListener != null) {
617 return mListener;
618 }
619 throw new IllegalStateException("Request for non-existent listener");
620 }
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
623 if (mListener != null) {
624 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700625 synchronized (this) {
626 // synchronize to ensure incrementPendingBroadcastsLocked()
627 // is called before decrementPendingBroadcasts()
628 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700629 // call this after broadcasting so we do not increment
630 // if we throw an exeption.
631 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 } catch (RemoteException e) {
634 return false;
635 }
636 } else {
637 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800638 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
640 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700641 synchronized (this) {
642 // synchronize to ensure incrementPendingBroadcastsLocked()
643 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700644 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700645 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700646 // call this after broadcasting so we do not increment
647 // if we throw an exeption.
648 incrementPendingBroadcastsLocked();
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 } catch (PendingIntent.CanceledException e) {
651 return false;
652 }
653 }
654 return true;
655 }
656
657 public boolean callLocationChangedLocked(Location location) {
658 if (mListener != null) {
659 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700660 synchronized (this) {
661 // synchronize to ensure incrementPendingBroadcastsLocked()
662 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800663 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700664 // call this after broadcasting so we do not increment
665 // if we throw an exeption.
666 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 } catch (RemoteException e) {
669 return false;
670 }
671 } else {
672 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800673 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700675 synchronized (this) {
676 // synchronize to ensure incrementPendingBroadcastsLocked()
677 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700678 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700679 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700680 // call this after broadcasting so we do not increment
681 // if we throw an exeption.
682 incrementPendingBroadcastsLocked();
683 }
684 } catch (PendingIntent.CanceledException e) {
685 return false;
686 }
687 }
688 return true;
689 }
690
691 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
692 if (mListener != null) {
693 try {
694 synchronized (this) {
695 // synchronize to ensure incrementPendingBroadcastsLocked()
696 // is called before decrementPendingBroadcasts()
697 if (enabled) {
698 mListener.onProviderEnabled(provider);
699 } else {
700 mListener.onProviderDisabled(provider);
701 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700702 // call this after broadcasting so we do not increment
703 // if we throw an exeption.
704 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700705 }
706 } catch (RemoteException e) {
707 return false;
708 }
709 } else {
710 Intent providerIntent = new Intent();
711 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
712 try {
713 synchronized (this) {
714 // synchronize to ensure incrementPendingBroadcastsLocked()
715 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700716 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700717 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700718 // call this after broadcasting so we do not increment
719 // if we throw an exeption.
720 incrementPendingBroadcastsLocked();
721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 } catch (PendingIntent.CanceledException e) {
723 return false;
724 }
725 }
726 return true;
727 }
728
Nick Pellyf1be6862012-05-15 10:53:42 -0700729 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700731 if (D) Log.d(TAG, "Location listener died");
732
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400733 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 removeUpdatesLocked(this);
735 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700736 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700737 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700738 }
739 }
740
Nick Pellye0fd6932012-07-11 10:26:13 -0700741 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700742 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
743 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400744 synchronized (this) {
745 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700746 }
747 }
748
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400749 // this must be called while synchronized by caller in a synchronized block
750 // containing the sending of the broadcaset
751 private void incrementPendingBroadcastsLocked() {
752 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700753 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400754 }
755 }
756
757 private void decrementPendingBroadcastsLocked() {
758 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700759 if (mWakeLock.isHeld()) {
760 mWakeLock.release();
761 }
762 }
763 }
764
765 public void clearPendingBroadcastsLocked() {
766 if (mPendingBroadcasts > 0) {
767 mPendingBroadcasts = 0;
768 if (mWakeLock.isHeld()) {
769 mWakeLock.release();
770 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700771 }
772 }
773 }
774
Nick Pellye0fd6932012-07-11 10:26:13 -0700775 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700776 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700777 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400778 //the receiver list if it is not found. If it is not found then the
779 //LocationListener was removed when it had a pending broadcast and should
780 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700781 synchronized (mLock) {
782 IBinder binder = listener.asBinder();
783 Receiver receiver = mReceivers.get(binder);
784 if (receiver != null) {
785 synchronized (receiver) {
786 // so wakelock calls will succeed
787 long identity = Binder.clearCallingIdentity();
788 receiver.decrementPendingBroadcastsLocked();
789 Binder.restoreCallingIdentity(identity);
790 }
791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793 }
794
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700795 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400796 mProviders.add(provider);
797 mProvidersByName.put(provider.getName(), provider);
798 }
799
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700800 private void removeProviderLocked(LocationProviderInterface provider) {
801 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400802 mProviders.remove(provider);
803 mProvidersByName.remove(provider.getName());
804 }
805
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800806 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800807 * Returns "true" if access to the specified location provider is allowed by the current
808 * user's settings. Access to all location providers is forbidden to non-location-provider
809 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800810 *
811 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800812 * @return
813 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800814 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 if (mEnabledProviders.contains(provider)) {
816 return true;
817 }
818 if (mDisabledProviders.contains(provider)) {
819 return false;
820 }
821 // Use system settings
822 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
Victoria Leaseb711d572012-10-02 13:14:11 -0700824 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700827 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800828 * Returns "true" if access to the specified location provider is allowed by the specified
829 * user's settings. Access to all location providers is forbidden to non-location-provider
830 * processes belonging to background users.
831 *
832 * @param provider the name of the location provider
833 * @param uid the requestor's UID
834 * @return
835 */
836 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
837 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
838 return false;
839 }
840 return isAllowedByCurrentUserSettingsLocked(provider);
841 }
842
843 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700844 * Returns the permission string associated with the specified resolution level.
845 *
846 * @param resolutionLevel the resolution level
847 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700848 */
Victoria Lease37425c32012-10-16 16:08:48 -0700849 private String getResolutionPermission(int resolutionLevel) {
850 switch (resolutionLevel) {
851 case RESOLUTION_LEVEL_FINE:
852 return android.Manifest.permission.ACCESS_FINE_LOCATION;
853 case RESOLUTION_LEVEL_COARSE:
854 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
855 default:
856 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700858 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700859
Victoria Leaseda479c52012-10-15 15:24:16 -0700860 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700861 * Returns the resolution level allowed to the given PID/UID pair.
862 *
863 * @param pid the PID
864 * @param uid the UID
865 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700866 */
Victoria Lease37425c32012-10-16 16:08:48 -0700867 private int getAllowedResolutionLevel(int pid, int uid) {
868 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
869 pid, uid) == PackageManager.PERMISSION_GRANTED) {
870 return RESOLUTION_LEVEL_FINE;
871 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
872 pid, uid) == PackageManager.PERMISSION_GRANTED) {
873 return RESOLUTION_LEVEL_COARSE;
874 } else {
875 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700876 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700877 }
878
879 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700880 * Returns the resolution level allowed to the caller
881 *
882 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700883 */
Victoria Lease37425c32012-10-16 16:08:48 -0700884 private int getCallerAllowedResolutionLevel() {
885 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
886 }
887
888 /**
889 * Throw SecurityException if specified resolution level is insufficient to use geofences.
890 *
891 * @param allowedResolutionLevel resolution level allowed to caller
892 */
893 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
894 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700895 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898
Victoria Lease37425c32012-10-16 16:08:48 -0700899 /**
900 * Return the minimum resolution level required to use the specified location provider.
901 *
902 * @param provider the name of the location provider
903 * @return minimum resolution level required for provider
904 */
905 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700906 if (LocationManager.GPS_PROVIDER.equals(provider) ||
907 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
908 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700909 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700910 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
911 LocationManager.FUSED_PROVIDER.equals(provider)) {
912 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700913 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700914 } else {
915 // mock providers
916 LocationProviderInterface lp = mMockProviders.get(provider);
917 if (lp != null) {
918 ProviderProperties properties = lp.getProperties();
919 if (properties != null) {
920 if (properties.mRequiresSatellite) {
921 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700922 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700923 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
924 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700925 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700926 }
927 }
928 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700929 }
Victoria Lease37425c32012-10-16 16:08:48 -0700930 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700931 }
932
Victoria Lease37425c32012-10-16 16:08:48 -0700933 /**
934 * Throw SecurityException if specified resolution level is insufficient to use the named
935 * location provider.
936 *
937 * @param allowedResolutionLevel resolution level allowed to caller
938 * @param providerName the name of the location provider
939 */
940 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
941 String providerName) {
942 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
943 if (allowedResolutionLevel < requiredResolutionLevel) {
944 switch (requiredResolutionLevel) {
945 case RESOLUTION_LEVEL_FINE:
946 throw new SecurityException("\"" + providerName + "\" location provider " +
947 "requires ACCESS_FINE_LOCATION permission.");
948 case RESOLUTION_LEVEL_COARSE:
949 throw new SecurityException("\"" + providerName + "\" location provider " +
950 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
951 default:
952 throw new SecurityException("Insufficient permission for \"" + providerName +
953 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700954 }
955 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700956 }
957
David Christie82edc9b2013-07-19 11:31:42 -0700958 /**
959 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
960 * for battery).
961 */
David Christie40e57822013-07-30 11:36:48 -0700962 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -0700963 mContext.enforceCallingOrSelfPermission(
964 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
965 }
966
David Christie40e57822013-07-30 11:36:48 -0700967 private void checkUpdateAppOpsAllowed() {
968 mContext.enforceCallingOrSelfPermission(
969 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
970 }
971
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800972 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800973 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
974 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800975 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800976 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800977 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800978 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800979 }
980 return -1;
981 }
982
983 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
984 int op = resolutionLevelToOp(allowedResolutionLevel);
985 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800986 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
987 return false;
988 }
989 }
990 return true;
991 }
992
993 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800994 int op = resolutionLevelToOp(allowedResolutionLevel);
995 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800996 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
997 return false;
998 }
999 }
1000 return true;
1001 }
1002
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001003 /**
1004 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001005 * fused, also including ones that are not permitted to
1006 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001007 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001008 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001010 ArrayList<String> out;
1011 synchronized (mLock) {
1012 out = new ArrayList<String>(mProviders.size());
1013 for (LocationProviderInterface provider : mProviders) {
1014 String name = provider.getName();
1015 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001016 continue;
1017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 out.add(name);
1019 }
1020 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001021
1022 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 return out;
1024 }
1025
Mike Lockwood03ca2162010-04-01 08:10:09 -07001026 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001027 * Return all providers by name, that match criteria and are optionally
1028 * enabled.
1029 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001030 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001031 @Override
1032 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001033 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001034 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001035 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001036 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001037 try {
1038 synchronized (mLock) {
1039 out = new ArrayList<String>(mProviders.size());
1040 for (LocationProviderInterface provider : mProviders) {
1041 String name = provider.getName();
1042 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001043 continue;
1044 }
Victoria Lease37425c32012-10-16 16:08:48 -07001045 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001046 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001047 continue;
1048 }
1049 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1050 name, provider.getProperties(), criteria)) {
1051 continue;
1052 }
1053 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001054 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001055 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001056 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001057 } finally {
1058 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001059 }
1060
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001061 if (D) Log.d(TAG, "getProviders()=" + out);
1062 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001063 }
1064
1065 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001066 * Return the name of the best provider given a Criteria object.
1067 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001068 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001069 * has been deprecated as well. So this method now uses
1070 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001071 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001072 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001073 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001074 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001075
1076 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001077 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001078 result = pickBest(providers);
1079 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1080 return result;
1081 }
1082 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001083 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001084 result = pickBest(providers);
1085 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1086 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001087 }
1088
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001089 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001090 return null;
1091 }
1092
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001093 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001094 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001095 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001096 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1097 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001098 } else {
1099 return providers.get(0);
1100 }
1101 }
1102
Nick Pellye0fd6932012-07-11 10:26:13 -07001103 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001104 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1105 LocationProviderInterface p = mProvidersByName.get(provider);
1106 if (p == null) {
1107 throw new IllegalArgumentException("provider=" + provider);
1108 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001109
1110 boolean result = LocationProvider.propertiesMeetCriteria(
1111 p.getName(), p.getProperties(), criteria);
1112 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1113 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001114 }
1115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001117 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001118 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001119 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 boolean isEnabled = p.isEnabled();
1121 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001122 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001124 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001125 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001127 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001128 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001130 }
1131 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001132 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1133 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
1135 }
1136
Victoria Leaseb711d572012-10-02 13:14:11 -07001137 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 int listeners = 0;
1139
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001140 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001141 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142
1143 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1146 if (records != null) {
1147 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001148 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001150 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001151 // Sends a notification message to the receiver
1152 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1153 if (deadReceivers == null) {
1154 deadReceivers = new ArrayList<Receiver>();
1155 }
1156 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001158 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
1161 }
1162
1163 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001164 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 removeUpdatesLocked(deadReceivers.get(i));
1166 }
1167 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 if (enabled) {
1170 p.enable();
1171 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001172 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001179 private void applyRequirementsLocked(String provider) {
1180 LocationProviderInterface p = mProvidersByName.get(provider);
1181 if (p == null) return;
1182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001184 WorkSource worksource = new WorkSource();
1185 ProviderRequest providerRequest = new ProviderRequest();
1186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001188 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001189 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001190 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1191 record.mReceiver.mAllowedResolutionLevel)) {
1192 LocationRequest locationRequest = record.mRequest;
1193 providerRequest.locationRequests.add(locationRequest);
1194 if (locationRequest.getInterval() < providerRequest.interval) {
1195 providerRequest.reportLocation = true;
1196 providerRequest.interval = locationRequest.getInterval();
1197 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001198 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001199 }
1200 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001201
1202 if (providerRequest.reportLocation) {
1203 // calculate who to blame for power
1204 // This is somewhat arbitrary. We pick a threshold interval
1205 // that is slightly higher that the minimum interval, and
1206 // spread the blame across all applications with a request
1207 // under that threshold.
1208 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1209 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001210 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001211 LocationRequest locationRequest = record.mRequest;
1212 if (locationRequest.getInterval() <= thresholdInterval) {
David Christie82edc9b2013-07-19 11:31:42 -07001213 if (record.mReceiver.mWorkSource != null) {
1214 // Assign blame to another work source.
1215 worksource.add(record.mReceiver.mWorkSource);
1216 } else {
1217 // Assign blame to caller.
1218 worksource.add(
1219 record.mReceiver.mUid,
1220 record.mReceiver.mPackageName);
1221 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001222 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001223 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001227
1228 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1229 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
1231
1232 private class UpdateRecord {
1233 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001234 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001236 Location mLastFixBroadcast;
1237 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238
1239 /**
1240 * Note: must be constructed with lock held.
1241 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001242 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001244 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246
1247 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1248 if (records == null) {
1249 records = new ArrayList<UpdateRecord>();
1250 mRecordsByProvider.put(provider, records);
1251 }
1252 if (!records.contains(this)) {
1253 records.add(this);
1254 }
1255 }
1256
1257 /**
1258 * Method to be called when a record will no longer be used. Calling this multiple times
1259 * must have the same effect as calling it once.
1260 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001261 void disposeLocked(boolean removeReceiver) {
1262 // remove from mRecordsByProvider
1263 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1264 if (globalRecords != null) {
1265 globalRecords.remove(this);
1266 }
1267
1268 if (!removeReceiver) return; // the caller will handle the rest
1269
1270 // remove from Receiver#mUpdateRecords
1271 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1272 if (receiverRecords != null) {
1273 receiverRecords.remove(this.mProvider);
1274
1275 // and also remove the Receiver if it has no more update records
1276 if (removeReceiver && receiverRecords.size() == 0) {
1277 removeUpdatesLocked(mReceiver);
1278 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281
1282 @Override
1283 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001284 StringBuilder s = new StringBuilder();
1285 s.append("UpdateRecord[");
1286 s.append(mProvider);
1287 s.append(' ').append(mReceiver.mPackageName).append('(');
1288 s.append(mReceiver.mUid).append(')');
1289 s.append(' ').append(mRequest);
1290 s.append(']');
1291 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001295 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001296 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001297 IBinder binder = listener.asBinder();
1298 Receiver receiver = mReceivers.get(binder);
1299 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001300 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1301 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001302 mReceivers.put(binder, receiver);
1303
1304 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001305 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001306 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001307 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001308 return null;
1309 }
1310 }
1311 return receiver;
1312 }
1313
David Christie82edc9b2013-07-19 11:31:42 -07001314 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001315 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001316 Receiver receiver = mReceivers.get(intent);
1317 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001318 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1319 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001320 mReceivers.put(intent, receiver);
1321 }
1322 return receiver;
1323 }
1324
Victoria Lease37425c32012-10-16 16:08:48 -07001325 /**
1326 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1327 * and consistency requirements.
1328 *
1329 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001330 * @return a version of request that meets the given resolution and consistency requirements
1331 * @hide
1332 */
1333 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1334 LocationRequest sanitizedRequest = new LocationRequest(request);
1335 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1336 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001337 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001338 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001339 break;
1340 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001341 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001342 break;
1343 }
1344 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001345 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1346 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001347 }
Victoria Lease37425c32012-10-16 16:08:48 -07001348 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1349 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001350 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001351 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001352 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001353 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001354 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001355 }
Victoria Lease37425c32012-10-16 16:08:48 -07001356 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001357 }
1358
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001359 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001360 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001361 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001362 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001363 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001364 String[] packages = mPackageManager.getPackagesForUid(uid);
1365 if (packages == null) {
1366 throw new SecurityException("invalid UID " + uid);
1367 }
1368 for (String pkg : packages) {
1369 if (packageName.equals(pkg)) return;
1370 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001371 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001372 }
1373
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001374 private void checkPendingIntent(PendingIntent intent) {
1375 if (intent == null) {
1376 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001377 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001378 }
1379
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001380 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001381 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001382 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001383 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001384 } else if (intent != null && listener != null) {
1385 throw new IllegalArgumentException("cannot register both listener and intent");
1386 } else if (intent != null) {
1387 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001388 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001389 } else {
David Christie40e57822013-07-30 11:36:48 -07001390 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001391 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001392 }
1393
Nick Pellye0fd6932012-07-11 10:26:13 -07001394 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001395 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1396 PendingIntent intent, String packageName) {
1397 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1398 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001399 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1400 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1401 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001402 WorkSource workSource = request.getWorkSource();
1403 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001404 checkDeviceStatsAllowed();
1405 }
1406 boolean hideFromAppOps = request.getHideFromAppOps();
1407 if (hideFromAppOps) {
1408 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001409 }
Victoria Lease37425c32012-10-16 16:08:48 -07001410 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001412 final int pid = Binder.getCallingPid();
1413 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001414 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 long identity = Binder.clearCallingIdentity();
1416 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001417 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1418 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001419 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001420
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001421 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001422 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001423 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001424 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 } finally {
1427 Binder.restoreCallingIdentity(identity);
1428 }
1429 }
1430
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001431 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1432 int pid, int uid, String packageName) {
1433 // Figure out the provider. Either its explicitly request (legacy use cases), or
1434 // use the fused provider
1435 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1436 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001437 if (name == null) {
1438 throw new IllegalArgumentException("provider name must not be null");
1439 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001440
1441 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1442 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001443 LocationProviderInterface provider = mProvidersByName.get(name);
1444 if (provider == null) {
1445 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1446 }
1447
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001448 UpdateRecord record = new UpdateRecord(name, request, receiver);
1449 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1450 if (oldRecord != null) {
1451 oldRecord.disposeLocked(false);
1452 }
1453
Victoria Lease09eeaec2013-02-05 11:34:13 -08001454 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001455 if (isProviderEnabled) {
1456 applyRequirementsLocked(name);
1457 } else {
1458 // Notify the listener that updates are currently disabled
1459 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 }
David Christie0b837452013-07-29 16:02:13 -07001461 // Update the monitoring here just in case multiple location requests were added to the
1462 // same receiver (this request may be high power and the initial might not have been).
1463 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
1465
Nick Pellye0fd6932012-07-11 10:26:13 -07001466 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001467 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1468 String packageName) {
1469 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001470
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001471 final int pid = Binder.getCallingPid();
1472 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001473
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001474 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001475 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001476 boolean hideFromAppOps = false;
1477 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1478 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001479
1480 // providers may use public location API's, need to clear identity
1481 long identity = Binder.clearCallingIdentity();
1482 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001483 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001484 } finally {
1485 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
1488 }
1489
1490 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001491 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001492
1493 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1494 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1495 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001496 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001500 receiver.updateMonitoring(false);
1501
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001502 // Record which providers were associated with this listener
1503 HashSet<String> providers = new HashSet<String>();
1504 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1505 if (oldRecords != null) {
1506 // Call dispose() on the obsolete update records.
1507 for (UpdateRecord record : oldRecords.values()) {
1508 record.disposeLocked(false);
1509 }
1510 // Accumulate providers
1511 providers.addAll(oldRecords.keySet());
1512 }
1513
1514 // update provider
1515 for (String provider : providers) {
1516 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001517 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001518 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001521 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523 }
1524
Dianne Hackbornc2293022013-02-06 23:14:49 -08001525 private void applyAllProviderRequirementsLocked() {
1526 for (LocationProviderInterface p : mProviders) {
1527 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001528 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001529 continue;
1530 }
1531
1532 applyRequirementsLocked(p.getName());
1533 }
1534 }
1535
Nick Pellye0fd6932012-07-11 10:26:13 -07001536 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001537 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001538 if (D) Log.d(TAG, "getLastLocation: " + request);
1539 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001540 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001541 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001542 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1543 request.getProvider());
1544 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001545
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001546 final int uid = Binder.getCallingUid();
1547 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001548 try {
1549 if (mBlacklist.isBlacklisted(packageName)) {
1550 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1551 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001552 return null;
1553 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001554
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001555 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1556 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1557 packageName);
1558 return null;
1559 }
1560
Victoria Leaseb711d572012-10-02 13:14:11 -07001561 synchronized (mLock) {
1562 // Figure out the provider. Either its explicitly request (deprecated API's),
1563 // or use the fused provider
1564 String name = request.getProvider();
1565 if (name == null) name = LocationManager.FUSED_PROVIDER;
1566 LocationProviderInterface provider = mProvidersByName.get(name);
1567 if (provider == null) return null;
1568
Victoria Lease09eeaec2013-02-05 11:34:13 -08001569 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001570
David Christie1b9b7b12013-04-15 15:31:11 -07001571 Location location;
1572 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1573 // Make sure that an app with coarse permissions can't get frequent location
1574 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1575 location = mLastLocationCoarseInterval.get(name);
1576 } else {
1577 location = mLastLocation.get(name);
1578 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001579 if (location == null) {
1580 return null;
1581 }
Victoria Lease37425c32012-10-16 16:08:48 -07001582 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001583 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1584 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001585 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001586 }
Victoria Lease37425c32012-10-16 16:08:48 -07001587 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001588 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001589 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001590 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001591 return null;
1592 } finally {
1593 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001594 }
1595 }
1596
1597 @Override
1598 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1599 String packageName) {
1600 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001601 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1602 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001603 checkPendingIntent(intent);
1604 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001605 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1606 request.getProvider());
1607 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001608
Victoria Lease37425c32012-10-16 16:08:48 -07001609 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001610
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001611 // geo-fence manager uses the public location API, need to clear identity
1612 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001613 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1614 // temporary measure until geofences work for secondary users
1615 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1616 return;
1617 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001618 long identity = Binder.clearCallingIdentity();
1619 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001620 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1621 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001622 } finally {
1623 Binder.restoreCallingIdentity(identity);
1624 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001625 }
1626
1627 @Override
1628 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001629 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001630 checkPendingIntent(intent);
1631 checkPackageName(packageName);
1632
1633 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1634
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001635 // geo-fence manager uses the public location API, need to clear identity
1636 long identity = Binder.clearCallingIdentity();
1637 try {
1638 mGeofenceManager.removeFence(geofence, intent);
1639 } finally {
1640 Binder.restoreCallingIdentity(identity);
1641 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001642 }
1643
1644
1645 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001646 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001647 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 return false;
1649 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001650 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1651 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001652 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001654 final int uid = Binder.getCallingUid();
1655 final long ident = Binder.clearCallingIdentity();
1656 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001657 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001658 return false;
1659 }
1660 } finally {
1661 Binder.restoreCallingIdentity(ident);
1662 }
1663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001665 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001667 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 return false;
1669 }
1670 return true;
1671 }
1672
Nick Pellye0fd6932012-07-11 10:26:13 -07001673 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001675 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001676 try {
1677 mGpsStatusProvider.removeGpsStatusListener(listener);
1678 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001679 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
1682 }
1683
Nick Pellye0fd6932012-07-11 10:26:13 -07001684 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001686 if (provider == null) {
1687 // throw NullPointerException to remain compatible with previous implementation
1688 throw new NullPointerException();
1689 }
Victoria Lease37425c32012-10-16 16:08:48 -07001690 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1691 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001694 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 != PackageManager.PERMISSION_GRANTED)) {
1696 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1697 }
1698
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001699 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001700 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001701 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001702
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001703 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
1705 }
1706
Nick Pellye0fd6932012-07-11 10:26:13 -07001707 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001708 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001709 if (Binder.getCallingUid() != Process.myUid()) {
1710 throw new SecurityException(
1711 "calling sendNiResponse from outside of the system is not allowed");
1712 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001713 try {
1714 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001715 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001716 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001717 return false;
1718 }
1719 }
1720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001722 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001723 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 * accessed by the caller
1725 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001726 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001727 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001728 if (mProvidersByName.get(provider) == null) {
1729 return null;
1730 }
1731
Victoria Lease37425c32012-10-16 16:08:48 -07001732 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1733 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001735 LocationProviderInterface p;
1736 synchronized (mLock) {
1737 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001740 if (p == null) return null;
1741 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743
Nick Pellye0fd6932012-07-11 10:26:13 -07001744 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001746 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1747 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001748 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1749
Victoria Lease09eeaec2013-02-05 11:34:13 -08001750 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001751 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001752 try {
1753 synchronized (mLock) {
1754 LocationProviderInterface p = mProvidersByName.get(provider);
1755 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001756
Victoria Lease09eeaec2013-02-05 11:34:13 -08001757 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001758 }
1759 } finally {
1760 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001761 }
1762 }
1763
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001764 /**
1765 * Returns "true" if the UID belongs to a bound location provider.
1766 *
1767 * @param uid the uid
1768 * @return true if uid belongs to a bound location provider
1769 */
1770 private boolean isUidALocationProvider(int uid) {
1771 if (uid == Process.SYSTEM_UID) {
1772 return true;
1773 }
1774 if (mGeocodeProvider != null) {
1775 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1776 }
1777 for (LocationProviderProxy proxy : mProxyProviders) {
1778 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1779 }
1780 return false;
1781 }
1782
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001783 private void checkCallerIsProvider() {
1784 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1785 == PackageManager.PERMISSION_GRANTED) {
1786 return;
1787 }
1788
1789 // Previously we only used the INSTALL_LOCATION_PROVIDER
1790 // check. But that is system or signature
1791 // protection level which is not flexible enough for
1792 // providers installed oustide the system image. So
1793 // also allow providers with a UID matching the
1794 // currently bound package name
1795
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001796 if (isUidALocationProvider(Binder.getCallingUid())) {
1797 return;
1798 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001799
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001800 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1801 "or UID of a currently bound location provider");
1802 }
1803
1804 private boolean doesPackageHaveUid(int uid, String packageName) {
1805 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 return false;
1807 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001808 try {
1809 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1810 if (appInfo.uid != uid) {
1811 return false;
1812 }
1813 } catch (NameNotFoundException e) {
1814 return false;
1815 }
1816 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818
Nick Pellye0fd6932012-07-11 10:26:13 -07001819 @Override
Mike Lockwooda4903f252010-02-17 06:42:23 -05001820 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001821 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001822
Nick Pelly2eeeec22012-07-18 13:13:37 -07001823 if (!location.isComplete()) {
1824 Log.w(TAG, "Dropping incomplete location: " + location);
1825 return;
1826 }
1827
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001828 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1829 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f252010-02-17 06:42:23 -05001830 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001831 mLocationHandler.sendMessageAtFrontOfQueue(m);
1832 }
1833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834
Laurent Tu75defb62012-11-01 16:21:52 -07001835 private static boolean shouldBroadcastSafe(
1836 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 // Always broadcast the first update
1838 if (lastLoc == null) {
1839 return true;
1840 }
1841
Nick Pellyf1be6862012-05-15 10:53:42 -07001842 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001843 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001844 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1845 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001846 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 return false;
1848 }
1849
1850 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001851 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 if (minDistance > 0.0) {
1853 if (loc.distanceTo(lastLoc) <= minDistance) {
1854 return false;
1855 }
1856 }
1857
Laurent Tu75defb62012-11-01 16:21:52 -07001858 // Check whether sufficient number of udpates is left
1859 if (record.mRequest.getNumUpdates() <= 0) {
1860 return false;
1861 }
1862
1863 // Check whether the expiry date has passed
1864 if (record.mRequest.getExpireAt() < now) {
1865 return false;
1866 }
1867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 return true;
1869 }
1870
Mike Lockwooda4903f252010-02-17 06:42:23 -05001871 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001872 if (D) Log.d(TAG, "incoming location: " + location);
1873
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001874 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f252010-02-17 06:42:23 -05001875 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876
Laurent Tu60ec50a2012-10-04 17:00:10 -07001877 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001878 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001879 if (p == null) return;
1880
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001881 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001882 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1883 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001884 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001885 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001886 lastLocation = new Location(provider);
1887 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001888 } else {
1889 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1890 if (noGPSLocation == null && lastNoGPSLocation != null) {
1891 // New location has no no-GPS location: adopt last no-GPS location. This is set
1892 // directly into location because we do not want to notify COARSE clients.
1893 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1894 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001895 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001896 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897
David Christie1b9b7b12013-04-15 15:31:11 -07001898 // Update last known coarse interval location if enough time has passed.
1899 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1900 if (lastLocationCoarseInterval == null) {
1901 lastLocationCoarseInterval = new Location(location);
1902 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1903 }
1904 long timeDiffNanos = location.getElapsedRealtimeNanos()
1905 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1906 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1907 lastLocationCoarseInterval.set(location);
1908 }
1909 // Don't ever return a coarse location that is more recent than the allowed update
1910 // interval (i.e. don't allow an app to keep registering and unregistering for
1911 // location updates to overcome the minimum interval).
1912 noGPSLocation =
1913 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1914
Laurent Tu60ec50a2012-10-04 17:00:10 -07001915 // Skip if there are no UpdateRecords for this provider.
1916 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1917 if (records == null || records.size() == 0) return;
1918
Victoria Lease09016ab2012-09-16 12:33:15 -07001919 // Fetch coarse location
1920 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001921 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001922 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1923 }
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 // Fetch latest status update time
1926 long newStatusUpdateTime = p.getStatusUpdateTime();
1927
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001928 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 Bundle extras = new Bundle();
1930 int status = p.getStatus(extras);
1931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001933 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001936 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001938 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001939
Victoria Lease269518e2012-10-29 08:25:39 -07001940 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001941 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001942 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001943 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001944 " (current user: " + mCurrentUserId + ", app: " +
1945 receiver.mPackageName + ")");
1946 }
1947 continue;
1948 }
1949
Nick Pelly4035f5a2012-08-17 14:43:49 -07001950 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1951 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1952 receiver.mPackageName);
1953 continue;
1954 }
1955
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001956 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1957 receiver.mAllowedResolutionLevel)) {
1958 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1959 receiver.mPackageName);
1960 continue;
1961 }
1962
Victoria Lease09016ab2012-09-16 12:33:15 -07001963 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001964 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1965 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001966 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001967 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001968 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001969 if (notifyLocation != null) {
1970 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001971 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001972 if (lastLoc == null) {
1973 lastLoc = new Location(notifyLocation);
1974 r.mLastFixBroadcast = lastLoc;
1975 } else {
1976 lastLoc.set(notifyLocation);
1977 }
1978 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1979 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1980 receiverDead = true;
1981 }
Laurent Tu75defb62012-11-01 16:21:52 -07001982 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984 }
1985
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001986 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001988 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001990 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001992 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001993 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001994 }
1995 }
1996
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001997 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001998 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001999 if (deadUpdateRecords == null) {
2000 deadUpdateRecords = new ArrayList<UpdateRecord>();
2001 }
2002 deadUpdateRecords.add(r);
2003 }
2004 // track dead receivers
2005 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002006 if (deadReceivers == null) {
2007 deadReceivers = new ArrayList<Receiver>();
2008 }
2009 if (!deadReceivers.contains(receiver)) {
2010 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
2012 }
2013 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002014
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002015 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002017 for (Receiver receiver : deadReceivers) {
2018 removeUpdatesLocked(receiver);
2019 }
2020 }
2021 if (deadUpdateRecords != null) {
2022 for (UpdateRecord r : deadUpdateRecords) {
2023 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002025 applyRequirementsLocked(provider);
2026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028
2029 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002030 public LocationWorkerHandler(Looper looper) {
2031 super(looper, null, true);
2032 }
2033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 @Override
2035 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002036 switch (msg.what) {
2037 case MSG_LOCATION_CHANGED:
2038 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2039 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
2041 }
2042 }
2043
Victoria Lease54ca7aef22013-01-08 09:39:50 -08002044 private boolean isMockProvider(String provider) {
2045 synchronized (mLock) {
2046 return mMockProviders.containsKey(provider);
2047 }
2048 }
2049
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002050 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7aef22013-01-08 09:39:50 -08002051 // create a working copy of the incoming Location so that the service can modify it without
2052 // disturbing the caller's copy
2053 Location myLocation = new Location(location);
2054 String provider = myLocation.getProvider();
2055
2056 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2057 // bit if location did not come from a mock provider because passive/fused providers can
2058 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2059 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2060 myLocation.setIsFromMockProvider(true);
2061 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002062
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002063 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002064 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2065 if (!passive) {
2066 // notify passive provider of the new location
2067 mPassiveProvider.updateLocation(myLocation);
2068 }
Victoria Lease54ca7aef22013-01-08 09:39:50 -08002069 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073
Mike Lockwoode97ae402010-09-29 15:23:46 -04002074 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2075 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002076 public void onPackageDisappeared(String packageName, int reason) {
2077 // remove all receivers associated with this package name
2078 synchronized (mLock) {
2079 ArrayList<Receiver> deadReceivers = null;
2080
2081 for (Receiver receiver : mReceivers.values()) {
2082 if (receiver.mPackageName.equals(packageName)) {
2083 if (deadReceivers == null) {
2084 deadReceivers = new ArrayList<Receiver>();
2085 }
2086 deadReceivers.add(receiver);
2087 }
2088 }
2089
2090 // perform removal outside of mReceivers loop
2091 if (deadReceivers != null) {
2092 for (Receiver receiver : deadReceivers) {
2093 removeUpdatesLocked(receiver);
2094 }
2095 }
2096 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002097 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002098 };
2099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 // Geocoder
2101
Nick Pellye0fd6932012-07-11 10:26:13 -07002102 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002103 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002104 return mGeocodeProvider != null;
2105 }
2106
Nick Pellye0fd6932012-07-11 10:26:13 -07002107 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002109 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002110 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002111 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2112 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002114 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 }
2116
Mike Lockwooda55c3212009-04-15 11:10:11 -04002117
Nick Pellye0fd6932012-07-11 10:26:13 -07002118 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002120 double lowerLeftLatitude, double lowerLeftLongitude,
2121 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002122 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002123
2124 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002125 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2126 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2127 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002129 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
2131
2132 // Mock Providers
2133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 private void checkMockPermissionsSafe() {
2135 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2136 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2137 if (!allowMocks) {
2138 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2139 }
2140
2141 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2142 PackageManager.PERMISSION_GRANTED) {
2143 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
2146
Nick Pellye0fd6932012-07-11 10:26:13 -07002147 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002148 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 checkMockPermissionsSafe();
2150
Mike Lockwooda4903f252010-02-17 06:42:23 -05002151 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2152 throw new IllegalArgumentException("Cannot mock the passive location provider");
2153 }
2154
Mike Lockwood86328a92009-10-23 08:38:25 -04002155 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002156 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002157 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002158 // remove the real provider if we are replacing GPS or network provider
2159 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002160 || LocationManager.NETWORK_PROVIDER.equals(name)
2161 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002162 LocationProviderInterface p = mProvidersByName.get(name);
2163 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002164 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002165 }
2166 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002167 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2169 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002170 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002171 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002172 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002173 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 updateProvidersLocked();
2175 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002176 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178
Nick Pellye0fd6932012-07-11 10:26:13 -07002179 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 public void removeTestProvider(String provider) {
2181 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002182 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002183 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002184 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2186 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002187 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002188 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002189
2190 // reinstate real provider if available
2191 LocationProviderInterface realProvider = mRealProviders.get(provider);
2192 if (realProvider != null) {
2193 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002194 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002195 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002196 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002198 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
2200 }
2201
Nick Pellye0fd6932012-07-11 10:26:13 -07002202 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 public void setTestProviderLocation(String provider, Location loc) {
2204 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002205 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002206 MockProvider mockProvider = mMockProviders.get(provider);
2207 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2209 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002210 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2211 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002212 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002213 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 }
2215 }
2216
Nick Pellye0fd6932012-07-11 10:26:13 -07002217 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 public void clearTestProviderLocation(String provider) {
2219 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002220 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002221 MockProvider mockProvider = mMockProviders.get(provider);
2222 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2224 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002225 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
2227 }
2228
Nick Pellye0fd6932012-07-11 10:26:13 -07002229 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 public void setTestProviderEnabled(String provider, boolean enabled) {
2231 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002232 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002233 MockProvider mockProvider = mMockProviders.get(provider);
2234 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2236 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002237 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002239 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 mEnabledProviders.add(provider);
2241 mDisabledProviders.remove(provider);
2242 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002243 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 mEnabledProviders.remove(provider);
2245 mDisabledProviders.add(provider);
2246 }
2247 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002248 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 }
2251
Nick Pellye0fd6932012-07-11 10:26:13 -07002252 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 public void clearTestProviderEnabled(String provider) {
2254 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002255 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002256 MockProvider mockProvider = mMockProviders.get(provider);
2257 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2259 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002260 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 mEnabledProviders.remove(provider);
2262 mDisabledProviders.remove(provider);
2263 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002264 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
2266 }
2267
Nick Pellye0fd6932012-07-11 10:26:13 -07002268 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2270 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002271 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002272 MockProvider mockProvider = mMockProviders.get(provider);
2273 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2275 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002276 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 }
2278 }
2279
Nick Pellye0fd6932012-07-11 10:26:13 -07002280 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 public void clearTestProviderStatus(String provider) {
2282 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002283 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002284 MockProvider mockProvider = mMockProviders.get(provider);
2285 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2287 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002288 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
2290 }
2291
2292 private void log(String log) {
2293 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002294 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
2296 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002297
2298 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2300 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2301 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002302 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 + Binder.getCallingPid()
2304 + ", uid=" + Binder.getCallingUid());
2305 return;
2306 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002307
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002308 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002311 for (Receiver receiver : mReceivers.values()) {
2312 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002315 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2316 pw.println(" " + entry.getKey() + ":");
2317 for (UpdateRecord record : entry.getValue()) {
2318 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
2320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002322 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2323 String provider = entry.getKey();
2324 Location location = entry.getValue();
2325 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002327
David Christie1b9b7b12013-04-15 15:31:11 -07002328 pw.println(" Last Known Locations Coarse Intervals:");
2329 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2330 String provider = entry.getKey();
2331 Location location = entry.getValue();
2332 pw.println(" " + provider + ": " + location);
2333 }
2334
Nick Pellye0fd6932012-07-11 10:26:13 -07002335 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 if (mEnabledProviders.size() > 0) {
2338 pw.println(" Enabled Providers:");
2339 for (String i : mEnabledProviders) {
2340 pw.println(" " + i);
2341 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
2344 if (mDisabledProviders.size() > 0) {
2345 pw.println(" Disabled Providers:");
2346 for (String i : mDisabledProviders) {
2347 pw.println(" " + i);
2348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002350 pw.append(" ");
2351 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 if (mMockProviders.size() > 0) {
2353 pw.println(" Mock Providers:");
2354 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002355 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
2357 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002358
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002359 pw.append(" fudger: ");
2360 mLocationFudger.dump(fd, pw, args);
2361
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002362 if (args.length > 0 && "short".equals(args[0])) {
2363 return;
2364 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002365 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002366 pw.print(provider.getName() + " Internal State");
2367 if (provider instanceof LocationProviderProxy) {
2368 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2369 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002370 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002371 pw.println(":");
2372 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375 }
2376}