blob: 61752684e1b29af96b0ebf7e80d71ba80c751866 [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;
Mike Lockwood43e33f22010-03-26 10:41:48 -040066import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070067import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070068import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040069import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070070import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070071import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040072import com.android.server.location.LocationProviderInterface;
73import com.android.server.location.LocationProviderProxy;
74import com.android.server.location.MockProvider;
75import com.android.server.location.PassiveProvider;
76
77import java.io.FileDescriptor;
78import java.io.PrintWriter;
79import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070080import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040081import java.util.HashMap;
82import java.util.HashSet;
83import java.util.List;
84import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040085import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * The service class that manages LocationProviders and issues location
89 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080091public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080093 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070094
95 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Victoria Lease37425c32012-10-16 16:08:48 -070097 // Location resolution level: no location data whatsoever
98 private static final int RESOLUTION_LEVEL_NONE = 0;
99 // Location resolution level: coarse location data only
100 private static final int RESOLUTION_LEVEL_COARSE = 1;
101 // Location resolution level: fine location data
102 private static final int RESOLUTION_LEVEL_FINE = 2;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700105 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700107 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400108 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700109 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
110
111 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700112 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700113 private static final String FUSED_LOCATION_SERVICE_ACTION =
114 "com.android.location.service.FusedLocationProvider";
115
116 private static final int MSG_LOCATION_CHANGED = 1;
117
David Christie1b9b7b12013-04-15 15:31:11 -0700118 private static final long NANOS_PER_MILLI = 1000000L;
119
David Christie0b837452013-07-29 16:02:13 -0700120 // The maximum interval a location request can have and still be considered "high power".
121 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
122
Nick Pellyf1be6862012-05-15 10:53:42 -0700123 // Location Providers may sometimes deliver location updates
124 // slightly faster that requested - provide grace period so
125 // we don't unnecessarily filter events that are otherwise on
126 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700127 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700128
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700129 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
130
131 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800132 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700133
134 // used internally for synchronization
135 private final Object mLock = new Object();
136
Victoria Lease5cd731a2012-12-19 15:04:21 -0800137 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700138 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700139 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700140 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700141 private PowerManager mPowerManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700142 private GeocoderProxy mGeocodeProvider;
143 private IGpsStatusProvider mGpsStatusProvider;
144 private INetInitiatedListener mNetInitiatedListener;
145 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700146 private PassiveProvider mPassiveProvider; // track passive provider for special cases
147 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700148
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700149 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // Set of providers that are explicitly enabled
151 private final Set<String> mEnabledProviders = new HashSet<String>();
152
153 // Set of providers that are explicitly disabled
154 private final Set<String> mDisabledProviders = new HashSet<String>();
155
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700156 // Mock (test) providers
157 private final HashMap<String, MockProvider> mMockProviders =
158 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700160 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400161 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700163 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500164 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400166
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 // real providers, saved here when mocked out
168 private final HashMap<String, LocationProviderInterface> mRealProviders =
169 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700171 // mapping from provider name to provider
172 private final HashMap<String, LocationProviderInterface> mProvidersByName =
173 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700175 // mapping from provider name to all its UpdateRecords
176 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
177 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700178
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700179 // mapping from provider name to last known location
180 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
David Christie1b9b7b12013-04-15 15:31:11 -0700182 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
183 // locations stored here are not fudged for coarse permissions.
184 private final HashMap<String, Location> mLastLocationCoarseInterval =
185 new HashMap<String, Location>();
186
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700187 // all providers that operate over proxy, for authorizing incoming location
188 private final ArrayList<LocationProviderProxy> mProxyProviders =
189 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Victoria Lease38389b62012-09-30 11:44:22 -0700191 // current active user on the device - other users are denied location data
192 private int mCurrentUserId = UserHandle.USER_OWNER;
193
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700194 public LocationManagerService(Context context) {
195 super();
196 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800197 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800198
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700199 if (D) Log.d(TAG, "Constructed");
200
201 // most startup is deferred until systemReady()
202 }
203
Svetoslav Ganova0027152013-06-25 14:59:53 -0700204 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700205 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800206 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700207
Victoria Lease5cd731a2012-12-19 15:04:21 -0800208 // fetch package manager
209 mPackageManager = mContext.getPackageManager();
210
Victoria Lease0aa28602013-05-29 15:28:26 -0700211 // fetch power manager
212 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800213
214 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700215 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800216
217 // prepare mLocationHandler's dependents
218 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
219 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
220 mBlacklist.init();
221 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
222
Dianne Hackbornc2293022013-02-06 23:14:49 -0800223 // Monitor for app ops mode changes.
224 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
225 public void opChanged(int op, String packageName) {
226 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700227 for (Receiver receiver : mReceivers.values()) {
228 receiver.updateMonitoring(true);
229 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800230 applyAllProviderRequirementsLocked();
231 }
232 }
233 };
234 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
235
Victoria Lease5cd731a2012-12-19 15:04:21 -0800236 // prepare providers
237 loadProvidersLocked();
238 updateProvidersLocked();
239 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700240
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700241 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700242 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700243 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700244 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800245 @Override
246 public void onChange(boolean selfChange) {
247 synchronized (mLock) {
248 updateProvidersLocked();
249 }
250 }
251 }, UserHandle.USER_ALL);
252 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700253
Victoria Lease38389b62012-09-30 11:44:22 -0700254 // listen for user change
255 IntentFilter intentFilter = new IntentFilter();
256 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
257
258 mContext.registerReceiverAsUser(new BroadcastReceiver() {
259 @Override
260 public void onReceive(Context context, Intent intent) {
261 String action = intent.getAction();
262 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
263 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
264 }
265 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800266 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700267 }
268
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500269 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
270 PackageManager pm = mContext.getPackageManager();
271 String systemPackageName = mContext.getPackageName();
272 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
273
274 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
275 new Intent(FUSED_LOCATION_SERVICE_ACTION),
276 PackageManager.GET_META_DATA, mCurrentUserId);
277 for (ResolveInfo rInfo : rInfos) {
278 String packageName = rInfo.serviceInfo.packageName;
279
280 // Check that the signature is in the list of supported sigs. If it's not in
281 // this list the standard provider binding logic won't bind to it.
282 try {
283 PackageInfo pInfo;
284 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
285 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
286 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
287 ", but has wrong signature, ignoring");
288 continue;
289 }
290 } catch (NameNotFoundException e) {
291 Log.e(TAG, "missing package: " + packageName);
292 continue;
293 }
294
295 // Get the version info
296 if (rInfo.serviceInfo.metaData == null) {
297 Log.w(TAG, "Found fused provider without metadata: " + packageName);
298 continue;
299 }
300
301 int version = rInfo.serviceInfo.metaData.getInt(
302 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
303 if (version == 0) {
304 // This should be the fallback fused location provider.
305
306 // Make sure it's in the system partition.
307 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
308 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
309 continue;
310 }
311
312 // Check that the fallback is signed the same as the OS
313 // as a proxy for coreApp="true"
314 if (pm.checkSignatures(systemPackageName, packageName)
315 != PackageManager.SIGNATURE_MATCH) {
316 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
317 + packageName);
318 continue;
319 }
320
321 // Found a valid fallback.
322 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
323 return;
324 } else {
325 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
326 }
327 }
328
329 throw new IllegalStateException("Unable to find a fused location provider that is in the "
330 + "system partition with version 0 and signed with the platform certificate. "
331 + "Such a package is needed to provide a default fused location provider in the "
332 + "event that no other fused location provider has been installed or is currently "
333 + "available. For example, coreOnly boot mode when decrypting the data "
334 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
335 }
336
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700337 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700338 // create a passive location provider, which is always enabled
339 PassiveProvider passiveProvider = new PassiveProvider(this);
340 addProviderLocked(passiveProvider);
341 mEnabledProviders.add(passiveProvider.getName());
342 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700343 // Create a gps location provider
344 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
345 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700346
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700347 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700348 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
349 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
350 addProviderLocked(gpsProvider);
351 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
352 }
353
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700354 /*
355 Load package name(s) containing location provider support.
356 These packages can contain services implementing location providers:
357 Geocoder Provider, Network Location Provider, and
358 Fused Location Provider. They will each be searched for
359 service components implementing these providers.
360 The location framework also has support for installation
361 of new location providers at run-time. The new package does not
362 have to be explicitly listed here, however it must have a signature
363 that matches the signature of at least one package on this list.
364 */
365 Resources resources = mContext.getResources();
366 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500367 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700368 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500369 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
370 Arrays.toString(pkgs));
371 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
372
373 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700374
375 // bind to network provider
376 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
377 mContext,
378 LocationManager.NETWORK_PROVIDER,
379 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700380 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
381 com.android.internal.R.string.config_networkLocationProviderPackageName,
382 com.android.internal.R.array.config_locationProviderPackageNames,
383 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700384 if (networkProvider != null) {
385 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
386 mProxyProviders.add(networkProvider);
387 addProviderLocked(networkProvider);
388 } else {
389 Slog.w(TAG, "no network location provider found");
390 }
391
392 // bind to fused provider
393 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
394 mContext,
395 LocationManager.FUSED_PROVIDER,
396 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700397 com.android.internal.R.bool.config_enableFusedLocationOverlay,
398 com.android.internal.R.string.config_fusedLocationProviderPackageName,
399 com.android.internal.R.array.config_locationProviderPackageNames,
400 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700401 if (fusedLocationProvider != null) {
402 addProviderLocked(fusedLocationProvider);
403 mProxyProviders.add(fusedLocationProvider);
404 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700405 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700406 } else {
407 Slog.e(TAG, "no fused location provider found",
408 new IllegalStateException("Location service needs a fused location provider"));
409 }
410
411 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700412 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
413 com.android.internal.R.bool.config_enableGeocoderOverlay,
414 com.android.internal.R.string.config_geocoderProviderPackageName,
415 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800416 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700417 if (mGeocodeProvider == null) {
418 Slog.e(TAG, "no geocoder provider found");
419 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700420
421 // bind to geofence provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700422 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
423 com.android.internal.R.bool.config_enableGeofenceOverlay,
424 com.android.internal.R.string.config_geofenceProviderPackageName,
425 com.android.internal.R.array.config_locationProviderPackageNames,
426 mLocationHandler,
427 gpsProvider.getGpsGeofenceProxy());
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700428 if (provider == null) {
429 Slog.e(TAG, "no geofence provider found");
430 }
431
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700432 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700435 * Called when the device's active user changes.
436 * @param userId the new active user's UserId
437 */
438 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700439 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800440 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700441 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700442 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700443 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700444 for (LocationProviderInterface p : mProviders) {
445 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700446 }
Victoria Lease38389b62012-09-30 11:44:22 -0700447 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700448 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700449 }
450 }
451
452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
454 * location updates.
455 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700456 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700457 final int mUid; // uid of receiver
458 final int mPid; // pid of receiver
459 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700460 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final ILocationListener mListener;
463 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700464 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700465 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700467
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400468 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700469
David Christie0b837452013-07-29 16:02:13 -0700470 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700471 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700472 // True if app ops has started monitoring this receiver for high power (gps) locations.
473 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700474 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700475 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700477 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700478 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700481 if (listener != null) {
482 mKey = listener.asBinder();
483 } else {
484 mKey = intent;
485 }
Victoria Lease37425c32012-10-16 16:08:48 -0700486 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700487 mUid = uid;
488 mPid = pid;
489 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700490 if (workSource != null && workSource.size() <= 0) {
491 workSource = null;
492 }
493 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700494 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700495
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700496 updateMonitoring(true);
497
Victoria Lease0aa28602013-05-29 15:28:26 -0700498 // construct/configure wakelock
499 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700500 if (workSource == null) {
501 workSource = new WorkSource(mUid, mPackageName);
502 }
503 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
506 @Override
507 public boolean equals(Object otherObj) {
508 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700509 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 return false;
512 }
513
514 @Override
515 public int hashCode() {
516 return mKey.hashCode();
517 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 @Override
520 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700521 StringBuilder s = new StringBuilder();
522 s.append("Reciever[");
523 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700525 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700527 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700529 for (String p : mUpdateRecords.keySet()) {
530 s.append(" ").append(mUpdateRecords.get(p).toString());
531 }
532 s.append("]");
533 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700536 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700537 if (mHideFromAppOps) {
538 return;
539 }
540
David Christie0b837452013-07-29 16:02:13 -0700541 // First update monitoring of any location request (including high power).
542 mOpMonitoring = updateMonitoring(allow, mOpMonitoring,
543 AppOpsManager.OP_MONITOR_LOCATION);
544
545 // Now update monitoring of high power requests only.
546 // A high power request is any gps request with interval under a threshold.
547 boolean allowHighPower = allow;
548 if (allowHighPower) {
549 UpdateRecord gpsRecord = mUpdateRecords.get(LocationManager.GPS_PROVIDER);
550 if (gpsRecord == null
551 || gpsRecord.mRequest.getInterval() > HIGH_POWER_INTERVAL_MS) {
552 allowHighPower = false;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700553 }
554 }
David Christiec750c1f2013-08-08 12:56:57 -0700555 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700556 mOpHighPowerMonitoring = updateMonitoring(allowHighPower, mOpHighPowerMonitoring,
557 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700558 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
559 // send an intent to notify that a high power request has been added/removed.
560 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
561 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
562 }
David Christie0b837452013-07-29 16:02:13 -0700563 }
564
565 /**
566 * Update AppOps monitoring for a single location request and op type.
567 *
568 * @param allowMonitoring True if monitoring is allowed for this request/op.
569 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
570 * @param op AppOps code for the op to update.
571 * @return True if monitoring is on for this request/op after updating.
572 */
573 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
574 int op) {
575 if (!currentlyMonitoring) {
576 if (allowMonitoring) {
577 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
578 == AppOpsManager.MODE_ALLOWED;
579 }
580 } else {
581 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
582 != AppOpsManager.MODE_ALLOWED) {
583 mAppOps.finishOp(op, mUid, mPackageName);
584 return false;
585 }
586 }
587
588 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700589 }
590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 public boolean isListener() {
592 return mListener != null;
593 }
594
595 public boolean isPendingIntent() {
596 return mPendingIntent != null;
597 }
598
599 public ILocationListener getListener() {
600 if (mListener != null) {
601 return mListener;
602 }
603 throw new IllegalStateException("Request for non-existent listener");
604 }
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
607 if (mListener != null) {
608 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700609 synchronized (this) {
610 // synchronize to ensure incrementPendingBroadcastsLocked()
611 // is called before decrementPendingBroadcasts()
612 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700613 // call this after broadcasting so we do not increment
614 // if we throw an exeption.
615 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 } catch (RemoteException e) {
618 return false;
619 }
620 } else {
621 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800622 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
624 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700625 synchronized (this) {
626 // synchronize to ensure incrementPendingBroadcastsLocked()
627 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700628 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700629 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700630 // call this after broadcasting so we do not increment
631 // if we throw an exeption.
632 incrementPendingBroadcastsLocked();
633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 } catch (PendingIntent.CanceledException e) {
635 return false;
636 }
637 }
638 return true;
639 }
640
641 public boolean callLocationChangedLocked(Location location) {
642 if (mListener != null) {
643 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700644 synchronized (this) {
645 // synchronize to ensure incrementPendingBroadcastsLocked()
646 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800647 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700648 // call this after broadcasting so we do not increment
649 // if we throw an exeption.
650 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 } catch (RemoteException e) {
653 return false;
654 }
655 } else {
656 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800657 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700659 synchronized (this) {
660 // synchronize to ensure incrementPendingBroadcastsLocked()
661 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700662 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700663 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700664 // call this after broadcasting so we do not increment
665 // if we throw an exeption.
666 incrementPendingBroadcastsLocked();
667 }
668 } catch (PendingIntent.CanceledException e) {
669 return false;
670 }
671 }
672 return true;
673 }
674
675 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
676 if (mListener != null) {
677 try {
678 synchronized (this) {
679 // synchronize to ensure incrementPendingBroadcastsLocked()
680 // is called before decrementPendingBroadcasts()
681 if (enabled) {
682 mListener.onProviderEnabled(provider);
683 } else {
684 mListener.onProviderDisabled(provider);
685 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700686 // call this after broadcasting so we do not increment
687 // if we throw an exeption.
688 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700689 }
690 } catch (RemoteException e) {
691 return false;
692 }
693 } else {
694 Intent providerIntent = new Intent();
695 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
696 try {
697 synchronized (this) {
698 // synchronize to ensure incrementPendingBroadcastsLocked()
699 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700700 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700701 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700702 // call this after broadcasting so we do not increment
703 // if we throw an exeption.
704 incrementPendingBroadcastsLocked();
705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 } catch (PendingIntent.CanceledException e) {
707 return false;
708 }
709 }
710 return true;
711 }
712
Nick Pellyf1be6862012-05-15 10:53:42 -0700713 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700715 if (D) Log.d(TAG, "Location listener died");
716
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400717 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 removeUpdatesLocked(this);
719 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700720 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700721 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700722 }
723 }
724
Nick Pellye0fd6932012-07-11 10:26:13 -0700725 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700726 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
727 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400728 synchronized (this) {
729 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700730 }
731 }
732
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400733 // this must be called while synchronized by caller in a synchronized block
734 // containing the sending of the broadcaset
735 private void incrementPendingBroadcastsLocked() {
736 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700737 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400738 }
739 }
740
741 private void decrementPendingBroadcastsLocked() {
742 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700743 if (mWakeLock.isHeld()) {
744 mWakeLock.release();
745 }
746 }
747 }
748
749 public void clearPendingBroadcastsLocked() {
750 if (mPendingBroadcasts > 0) {
751 mPendingBroadcasts = 0;
752 if (mWakeLock.isHeld()) {
753 mWakeLock.release();
754 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700755 }
756 }
757 }
758
Nick Pellye0fd6932012-07-11 10:26:13 -0700759 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700760 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700761 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400762 //the receiver list if it is not found. If it is not found then the
763 //LocationListener was removed when it had a pending broadcast and should
764 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700765 synchronized (mLock) {
766 IBinder binder = listener.asBinder();
767 Receiver receiver = mReceivers.get(binder);
768 if (receiver != null) {
769 synchronized (receiver) {
770 // so wakelock calls will succeed
771 long identity = Binder.clearCallingIdentity();
772 receiver.decrementPendingBroadcastsLocked();
773 Binder.restoreCallingIdentity(identity);
774 }
775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
777 }
778
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700779 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400780 mProviders.add(provider);
781 mProvidersByName.put(provider.getName(), provider);
782 }
783
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700784 private void removeProviderLocked(LocationProviderInterface provider) {
785 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400786 mProviders.remove(provider);
787 mProvidersByName.remove(provider.getName());
788 }
789
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800790 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800791 * Returns "true" if access to the specified location provider is allowed by the current
792 * user's settings. Access to all location providers is forbidden to non-location-provider
793 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800794 *
795 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800796 * @return
797 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800798 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 if (mEnabledProviders.contains(provider)) {
800 return true;
801 }
802 if (mDisabledProviders.contains(provider)) {
803 return false;
804 }
805 // Use system settings
806 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807
Victoria Leaseb711d572012-10-02 13:14:11 -0700808 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 }
810
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700811 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800812 * Returns "true" if access to the specified location provider is allowed by the specified
813 * user's settings. Access to all location providers is forbidden to non-location-provider
814 * processes belonging to background users.
815 *
816 * @param provider the name of the location provider
817 * @param uid the requestor's UID
818 * @return
819 */
820 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
821 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
822 return false;
823 }
824 return isAllowedByCurrentUserSettingsLocked(provider);
825 }
826
827 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700828 * Returns the permission string associated with the specified resolution level.
829 *
830 * @param resolutionLevel the resolution level
831 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700832 */
Victoria Lease37425c32012-10-16 16:08:48 -0700833 private String getResolutionPermission(int resolutionLevel) {
834 switch (resolutionLevel) {
835 case RESOLUTION_LEVEL_FINE:
836 return android.Manifest.permission.ACCESS_FINE_LOCATION;
837 case RESOLUTION_LEVEL_COARSE:
838 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
839 default:
840 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700842 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700843
Victoria Leaseda479c52012-10-15 15:24:16 -0700844 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700845 * Returns the resolution level allowed to the given PID/UID pair.
846 *
847 * @param pid the PID
848 * @param uid the UID
849 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700850 */
Victoria Lease37425c32012-10-16 16:08:48 -0700851 private int getAllowedResolutionLevel(int pid, int uid) {
852 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
853 pid, uid) == PackageManager.PERMISSION_GRANTED) {
854 return RESOLUTION_LEVEL_FINE;
855 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
856 pid, uid) == PackageManager.PERMISSION_GRANTED) {
857 return RESOLUTION_LEVEL_COARSE;
858 } else {
859 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700860 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700861 }
862
863 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700864 * Returns the resolution level allowed to the caller
865 *
866 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700867 */
Victoria Lease37425c32012-10-16 16:08:48 -0700868 private int getCallerAllowedResolutionLevel() {
869 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
870 }
871
872 /**
873 * Throw SecurityException if specified resolution level is insufficient to use geofences.
874 *
875 * @param allowedResolutionLevel resolution level allowed to caller
876 */
877 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
878 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700879 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882
Victoria Lease37425c32012-10-16 16:08:48 -0700883 /**
884 * Return the minimum resolution level required to use the specified location provider.
885 *
886 * @param provider the name of the location provider
887 * @return minimum resolution level required for provider
888 */
889 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700890 if (LocationManager.GPS_PROVIDER.equals(provider) ||
891 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
892 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700893 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700894 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
895 LocationManager.FUSED_PROVIDER.equals(provider)) {
896 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700897 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700898 } else {
899 // mock providers
900 LocationProviderInterface lp = mMockProviders.get(provider);
901 if (lp != null) {
902 ProviderProperties properties = lp.getProperties();
903 if (properties != null) {
904 if (properties.mRequiresSatellite) {
905 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700906 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700907 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
908 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700909 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700910 }
911 }
912 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700913 }
Victoria Lease37425c32012-10-16 16:08:48 -0700914 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700915 }
916
Victoria Lease37425c32012-10-16 16:08:48 -0700917 /**
918 * Throw SecurityException if specified resolution level is insufficient to use the named
919 * location provider.
920 *
921 * @param allowedResolutionLevel resolution level allowed to caller
922 * @param providerName the name of the location provider
923 */
924 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
925 String providerName) {
926 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
927 if (allowedResolutionLevel < requiredResolutionLevel) {
928 switch (requiredResolutionLevel) {
929 case RESOLUTION_LEVEL_FINE:
930 throw new SecurityException("\"" + providerName + "\" location provider " +
931 "requires ACCESS_FINE_LOCATION permission.");
932 case RESOLUTION_LEVEL_COARSE:
933 throw new SecurityException("\"" + providerName + "\" location provider " +
934 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
935 default:
936 throw new SecurityException("Insufficient permission for \"" + providerName +
937 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700938 }
939 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700940 }
941
David Christie82edc9b2013-07-19 11:31:42 -0700942 /**
943 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
944 * for battery).
945 */
David Christie40e57822013-07-30 11:36:48 -0700946 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -0700947 mContext.enforceCallingOrSelfPermission(
948 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
949 }
950
David Christie40e57822013-07-30 11:36:48 -0700951 private void checkUpdateAppOpsAllowed() {
952 mContext.enforceCallingOrSelfPermission(
953 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
954 }
955
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800956 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800957 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
958 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800959 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800960 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800961 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800962 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800963 }
964 return -1;
965 }
966
967 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
968 int op = resolutionLevelToOp(allowedResolutionLevel);
969 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800970 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
971 return false;
972 }
973 }
974 return true;
975 }
976
977 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800978 int op = resolutionLevelToOp(allowedResolutionLevel);
979 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800980 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
981 return false;
982 }
983 }
984 return true;
985 }
986
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700987 /**
988 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700989 * fused, also including ones that are not permitted to
990 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700991 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700992 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700994 ArrayList<String> out;
995 synchronized (mLock) {
996 out = new ArrayList<String>(mProviders.size());
997 for (LocationProviderInterface provider : mProviders) {
998 String name = provider.getName();
999 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001000 continue;
1001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 out.add(name);
1003 }
1004 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001005
1006 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 return out;
1008 }
1009
Mike Lockwood03ca2162010-04-01 08:10:09 -07001010 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001011 * Return all providers by name, that match criteria and are optionally
1012 * enabled.
1013 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001014 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001015 @Override
1016 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001017 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001018 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001019 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001020 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001021 try {
1022 synchronized (mLock) {
1023 out = new ArrayList<String>(mProviders.size());
1024 for (LocationProviderInterface provider : mProviders) {
1025 String name = provider.getName();
1026 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001027 continue;
1028 }
Victoria Lease37425c32012-10-16 16:08:48 -07001029 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001030 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001031 continue;
1032 }
1033 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1034 name, provider.getProperties(), criteria)) {
1035 continue;
1036 }
1037 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001038 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001039 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001040 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001041 } finally {
1042 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001043 }
1044
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001045 if (D) Log.d(TAG, "getProviders()=" + out);
1046 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001047 }
1048
1049 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001050 * Return the name of the best provider given a Criteria object.
1051 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001052 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001053 * has been deprecated as well. So this method now uses
1054 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001055 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001056 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001057 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001058 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001059
1060 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001061 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001062 result = pickBest(providers);
1063 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1064 return result;
1065 }
1066 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001067 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001068 result = pickBest(providers);
1069 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1070 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001071 }
1072
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001073 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001074 return null;
1075 }
1076
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001077 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001078 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001079 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001080 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1081 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001082 } else {
1083 return providers.get(0);
1084 }
1085 }
1086
Nick Pellye0fd6932012-07-11 10:26:13 -07001087 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001088 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1089 LocationProviderInterface p = mProvidersByName.get(provider);
1090 if (p == null) {
1091 throw new IllegalArgumentException("provider=" + provider);
1092 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001093
1094 boolean result = LocationProvider.propertiesMeetCriteria(
1095 p.getName(), p.getProperties(), criteria);
1096 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1097 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001098 }
1099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001101 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001102 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001103 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 boolean isEnabled = p.isEnabled();
1105 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001106 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001108 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001109 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001111 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001112 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001114 }
1115 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001116 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1117 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119 }
1120
Victoria Leaseb711d572012-10-02 13:14:11 -07001121 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 int listeners = 0;
1123
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001124 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001125 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126
1127 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1130 if (records != null) {
1131 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001132 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001134 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001135 // Sends a notification message to the receiver
1136 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1137 if (deadReceivers == null) {
1138 deadReceivers = new ArrayList<Receiver>();
1139 }
1140 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001142 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 }
1146
1147 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001148 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 removeUpdatesLocked(deadReceivers.get(i));
1150 }
1151 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 if (enabled) {
1154 p.enable();
1155 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001156 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001163 private void applyRequirementsLocked(String provider) {
1164 LocationProviderInterface p = mProvidersByName.get(provider);
1165 if (p == null) return;
1166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001168 WorkSource worksource = new WorkSource();
1169 ProviderRequest providerRequest = new ProviderRequest();
1170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001172 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001173 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001174 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1175 record.mReceiver.mAllowedResolutionLevel)) {
1176 LocationRequest locationRequest = record.mRequest;
1177 providerRequest.locationRequests.add(locationRequest);
1178 if (locationRequest.getInterval() < providerRequest.interval) {
1179 providerRequest.reportLocation = true;
1180 providerRequest.interval = locationRequest.getInterval();
1181 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001182 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001183 }
1184 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001185
1186 if (providerRequest.reportLocation) {
1187 // calculate who to blame for power
1188 // This is somewhat arbitrary. We pick a threshold interval
1189 // that is slightly higher that the minimum interval, and
1190 // spread the blame across all applications with a request
1191 // under that threshold.
1192 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1193 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001194 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001195 LocationRequest locationRequest = record.mRequest;
1196 if (locationRequest.getInterval() <= thresholdInterval) {
David Christie82edc9b2013-07-19 11:31:42 -07001197 if (record.mReceiver.mWorkSource != null) {
1198 // Assign blame to another work source.
1199 worksource.add(record.mReceiver.mWorkSource);
1200 } else {
1201 // Assign blame to caller.
1202 worksource.add(
1203 record.mReceiver.mUid,
1204 record.mReceiver.mPackageName);
1205 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001206 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001207 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001211
1212 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1213 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215
1216 private class UpdateRecord {
1217 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001218 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001220 Location mLastFixBroadcast;
1221 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222
1223 /**
1224 * Note: must be constructed with lock held.
1225 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001226 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001228 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230
1231 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1232 if (records == null) {
1233 records = new ArrayList<UpdateRecord>();
1234 mRecordsByProvider.put(provider, records);
1235 }
1236 if (!records.contains(this)) {
1237 records.add(this);
1238 }
1239 }
1240
1241 /**
1242 * Method to be called when a record will no longer be used. Calling this multiple times
1243 * must have the same effect as calling it once.
1244 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001245 void disposeLocked(boolean removeReceiver) {
1246 // remove from mRecordsByProvider
1247 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1248 if (globalRecords != null) {
1249 globalRecords.remove(this);
1250 }
1251
1252 if (!removeReceiver) return; // the caller will handle the rest
1253
1254 // remove from Receiver#mUpdateRecords
1255 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1256 if (receiverRecords != null) {
1257 receiverRecords.remove(this.mProvider);
1258
1259 // and also remove the Receiver if it has no more update records
1260 if (removeReceiver && receiverRecords.size() == 0) {
1261 removeUpdatesLocked(mReceiver);
1262 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265
1266 @Override
1267 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001268 StringBuilder s = new StringBuilder();
1269 s.append("UpdateRecord[");
1270 s.append(mProvider);
1271 s.append(' ').append(mReceiver.mPackageName).append('(');
1272 s.append(mReceiver.mUid).append(')');
1273 s.append(' ').append(mRequest);
1274 s.append(']');
1275 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001279 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001280 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001281 IBinder binder = listener.asBinder();
1282 Receiver receiver = mReceivers.get(binder);
1283 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001284 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1285 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001286 mReceivers.put(binder, receiver);
1287
1288 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001289 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001290 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001291 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001292 return null;
1293 }
1294 }
1295 return receiver;
1296 }
1297
David Christie82edc9b2013-07-19 11:31:42 -07001298 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001299 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001300 Receiver receiver = mReceivers.get(intent);
1301 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001302 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1303 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001304 mReceivers.put(intent, receiver);
1305 }
1306 return receiver;
1307 }
1308
Victoria Lease37425c32012-10-16 16:08:48 -07001309 /**
1310 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1311 * and consistency requirements.
1312 *
1313 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001314 * @return a version of request that meets the given resolution and consistency requirements
1315 * @hide
1316 */
1317 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1318 LocationRequest sanitizedRequest = new LocationRequest(request);
1319 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1320 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001321 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001322 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001323 break;
1324 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001325 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001326 break;
1327 }
1328 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001329 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1330 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001331 }
Victoria Lease37425c32012-10-16 16:08:48 -07001332 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1333 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001334 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001335 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001336 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001337 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001338 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001339 }
Victoria Lease37425c32012-10-16 16:08:48 -07001340 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001341 }
1342
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001343 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001344 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001345 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001346 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001347 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001348 String[] packages = mPackageManager.getPackagesForUid(uid);
1349 if (packages == null) {
1350 throw new SecurityException("invalid UID " + uid);
1351 }
1352 for (String pkg : packages) {
1353 if (packageName.equals(pkg)) return;
1354 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001355 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001356 }
1357
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001358 private void checkPendingIntent(PendingIntent intent) {
1359 if (intent == null) {
1360 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001361 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001362 }
1363
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001364 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001365 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001366 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001367 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001368 } else if (intent != null && listener != null) {
1369 throw new IllegalArgumentException("cannot register both listener and intent");
1370 } else if (intent != null) {
1371 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001372 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001373 } else {
David Christie40e57822013-07-30 11:36:48 -07001374 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001375 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001376 }
1377
Nick Pellye0fd6932012-07-11 10:26:13 -07001378 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001379 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1380 PendingIntent intent, String packageName) {
1381 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1382 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001383 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1384 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1385 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001386 WorkSource workSource = request.getWorkSource();
1387 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001388 checkDeviceStatsAllowed();
1389 }
1390 boolean hideFromAppOps = request.getHideFromAppOps();
1391 if (hideFromAppOps) {
1392 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001393 }
Victoria Lease37425c32012-10-16 16:08:48 -07001394 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001396 final int pid = Binder.getCallingPid();
1397 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001398 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 long identity = Binder.clearCallingIdentity();
1400 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001401 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1402 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001403 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001404
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001405 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001406 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001407 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001408 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 } finally {
1411 Binder.restoreCallingIdentity(identity);
1412 }
1413 }
1414
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001415 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1416 int pid, int uid, String packageName) {
1417 // Figure out the provider. Either its explicitly request (legacy use cases), or
1418 // use the fused provider
1419 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1420 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001421 if (name == null) {
1422 throw new IllegalArgumentException("provider name must not be null");
1423 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001424
1425 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1426 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001427 LocationProviderInterface provider = mProvidersByName.get(name);
1428 if (provider == null) {
1429 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1430 }
1431
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001432 UpdateRecord record = new UpdateRecord(name, request, receiver);
1433 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1434 if (oldRecord != null) {
1435 oldRecord.disposeLocked(false);
1436 }
1437
Victoria Lease09eeaec2013-02-05 11:34:13 -08001438 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001439 if (isProviderEnabled) {
1440 applyRequirementsLocked(name);
1441 } else {
1442 // Notify the listener that updates are currently disabled
1443 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
David Christie0b837452013-07-29 16:02:13 -07001445 // Update the monitoring here just in case multiple location requests were added to the
1446 // same receiver (this request may be high power and the initial might not have been).
1447 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 }
1449
Nick Pellye0fd6932012-07-11 10:26:13 -07001450 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001451 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1452 String packageName) {
1453 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001454
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001455 final int pid = Binder.getCallingPid();
1456 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001457
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001458 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001459 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001460 boolean hideFromAppOps = false;
1461 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1462 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001463
1464 // providers may use public location API's, need to clear identity
1465 long identity = Binder.clearCallingIdentity();
1466 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001467 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001468 } finally {
1469 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
1472 }
1473
1474 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001475 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001476
1477 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1478 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1479 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001480 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
1483
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001484 receiver.updateMonitoring(false);
1485
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001486 // Record which providers were associated with this listener
1487 HashSet<String> providers = new HashSet<String>();
1488 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1489 if (oldRecords != null) {
1490 // Call dispose() on the obsolete update records.
1491 for (UpdateRecord record : oldRecords.values()) {
1492 record.disposeLocked(false);
1493 }
1494 // Accumulate providers
1495 providers.addAll(oldRecords.keySet());
1496 }
1497
1498 // update provider
1499 for (String provider : providers) {
1500 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001501 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001502 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
1504
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001505 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
1507 }
1508
Dianne Hackbornc2293022013-02-06 23:14:49 -08001509 private void applyAllProviderRequirementsLocked() {
1510 for (LocationProviderInterface p : mProviders) {
1511 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001512 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001513 continue;
1514 }
1515
1516 applyRequirementsLocked(p.getName());
1517 }
1518 }
1519
Nick Pellye0fd6932012-07-11 10:26:13 -07001520 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001521 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001522 if (D) Log.d(TAG, "getLastLocation: " + request);
1523 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001524 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001525 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001526 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1527 request.getProvider());
1528 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001529
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001530 final int uid = Binder.getCallingUid();
1531 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001532 try {
1533 if (mBlacklist.isBlacklisted(packageName)) {
1534 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1535 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001536 return null;
1537 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001538
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001539 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1540 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1541 packageName);
1542 return null;
1543 }
1544
Victoria Leaseb711d572012-10-02 13:14:11 -07001545 synchronized (mLock) {
1546 // Figure out the provider. Either its explicitly request (deprecated API's),
1547 // or use the fused provider
1548 String name = request.getProvider();
1549 if (name == null) name = LocationManager.FUSED_PROVIDER;
1550 LocationProviderInterface provider = mProvidersByName.get(name);
1551 if (provider == null) return null;
1552
Victoria Lease09eeaec2013-02-05 11:34:13 -08001553 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001554
David Christie1b9b7b12013-04-15 15:31:11 -07001555 Location location;
1556 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1557 // Make sure that an app with coarse permissions can't get frequent location
1558 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1559 location = mLastLocationCoarseInterval.get(name);
1560 } else {
1561 location = mLastLocation.get(name);
1562 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001563 if (location == null) {
1564 return null;
1565 }
Victoria Lease37425c32012-10-16 16:08:48 -07001566 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001567 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1568 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001569 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001570 }
Victoria Lease37425c32012-10-16 16:08:48 -07001571 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001572 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001573 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001574 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001575 return null;
1576 } finally {
1577 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001578 }
1579 }
1580
1581 @Override
1582 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1583 String packageName) {
1584 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001585 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1586 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001587 checkPendingIntent(intent);
1588 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001589 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1590 request.getProvider());
1591 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001592
Victoria Lease37425c32012-10-16 16:08:48 -07001593 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001594
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001595 // geo-fence manager uses the public location API, need to clear identity
1596 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001597 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1598 // temporary measure until geofences work for secondary users
1599 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1600 return;
1601 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001602 long identity = Binder.clearCallingIdentity();
1603 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001604 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1605 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001606 } finally {
1607 Binder.restoreCallingIdentity(identity);
1608 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001609 }
1610
1611 @Override
1612 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001613 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001614 checkPendingIntent(intent);
1615 checkPackageName(packageName);
1616
1617 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1618
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001619 // geo-fence manager uses the public location API, need to clear identity
1620 long identity = Binder.clearCallingIdentity();
1621 try {
1622 mGeofenceManager.removeFence(geofence, intent);
1623 } finally {
1624 Binder.restoreCallingIdentity(identity);
1625 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001626 }
1627
1628
1629 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001630 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001631 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 return false;
1633 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001634 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1635 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001636 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001638 final int uid = Binder.getCallingUid();
1639 final long ident = Binder.clearCallingIdentity();
1640 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001641 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001642 return false;
1643 }
1644 } finally {
1645 Binder.restoreCallingIdentity(ident);
1646 }
1647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001649 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001651 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 return false;
1653 }
1654 return true;
1655 }
1656
Nick Pellye0fd6932012-07-11 10:26:13 -07001657 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001659 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001660 try {
1661 mGpsStatusProvider.removeGpsStatusListener(listener);
1662 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666 }
1667
Nick Pellye0fd6932012-07-11 10:26:13 -07001668 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001670 if (provider == null) {
1671 // throw NullPointerException to remain compatible with previous implementation
1672 throw new NullPointerException();
1673 }
Victoria Lease37425c32012-10-16 16:08:48 -07001674 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1675 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001678 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 != PackageManager.PERMISSION_GRANTED)) {
1680 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1681 }
1682
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001683 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001684 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001685 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001686
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001687 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689 }
1690
Nick Pellye0fd6932012-07-11 10:26:13 -07001691 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001692 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001693 if (Binder.getCallingUid() != Process.myUid()) {
1694 throw new SecurityException(
1695 "calling sendNiResponse from outside of the system is not allowed");
1696 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001697 try {
1698 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001699 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001701 return false;
1702 }
1703 }
1704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001706 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001707 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 * accessed by the caller
1709 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001710 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001711 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001712 if (mProvidersByName.get(provider) == null) {
1713 return null;
1714 }
1715
Victoria Lease37425c32012-10-16 16:08:48 -07001716 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1717 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001719 LocationProviderInterface p;
1720 synchronized (mLock) {
1721 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
1723
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001724 if (p == null) return null;
1725 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727
Nick Pellye0fd6932012-07-11 10:26:13 -07001728 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001730 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1731 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001732 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1733
Victoria Lease09eeaec2013-02-05 11:34:13 -08001734 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001735 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001736 try {
1737 synchronized (mLock) {
1738 LocationProviderInterface p = mProvidersByName.get(provider);
1739 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001740
Victoria Lease09eeaec2013-02-05 11:34:13 -08001741 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001742 }
1743 } finally {
1744 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001745 }
1746 }
1747
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001748 /**
1749 * Returns "true" if the UID belongs to a bound location provider.
1750 *
1751 * @param uid the uid
1752 * @return true if uid belongs to a bound location provider
1753 */
1754 private boolean isUidALocationProvider(int uid) {
1755 if (uid == Process.SYSTEM_UID) {
1756 return true;
1757 }
1758 if (mGeocodeProvider != null) {
1759 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1760 }
1761 for (LocationProviderProxy proxy : mProxyProviders) {
1762 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1763 }
1764 return false;
1765 }
1766
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001767 private void checkCallerIsProvider() {
1768 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1769 == PackageManager.PERMISSION_GRANTED) {
1770 return;
1771 }
1772
1773 // Previously we only used the INSTALL_LOCATION_PROVIDER
1774 // check. But that is system or signature
1775 // protection level which is not flexible enough for
1776 // providers installed oustide the system image. So
1777 // also allow providers with a UID matching the
1778 // currently bound package name
1779
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001780 if (isUidALocationProvider(Binder.getCallingUid())) {
1781 return;
1782 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001783
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001784 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1785 "or UID of a currently bound location provider");
1786 }
1787
1788 private boolean doesPackageHaveUid(int uid, String packageName) {
1789 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 return false;
1791 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001792 try {
1793 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1794 if (appInfo.uid != uid) {
1795 return false;
1796 }
1797 } catch (NameNotFoundException e) {
1798 return false;
1799 }
1800 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802
Nick Pellye0fd6932012-07-11 10:26:13 -07001803 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001804 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001805 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001806
Nick Pelly2eeeec22012-07-18 13:13:37 -07001807 if (!location.isComplete()) {
1808 Log.w(TAG, "Dropping incomplete location: " + location);
1809 return;
1810 }
1811
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001812 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1813 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001814 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001815 mLocationHandler.sendMessageAtFrontOfQueue(m);
1816 }
1817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818
Laurent Tu75defb62012-11-01 16:21:52 -07001819 private static boolean shouldBroadcastSafe(
1820 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 // Always broadcast the first update
1822 if (lastLoc == null) {
1823 return true;
1824 }
1825
Nick Pellyf1be6862012-05-15 10:53:42 -07001826 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001827 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001828 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1829 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001830 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 return false;
1832 }
1833
1834 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001835 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 if (minDistance > 0.0) {
1837 if (loc.distanceTo(lastLoc) <= minDistance) {
1838 return false;
1839 }
1840 }
1841
Laurent Tu75defb62012-11-01 16:21:52 -07001842 // Check whether sufficient number of udpates is left
1843 if (record.mRequest.getNumUpdates() <= 0) {
1844 return false;
1845 }
1846
1847 // Check whether the expiry date has passed
1848 if (record.mRequest.getExpireAt() < now) {
1849 return false;
1850 }
1851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 return true;
1853 }
1854
Mike Lockwooda4903f22010-02-17 06:42:23 -05001855 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001856 if (D) Log.d(TAG, "incoming location: " + location);
1857
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001858 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001859 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860
Laurent Tu60ec50a2012-10-04 17:00:10 -07001861 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001862 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001863 if (p == null) return;
1864
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001865 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001866 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1867 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001868 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001869 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001870 lastLocation = new Location(provider);
1871 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001872 } else {
1873 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1874 if (noGPSLocation == null && lastNoGPSLocation != null) {
1875 // New location has no no-GPS location: adopt last no-GPS location. This is set
1876 // directly into location because we do not want to notify COARSE clients.
1877 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1878 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001879 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001880 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881
David Christie1b9b7b12013-04-15 15:31:11 -07001882 // Update last known coarse interval location if enough time has passed.
1883 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1884 if (lastLocationCoarseInterval == null) {
1885 lastLocationCoarseInterval = new Location(location);
1886 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1887 }
1888 long timeDiffNanos = location.getElapsedRealtimeNanos()
1889 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1890 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1891 lastLocationCoarseInterval.set(location);
1892 }
1893 // Don't ever return a coarse location that is more recent than the allowed update
1894 // interval (i.e. don't allow an app to keep registering and unregistering for
1895 // location updates to overcome the minimum interval).
1896 noGPSLocation =
1897 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1898
Laurent Tu60ec50a2012-10-04 17:00:10 -07001899 // Skip if there are no UpdateRecords for this provider.
1900 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1901 if (records == null || records.size() == 0) return;
1902
Victoria Lease09016ab2012-09-16 12:33:15 -07001903 // Fetch coarse location
1904 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001905 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001906 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1907 }
1908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 // Fetch latest status update time
1910 long newStatusUpdateTime = p.getStatusUpdateTime();
1911
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001912 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 Bundle extras = new Bundle();
1914 int status = p.getStatus(extras);
1915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001917 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001920 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001922 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001923
Victoria Lease269518e2012-10-29 08:25:39 -07001924 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001925 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001926 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001927 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001928 " (current user: " + mCurrentUserId + ", app: " +
1929 receiver.mPackageName + ")");
1930 }
1931 continue;
1932 }
1933
Nick Pelly4035f5a2012-08-17 14:43:49 -07001934 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1935 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1936 receiver.mPackageName);
1937 continue;
1938 }
1939
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001940 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1941 receiver.mAllowedResolutionLevel)) {
1942 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1943 receiver.mPackageName);
1944 continue;
1945 }
1946
Victoria Lease09016ab2012-09-16 12:33:15 -07001947 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001948 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1949 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001950 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001951 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001952 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001953 if (notifyLocation != null) {
1954 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001955 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001956 if (lastLoc == null) {
1957 lastLoc = new Location(notifyLocation);
1958 r.mLastFixBroadcast = lastLoc;
1959 } else {
1960 lastLoc.set(notifyLocation);
1961 }
1962 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1963 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1964 receiverDead = true;
1965 }
Laurent Tu75defb62012-11-01 16:21:52 -07001966 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
1968 }
1969
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001970 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001972 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001974 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001976 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001977 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001978 }
1979 }
1980
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001981 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001982 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001983 if (deadUpdateRecords == null) {
1984 deadUpdateRecords = new ArrayList<UpdateRecord>();
1985 }
1986 deadUpdateRecords.add(r);
1987 }
1988 // track dead receivers
1989 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001990 if (deadReceivers == null) {
1991 deadReceivers = new ArrayList<Receiver>();
1992 }
1993 if (!deadReceivers.contains(receiver)) {
1994 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996 }
1997 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001998
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001999 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002001 for (Receiver receiver : deadReceivers) {
2002 removeUpdatesLocked(receiver);
2003 }
2004 }
2005 if (deadUpdateRecords != null) {
2006 for (UpdateRecord r : deadUpdateRecords) {
2007 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002009 applyRequirementsLocked(provider);
2010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
2012
2013 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002014 public LocationWorkerHandler(Looper looper) {
2015 super(looper, null, true);
2016 }
2017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 @Override
2019 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002020 switch (msg.what) {
2021 case MSG_LOCATION_CHANGED:
2022 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2023 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025 }
2026 }
2027
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002028 private boolean isMockProvider(String provider) {
2029 synchronized (mLock) {
2030 return mMockProviders.containsKey(provider);
2031 }
2032 }
2033
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002034 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002035 // create a working copy of the incoming Location so that the service can modify it without
2036 // disturbing the caller's copy
2037 Location myLocation = new Location(location);
2038 String provider = myLocation.getProvider();
2039
2040 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2041 // bit if location did not come from a mock provider because passive/fused providers can
2042 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2043 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2044 myLocation.setIsFromMockProvider(true);
2045 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002046
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002047 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002048 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2049 if (!passive) {
2050 // notify passive provider of the new location
2051 mPassiveProvider.updateLocation(myLocation);
2052 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002053 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
Mike Lockwoode97ae402010-09-29 15:23:46 -04002058 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2059 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002060 public void onPackageDisappeared(String packageName, int reason) {
2061 // remove all receivers associated with this package name
2062 synchronized (mLock) {
2063 ArrayList<Receiver> deadReceivers = null;
2064
2065 for (Receiver receiver : mReceivers.values()) {
2066 if (receiver.mPackageName.equals(packageName)) {
2067 if (deadReceivers == null) {
2068 deadReceivers = new ArrayList<Receiver>();
2069 }
2070 deadReceivers.add(receiver);
2071 }
2072 }
2073
2074 // perform removal outside of mReceivers loop
2075 if (deadReceivers != null) {
2076 for (Receiver receiver : deadReceivers) {
2077 removeUpdatesLocked(receiver);
2078 }
2079 }
2080 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002081 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002082 };
2083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 // Geocoder
2085
Nick Pellye0fd6932012-07-11 10:26:13 -07002086 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002087 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002088 return mGeocodeProvider != null;
2089 }
2090
Nick Pellye0fd6932012-07-11 10:26:13 -07002091 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002093 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002094 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002095 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2096 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002098 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100
Mike Lockwooda55c3212009-04-15 11:10:11 -04002101
Nick Pellye0fd6932012-07-11 10:26:13 -07002102 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002104 double lowerLeftLatitude, double lowerLeftLongitude,
2105 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002106 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002107
2108 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002109 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2110 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2111 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002113 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115
2116 // Mock Providers
2117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 private void checkMockPermissionsSafe() {
2119 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2120 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2121 if (!allowMocks) {
2122 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2123 }
2124
2125 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2126 PackageManager.PERMISSION_GRANTED) {
2127 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
2130
Nick Pellye0fd6932012-07-11 10:26:13 -07002131 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002132 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 checkMockPermissionsSafe();
2134
Mike Lockwooda4903f22010-02-17 06:42:23 -05002135 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2136 throw new IllegalArgumentException("Cannot mock the passive location provider");
2137 }
2138
Mike Lockwood86328a92009-10-23 08:38:25 -04002139 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002140 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002141 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002142 // remove the real provider if we are replacing GPS or network provider
2143 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002144 || LocationManager.NETWORK_PROVIDER.equals(name)
2145 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002146 LocationProviderInterface p = mProvidersByName.get(name);
2147 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002148 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002149 }
2150 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002151 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2153 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002154 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002155 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002156 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002157 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 updateProvidersLocked();
2159 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002160 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162
Nick Pellye0fd6932012-07-11 10:26:13 -07002163 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 public void removeTestProvider(String provider) {
2165 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002166 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002167 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002168 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2170 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002171 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002172 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002173
2174 // reinstate real provider if available
2175 LocationProviderInterface realProvider = mRealProviders.get(provider);
2176 if (realProvider != null) {
2177 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002178 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002179 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002180 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002182 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 }
2185
Nick Pellye0fd6932012-07-11 10:26:13 -07002186 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 public void setTestProviderLocation(String provider, Location loc) {
2188 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002189 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002190 MockProvider mockProvider = mMockProviders.get(provider);
2191 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2193 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002194 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2195 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002196 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002197 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
2199 }
2200
Nick Pellye0fd6932012-07-11 10:26:13 -07002201 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 public void clearTestProviderLocation(String provider) {
2203 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002204 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002205 MockProvider mockProvider = mMockProviders.get(provider);
2206 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2208 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002209 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
2211 }
2212
Nick Pellye0fd6932012-07-11 10:26:13 -07002213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 public void setTestProviderEnabled(String provider, boolean enabled) {
2215 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002216 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002217 MockProvider mockProvider = mMockProviders.get(provider);
2218 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2220 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002221 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002223 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 mEnabledProviders.add(provider);
2225 mDisabledProviders.remove(provider);
2226 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002227 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 mEnabledProviders.remove(provider);
2229 mDisabledProviders.add(provider);
2230 }
2231 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002232 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234 }
2235
Nick Pellye0fd6932012-07-11 10:26:13 -07002236 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 public void clearTestProviderEnabled(String provider) {
2238 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002239 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002240 MockProvider mockProvider = mMockProviders.get(provider);
2241 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2243 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002244 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 mEnabledProviders.remove(provider);
2246 mDisabledProviders.remove(provider);
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 setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2254 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002255 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002256 MockProvider mockProvider = mMockProviders.get(provider);
2257 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2259 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002260 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 }
2262 }
2263
Nick Pellye0fd6932012-07-11 10:26:13 -07002264 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 public void clearTestProviderStatus(String provider) {
2266 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002267 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002268 MockProvider mockProvider = mMockProviders.get(provider);
2269 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2271 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002272 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 }
2274 }
2275
2276 private void log(String log) {
2277 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002278 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
2280 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002281
2282 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2284 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2285 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002286 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 + Binder.getCallingPid()
2288 + ", uid=" + Binder.getCallingUid());
2289 return;
2290 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002291
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002292 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002295 for (Receiver receiver : mReceivers.values()) {
2296 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002299 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2300 pw.println(" " + entry.getKey() + ":");
2301 for (UpdateRecord record : entry.getValue()) {
2302 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002306 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2307 String provider = entry.getKey();
2308 Location location = entry.getValue();
2309 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002311
David Christie1b9b7b12013-04-15 15:31:11 -07002312 pw.println(" Last Known Locations Coarse Intervals:");
2313 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2314 String provider = entry.getKey();
2315 Location location = entry.getValue();
2316 pw.println(" " + provider + ": " + location);
2317 }
2318
Nick Pellye0fd6932012-07-11 10:26:13 -07002319 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 if (mEnabledProviders.size() > 0) {
2322 pw.println(" Enabled Providers:");
2323 for (String i : mEnabledProviders) {
2324 pw.println(" " + i);
2325 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 }
2328 if (mDisabledProviders.size() > 0) {
2329 pw.println(" Disabled Providers:");
2330 for (String i : mDisabledProviders) {
2331 pw.println(" " + i);
2332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002334 pw.append(" ");
2335 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 if (mMockProviders.size() > 0) {
2337 pw.println(" Mock Providers:");
2338 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002339 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
2341 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002342
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002343 pw.append(" fudger: ");
2344 mLocationFudger.dump(fd, pw, args);
2345
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002346 if (args.length > 0 && "short".equals(args[0])) {
2347 return;
2348 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002349 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002350 pw.print(provider.getName() + " Internal State");
2351 if (provider instanceof LocationProviderProxy) {
2352 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2353 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002354 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002355 pw.println(":");
2356 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359 }
2360}