blob: b162c6bb63546cadf9435fdc000896c8361fea55 [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;
Victoria Lease5cd731a2012-12-19 15:04:21 -080050import android.os.HandlerThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070052import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Message;
54import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070055import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070057import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070058import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070059import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080062import android.util.Slog;
Mike Lockwoode97ae402010-09-29 15:23:46 -040063import com.android.internal.content.PackageMonitor;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070064import com.android.internal.location.ProviderProperties;
65import com.android.internal.location.ProviderRequest;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070066import com.android.internal.os.BackgroundThread;
Mike Lockwood43e33f22010-03-26 10:41:48 -040067import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070068import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070069import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040070import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070071import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070072import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040073import com.android.server.location.LocationProviderInterface;
74import com.android.server.location.LocationProviderProxy;
75import com.android.server.location.MockProvider;
76import com.android.server.location.PassiveProvider;
77
78import java.io.FileDescriptor;
79import java.io.PrintWriter;
80import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070081import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040082import java.util.HashMap;
83import java.util.HashSet;
84import java.util.List;
85import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040086import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * The service class that manages LocationProviders and issues location
90 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080092public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080094 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070095
96 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Victoria Lease37425c32012-10-16 16:08:48 -070098 // Location resolution level: no location data whatsoever
99 private static final int RESOLUTION_LEVEL_NONE = 0;
100 // Location resolution level: coarse location data only
101 private static final int RESOLUTION_LEVEL_COARSE = 1;
102 // Location resolution level: fine location data
103 private static final int RESOLUTION_LEVEL_FINE = 2;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700106 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700108 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400109 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700110 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
111
112 private static final String NETWORK_LOCATION_SERVICE_ACTION =
113 "com.android.location.service.v2.NetworkLocationProvider";
114 private static final String FUSED_LOCATION_SERVICE_ACTION =
115 "com.android.location.service.FusedLocationProvider";
116
117 private static final int MSG_LOCATION_CHANGED = 1;
118
David Christie1b9b7b12013-04-15 15:31:11 -0700119 private static final long NANOS_PER_MILLI = 1000000L;
120
Nick Pellyf1be6862012-05-15 10:53:42 -0700121 // Location Providers may sometimes deliver location updates
122 // slightly faster that requested - provide grace period so
123 // we don't unnecessarily filter events that are otherwise on
124 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700125 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700126
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700127 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
128
129 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800130 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700131
132 // used internally for synchronization
133 private final Object mLock = new Object();
134
Victoria Lease5cd731a2012-12-19 15:04:21 -0800135 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700136 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700137 private GeofenceManager mGeofenceManager;
138 private PowerManager.WakeLock mWakeLock;
139 private PackageManager mPackageManager;
140 private GeocoderProxy mGeocodeProvider;
141 private IGpsStatusProvider mGpsStatusProvider;
142 private INetInitiatedListener mNetInitiatedListener;
143 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700144 private PassiveProvider mPassiveProvider; // track passive provider for special cases
145 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700146
147 // --- fields below are protected by mWakeLock ---
148 private int mPendingBroadcasts;
149
150 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 // Set of providers that are explicitly enabled
152 private final Set<String> mEnabledProviders = new HashSet<String>();
153
154 // Set of providers that are explicitly disabled
155 private final Set<String> mDisabledProviders = new HashSet<String>();
156
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700157 // Mock (test) providers
158 private final HashMap<String, MockProvider> mMockProviders =
159 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700161 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400162 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700164 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500165 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700166 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400167
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700168 // real providers, saved here when mocked out
169 private final HashMap<String, LocationProviderInterface> mRealProviders =
170 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700172 // mapping from provider name to provider
173 private final HashMap<String, LocationProviderInterface> mProvidersByName =
174 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700176 // mapping from provider name to all its UpdateRecords
177 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
178 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700179
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700180 // mapping from provider name to last known location
181 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
David Christie1b9b7b12013-04-15 15:31:11 -0700183 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
184 // locations stored here are not fudged for coarse permissions.
185 private final HashMap<String, Location> mLastLocationCoarseInterval =
186 new HashMap<String, Location>();
187
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700188 // all providers that operate over proxy, for authorizing incoming location
189 private final ArrayList<LocationProviderProxy> mProxyProviders =
190 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Victoria Lease38389b62012-09-30 11:44:22 -0700192 // current active user on the device - other users are denied location data
193 private int mCurrentUserId = UserHandle.USER_OWNER;
194
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700195 public LocationManagerService(Context context) {
196 super();
197 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800198 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800199
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700200 if (D) Log.d(TAG, "Constructed");
201
202 // most startup is deferred until systemReady()
203 }
204
205 public void systemReady() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700206 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800207 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700208
Victoria Lease5cd731a2012-12-19 15:04:21 -0800209 // fetch package manager
210 mPackageManager = mContext.getPackageManager();
211
212 // prepare wake lock
213 PowerManager powerManager =
214 (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
215 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
216
217 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700218 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800219
220 // prepare mLocationHandler's dependents
221 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
222 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
223 mBlacklist.init();
224 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
225
Dianne Hackbornc2293022013-02-06 23:14:49 -0800226 // Monitor for app ops mode changes.
227 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
228 public void opChanged(int op, String packageName) {
229 synchronized (mLock) {
230 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700465
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400466 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700467
Mike Lockwood48f17512009-04-23 09:12:08 -0700468 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700470 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
471 String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700474 if (listener != null) {
475 mKey = listener.asBinder();
476 } else {
477 mKey = intent;
478 }
Victoria Lease37425c32012-10-16 16:08:48 -0700479 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700480 mUid = uid;
481 mPid = pid;
482 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
484
485 @Override
486 public boolean equals(Object otherObj) {
487 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700488 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490 return false;
491 }
492
493 @Override
494 public int hashCode() {
495 return mKey.hashCode();
496 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 @Override
499 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700500 StringBuilder s = new StringBuilder();
501 s.append("Reciever[");
502 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700504 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700506 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700508 for (String p : mUpdateRecords.keySet()) {
509 s.append(" ").append(mUpdateRecords.get(p).toString());
510 }
511 s.append("]");
512 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 public boolean isListener() {
516 return mListener != null;
517 }
518
519 public boolean isPendingIntent() {
520 return mPendingIntent != null;
521 }
522
523 public ILocationListener getListener() {
524 if (mListener != null) {
525 return mListener;
526 }
527 throw new IllegalStateException("Request for non-existent listener");
528 }
529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
531 if (mListener != null) {
532 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700533 synchronized (this) {
534 // synchronize to ensure incrementPendingBroadcastsLocked()
535 // is called before decrementPendingBroadcasts()
536 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700537 // call this after broadcasting so we do not increment
538 // if we throw an exeption.
539 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 } catch (RemoteException e) {
542 return false;
543 }
544 } else {
545 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800546 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
548 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700549 synchronized (this) {
550 // synchronize to ensure incrementPendingBroadcastsLocked()
551 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700552 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700553 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700554 // call this after broadcasting so we do not increment
555 // if we throw an exeption.
556 incrementPendingBroadcastsLocked();
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 } catch (PendingIntent.CanceledException e) {
559 return false;
560 }
561 }
562 return true;
563 }
564
565 public boolean callLocationChangedLocked(Location location) {
566 if (mListener != null) {
567 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700568 synchronized (this) {
569 // synchronize to ensure incrementPendingBroadcastsLocked()
570 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800571 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700572 // call this after broadcasting so we do not increment
573 // if we throw an exeption.
574 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 } catch (RemoteException e) {
577 return false;
578 }
579 } else {
580 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800581 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700583 synchronized (this) {
584 // synchronize to ensure incrementPendingBroadcastsLocked()
585 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700586 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700587 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700588 // call this after broadcasting so we do not increment
589 // if we throw an exeption.
590 incrementPendingBroadcastsLocked();
591 }
592 } catch (PendingIntent.CanceledException e) {
593 return false;
594 }
595 }
596 return true;
597 }
598
599 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
600 if (mListener != null) {
601 try {
602 synchronized (this) {
603 // synchronize to ensure incrementPendingBroadcastsLocked()
604 // is called before decrementPendingBroadcasts()
605 if (enabled) {
606 mListener.onProviderEnabled(provider);
607 } else {
608 mListener.onProviderDisabled(provider);
609 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700610 // call this after broadcasting so we do not increment
611 // if we throw an exeption.
612 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700613 }
614 } catch (RemoteException e) {
615 return false;
616 }
617 } else {
618 Intent providerIntent = new Intent();
619 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
620 try {
621 synchronized (this) {
622 // synchronize to ensure incrementPendingBroadcastsLocked()
623 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700624 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700625 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700626 // call this after broadcasting so we do not increment
627 // if we throw an exeption.
628 incrementPendingBroadcastsLocked();
629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 } catch (PendingIntent.CanceledException e) {
631 return false;
632 }
633 }
634 return true;
635 }
636
Nick Pellyf1be6862012-05-15 10:53:42 -0700637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700639 if (D) Log.d(TAG, "Location listener died");
640
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400641 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 removeUpdatesLocked(this);
643 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700644 synchronized (this) {
645 if (mPendingBroadcasts > 0) {
646 LocationManagerService.this.decrementPendingBroadcasts();
647 mPendingBroadcasts = 0;
648 }
649 }
650 }
651
Nick Pellye0fd6932012-07-11 10:26:13 -0700652 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700653 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
654 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400655 synchronized (this) {
656 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700657 }
658 }
659
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400660 // this must be called while synchronized by caller in a synchronized block
661 // containing the sending of the broadcaset
662 private void incrementPendingBroadcastsLocked() {
663 if (mPendingBroadcasts++ == 0) {
664 LocationManagerService.this.incrementPendingBroadcasts();
665 }
666 }
667
668 private void decrementPendingBroadcastsLocked() {
669 if (--mPendingBroadcasts == 0) {
670 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700671 }
672 }
673 }
674
Nick Pellye0fd6932012-07-11 10:26:13 -0700675 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700676 public void locationCallbackFinished(ILocationListener listener) {
Joshua Bartel080b61b2009-10-05 12:44:46 -0400677 //Do not use getReceiver here as that will add the ILocationListener to
678 //the receiver list if it is not found. If it is not found then the
679 //LocationListener was removed when it had a pending broadcast and should
680 //not be added back.
681 IBinder binder = listener.asBinder();
682 Receiver receiver = mReceivers.get(binder);
Mike Lockwood48f17512009-04-23 09:12:08 -0700683 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400684 synchronized (receiver) {
685 // so wakelock calls will succeed
686 long identity = Binder.clearCallingIdentity();
687 receiver.decrementPendingBroadcastsLocked();
688 Binder.restoreCallingIdentity(identity);
689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691 }
692
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700693 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400694 mProviders.add(provider);
695 mProvidersByName.put(provider.getName(), provider);
696 }
697
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700698 private void removeProviderLocked(LocationProviderInterface provider) {
699 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400700 mProviders.remove(provider);
701 mProvidersByName.remove(provider.getName());
702 }
703
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800704 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800705 * Returns "true" if access to the specified location provider is allowed by the current
706 * user's settings. Access to all location providers is forbidden to non-location-provider
707 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800708 *
709 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800710 * @return
711 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800712 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 if (mEnabledProviders.contains(provider)) {
714 return true;
715 }
716 if (mDisabledProviders.contains(provider)) {
717 return false;
718 }
719 // Use system settings
720 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
Victoria Leaseb711d572012-10-02 13:14:11 -0700722 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700725 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800726 * Returns "true" if access to the specified location provider is allowed by the specified
727 * user's settings. Access to all location providers is forbidden to non-location-provider
728 * processes belonging to background users.
729 *
730 * @param provider the name of the location provider
731 * @param uid the requestor's UID
732 * @return
733 */
734 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
735 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
736 return false;
737 }
738 return isAllowedByCurrentUserSettingsLocked(provider);
739 }
740
741 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700742 * Returns the permission string associated with the specified resolution level.
743 *
744 * @param resolutionLevel the resolution level
745 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700746 */
Victoria Lease37425c32012-10-16 16:08:48 -0700747 private String getResolutionPermission(int resolutionLevel) {
748 switch (resolutionLevel) {
749 case RESOLUTION_LEVEL_FINE:
750 return android.Manifest.permission.ACCESS_FINE_LOCATION;
751 case RESOLUTION_LEVEL_COARSE:
752 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
753 default:
754 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700756 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700757
Victoria Leaseda479c52012-10-15 15:24:16 -0700758 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700759 * Returns the resolution level allowed to the given PID/UID pair.
760 *
761 * @param pid the PID
762 * @param uid the UID
763 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700764 */
Victoria Lease37425c32012-10-16 16:08:48 -0700765 private int getAllowedResolutionLevel(int pid, int uid) {
766 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
767 pid, uid) == PackageManager.PERMISSION_GRANTED) {
768 return RESOLUTION_LEVEL_FINE;
769 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
770 pid, uid) == PackageManager.PERMISSION_GRANTED) {
771 return RESOLUTION_LEVEL_COARSE;
772 } else {
773 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700774 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700775 }
776
777 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700778 * Returns the resolution level allowed to the caller
779 *
780 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700781 */
Victoria Lease37425c32012-10-16 16:08:48 -0700782 private int getCallerAllowedResolutionLevel() {
783 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
784 }
785
786 /**
787 * Throw SecurityException if specified resolution level is insufficient to use geofences.
788 *
789 * @param allowedResolutionLevel resolution level allowed to caller
790 */
791 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
792 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700793 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796
Victoria Lease37425c32012-10-16 16:08:48 -0700797 /**
798 * Return the minimum resolution level required to use the specified location provider.
799 *
800 * @param provider the name of the location provider
801 * @return minimum resolution level required for provider
802 */
803 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700804 if (LocationManager.GPS_PROVIDER.equals(provider) ||
805 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
806 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700807 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700808 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
809 LocationManager.FUSED_PROVIDER.equals(provider)) {
810 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700811 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700812 } else {
813 // mock providers
814 LocationProviderInterface lp = mMockProviders.get(provider);
815 if (lp != null) {
816 ProviderProperties properties = lp.getProperties();
817 if (properties != null) {
818 if (properties.mRequiresSatellite) {
819 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700820 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700821 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
822 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700823 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700824 }
825 }
826 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700827 }
Victoria Lease37425c32012-10-16 16:08:48 -0700828 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700829 }
830
Victoria Lease37425c32012-10-16 16:08:48 -0700831 /**
832 * Throw SecurityException if specified resolution level is insufficient to use the named
833 * location provider.
834 *
835 * @param allowedResolutionLevel resolution level allowed to caller
836 * @param providerName the name of the location provider
837 */
838 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
839 String providerName) {
840 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
841 if (allowedResolutionLevel < requiredResolutionLevel) {
842 switch (requiredResolutionLevel) {
843 case RESOLUTION_LEVEL_FINE:
844 throw new SecurityException("\"" + providerName + "\" location provider " +
845 "requires ACCESS_FINE_LOCATION permission.");
846 case RESOLUTION_LEVEL_COARSE:
847 throw new SecurityException("\"" + providerName + "\" location provider " +
848 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
849 default:
850 throw new SecurityException("Insufficient permission for \"" + providerName +
851 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700852 }
853 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700854 }
855
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800856 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800857 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
858 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800859 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800860 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800861 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800862 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800863 }
864 return -1;
865 }
866
867 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
868 int op = resolutionLevelToOp(allowedResolutionLevel);
869 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800870 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
871 return false;
872 }
873 }
874 return true;
875 }
876
877 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800878 int op = resolutionLevelToOp(allowedResolutionLevel);
879 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800880 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
881 return false;
882 }
883 }
884 return true;
885 }
886
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700887 /**
888 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700889 * fused, also including ones that are not permitted to
890 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700891 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700892 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700894 ArrayList<String> out;
895 synchronized (mLock) {
896 out = new ArrayList<String>(mProviders.size());
897 for (LocationProviderInterface provider : mProviders) {
898 String name = provider.getName();
899 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700900 continue;
901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 out.add(name);
903 }
904 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700905
906 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 return out;
908 }
909
Mike Lockwood03ca2162010-04-01 08:10:09 -0700910 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700911 * Return all providers by name, that match criteria and are optionally
912 * enabled.
913 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700914 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700915 @Override
916 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -0700917 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700918 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800919 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -0700920 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -0700921 try {
922 synchronized (mLock) {
923 out = new ArrayList<String>(mProviders.size());
924 for (LocationProviderInterface provider : mProviders) {
925 String name = provider.getName();
926 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700927 continue;
928 }
Victoria Lease37425c32012-10-16 16:08:48 -0700929 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -0800930 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700931 continue;
932 }
933 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
934 name, provider.getProperties(), criteria)) {
935 continue;
936 }
937 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700938 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700939 }
Mike Lockwood03ca2162010-04-01 08:10:09 -0700940 }
Victoria Leaseb711d572012-10-02 13:14:11 -0700941 } finally {
942 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700943 }
944
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700945 if (D) Log.d(TAG, "getProviders()=" + out);
946 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700947 }
948
949 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700950 * Return the name of the best provider given a Criteria object.
951 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -0700952 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700953 * has been deprecated as well. So this method now uses
954 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700955 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700956 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700957 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700958 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700959
960 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700961 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700962 result = pickBest(providers);
963 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
964 return result;
965 }
966 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700967 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700968 result = pickBest(providers);
969 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
970 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700971 }
972
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700973 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700974 return null;
975 }
976
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700977 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -0700978 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700979 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -0700980 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
981 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700982 } else {
983 return providers.get(0);
984 }
985 }
986
Nick Pellye0fd6932012-07-11 10:26:13 -0700987 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700988 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
989 LocationProviderInterface p = mProvidersByName.get(provider);
990 if (p == null) {
991 throw new IllegalArgumentException("provider=" + provider);
992 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700993
994 boolean result = LocationProvider.propertiesMeetCriteria(
995 p.getName(), p.getProperties(), criteria);
996 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
997 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700998 }
999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001001 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001002 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001003 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 boolean isEnabled = p.isEnabled();
1005 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001006 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001008 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001009 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001011 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001012 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001014 }
1015 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001016 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1017 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 }
1020
Victoria Leaseb711d572012-10-02 13:14:11 -07001021 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 int listeners = 0;
1023
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001024 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001025 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
1027 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1030 if (records != null) {
1031 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001032 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001034 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001035 // Sends a notification message to the receiver
1036 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1037 if (deadReceivers == null) {
1038 deadReceivers = new ArrayList<Receiver>();
1039 }
1040 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001042 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045 }
1046
1047 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001048 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 removeUpdatesLocked(deadReceivers.get(i));
1050 }
1051 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 if (enabled) {
1054 p.enable();
1055 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001056 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001063 private void applyRequirementsLocked(String provider) {
1064 LocationProviderInterface p = mProvidersByName.get(provider);
1065 if (p == null) return;
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001068 WorkSource worksource = new WorkSource();
1069 ProviderRequest providerRequest = new ProviderRequest();
1070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001072 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001073 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001074 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1075 record.mReceiver.mAllowedResolutionLevel)) {
1076 LocationRequest locationRequest = record.mRequest;
1077 providerRequest.locationRequests.add(locationRequest);
1078 if (locationRequest.getInterval() < providerRequest.interval) {
1079 providerRequest.reportLocation = true;
1080 providerRequest.interval = locationRequest.getInterval();
1081 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001082 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001083 }
1084 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001085
1086 if (providerRequest.reportLocation) {
1087 // calculate who to blame for power
1088 // This is somewhat arbitrary. We pick a threshold interval
1089 // that is slightly higher that the minimum interval, and
1090 // spread the blame across all applications with a request
1091 // under that threshold.
1092 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1093 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001094 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001095 LocationRequest locationRequest = record.mRequest;
1096 if (locationRequest.getInterval() <= thresholdInterval) {
Dianne Hackborn002a54e2013-01-10 17:34:55 -08001097 worksource.add(record.mReceiver.mUid, record.mReceiver.mPackageName);
Victoria Leaseb711d572012-10-02 13:14:11 -07001098 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001099 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001103
1104 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1105 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 }
1107
1108 private class UpdateRecord {
1109 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001110 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001112 Location mLastFixBroadcast;
1113 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114
1115 /**
1116 * Note: must be constructed with lock held.
1117 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001118 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001120 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122
1123 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1124 if (records == null) {
1125 records = new ArrayList<UpdateRecord>();
1126 mRecordsByProvider.put(provider, records);
1127 }
1128 if (!records.contains(this)) {
1129 records.add(this);
1130 }
1131 }
1132
1133 /**
1134 * Method to be called when a record will no longer be used. Calling this multiple times
1135 * must have the same effect as calling it once.
1136 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001137 void disposeLocked(boolean removeReceiver) {
1138 // remove from mRecordsByProvider
1139 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1140 if (globalRecords != null) {
1141 globalRecords.remove(this);
1142 }
1143
1144 if (!removeReceiver) return; // the caller will handle the rest
1145
1146 // remove from Receiver#mUpdateRecords
1147 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1148 if (receiverRecords != null) {
1149 receiverRecords.remove(this.mProvider);
1150
1151 // and also remove the Receiver if it has no more update records
1152 if (removeReceiver && receiverRecords.size() == 0) {
1153 removeUpdatesLocked(mReceiver);
1154 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
1158 @Override
1159 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001160 StringBuilder s = new StringBuilder();
1161 s.append("UpdateRecord[");
1162 s.append(mProvider);
1163 s.append(' ').append(mReceiver.mPackageName).append('(');
1164 s.append(mReceiver.mUid).append(')');
1165 s.append(' ').append(mRequest);
1166 s.append(']');
1167 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001171 private Receiver getReceiver(ILocationListener listener, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001172 IBinder binder = listener.asBinder();
1173 Receiver receiver = mReceivers.get(binder);
1174 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001175 receiver = new Receiver(listener, null, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001176 mReceivers.put(binder, receiver);
1177
1178 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001179 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001180 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001181 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001182 return null;
1183 }
1184 }
1185 return receiver;
1186 }
1187
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001188 private Receiver getReceiver(PendingIntent intent, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001189 Receiver receiver = mReceivers.get(intent);
1190 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001191 receiver = new Receiver(null, intent, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001192 mReceivers.put(intent, receiver);
1193 }
1194 return receiver;
1195 }
1196
Victoria Lease37425c32012-10-16 16:08:48 -07001197 /**
1198 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1199 * and consistency requirements.
1200 *
1201 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001202 * @return a version of request that meets the given resolution and consistency requirements
1203 * @hide
1204 */
1205 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1206 LocationRequest sanitizedRequest = new LocationRequest(request);
1207 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1208 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001209 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001210 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001211 break;
1212 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001213 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001214 break;
1215 }
1216 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001217 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1218 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001219 }
Victoria Lease37425c32012-10-16 16:08:48 -07001220 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1221 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001222 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001223 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001224 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001225 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001226 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001227 }
Victoria Lease37425c32012-10-16 16:08:48 -07001228 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001229 }
1230
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001231 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001232 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001233 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001234 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001235 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001236 String[] packages = mPackageManager.getPackagesForUid(uid);
1237 if (packages == null) {
1238 throw new SecurityException("invalid UID " + uid);
1239 }
1240 for (String pkg : packages) {
1241 if (packageName.equals(pkg)) return;
1242 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001243 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001244 }
1245
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001246 private void checkPendingIntent(PendingIntent intent) {
1247 if (intent == null) {
1248 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001249 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001250 }
1251
1252 private Receiver checkListenerOrIntent(ILocationListener listener, PendingIntent intent,
1253 int pid, int uid, String packageName) {
1254 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001255 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001256 } else if (intent != null && listener != null) {
1257 throw new IllegalArgumentException("cannot register both listener and intent");
1258 } else if (intent != null) {
1259 checkPendingIntent(intent);
1260 return getReceiver(intent, pid, uid, packageName);
1261 } else {
1262 return getReceiver(listener, pid, uid, packageName);
1263 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001264 }
1265
Nick Pellye0fd6932012-07-11 10:26:13 -07001266 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001267 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1268 PendingIntent intent, String packageName) {
1269 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1270 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001271 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1272 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1273 request.getProvider());
1274 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001276 final int pid = Binder.getCallingPid();
1277 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001278 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 long identity = Binder.clearCallingIdentity();
1280 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001281 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1282 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001283 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001284 Receiver recevier = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1285
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001286 synchronized (mLock) {
Victoria Lease37425c32012-10-16 16:08:48 -07001287 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 } finally {
1290 Binder.restoreCallingIdentity(identity);
1291 }
1292 }
1293
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001294 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1295 int pid, int uid, String packageName) {
1296 // Figure out the provider. Either its explicitly request (legacy use cases), or
1297 // use the fused provider
1298 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1299 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001300 if (name == null) {
1301 throw new IllegalArgumentException("provider name must not be null");
1302 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001303
1304 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1305 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001306 LocationProviderInterface provider = mProvidersByName.get(name);
1307 if (provider == null) {
1308 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1309 }
1310
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001311 UpdateRecord record = new UpdateRecord(name, request, receiver);
1312 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1313 if (oldRecord != null) {
1314 oldRecord.disposeLocked(false);
1315 }
1316
Victoria Lease09eeaec2013-02-05 11:34:13 -08001317 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001318 if (isProviderEnabled) {
1319 applyRequirementsLocked(name);
1320 } else {
1321 // Notify the listener that updates are currently disabled
1322 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324 }
1325
Nick Pellye0fd6932012-07-11 10:26:13 -07001326 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001327 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1328 String packageName) {
1329 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001330
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001331 final int pid = Binder.getCallingPid();
1332 final int uid = Binder.getCallingUid();
1333 Receiver receiver = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1334
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001335 // providers may use public location API's, need to clear identity
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001336 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001338 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001339 removeUpdatesLocked(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001341 } finally {
1342 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344 }
1345
1346 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001347 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001348
1349 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1350 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1351 synchronized (receiver) {
1352 if (receiver.mPendingBroadcasts > 0) {
1353 decrementPendingBroadcasts();
1354 receiver.mPendingBroadcasts = 0;
1355 }
1356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001359 // Record which providers were associated with this listener
1360 HashSet<String> providers = new HashSet<String>();
1361 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1362 if (oldRecords != null) {
1363 // Call dispose() on the obsolete update records.
1364 for (UpdateRecord record : oldRecords.values()) {
1365 record.disposeLocked(false);
1366 }
1367 // Accumulate providers
1368 providers.addAll(oldRecords.keySet());
1369 }
1370
1371 // update provider
1372 for (String provider : providers) {
1373 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001374 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001375 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001378 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380 }
1381
Dianne Hackbornc2293022013-02-06 23:14:49 -08001382 private void applyAllProviderRequirementsLocked() {
1383 for (LocationProviderInterface p : mProviders) {
1384 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001385 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001386 continue;
1387 }
1388
1389 applyRequirementsLocked(p.getName());
1390 }
1391 }
1392
Nick Pellye0fd6932012-07-11 10:26:13 -07001393 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001394 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001395 if (D) Log.d(TAG, "getLastLocation: " + request);
1396 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001397 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001398 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001399 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1400 request.getProvider());
1401 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001402
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001403 final int uid = Binder.getCallingUid();
1404 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001405 try {
1406 if (mBlacklist.isBlacklisted(packageName)) {
1407 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1408 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001409 return null;
1410 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001411
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001412 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1413 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1414 packageName);
1415 return null;
1416 }
1417
Victoria Leaseb711d572012-10-02 13:14:11 -07001418 synchronized (mLock) {
1419 // Figure out the provider. Either its explicitly request (deprecated API's),
1420 // or use the fused provider
1421 String name = request.getProvider();
1422 if (name == null) name = LocationManager.FUSED_PROVIDER;
1423 LocationProviderInterface provider = mProvidersByName.get(name);
1424 if (provider == null) return null;
1425
Victoria Lease09eeaec2013-02-05 11:34:13 -08001426 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001427
David Christie1b9b7b12013-04-15 15:31:11 -07001428 Location location;
1429 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1430 // Make sure that an app with coarse permissions can't get frequent location
1431 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1432 location = mLastLocationCoarseInterval.get(name);
1433 } else {
1434 location = mLastLocation.get(name);
1435 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001436 if (location == null) {
1437 return null;
1438 }
Victoria Lease37425c32012-10-16 16:08:48 -07001439 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001440 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1441 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001442 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001443 }
Victoria Lease37425c32012-10-16 16:08:48 -07001444 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001445 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001446 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001447 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001448 return null;
1449 } finally {
1450 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001451 }
1452 }
1453
1454 @Override
1455 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1456 String packageName) {
1457 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001458 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1459 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001460 checkPendingIntent(intent);
1461 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001462 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1463 request.getProvider());
1464 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001465
Victoria Lease37425c32012-10-16 16:08:48 -07001466 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001467
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001468 // geo-fence manager uses the public location API, need to clear identity
1469 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001470 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1471 // temporary measure until geofences work for secondary users
1472 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1473 return;
1474 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001475 long identity = Binder.clearCallingIdentity();
1476 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001477 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1478 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001479 } finally {
1480 Binder.restoreCallingIdentity(identity);
1481 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001482 }
1483
1484 @Override
1485 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001486 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001487 checkPendingIntent(intent);
1488 checkPackageName(packageName);
1489
1490 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1491
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001492 // geo-fence manager uses the public location API, need to clear identity
1493 long identity = Binder.clearCallingIdentity();
1494 try {
1495 mGeofenceManager.removeFence(geofence, intent);
1496 } finally {
1497 Binder.restoreCallingIdentity(identity);
1498 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001499 }
1500
1501
1502 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001503 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001504 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 return false;
1506 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001507 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1508 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001509 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001511 final int uid = Binder.getCallingUid();
1512 final long ident = Binder.clearCallingIdentity();
1513 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001514 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001515 return false;
1516 }
1517 } finally {
1518 Binder.restoreCallingIdentity(ident);
1519 }
1520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001522 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001524 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 return false;
1526 }
1527 return true;
1528 }
1529
Nick Pellye0fd6932012-07-11 10:26:13 -07001530 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001532 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001533 try {
1534 mGpsStatusProvider.removeGpsStatusListener(listener);
1535 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001536 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 }
1540
Nick Pellye0fd6932012-07-11 10:26:13 -07001541 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001543 if (provider == null) {
1544 // throw NullPointerException to remain compatible with previous implementation
1545 throw new NullPointerException();
1546 }
Victoria Lease37425c32012-10-16 16:08:48 -07001547 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1548 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001551 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 != PackageManager.PERMISSION_GRANTED)) {
1553 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1554 }
1555
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001556 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001557 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001558 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001559
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001560 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562 }
1563
Nick Pellye0fd6932012-07-11 10:26:13 -07001564 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001565 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001566 if (Binder.getCallingUid() != Process.myUid()) {
1567 throw new SecurityException(
1568 "calling sendNiResponse from outside of the system is not allowed");
1569 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001570 try {
1571 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001572 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001574 return false;
1575 }
1576 }
1577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001579 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001580 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 * accessed by the caller
1582 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001583 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001584 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001585 if (mProvidersByName.get(provider) == null) {
1586 return null;
1587 }
1588
Victoria Lease37425c32012-10-16 16:08:48 -07001589 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1590 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001592 LocationProviderInterface p;
1593 synchronized (mLock) {
1594 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001597 if (p == null) return null;
1598 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
1600
Nick Pellye0fd6932012-07-11 10:26:13 -07001601 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001603 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1604 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001605 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1606
Victoria Lease09eeaec2013-02-05 11:34:13 -08001607 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001608 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001609 try {
1610 synchronized (mLock) {
1611 LocationProviderInterface p = mProvidersByName.get(provider);
1612 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001613
Victoria Lease09eeaec2013-02-05 11:34:13 -08001614 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001615 }
1616 } finally {
1617 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001618 }
1619 }
1620
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001621 /**
1622 * Returns "true" if the UID belongs to a bound location provider.
1623 *
1624 * @param uid the uid
1625 * @return true if uid belongs to a bound location provider
1626 */
1627 private boolean isUidALocationProvider(int uid) {
1628 if (uid == Process.SYSTEM_UID) {
1629 return true;
1630 }
1631 if (mGeocodeProvider != null) {
1632 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1633 }
1634 for (LocationProviderProxy proxy : mProxyProviders) {
1635 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1636 }
1637 return false;
1638 }
1639
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001640 private void checkCallerIsProvider() {
1641 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1642 == PackageManager.PERMISSION_GRANTED) {
1643 return;
1644 }
1645
1646 // Previously we only used the INSTALL_LOCATION_PROVIDER
1647 // check. But that is system or signature
1648 // protection level which is not flexible enough for
1649 // providers installed oustide the system image. So
1650 // also allow providers with a UID matching the
1651 // currently bound package name
1652
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001653 if (isUidALocationProvider(Binder.getCallingUid())) {
1654 return;
1655 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001656
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001657 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1658 "or UID of a currently bound location provider");
1659 }
1660
1661 private boolean doesPackageHaveUid(int uid, String packageName) {
1662 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 return false;
1664 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001665 try {
1666 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1667 if (appInfo.uid != uid) {
1668 return false;
1669 }
1670 } catch (NameNotFoundException e) {
1671 return false;
1672 }
1673 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675
Nick Pellye0fd6932012-07-11 10:26:13 -07001676 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001677 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001678 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001679
Nick Pelly2eeeec22012-07-18 13:13:37 -07001680 if (!location.isComplete()) {
1681 Log.w(TAG, "Dropping incomplete location: " + location);
1682 return;
1683 }
1684
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001685 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1686 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001687 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001688 mLocationHandler.sendMessageAtFrontOfQueue(m);
1689 }
1690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691
Laurent Tu75defb62012-11-01 16:21:52 -07001692 private static boolean shouldBroadcastSafe(
1693 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 // Always broadcast the first update
1695 if (lastLoc == null) {
1696 return true;
1697 }
1698
Nick Pellyf1be6862012-05-15 10:53:42 -07001699 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001700 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001701 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1702 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001703 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 return false;
1705 }
1706
1707 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001708 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 if (minDistance > 0.0) {
1710 if (loc.distanceTo(lastLoc) <= minDistance) {
1711 return false;
1712 }
1713 }
1714
Laurent Tu75defb62012-11-01 16:21:52 -07001715 // Check whether sufficient number of udpates is left
1716 if (record.mRequest.getNumUpdates() <= 0) {
1717 return false;
1718 }
1719
1720 // Check whether the expiry date has passed
1721 if (record.mRequest.getExpireAt() < now) {
1722 return false;
1723 }
1724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 return true;
1726 }
1727
Mike Lockwooda4903f22010-02-17 06:42:23 -05001728 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001729 if (D) Log.d(TAG, "incoming location: " + location);
1730
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001731 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001732 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733
Laurent Tu60ec50a2012-10-04 17:00:10 -07001734 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001735 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001736 if (p == null) return;
1737
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001738 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001739 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1740 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001741 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001742 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001743 lastLocation = new Location(provider);
1744 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001745 } else {
1746 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1747 if (noGPSLocation == null && lastNoGPSLocation != null) {
1748 // New location has no no-GPS location: adopt last no-GPS location. This is set
1749 // directly into location because we do not want to notify COARSE clients.
1750 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1751 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001752 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001753 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754
David Christie1b9b7b12013-04-15 15:31:11 -07001755 // Update last known coarse interval location if enough time has passed.
1756 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1757 if (lastLocationCoarseInterval == null) {
1758 lastLocationCoarseInterval = new Location(location);
1759 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1760 }
1761 long timeDiffNanos = location.getElapsedRealtimeNanos()
1762 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1763 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1764 lastLocationCoarseInterval.set(location);
1765 }
1766 // Don't ever return a coarse location that is more recent than the allowed update
1767 // interval (i.e. don't allow an app to keep registering and unregistering for
1768 // location updates to overcome the minimum interval).
1769 noGPSLocation =
1770 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1771
Laurent Tu60ec50a2012-10-04 17:00:10 -07001772 // Skip if there are no UpdateRecords for this provider.
1773 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1774 if (records == null || records.size() == 0) return;
1775
Victoria Lease09016ab2012-09-16 12:33:15 -07001776 // Fetch coarse location
1777 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001778 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001779 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1780 }
1781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 // Fetch latest status update time
1783 long newStatusUpdateTime = p.getStatusUpdateTime();
1784
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001785 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 Bundle extras = new Bundle();
1787 int status = p.getStatus(extras);
1788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001790 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001793 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001795 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001796
Victoria Lease269518e2012-10-29 08:25:39 -07001797 int receiverUserId = UserHandle.getUserId(receiver.mUid);
1798 if (receiverUserId != mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001799 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001800 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001801 " (current user: " + mCurrentUserId + ", app: " +
1802 receiver.mPackageName + ")");
1803 }
1804 continue;
1805 }
1806
Nick Pelly4035f5a2012-08-17 14:43:49 -07001807 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1808 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1809 receiver.mPackageName);
1810 continue;
1811 }
1812
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001813 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1814 receiver.mAllowedResolutionLevel)) {
1815 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1816 receiver.mPackageName);
1817 continue;
1818 }
1819
Victoria Lease09016ab2012-09-16 12:33:15 -07001820 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001821 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1822 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001823 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001824 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001825 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001826 if (notifyLocation != null) {
1827 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001828 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001829 if (lastLoc == null) {
1830 lastLoc = new Location(notifyLocation);
1831 r.mLastFixBroadcast = lastLoc;
1832 } else {
1833 lastLoc.set(notifyLocation);
1834 }
1835 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1836 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1837 receiverDead = true;
1838 }
Laurent Tu75defb62012-11-01 16:21:52 -07001839 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841 }
1842
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001843 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001845 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001847 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001849 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001850 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001851 }
1852 }
1853
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001854 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001855 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001856 if (deadUpdateRecords == null) {
1857 deadUpdateRecords = new ArrayList<UpdateRecord>();
1858 }
1859 deadUpdateRecords.add(r);
1860 }
1861 // track dead receivers
1862 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001863 if (deadReceivers == null) {
1864 deadReceivers = new ArrayList<Receiver>();
1865 }
1866 if (!deadReceivers.contains(receiver)) {
1867 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 }
1869 }
1870 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001871
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001872 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001874 for (Receiver receiver : deadReceivers) {
1875 removeUpdatesLocked(receiver);
1876 }
1877 }
1878 if (deadUpdateRecords != null) {
1879 for (UpdateRecord r : deadUpdateRecords) {
1880 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
Victoria Lease8b38b292012-12-04 15:04:43 -08001882 applyRequirementsLocked(provider);
1883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885
1886 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08001887 public LocationWorkerHandler(Looper looper) {
1888 super(looper, null, true);
1889 }
1890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 @Override
1892 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001893 switch (msg.what) {
1894 case MSG_LOCATION_CHANGED:
1895 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
1896 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 }
1898 }
1899 }
1900
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001901 private boolean isMockProvider(String provider) {
1902 synchronized (mLock) {
1903 return mMockProviders.containsKey(provider);
1904 }
1905 }
1906
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001907 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001908 // create a working copy of the incoming Location so that the service can modify it without
1909 // disturbing the caller's copy
1910 Location myLocation = new Location(location);
1911 String provider = myLocation.getProvider();
1912
1913 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
1914 // bit if location did not come from a mock provider because passive/fused providers can
1915 // forward locations from mock providers, and should not grant them legitimacy in doing so.
1916 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
1917 myLocation.setIsFromMockProvider(true);
1918 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08001919
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001920 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001921 if (isAllowedByCurrentUserSettingsLocked(provider)) {
1922 if (!passive) {
1923 // notify passive provider of the new location
1924 mPassiveProvider.updateLocation(myLocation);
1925 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001926 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930
Mike Lockwoode97ae402010-09-29 15:23:46 -04001931 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
1932 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001933 public void onPackageDisappeared(String packageName, int reason) {
1934 // remove all receivers associated with this package name
1935 synchronized (mLock) {
1936 ArrayList<Receiver> deadReceivers = null;
1937
1938 for (Receiver receiver : mReceivers.values()) {
1939 if (receiver.mPackageName.equals(packageName)) {
1940 if (deadReceivers == null) {
1941 deadReceivers = new ArrayList<Receiver>();
1942 }
1943 deadReceivers.add(receiver);
1944 }
1945 }
1946
1947 // perform removal outside of mReceivers loop
1948 if (deadReceivers != null) {
1949 for (Receiver receiver : deadReceivers) {
1950 removeUpdatesLocked(receiver);
1951 }
1952 }
1953 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001954 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04001955 };
1956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 // Wake locks
1958
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001959 private void incrementPendingBroadcasts() {
1960 synchronized (mWakeLock) {
1961 if (mPendingBroadcasts++ == 0) {
1962 try {
1963 mWakeLock.acquire();
1964 log("Acquired wakelock");
1965 } catch (Exception e) {
1966 // This is to catch a runtime exception thrown when we try to release an
1967 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001968 Slog.e(TAG, "exception in acquireWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001969 }
1970 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001971 }
1972 }
1973
1974 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001975 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001976 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001977 try {
1978 // Release wake lock
1979 if (mWakeLock.isHeld()) {
1980 mWakeLock.release();
1981 log("Released wakelock");
1982 } else {
1983 log("Can't release wakelock again!");
1984 }
1985 } catch (Exception e) {
1986 // This is to catch a runtime exception thrown when we try to release an
1987 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 Slog.e(TAG, "exception in releaseWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001989 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001990 }
1991 }
1992 }
1993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 // Geocoder
1995
Nick Pellye0fd6932012-07-11 10:26:13 -07001996 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04001997 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07001998 return mGeocodeProvider != null;
1999 }
2000
Nick Pellye0fd6932012-07-11 10:26:13 -07002001 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002003 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002004 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002005 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2006 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002008 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010
Mike Lockwooda55c3212009-04-15 11:10:11 -04002011
Nick Pellye0fd6932012-07-11 10:26:13 -07002012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002014 double lowerLeftLatitude, double lowerLeftLongitude,
2015 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002016 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002017
2018 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002019 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2020 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2021 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002023 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025
2026 // Mock Providers
2027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 private void checkMockPermissionsSafe() {
2029 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2030 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2031 if (!allowMocks) {
2032 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2033 }
2034
2035 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2036 PackageManager.PERMISSION_GRANTED) {
2037 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
2040
Nick Pellye0fd6932012-07-11 10:26:13 -07002041 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002042 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 checkMockPermissionsSafe();
2044
Mike Lockwooda4903f22010-02-17 06:42:23 -05002045 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2046 throw new IllegalArgumentException("Cannot mock the passive location provider");
2047 }
2048
Mike Lockwood86328a92009-10-23 08:38:25 -04002049 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002050 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002051 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002052 // remove the real provider if we are replacing GPS or network provider
2053 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002054 || LocationManager.NETWORK_PROVIDER.equals(name)
2055 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002056 LocationProviderInterface p = mProvidersByName.get(name);
2057 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002058 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002059 }
2060 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002061 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2063 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002064 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002065 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002066 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002067 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 updateProvidersLocked();
2069 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002070 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
2072
Nick Pellye0fd6932012-07-11 10:26:13 -07002073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 public void removeTestProvider(String provider) {
2075 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002076 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002077 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002078 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2080 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002081 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002082 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002083
2084 // reinstate real provider if available
2085 LocationProviderInterface realProvider = mRealProviders.get(provider);
2086 if (realProvider != null) {
2087 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002088 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002089 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002090 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002092 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094 }
2095
Nick Pellye0fd6932012-07-11 10:26:13 -07002096 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 public void setTestProviderLocation(String provider, Location loc) {
2098 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002099 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002100 MockProvider mockProvider = mMockProviders.get(provider);
2101 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2103 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002104 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2105 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002106 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002107 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109 }
2110
Nick Pellye0fd6932012-07-11 10:26:13 -07002111 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 public void clearTestProviderLocation(String provider) {
2113 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002114 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002115 MockProvider mockProvider = mMockProviders.get(provider);
2116 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2118 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002119 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
2121 }
2122
Nick Pellye0fd6932012-07-11 10:26:13 -07002123 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 public void setTestProviderEnabled(String provider, boolean enabled) {
2125 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002126 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002127 MockProvider mockProvider = mMockProviders.get(provider);
2128 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2130 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002131 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002133 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 mEnabledProviders.add(provider);
2135 mDisabledProviders.remove(provider);
2136 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002137 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 mEnabledProviders.remove(provider);
2139 mDisabledProviders.add(provider);
2140 }
2141 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002142 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144 }
2145
Nick Pellye0fd6932012-07-11 10:26:13 -07002146 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 public void clearTestProviderEnabled(String provider) {
2148 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002149 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002150 MockProvider mockProvider = mMockProviders.get(provider);
2151 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2153 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002154 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 mEnabledProviders.remove(provider);
2156 mDisabledProviders.remove(provider);
2157 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002158 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
2160 }
2161
Nick Pellye0fd6932012-07-11 10:26:13 -07002162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2164 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002165 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002166 MockProvider mockProvider = mMockProviders.get(provider);
2167 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2169 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002170 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172 }
2173
Nick Pellye0fd6932012-07-11 10:26:13 -07002174 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 public void clearTestProviderStatus(String provider) {
2176 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002177 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002178 MockProvider mockProvider = mMockProviders.get(provider);
2179 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2181 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002182 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 }
2185
2186 private void log(String log) {
2187 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002188 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
2190 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002191
2192 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2194 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2195 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002196 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 + Binder.getCallingPid()
2198 + ", uid=" + Binder.getCallingUid());
2199 return;
2200 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002201
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002202 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002205 for (Receiver receiver : mReceivers.values()) {
2206 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002209 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2210 pw.println(" " + entry.getKey() + ":");
2211 for (UpdateRecord record : entry.getValue()) {
2212 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 }
2214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002216 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2217 String provider = entry.getKey();
2218 Location location = entry.getValue();
2219 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002221
David Christie1b9b7b12013-04-15 15:31:11 -07002222 pw.println(" Last Known Locations Coarse Intervals:");
2223 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2224 String provider = entry.getKey();
2225 Location location = entry.getValue();
2226 pw.println(" " + provider + ": " + location);
2227 }
2228
Nick Pellye0fd6932012-07-11 10:26:13 -07002229 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 if (mEnabledProviders.size() > 0) {
2232 pw.println(" Enabled Providers:");
2233 for (String i : mEnabledProviders) {
2234 pw.println(" " + i);
2235 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
2238 if (mDisabledProviders.size() > 0) {
2239 pw.println(" Disabled Providers:");
2240 for (String i : mDisabledProviders) {
2241 pw.println(" " + i);
2242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002244 pw.append(" ");
2245 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 if (mMockProviders.size() > 0) {
2247 pw.println(" Mock Providers:");
2248 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002249 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 }
2251 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002252
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002253 pw.append(" fudger: ");
2254 mLocationFudger.dump(fd, pw, args);
2255
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002256 if (args.length > 0 && "short".equals(args[0])) {
2257 return;
2258 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002259 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002260 pw.print(provider.getName() + " Internal State");
2261 if (provider instanceof LocationProviderProxy) {
2262 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2263 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002264 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002265 pw.println(":");
2266 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
2269 }
2270}