blob: 26753094a978220600f23b22ea7b053a1c30e3aa [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;
Mike Lockwood43e33f22010-03-26 10:41:48 -040066import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070067import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070068import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040069import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070070import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070071import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040072import com.android.server.location.LocationProviderInterface;
73import com.android.server.location.LocationProviderProxy;
74import com.android.server.location.MockProvider;
75import com.android.server.location.PassiveProvider;
76
77import java.io.FileDescriptor;
78import java.io.PrintWriter;
79import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070080import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040081import java.util.HashMap;
82import java.util.HashSet;
83import java.util.List;
84import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040085import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * The service class that manages LocationProviders and issues location
89 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080091public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080093 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070094
95 private static final String WAKELOCK_KEY = TAG;
96 private static final String THREAD_NAME = 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;
Victoria Lease5cd731a2012-12-19 15:04:21 -0800146 private HandlerThread mHandlerThread;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700147
148 // --- fields below are protected by mWakeLock ---
149 private int mPendingBroadcasts;
150
151 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 // Set of providers that are explicitly enabled
153 private final Set<String> mEnabledProviders = new HashSet<String>();
154
155 // Set of providers that are explicitly disabled
156 private final Set<String> mDisabledProviders = new HashSet<String>();
157
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700158 // Mock (test) providers
159 private final HashMap<String, MockProvider> mMockProviders =
160 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700162 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400163 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500166 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400168
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700169 // real providers, saved here when mocked out
170 private final HashMap<String, LocationProviderInterface> mRealProviders =
171 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700173 // mapping from provider name to provider
174 private final HashMap<String, LocationProviderInterface> mProvidersByName =
175 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700177 // mapping from provider name to all its UpdateRecords
178 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
179 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700180
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700181 // mapping from provider name to last known location
182 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
David Christie1b9b7b12013-04-15 15:31:11 -0700184 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
185 // locations stored here are not fudged for coarse permissions.
186 private final HashMap<String, Location> mLastLocationCoarseInterval =
187 new HashMap<String, Location>();
188
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700189 // all providers that operate over proxy, for authorizing incoming location
190 private final ArrayList<LocationProviderProxy> mProxyProviders =
191 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Victoria Lease38389b62012-09-30 11:44:22 -0700193 // current active user on the device - other users are denied location data
194 private int mCurrentUserId = UserHandle.USER_OWNER;
195
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700196 public LocationManagerService(Context context) {
197 super();
198 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800199 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800200
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700201 if (D) Log.d(TAG, "Constructed");
202
203 // most startup is deferred until systemReady()
204 }
205
206 public void systemReady() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700207 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800208 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700209
Victoria Lease5cd731a2012-12-19 15:04:21 -0800210 // fetch package manager
211 mPackageManager = mContext.getPackageManager();
212
213 // prepare wake lock
214 PowerManager powerManager =
215 (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
216 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
217
218 // prepare worker thread
219 mHandlerThread = new HandlerThread(THREAD_NAME, Process.THREAD_PRIORITY_BACKGROUND);
220 mHandlerThread.start();
221 mLocationHandler = new LocationWorkerHandler(mHandlerThread.getLooper());
222
223 // prepare mLocationHandler's dependents
224 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
225 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
226 mBlacklist.init();
227 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
228
Dianne Hackbornc2293022013-02-06 23:14:49 -0800229 // Monitor for app ops mode changes.
230 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
231 public void opChanged(int op, String packageName) {
232 synchronized (mLock) {
233 applyAllProviderRequirementsLocked();
234 }
235 }
236 };
237 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
238
Victoria Lease5cd731a2012-12-19 15:04:21 -0800239 // prepare providers
240 loadProvidersLocked();
241 updateProvidersLocked();
242 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700243
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700244 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700245 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700246 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700247 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800248 @Override
249 public void onChange(boolean selfChange) {
250 synchronized (mLock) {
251 updateProvidersLocked();
252 }
253 }
254 }, UserHandle.USER_ALL);
255 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700256
Victoria Lease38389b62012-09-30 11:44:22 -0700257 // listen for user change
258 IntentFilter intentFilter = new IntentFilter();
259 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
260
261 mContext.registerReceiverAsUser(new BroadcastReceiver() {
262 @Override
263 public void onReceive(Context context, Intent intent) {
264 String action = intent.getAction();
265 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
266 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
267 }
268 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800269 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700270 }
271
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500272 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
273 PackageManager pm = mContext.getPackageManager();
274 String systemPackageName = mContext.getPackageName();
275 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
276
277 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
278 new Intent(FUSED_LOCATION_SERVICE_ACTION),
279 PackageManager.GET_META_DATA, mCurrentUserId);
280 for (ResolveInfo rInfo : rInfos) {
281 String packageName = rInfo.serviceInfo.packageName;
282
283 // Check that the signature is in the list of supported sigs. If it's not in
284 // this list the standard provider binding logic won't bind to it.
285 try {
286 PackageInfo pInfo;
287 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
288 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
289 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
290 ", but has wrong signature, ignoring");
291 continue;
292 }
293 } catch (NameNotFoundException e) {
294 Log.e(TAG, "missing package: " + packageName);
295 continue;
296 }
297
298 // Get the version info
299 if (rInfo.serviceInfo.metaData == null) {
300 Log.w(TAG, "Found fused provider without metadata: " + packageName);
301 continue;
302 }
303
304 int version = rInfo.serviceInfo.metaData.getInt(
305 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
306 if (version == 0) {
307 // This should be the fallback fused location provider.
308
309 // Make sure it's in the system partition.
310 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
311 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
312 continue;
313 }
314
315 // Check that the fallback is signed the same as the OS
316 // as a proxy for coreApp="true"
317 if (pm.checkSignatures(systemPackageName, packageName)
318 != PackageManager.SIGNATURE_MATCH) {
319 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
320 + packageName);
321 continue;
322 }
323
324 // Found a valid fallback.
325 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
326 return;
327 } else {
328 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
329 }
330 }
331
332 throw new IllegalStateException("Unable to find a fused location provider that is in the "
333 + "system partition with version 0 and signed with the platform certificate. "
334 + "Such a package is needed to provide a default fused location provider in the "
335 + "event that no other fused location provider has been installed or is currently "
336 + "available. For example, coreOnly boot mode when decrypting the data "
337 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
338 }
339
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700340 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700341 // create a passive location provider, which is always enabled
342 PassiveProvider passiveProvider = new PassiveProvider(this);
343 addProviderLocked(passiveProvider);
344 mEnabledProviders.add(passiveProvider.getName());
345 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700346 // Create a gps location provider
347 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
348 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700349
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700350 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700351 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
352 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
353 addProviderLocked(gpsProvider);
354 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
355 }
356
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700357 /*
358 Load package name(s) containing location provider support.
359 These packages can contain services implementing location providers:
360 Geocoder Provider, Network Location Provider, and
361 Fused Location Provider. They will each be searched for
362 service components implementing these providers.
363 The location framework also has support for installation
364 of new location providers at run-time. The new package does not
365 have to be explicitly listed here, however it must have a signature
366 that matches the signature of at least one package on this list.
367 */
368 Resources resources = mContext.getResources();
369 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500370 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700371 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500372 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
373 Arrays.toString(pkgs));
374 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
375
376 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700377
378 // bind to network provider
379 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
380 mContext,
381 LocationManager.NETWORK_PROVIDER,
382 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700383 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
384 com.android.internal.R.string.config_networkLocationProviderPackageName,
385 com.android.internal.R.array.config_locationProviderPackageNames,
386 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700387 if (networkProvider != null) {
388 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
389 mProxyProviders.add(networkProvider);
390 addProviderLocked(networkProvider);
391 } else {
392 Slog.w(TAG, "no network location provider found");
393 }
394
395 // bind to fused provider
396 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
397 mContext,
398 LocationManager.FUSED_PROVIDER,
399 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700400 com.android.internal.R.bool.config_enableFusedLocationOverlay,
401 com.android.internal.R.string.config_fusedLocationProviderPackageName,
402 com.android.internal.R.array.config_locationProviderPackageNames,
403 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700404 if (fusedLocationProvider != null) {
405 addProviderLocked(fusedLocationProvider);
406 mProxyProviders.add(fusedLocationProvider);
407 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700408 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700409 } else {
410 Slog.e(TAG, "no fused location provider found",
411 new IllegalStateException("Location service needs a fused location provider"));
412 }
413
414 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700415 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
416 com.android.internal.R.bool.config_enableGeocoderOverlay,
417 com.android.internal.R.string.config_geocoderProviderPackageName,
418 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800419 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700420 if (mGeocodeProvider == null) {
421 Slog.e(TAG, "no geocoder provider found");
422 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700423
424 // bind to geofence provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700425 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
426 com.android.internal.R.bool.config_enableGeofenceOverlay,
427 com.android.internal.R.string.config_geofenceProviderPackageName,
428 com.android.internal.R.array.config_locationProviderPackageNames,
429 mLocationHandler,
430 gpsProvider.getGpsGeofenceProxy());
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700431 if (provider == null) {
432 Slog.e(TAG, "no geofence provider found");
433 }
434
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700435 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700438 * Called when the device's active user changes.
439 * @param userId the new active user's UserId
440 */
441 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700442 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800443 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700444 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700445 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700446 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700447 for (LocationProviderInterface p : mProviders) {
448 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700449 }
Victoria Lease38389b62012-09-30 11:44:22 -0700450 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700451 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700452 }
453 }
454
455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
457 * location updates.
458 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700459 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700460 final int mUid; // uid of receiver
461 final int mPid; // pid of receiver
462 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700463 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 final ILocationListener mListener;
466 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700468
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400469 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700470
Mike Lockwood48f17512009-04-23 09:12:08 -0700471 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700473 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
474 String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700477 if (listener != null) {
478 mKey = listener.asBinder();
479 } else {
480 mKey = intent;
481 }
Victoria Lease37425c32012-10-16 16:08:48 -0700482 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700483 mUid = uid;
484 mPid = pid;
485 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487
488 @Override
489 public boolean equals(Object otherObj) {
490 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700491 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493 return false;
494 }
495
496 @Override
497 public int hashCode() {
498 return mKey.hashCode();
499 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 @Override
502 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700503 StringBuilder s = new StringBuilder();
504 s.append("Reciever[");
505 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700507 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700509 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700511 for (String p : mUpdateRecords.keySet()) {
512 s.append(" ").append(mUpdateRecords.get(p).toString());
513 }
514 s.append("]");
515 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517
518 public boolean isListener() {
519 return mListener != null;
520 }
521
522 public boolean isPendingIntent() {
523 return mPendingIntent != null;
524 }
525
526 public ILocationListener getListener() {
527 if (mListener != null) {
528 return mListener;
529 }
530 throw new IllegalStateException("Request for non-existent listener");
531 }
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
534 if (mListener != null) {
535 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700536 synchronized (this) {
537 // synchronize to ensure incrementPendingBroadcastsLocked()
538 // is called before decrementPendingBroadcasts()
539 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700540 // call this after broadcasting so we do not increment
541 // if we throw an exeption.
542 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 } catch (RemoteException e) {
545 return false;
546 }
547 } else {
548 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800549 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
551 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700552 synchronized (this) {
553 // synchronize to ensure incrementPendingBroadcastsLocked()
554 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700555 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700556 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700557 // call this after broadcasting so we do not increment
558 // if we throw an exeption.
559 incrementPendingBroadcastsLocked();
560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 } catch (PendingIntent.CanceledException e) {
562 return false;
563 }
564 }
565 return true;
566 }
567
568 public boolean callLocationChangedLocked(Location location) {
569 if (mListener != null) {
570 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700571 synchronized (this) {
572 // synchronize to ensure incrementPendingBroadcastsLocked()
573 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800574 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700575 // call this after broadcasting so we do not increment
576 // if we throw an exeption.
577 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 } catch (RemoteException e) {
580 return false;
581 }
582 } else {
583 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800584 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700586 synchronized (this) {
587 // synchronize to ensure incrementPendingBroadcastsLocked()
588 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700589 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700590 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700591 // call this after broadcasting so we do not increment
592 // if we throw an exeption.
593 incrementPendingBroadcastsLocked();
594 }
595 } catch (PendingIntent.CanceledException e) {
596 return false;
597 }
598 }
599 return true;
600 }
601
602 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
603 if (mListener != null) {
604 try {
605 synchronized (this) {
606 // synchronize to ensure incrementPendingBroadcastsLocked()
607 // is called before decrementPendingBroadcasts()
608 if (enabled) {
609 mListener.onProviderEnabled(provider);
610 } else {
611 mListener.onProviderDisabled(provider);
612 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700613 // call this after broadcasting so we do not increment
614 // if we throw an exeption.
615 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700616 }
617 } catch (RemoteException e) {
618 return false;
619 }
620 } else {
621 Intent providerIntent = new Intent();
622 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
623 try {
624 synchronized (this) {
625 // synchronize to ensure incrementPendingBroadcastsLocked()
626 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700627 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700628 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700629 // call this after broadcasting so we do not increment
630 // if we throw an exeption.
631 incrementPendingBroadcastsLocked();
632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 } catch (PendingIntent.CanceledException e) {
634 return false;
635 }
636 }
637 return true;
638 }
639
Nick Pellyf1be6862012-05-15 10:53:42 -0700640 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700642 if (D) Log.d(TAG, "Location listener died");
643
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400644 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 removeUpdatesLocked(this);
646 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700647 synchronized (this) {
648 if (mPendingBroadcasts > 0) {
649 LocationManagerService.this.decrementPendingBroadcasts();
650 mPendingBroadcasts = 0;
651 }
652 }
653 }
654
Nick Pellye0fd6932012-07-11 10:26:13 -0700655 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700656 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
657 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400658 synchronized (this) {
659 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700660 }
661 }
662
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400663 // this must be called while synchronized by caller in a synchronized block
664 // containing the sending of the broadcaset
665 private void incrementPendingBroadcastsLocked() {
666 if (mPendingBroadcasts++ == 0) {
667 LocationManagerService.this.incrementPendingBroadcasts();
668 }
669 }
670
671 private void decrementPendingBroadcastsLocked() {
672 if (--mPendingBroadcasts == 0) {
673 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700674 }
675 }
676 }
677
Nick Pellye0fd6932012-07-11 10:26:13 -0700678 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700679 public void locationCallbackFinished(ILocationListener listener) {
Joshua Bartel080b61b2009-10-05 12:44:46 -0400680 //Do not use getReceiver here as that will add the ILocationListener to
681 //the receiver list if it is not found. If it is not found then the
682 //LocationListener was removed when it had a pending broadcast and should
683 //not be added back.
684 IBinder binder = listener.asBinder();
685 Receiver receiver = mReceivers.get(binder);
Mike Lockwood48f17512009-04-23 09:12:08 -0700686 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400687 synchronized (receiver) {
688 // so wakelock calls will succeed
689 long identity = Binder.clearCallingIdentity();
690 receiver.decrementPendingBroadcastsLocked();
691 Binder.restoreCallingIdentity(identity);
692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 }
695
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700696 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400697 mProviders.add(provider);
698 mProvidersByName.put(provider.getName(), provider);
699 }
700
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700701 private void removeProviderLocked(LocationProviderInterface provider) {
702 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400703 mProviders.remove(provider);
704 mProvidersByName.remove(provider.getName());
705 }
706
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800707 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800708 * Returns "true" if access to the specified location provider is allowed by the current
709 * user's settings. Access to all location providers is forbidden to non-location-provider
710 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800711 *
712 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800713 * @return
714 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800715 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 if (mEnabledProviders.contains(provider)) {
717 return true;
718 }
719 if (mDisabledProviders.contains(provider)) {
720 return false;
721 }
722 // Use system settings
723 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
Victoria Leaseb711d572012-10-02 13:14:11 -0700725 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700728 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800729 * Returns "true" if access to the specified location provider is allowed by the specified
730 * user's settings. Access to all location providers is forbidden to non-location-provider
731 * processes belonging to background users.
732 *
733 * @param provider the name of the location provider
734 * @param uid the requestor's UID
735 * @return
736 */
737 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
738 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
739 return false;
740 }
741 return isAllowedByCurrentUserSettingsLocked(provider);
742 }
743
744 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700745 * Returns the permission string associated with the specified resolution level.
746 *
747 * @param resolutionLevel the resolution level
748 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700749 */
Victoria Lease37425c32012-10-16 16:08:48 -0700750 private String getResolutionPermission(int resolutionLevel) {
751 switch (resolutionLevel) {
752 case RESOLUTION_LEVEL_FINE:
753 return android.Manifest.permission.ACCESS_FINE_LOCATION;
754 case RESOLUTION_LEVEL_COARSE:
755 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
756 default:
757 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700759 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700760
Victoria Leaseda479c52012-10-15 15:24:16 -0700761 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700762 * Returns the resolution level allowed to the given PID/UID pair.
763 *
764 * @param pid the PID
765 * @param uid the UID
766 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700767 */
Victoria Lease37425c32012-10-16 16:08:48 -0700768 private int getAllowedResolutionLevel(int pid, int uid) {
769 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
770 pid, uid) == PackageManager.PERMISSION_GRANTED) {
771 return RESOLUTION_LEVEL_FINE;
772 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
773 pid, uid) == PackageManager.PERMISSION_GRANTED) {
774 return RESOLUTION_LEVEL_COARSE;
775 } else {
776 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700777 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700778 }
779
780 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700781 * Returns the resolution level allowed to the caller
782 *
783 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700784 */
Victoria Lease37425c32012-10-16 16:08:48 -0700785 private int getCallerAllowedResolutionLevel() {
786 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
787 }
788
789 /**
790 * Throw SecurityException if specified resolution level is insufficient to use geofences.
791 *
792 * @param allowedResolutionLevel resolution level allowed to caller
793 */
794 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
795 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700796 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
Victoria Lease37425c32012-10-16 16:08:48 -0700800 /**
801 * Return the minimum resolution level required to use the specified location provider.
802 *
803 * @param provider the name of the location provider
804 * @return minimum resolution level required for provider
805 */
806 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700807 if (LocationManager.GPS_PROVIDER.equals(provider) ||
808 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
809 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700810 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700811 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
812 LocationManager.FUSED_PROVIDER.equals(provider)) {
813 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700814 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700815 } else {
816 // mock providers
817 LocationProviderInterface lp = mMockProviders.get(provider);
818 if (lp != null) {
819 ProviderProperties properties = lp.getProperties();
820 if (properties != null) {
821 if (properties.mRequiresSatellite) {
822 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700823 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700824 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
825 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700826 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700827 }
828 }
829 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700830 }
Victoria Lease37425c32012-10-16 16:08:48 -0700831 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700832 }
833
Victoria Lease37425c32012-10-16 16:08:48 -0700834 /**
835 * Throw SecurityException if specified resolution level is insufficient to use the named
836 * location provider.
837 *
838 * @param allowedResolutionLevel resolution level allowed to caller
839 * @param providerName the name of the location provider
840 */
841 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
842 String providerName) {
843 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
844 if (allowedResolutionLevel < requiredResolutionLevel) {
845 switch (requiredResolutionLevel) {
846 case RESOLUTION_LEVEL_FINE:
847 throw new SecurityException("\"" + providerName + "\" location provider " +
848 "requires ACCESS_FINE_LOCATION permission.");
849 case RESOLUTION_LEVEL_COARSE:
850 throw new SecurityException("\"" + providerName + "\" location provider " +
851 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
852 default:
853 throw new SecurityException("Insufficient permission for \"" + providerName +
854 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700855 }
856 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700857 }
858
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800859 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800860 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
861 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800862 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800863 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800864 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800865 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800866 }
867 return -1;
868 }
869
870 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
871 int op = resolutionLevelToOp(allowedResolutionLevel);
872 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800873 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
874 return false;
875 }
876 }
877 return true;
878 }
879
880 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800881 int op = resolutionLevelToOp(allowedResolutionLevel);
882 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800883 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
884 return false;
885 }
886 }
887 return true;
888 }
889
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700890 /**
891 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700892 * fused, also including ones that are not permitted to
893 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700894 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700895 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700897 ArrayList<String> out;
898 synchronized (mLock) {
899 out = new ArrayList<String>(mProviders.size());
900 for (LocationProviderInterface provider : mProviders) {
901 String name = provider.getName();
902 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700903 continue;
904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 out.add(name);
906 }
907 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700908
909 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 return out;
911 }
912
Mike Lockwood03ca2162010-04-01 08:10:09 -0700913 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700914 * Return all providers by name, that match criteria and are optionally
915 * enabled.
916 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700917 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700918 @Override
919 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -0700920 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700921 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800922 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -0700923 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -0700924 try {
925 synchronized (mLock) {
926 out = new ArrayList<String>(mProviders.size());
927 for (LocationProviderInterface provider : mProviders) {
928 String name = provider.getName();
929 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700930 continue;
931 }
Victoria Lease37425c32012-10-16 16:08:48 -0700932 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -0800933 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700934 continue;
935 }
936 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
937 name, provider.getProperties(), criteria)) {
938 continue;
939 }
940 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700941 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700942 }
Mike Lockwood03ca2162010-04-01 08:10:09 -0700943 }
Victoria Leaseb711d572012-10-02 13:14:11 -0700944 } finally {
945 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700946 }
947
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700948 if (D) Log.d(TAG, "getProviders()=" + out);
949 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700950 }
951
952 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700953 * Return the name of the best provider given a Criteria object.
954 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -0700955 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700956 * has been deprecated as well. So this method now uses
957 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700958 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700959 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700960 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700961 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700962
963 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700964 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700965 result = pickBest(providers);
966 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
967 return result;
968 }
969 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700970 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700971 result = pickBest(providers);
972 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
973 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700974 }
975
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700976 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700977 return null;
978 }
979
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700980 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -0700981 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700982 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -0700983 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
984 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700985 } else {
986 return providers.get(0);
987 }
988 }
989
Nick Pellye0fd6932012-07-11 10:26:13 -0700990 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700991 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
992 LocationProviderInterface p = mProvidersByName.get(provider);
993 if (p == null) {
994 throw new IllegalArgumentException("provider=" + provider);
995 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700996
997 boolean result = LocationProvider.propertiesMeetCriteria(
998 p.getName(), p.getProperties(), criteria);
999 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1000 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001001 }
1002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001004 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001005 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001006 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 boolean isEnabled = p.isEnabled();
1008 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001009 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001011 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001012 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001014 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001015 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001017 }
1018 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001019 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1020 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022 }
1023
Victoria Leaseb711d572012-10-02 13:14:11 -07001024 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 int listeners = 0;
1026
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001027 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001028 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1033 if (records != null) {
1034 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001035 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001037 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001038 // Sends a notification message to the receiver
1039 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1040 if (deadReceivers == null) {
1041 deadReceivers = new ArrayList<Receiver>();
1042 }
1043 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001045 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048 }
1049
1050 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001051 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 removeUpdatesLocked(deadReceivers.get(i));
1053 }
1054 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (enabled) {
1057 p.enable();
1058 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001059 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
1065
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001066 private void applyRequirementsLocked(String provider) {
1067 LocationProviderInterface p = mProvidersByName.get(provider);
1068 if (p == null) return;
1069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001071 WorkSource worksource = new WorkSource();
1072 ProviderRequest providerRequest = new ProviderRequest();
1073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001075 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001076 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001077 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1078 record.mReceiver.mAllowedResolutionLevel)) {
1079 LocationRequest locationRequest = record.mRequest;
1080 providerRequest.locationRequests.add(locationRequest);
1081 if (locationRequest.getInterval() < providerRequest.interval) {
1082 providerRequest.reportLocation = true;
1083 providerRequest.interval = locationRequest.getInterval();
1084 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001085 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001086 }
1087 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001088
1089 if (providerRequest.reportLocation) {
1090 // calculate who to blame for power
1091 // This is somewhat arbitrary. We pick a threshold interval
1092 // that is slightly higher that the minimum interval, and
1093 // spread the blame across all applications with a request
1094 // under that threshold.
1095 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1096 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001097 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001098 LocationRequest locationRequest = record.mRequest;
1099 if (locationRequest.getInterval() <= thresholdInterval) {
Dianne Hackborn002a54e2013-01-10 17:34:55 -08001100 worksource.add(record.mReceiver.mUid, record.mReceiver.mPackageName);
Victoria Leaseb711d572012-10-02 13:14:11 -07001101 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001102 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001106
1107 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1108 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110
1111 private class UpdateRecord {
1112 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001113 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001115 Location mLastFixBroadcast;
1116 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117
1118 /**
1119 * Note: must be constructed with lock held.
1120 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001121 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001123 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125
1126 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1127 if (records == null) {
1128 records = new ArrayList<UpdateRecord>();
1129 mRecordsByProvider.put(provider, records);
1130 }
1131 if (!records.contains(this)) {
1132 records.add(this);
1133 }
1134 }
1135
1136 /**
1137 * Method to be called when a record will no longer be used. Calling this multiple times
1138 * must have the same effect as calling it once.
1139 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001140 void disposeLocked(boolean removeReceiver) {
1141 // remove from mRecordsByProvider
1142 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1143 if (globalRecords != null) {
1144 globalRecords.remove(this);
1145 }
1146
1147 if (!removeReceiver) return; // the caller will handle the rest
1148
1149 // remove from Receiver#mUpdateRecords
1150 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1151 if (receiverRecords != null) {
1152 receiverRecords.remove(this.mProvider);
1153
1154 // and also remove the Receiver if it has no more update records
1155 if (removeReceiver && receiverRecords.size() == 0) {
1156 removeUpdatesLocked(mReceiver);
1157 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160
1161 @Override
1162 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001163 StringBuilder s = new StringBuilder();
1164 s.append("UpdateRecord[");
1165 s.append(mProvider);
1166 s.append(' ').append(mReceiver.mPackageName).append('(');
1167 s.append(mReceiver.mUid).append(')');
1168 s.append(' ').append(mRequest);
1169 s.append(']');
1170 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001174 private Receiver getReceiver(ILocationListener listener, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001175 IBinder binder = listener.asBinder();
1176 Receiver receiver = mReceivers.get(binder);
1177 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001178 receiver = new Receiver(listener, null, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001179 mReceivers.put(binder, receiver);
1180
1181 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001182 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001183 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001184 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001185 return null;
1186 }
1187 }
1188 return receiver;
1189 }
1190
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001191 private Receiver getReceiver(PendingIntent intent, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001192 Receiver receiver = mReceivers.get(intent);
1193 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001194 receiver = new Receiver(null, intent, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001195 mReceivers.put(intent, receiver);
1196 }
1197 return receiver;
1198 }
1199
Victoria Lease37425c32012-10-16 16:08:48 -07001200 /**
1201 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1202 * and consistency requirements.
1203 *
1204 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001205 * @return a version of request that meets the given resolution and consistency requirements
1206 * @hide
1207 */
1208 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1209 LocationRequest sanitizedRequest = new LocationRequest(request);
1210 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1211 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001212 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001213 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001214 break;
1215 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001216 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001217 break;
1218 }
1219 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001220 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1221 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001222 }
Victoria Lease37425c32012-10-16 16:08:48 -07001223 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1224 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001225 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001226 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001227 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001228 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001229 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001230 }
Victoria Lease37425c32012-10-16 16:08:48 -07001231 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001232 }
1233
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001234 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001235 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001236 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001237 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001238 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001239 String[] packages = mPackageManager.getPackagesForUid(uid);
1240 if (packages == null) {
1241 throw new SecurityException("invalid UID " + uid);
1242 }
1243 for (String pkg : packages) {
1244 if (packageName.equals(pkg)) return;
1245 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001246 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001247 }
1248
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001249 private void checkPendingIntent(PendingIntent intent) {
1250 if (intent == null) {
1251 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001252 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001253 }
1254
1255 private Receiver checkListenerOrIntent(ILocationListener listener, PendingIntent intent,
1256 int pid, int uid, String packageName) {
1257 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001258 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001259 } else if (intent != null && listener != null) {
1260 throw new IllegalArgumentException("cannot register both listener and intent");
1261 } else if (intent != null) {
1262 checkPendingIntent(intent);
1263 return getReceiver(intent, pid, uid, packageName);
1264 } else {
1265 return getReceiver(listener, pid, uid, packageName);
1266 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001267 }
1268
Nick Pellye0fd6932012-07-11 10:26:13 -07001269 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001270 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1271 PendingIntent intent, String packageName) {
1272 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1273 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001274 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1275 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1276 request.getProvider());
1277 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001279 final int pid = Binder.getCallingPid();
1280 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001281 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 long identity = Binder.clearCallingIdentity();
1283 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001284 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1285 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001286 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001287 Receiver recevier = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1288
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001289 synchronized (mLock) {
Victoria Lease37425c32012-10-16 16:08:48 -07001290 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 } finally {
1293 Binder.restoreCallingIdentity(identity);
1294 }
1295 }
1296
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001297 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1298 int pid, int uid, String packageName) {
1299 // Figure out the provider. Either its explicitly request (legacy use cases), or
1300 // use the fused provider
1301 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1302 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001303 if (name == null) {
1304 throw new IllegalArgumentException("provider name must not be null");
1305 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001306
1307 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1308 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001309 LocationProviderInterface provider = mProvidersByName.get(name);
1310 if (provider == null) {
1311 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1312 }
1313
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001314 UpdateRecord record = new UpdateRecord(name, request, receiver);
1315 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1316 if (oldRecord != null) {
1317 oldRecord.disposeLocked(false);
1318 }
1319
Victoria Lease09eeaec2013-02-05 11:34:13 -08001320 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001321 if (isProviderEnabled) {
1322 applyRequirementsLocked(name);
1323 } else {
1324 // Notify the listener that updates are currently disabled
1325 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327 }
1328
Nick Pellye0fd6932012-07-11 10:26:13 -07001329 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001330 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1331 String packageName) {
1332 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001333
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001334 final int pid = Binder.getCallingPid();
1335 final int uid = Binder.getCallingUid();
1336 Receiver receiver = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1337
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001338 // providers may use public location API's, need to clear identity
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001339 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001341 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001342 removeUpdatesLocked(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001344 } finally {
1345 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
1347 }
1348
1349 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001350 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001351
1352 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1353 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1354 synchronized (receiver) {
1355 if (receiver.mPendingBroadcasts > 0) {
1356 decrementPendingBroadcasts();
1357 receiver.mPendingBroadcasts = 0;
1358 }
1359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001362 // Record which providers were associated with this listener
1363 HashSet<String> providers = new HashSet<String>();
1364 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1365 if (oldRecords != null) {
1366 // Call dispose() on the obsolete update records.
1367 for (UpdateRecord record : oldRecords.values()) {
1368 record.disposeLocked(false);
1369 }
1370 // Accumulate providers
1371 providers.addAll(oldRecords.keySet());
1372 }
1373
1374 // update provider
1375 for (String provider : providers) {
1376 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001377 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001378 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001381 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383 }
1384
Dianne Hackbornc2293022013-02-06 23:14:49 -08001385 private void applyAllProviderRequirementsLocked() {
1386 for (LocationProviderInterface p : mProviders) {
1387 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001388 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001389 continue;
1390 }
1391
1392 applyRequirementsLocked(p.getName());
1393 }
1394 }
1395
Nick Pellye0fd6932012-07-11 10:26:13 -07001396 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001397 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001398 if (D) Log.d(TAG, "getLastLocation: " + request);
1399 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001400 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001401 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001402 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1403 request.getProvider());
1404 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001405
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001406 final int uid = Binder.getCallingUid();
1407 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001408 try {
1409 if (mBlacklist.isBlacklisted(packageName)) {
1410 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1411 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001412 return null;
1413 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001414
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001415 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1416 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1417 packageName);
1418 return null;
1419 }
1420
Victoria Leaseb711d572012-10-02 13:14:11 -07001421 synchronized (mLock) {
1422 // Figure out the provider. Either its explicitly request (deprecated API's),
1423 // or use the fused provider
1424 String name = request.getProvider();
1425 if (name == null) name = LocationManager.FUSED_PROVIDER;
1426 LocationProviderInterface provider = mProvidersByName.get(name);
1427 if (provider == null) return null;
1428
Victoria Lease09eeaec2013-02-05 11:34:13 -08001429 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001430
David Christie1b9b7b12013-04-15 15:31:11 -07001431 Location location;
1432 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1433 // Make sure that an app with coarse permissions can't get frequent location
1434 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1435 location = mLastLocationCoarseInterval.get(name);
1436 } else {
1437 location = mLastLocation.get(name);
1438 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001439 if (location == null) {
1440 return null;
1441 }
Victoria Lease37425c32012-10-16 16:08:48 -07001442 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001443 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1444 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001445 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001446 }
Victoria Lease37425c32012-10-16 16:08:48 -07001447 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001448 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001449 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001450 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001451 return null;
1452 } finally {
1453 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001454 }
1455 }
1456
1457 @Override
1458 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1459 String packageName) {
1460 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001461 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1462 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001463 checkPendingIntent(intent);
1464 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001465 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1466 request.getProvider());
1467 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001468
Victoria Lease37425c32012-10-16 16:08:48 -07001469 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001470
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001471 // geo-fence manager uses the public location API, need to clear identity
1472 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001473 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1474 // temporary measure until geofences work for secondary users
1475 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1476 return;
1477 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001478 long identity = Binder.clearCallingIdentity();
1479 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001480 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1481 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001482 } finally {
1483 Binder.restoreCallingIdentity(identity);
1484 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001485 }
1486
1487 @Override
1488 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001489 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001490 checkPendingIntent(intent);
1491 checkPackageName(packageName);
1492
1493 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1494
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001495 // geo-fence manager uses the public location API, need to clear identity
1496 long identity = Binder.clearCallingIdentity();
1497 try {
1498 mGeofenceManager.removeFence(geofence, intent);
1499 } finally {
1500 Binder.restoreCallingIdentity(identity);
1501 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001502 }
1503
1504
1505 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001506 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001507 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 return false;
1509 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001510 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1511 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001512 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001514 final int uid = Binder.getCallingUid();
1515 final long ident = Binder.clearCallingIdentity();
1516 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001517 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001518 return false;
1519 }
1520 } finally {
1521 Binder.restoreCallingIdentity(ident);
1522 }
1523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001525 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 return false;
1529 }
1530 return true;
1531 }
1532
Nick Pellye0fd6932012-07-11 10:26:13 -07001533 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001535 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001536 try {
1537 mGpsStatusProvider.removeGpsStatusListener(listener);
1538 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001539 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
1542 }
1543
Nick Pellye0fd6932012-07-11 10:26:13 -07001544 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001546 if (provider == null) {
1547 // throw NullPointerException to remain compatible with previous implementation
1548 throw new NullPointerException();
1549 }
Victoria Lease37425c32012-10-16 16:08:48 -07001550 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1551 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001554 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 != PackageManager.PERMISSION_GRANTED)) {
1556 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1557 }
1558
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001559 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001560 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001561 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001562
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001563 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565 }
1566
Nick Pellye0fd6932012-07-11 10:26:13 -07001567 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001568 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001569 if (Binder.getCallingUid() != Process.myUid()) {
1570 throw new SecurityException(
1571 "calling sendNiResponse from outside of the system is not allowed");
1572 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001573 try {
1574 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001575 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001576 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001577 return false;
1578 }
1579 }
1580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001582 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001583 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 * accessed by the caller
1585 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001586 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001587 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001588 if (mProvidersByName.get(provider) == null) {
1589 return null;
1590 }
1591
Victoria Lease37425c32012-10-16 16:08:48 -07001592 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1593 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001595 LocationProviderInterface p;
1596 synchronized (mLock) {
1597 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
1599
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001600 if (p == null) return null;
1601 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
1603
Nick Pellye0fd6932012-07-11 10:26:13 -07001604 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001606 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1607 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001608 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1609
Victoria Lease09eeaec2013-02-05 11:34:13 -08001610 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001611 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001612 try {
1613 synchronized (mLock) {
1614 LocationProviderInterface p = mProvidersByName.get(provider);
1615 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001616
Victoria Lease09eeaec2013-02-05 11:34:13 -08001617 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001618 }
1619 } finally {
1620 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001621 }
1622 }
1623
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001624 /**
1625 * Returns "true" if the UID belongs to a bound location provider.
1626 *
1627 * @param uid the uid
1628 * @return true if uid belongs to a bound location provider
1629 */
1630 private boolean isUidALocationProvider(int uid) {
1631 if (uid == Process.SYSTEM_UID) {
1632 return true;
1633 }
1634 if (mGeocodeProvider != null) {
1635 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1636 }
1637 for (LocationProviderProxy proxy : mProxyProviders) {
1638 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1639 }
1640 return false;
1641 }
1642
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001643 private void checkCallerIsProvider() {
1644 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1645 == PackageManager.PERMISSION_GRANTED) {
1646 return;
1647 }
1648
1649 // Previously we only used the INSTALL_LOCATION_PROVIDER
1650 // check. But that is system or signature
1651 // protection level which is not flexible enough for
1652 // providers installed oustide the system image. So
1653 // also allow providers with a UID matching the
1654 // currently bound package name
1655
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001656 if (isUidALocationProvider(Binder.getCallingUid())) {
1657 return;
1658 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001659
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001660 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1661 "or UID of a currently bound location provider");
1662 }
1663
1664 private boolean doesPackageHaveUid(int uid, String packageName) {
1665 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 return false;
1667 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001668 try {
1669 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1670 if (appInfo.uid != uid) {
1671 return false;
1672 }
1673 } catch (NameNotFoundException e) {
1674 return false;
1675 }
1676 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
Nick Pellye0fd6932012-07-11 10:26:13 -07001679 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001680 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001681 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001682
Nick Pelly2eeeec22012-07-18 13:13:37 -07001683 if (!location.isComplete()) {
1684 Log.w(TAG, "Dropping incomplete location: " + location);
1685 return;
1686 }
1687
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001688 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1689 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001690 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001691 mLocationHandler.sendMessageAtFrontOfQueue(m);
1692 }
1693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694
Laurent Tu75defb62012-11-01 16:21:52 -07001695 private static boolean shouldBroadcastSafe(
1696 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 // Always broadcast the first update
1698 if (lastLoc == null) {
1699 return true;
1700 }
1701
Nick Pellyf1be6862012-05-15 10:53:42 -07001702 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001703 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001704 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1705 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001706 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 return false;
1708 }
1709
1710 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001711 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 if (minDistance > 0.0) {
1713 if (loc.distanceTo(lastLoc) <= minDistance) {
1714 return false;
1715 }
1716 }
1717
Laurent Tu75defb62012-11-01 16:21:52 -07001718 // Check whether sufficient number of udpates is left
1719 if (record.mRequest.getNumUpdates() <= 0) {
1720 return false;
1721 }
1722
1723 // Check whether the expiry date has passed
1724 if (record.mRequest.getExpireAt() < now) {
1725 return false;
1726 }
1727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 return true;
1729 }
1730
Mike Lockwooda4903f22010-02-17 06:42:23 -05001731 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001732 if (D) Log.d(TAG, "incoming location: " + location);
1733
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001734 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001735 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736
Laurent Tu60ec50a2012-10-04 17:00:10 -07001737 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001738 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001739 if (p == null) return;
1740
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001741 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001742 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1743 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001744 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001745 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001746 lastLocation = new Location(provider);
1747 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001748 } else {
1749 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1750 if (noGPSLocation == null && lastNoGPSLocation != null) {
1751 // New location has no no-GPS location: adopt last no-GPS location. This is set
1752 // directly into location because we do not want to notify COARSE clients.
1753 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1754 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001755 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001756 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757
David Christie1b9b7b12013-04-15 15:31:11 -07001758 // Update last known coarse interval location if enough time has passed.
1759 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1760 if (lastLocationCoarseInterval == null) {
1761 lastLocationCoarseInterval = new Location(location);
1762 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1763 }
1764 long timeDiffNanos = location.getElapsedRealtimeNanos()
1765 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1766 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1767 lastLocationCoarseInterval.set(location);
1768 }
1769 // Don't ever return a coarse location that is more recent than the allowed update
1770 // interval (i.e. don't allow an app to keep registering and unregistering for
1771 // location updates to overcome the minimum interval).
1772 noGPSLocation =
1773 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1774
Laurent Tu60ec50a2012-10-04 17:00:10 -07001775 // Skip if there are no UpdateRecords for this provider.
1776 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1777 if (records == null || records.size() == 0) return;
1778
Victoria Lease09016ab2012-09-16 12:33:15 -07001779 // Fetch coarse location
1780 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001781 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001782 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1783 }
1784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 // Fetch latest status update time
1786 long newStatusUpdateTime = p.getStatusUpdateTime();
1787
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001788 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 Bundle extras = new Bundle();
1790 int status = p.getStatus(extras);
1791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001793 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001796 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001798 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001799
Victoria Lease269518e2012-10-29 08:25:39 -07001800 int receiverUserId = UserHandle.getUserId(receiver.mUid);
1801 if (receiverUserId != mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001802 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001803 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001804 " (current user: " + mCurrentUserId + ", app: " +
1805 receiver.mPackageName + ")");
1806 }
1807 continue;
1808 }
1809
Nick Pelly4035f5a2012-08-17 14:43:49 -07001810 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1811 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1812 receiver.mPackageName);
1813 continue;
1814 }
1815
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001816 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1817 receiver.mAllowedResolutionLevel)) {
1818 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1819 receiver.mPackageName);
1820 continue;
1821 }
1822
Victoria Lease09016ab2012-09-16 12:33:15 -07001823 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001824 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1825 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001826 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001827 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001828 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001829 if (notifyLocation != null) {
1830 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001831 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001832 if (lastLoc == null) {
1833 lastLoc = new Location(notifyLocation);
1834 r.mLastFixBroadcast = lastLoc;
1835 } else {
1836 lastLoc.set(notifyLocation);
1837 }
1838 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1839 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1840 receiverDead = true;
1841 }
Laurent Tu75defb62012-11-01 16:21:52 -07001842 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
1844 }
1845
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001846 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001848 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001850 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001852 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001853 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001854 }
1855 }
1856
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001857 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001858 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001859 if (deadUpdateRecords == null) {
1860 deadUpdateRecords = new ArrayList<UpdateRecord>();
1861 }
1862 deadUpdateRecords.add(r);
1863 }
1864 // track dead receivers
1865 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001866 if (deadReceivers == null) {
1867 deadReceivers = new ArrayList<Receiver>();
1868 }
1869 if (!deadReceivers.contains(receiver)) {
1870 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872 }
1873 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001874
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001875 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001877 for (Receiver receiver : deadReceivers) {
1878 removeUpdatesLocked(receiver);
1879 }
1880 }
1881 if (deadUpdateRecords != null) {
1882 for (UpdateRecord r : deadUpdateRecords) {
1883 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
Victoria Lease8b38b292012-12-04 15:04:43 -08001885 applyRequirementsLocked(provider);
1886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 }
1888
1889 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08001890 public LocationWorkerHandler(Looper looper) {
1891 super(looper, null, true);
1892 }
1893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 @Override
1895 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001896 switch (msg.what) {
1897 case MSG_LOCATION_CHANGED:
1898 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
1899 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
1901 }
1902 }
1903
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001904 private boolean isMockProvider(String provider) {
1905 synchronized (mLock) {
1906 return mMockProviders.containsKey(provider);
1907 }
1908 }
1909
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001910 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001911 // create a working copy of the incoming Location so that the service can modify it without
1912 // disturbing the caller's copy
1913 Location myLocation = new Location(location);
1914 String provider = myLocation.getProvider();
1915
1916 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
1917 // bit if location did not come from a mock provider because passive/fused providers can
1918 // forward locations from mock providers, and should not grant them legitimacy in doing so.
1919 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
1920 myLocation.setIsFromMockProvider(true);
1921 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08001922
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001923 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001924 if (isAllowedByCurrentUserSettingsLocked(provider)) {
1925 if (!passive) {
1926 // notify passive provider of the new location
1927 mPassiveProvider.updateLocation(myLocation);
1928 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001929 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933
Mike Lockwoode97ae402010-09-29 15:23:46 -04001934 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
1935 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001936 public void onPackageDisappeared(String packageName, int reason) {
1937 // remove all receivers associated with this package name
1938 synchronized (mLock) {
1939 ArrayList<Receiver> deadReceivers = null;
1940
1941 for (Receiver receiver : mReceivers.values()) {
1942 if (receiver.mPackageName.equals(packageName)) {
1943 if (deadReceivers == null) {
1944 deadReceivers = new ArrayList<Receiver>();
1945 }
1946 deadReceivers.add(receiver);
1947 }
1948 }
1949
1950 // perform removal outside of mReceivers loop
1951 if (deadReceivers != null) {
1952 for (Receiver receiver : deadReceivers) {
1953 removeUpdatesLocked(receiver);
1954 }
1955 }
1956 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001957 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04001958 };
1959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 // Wake locks
1961
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001962 private void incrementPendingBroadcasts() {
1963 synchronized (mWakeLock) {
1964 if (mPendingBroadcasts++ == 0) {
1965 try {
1966 mWakeLock.acquire();
1967 log("Acquired wakelock");
1968 } catch (Exception e) {
1969 // This is to catch a runtime exception thrown when we try to release an
1970 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001971 Slog.e(TAG, "exception in acquireWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001972 }
1973 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001974 }
1975 }
1976
1977 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001978 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001979 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001980 try {
1981 // Release wake lock
1982 if (mWakeLock.isHeld()) {
1983 mWakeLock.release();
1984 log("Released wakelock");
1985 } else {
1986 log("Can't release wakelock again!");
1987 }
1988 } catch (Exception e) {
1989 // This is to catch a runtime exception thrown when we try to release an
1990 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001991 Slog.e(TAG, "exception in releaseWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001992 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001993 }
1994 }
1995 }
1996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 // Geocoder
1998
Nick Pellye0fd6932012-07-11 10:26:13 -07001999 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002000 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002001 return mGeocodeProvider != null;
2002 }
2003
Nick Pellye0fd6932012-07-11 10:26:13 -07002004 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002006 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002007 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002008 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2009 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002011 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
2013
Mike Lockwooda55c3212009-04-15 11:10:11 -04002014
Nick Pellye0fd6932012-07-11 10:26:13 -07002015 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002017 double lowerLeftLatitude, double lowerLeftLongitude,
2018 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002019 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002020
2021 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002022 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2023 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2024 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002026 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028
2029 // Mock Providers
2030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 private void checkMockPermissionsSafe() {
2032 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
2033 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
2034 if (!allowMocks) {
2035 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2036 }
2037
2038 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2039 PackageManager.PERMISSION_GRANTED) {
2040 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043
Nick Pellye0fd6932012-07-11 10:26:13 -07002044 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002045 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 checkMockPermissionsSafe();
2047
Mike Lockwooda4903f22010-02-17 06:42:23 -05002048 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2049 throw new IllegalArgumentException("Cannot mock the passive location provider");
2050 }
2051
Mike Lockwood86328a92009-10-23 08:38:25 -04002052 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002053 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002054 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002055 // remove the real provider if we are replacing GPS or network provider
2056 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002057 || LocationManager.NETWORK_PROVIDER.equals(name)
2058 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002059 LocationProviderInterface p = mProvidersByName.get(name);
2060 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002061 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002062 }
2063 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002064 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2066 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002067 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002068 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002069 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002070 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 updateProvidersLocked();
2072 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002073 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
2075
Nick Pellye0fd6932012-07-11 10:26:13 -07002076 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 public void removeTestProvider(String provider) {
2078 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002079 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002080 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002081 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2083 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002084 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002085 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002086
2087 // reinstate real provider if available
2088 LocationProviderInterface realProvider = mRealProviders.get(provider);
2089 if (realProvider != null) {
2090 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002091 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002092 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002093 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002095 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 }
2098
Nick Pellye0fd6932012-07-11 10:26:13 -07002099 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 public void setTestProviderLocation(String provider, Location loc) {
2101 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002102 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002103 MockProvider mockProvider = mMockProviders.get(provider);
2104 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2106 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002107 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2108 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002109 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002110 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112 }
2113
Nick Pellye0fd6932012-07-11 10:26:13 -07002114 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 public void clearTestProviderLocation(String provider) {
2116 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002117 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002118 MockProvider mockProvider = mMockProviders.get(provider);
2119 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2121 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002122 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124 }
2125
Nick Pellye0fd6932012-07-11 10:26:13 -07002126 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 public void setTestProviderEnabled(String provider, boolean enabled) {
2128 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002129 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002130 MockProvider mockProvider = mMockProviders.get(provider);
2131 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2133 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002134 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002136 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 mEnabledProviders.add(provider);
2138 mDisabledProviders.remove(provider);
2139 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002140 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 mEnabledProviders.remove(provider);
2142 mDisabledProviders.add(provider);
2143 }
2144 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002145 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147 }
2148
Nick Pellye0fd6932012-07-11 10:26:13 -07002149 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 public void clearTestProviderEnabled(String provider) {
2151 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002152 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002153 MockProvider mockProvider = mMockProviders.get(provider);
2154 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2156 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002157 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 mEnabledProviders.remove(provider);
2159 mDisabledProviders.remove(provider);
2160 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002161 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163 }
2164
Nick Pellye0fd6932012-07-11 10:26:13 -07002165 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2167 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002168 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002169 MockProvider mockProvider = mMockProviders.get(provider);
2170 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2172 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002173 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175 }
2176
Nick Pellye0fd6932012-07-11 10:26:13 -07002177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 public void clearTestProviderStatus(String provider) {
2179 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002180 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002181 MockProvider mockProvider = mMockProviders.get(provider);
2182 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2184 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002185 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
2187 }
2188
2189 private void log(String log) {
2190 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002191 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
2193 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002194
2195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2197 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2198 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002199 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 + Binder.getCallingPid()
2201 + ", uid=" + Binder.getCallingUid());
2202 return;
2203 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002204
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002205 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002208 for (Receiver receiver : mReceivers.values()) {
2209 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002212 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2213 pw.println(" " + entry.getKey() + ":");
2214 for (UpdateRecord record : entry.getValue()) {
2215 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
2217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002219 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2220 String provider = entry.getKey();
2221 Location location = entry.getValue();
2222 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002224
David Christie1b9b7b12013-04-15 15:31:11 -07002225 pw.println(" Last Known Locations Coarse Intervals:");
2226 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2227 String provider = entry.getKey();
2228 Location location = entry.getValue();
2229 pw.println(" " + provider + ": " + location);
2230 }
2231
Nick Pellye0fd6932012-07-11 10:26:13 -07002232 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 if (mEnabledProviders.size() > 0) {
2235 pw.println(" Enabled Providers:");
2236 for (String i : mEnabledProviders) {
2237 pw.println(" " + i);
2238 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241 if (mDisabledProviders.size() > 0) {
2242 pw.println(" Disabled Providers:");
2243 for (String i : mDisabledProviders) {
2244 pw.println(" " + i);
2245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002247 pw.append(" ");
2248 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 if (mMockProviders.size() > 0) {
2250 pw.println(" Mock Providers:");
2251 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002252 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
2254 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002255
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002256 pw.append(" fudger: ");
2257 mLocationFudger.dump(fd, pw, args);
2258
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002259 if (args.length > 0 && "short".equals(args[0])) {
2260 return;
2261 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002262 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002263 pw.print(provider.getName() + " Internal State");
2264 if (provider instanceof LocationProviderProxy) {
2265 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2266 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002267 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002268 pw.println(":");
2269 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 }
2272 }
2273}