blob: 0bf3a11a5c427cecc700602ef484d61c68db6335 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070021import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070025import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070026import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050027import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070029import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050030import android.content.pm.ResolveInfo;
31import android.content.pm.Signature;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050032import android.content.res.Resources;
Brian Muramatsubb95cb92012-08-29 10:43:21 -070033import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.location.Address;
Mike Lockwood03ca2162010-04-01 08:10:09 -070035import android.location.Criteria;
Mike Lockwood34901402010-01-04 12:14:21 -050036import android.location.GeocoderParams;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070037import android.location.Geofence;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040039import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.location.ILocationListener;
41import android.location.ILocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040042import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.location.Location;
44import android.location.LocationManager;
45import android.location.LocationProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070046import android.location.LocationRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
48import android.os.Bundle;
49import android.os.Handler;
Victoria Lease5cd731a2012-12-19 15:04:21 -080050import android.os.HandlerThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070052import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Message;
54import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070055import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070057import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070058import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070059import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080062import android.util.Slog;
Mike Lockwoode97ae402010-09-29 15:23:46 -040063import com.android.internal.content.PackageMonitor;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070064import com.android.internal.location.ProviderProperties;
65import com.android.internal.location.ProviderRequest;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070066import com.android.internal.os.BackgroundThread;
Mike Lockwood43e33f22010-03-26 10:41:48 -040067import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070068import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070069import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040070import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070071import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070072import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040073import com.android.server.location.LocationProviderInterface;
74import com.android.server.location.LocationProviderProxy;
75import com.android.server.location.MockProvider;
76import com.android.server.location.PassiveProvider;
77
78import java.io.FileDescriptor;
79import java.io.PrintWriter;
80import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070081import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040082import java.util.HashMap;
83import java.util.HashSet;
84import java.util.List;
85import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040086import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * The service class that manages LocationProviders and issues location
90 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080092public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080094 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070095
96 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Victoria Lease37425c32012-10-16 16:08:48 -070098 // Location resolution level: no location data whatsoever
99 private static final int RESOLUTION_LEVEL_NONE = 0;
100 // Location resolution level: coarse location data only
101 private static final int RESOLUTION_LEVEL_COARSE = 1;
102 // Location resolution level: fine location data
103 private static final int RESOLUTION_LEVEL_FINE = 2;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700106 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700108 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400109 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700110 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
111
112 private static final String NETWORK_LOCATION_SERVICE_ACTION =
113 "com.android.location.service.v2.NetworkLocationProvider";
114 private static final String FUSED_LOCATION_SERVICE_ACTION =
115 "com.android.location.service.FusedLocationProvider";
116
117 private static final int MSG_LOCATION_CHANGED = 1;
118
David Christie1b9b7b12013-04-15 15:31:11 -0700119 private static final long NANOS_PER_MILLI = 1000000L;
120
Nick Pellyf1be6862012-05-15 10:53:42 -0700121 // Location Providers may sometimes deliver location updates
122 // slightly faster that requested - provide grace period so
123 // we don't unnecessarily filter events that are otherwise on
124 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700125 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700126
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700127 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
128
129 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800130 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700131
132 // used internally for synchronization
133 private final Object mLock = new Object();
134
Victoria Lease5cd731a2012-12-19 15:04:21 -0800135 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700136 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700137 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700138 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700139 private PowerManager mPowerManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700140 private GeocoderProxy mGeocodeProvider;
141 private IGpsStatusProvider mGpsStatusProvider;
142 private INetInitiatedListener mNetInitiatedListener;
143 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700144 private PassiveProvider mPassiveProvider; // track passive provider for special cases
145 private LocationBlacklist mBlacklist;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700146
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700147 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 // Set of providers that are explicitly enabled
149 private final Set<String> mEnabledProviders = new HashSet<String>();
150
151 // Set of providers that are explicitly disabled
152 private final Set<String> mDisabledProviders = new HashSet<String>();
153
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700154 // Mock (test) providers
155 private final HashMap<String, MockProvider> mMockProviders =
156 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700158 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400159 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700161 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500162 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700163 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400164
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 // real providers, saved here when mocked out
166 private final HashMap<String, LocationProviderInterface> mRealProviders =
167 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700169 // mapping from provider name to provider
170 private final HashMap<String, LocationProviderInterface> mProvidersByName =
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 all its UpdateRecords
174 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
175 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700176
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700177 // mapping from provider name to last known location
178 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
David Christie1b9b7b12013-04-15 15:31:11 -0700180 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
181 // locations stored here are not fudged for coarse permissions.
182 private final HashMap<String, Location> mLastLocationCoarseInterval =
183 new HashMap<String, Location>();
184
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700185 // all providers that operate over proxy, for authorizing incoming location
186 private final ArrayList<LocationProviderProxy> mProxyProviders =
187 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Victoria Lease38389b62012-09-30 11:44:22 -0700189 // current active user on the device - other users are denied location data
190 private int mCurrentUserId = UserHandle.USER_OWNER;
191
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700192 public LocationManagerService(Context context) {
193 super();
194 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800195 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800196
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700197 if (D) Log.d(TAG, "Constructed");
198
199 // most startup is deferred until systemReady()
200 }
201
202 public void systemReady() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700203 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800204 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700205
Victoria Lease5cd731a2012-12-19 15:04:21 -0800206 // fetch package manager
207 mPackageManager = mContext.getPackageManager();
208
Victoria Lease0aa28602013-05-29 15:28:26 -0700209 // fetch power manager
210 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800211
212 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700213 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800214
215 // prepare mLocationHandler's dependents
216 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
217 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
218 mBlacklist.init();
219 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
220
Dianne Hackbornc2293022013-02-06 23:14:49 -0800221 // Monitor for app ops mode changes.
222 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
223 public void opChanged(int op, String packageName) {
224 synchronized (mLock) {
225 applyAllProviderRequirementsLocked();
226 }
227 }
228 };
229 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
230
Victoria Lease5cd731a2012-12-19 15:04:21 -0800231 // prepare providers
232 loadProvidersLocked();
233 updateProvidersLocked();
234 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700235
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700236 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700237 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700238 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700239 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800240 @Override
241 public void onChange(boolean selfChange) {
242 synchronized (mLock) {
243 updateProvidersLocked();
244 }
245 }
246 }, UserHandle.USER_ALL);
247 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700248
Victoria Lease38389b62012-09-30 11:44:22 -0700249 // listen for user change
250 IntentFilter intentFilter = new IntentFilter();
251 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
252
253 mContext.registerReceiverAsUser(new BroadcastReceiver() {
254 @Override
255 public void onReceive(Context context, Intent intent) {
256 String action = intent.getAction();
257 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
258 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
259 }
260 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800261 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700262 }
263
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500264 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
265 PackageManager pm = mContext.getPackageManager();
266 String systemPackageName = mContext.getPackageName();
267 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
268
269 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
270 new Intent(FUSED_LOCATION_SERVICE_ACTION),
271 PackageManager.GET_META_DATA, mCurrentUserId);
272 for (ResolveInfo rInfo : rInfos) {
273 String packageName = rInfo.serviceInfo.packageName;
274
275 // Check that the signature is in the list of supported sigs. If it's not in
276 // this list the standard provider binding logic won't bind to it.
277 try {
278 PackageInfo pInfo;
279 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
280 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
281 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
282 ", but has wrong signature, ignoring");
283 continue;
284 }
285 } catch (NameNotFoundException e) {
286 Log.e(TAG, "missing package: " + packageName);
287 continue;
288 }
289
290 // Get the version info
291 if (rInfo.serviceInfo.metaData == null) {
292 Log.w(TAG, "Found fused provider without metadata: " + packageName);
293 continue;
294 }
295
296 int version = rInfo.serviceInfo.metaData.getInt(
297 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
298 if (version == 0) {
299 // This should be the fallback fused location provider.
300
301 // Make sure it's in the system partition.
302 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
303 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
304 continue;
305 }
306
307 // Check that the fallback is signed the same as the OS
308 // as a proxy for coreApp="true"
309 if (pm.checkSignatures(systemPackageName, packageName)
310 != PackageManager.SIGNATURE_MATCH) {
311 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
312 + packageName);
313 continue;
314 }
315
316 // Found a valid fallback.
317 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
318 return;
319 } else {
320 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
321 }
322 }
323
324 throw new IllegalStateException("Unable to find a fused location provider that is in the "
325 + "system partition with version 0 and signed with the platform certificate. "
326 + "Such a package is needed to provide a default fused location provider in the "
327 + "event that no other fused location provider has been installed or is currently "
328 + "available. For example, coreOnly boot mode when decrypting the data "
329 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
330 }
331
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700332 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700333 // create a passive location provider, which is always enabled
334 PassiveProvider passiveProvider = new PassiveProvider(this);
335 addProviderLocked(passiveProvider);
336 mEnabledProviders.add(passiveProvider.getName());
337 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700338 // Create a gps location provider
339 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
340 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700341
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700342 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700343 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
344 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
345 addProviderLocked(gpsProvider);
346 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
347 }
348
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700349 /*
350 Load package name(s) containing location provider support.
351 These packages can contain services implementing location providers:
352 Geocoder Provider, Network Location Provider, and
353 Fused Location Provider. They will each be searched for
354 service components implementing these providers.
355 The location framework also has support for installation
356 of new location providers at run-time. The new package does not
357 have to be explicitly listed here, however it must have a signature
358 that matches the signature of at least one package on this list.
359 */
360 Resources resources = mContext.getResources();
361 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500362 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700363 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500364 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
365 Arrays.toString(pkgs));
366 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
367
368 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700369
370 // bind to network provider
371 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
372 mContext,
373 LocationManager.NETWORK_PROVIDER,
374 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700375 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
376 com.android.internal.R.string.config_networkLocationProviderPackageName,
377 com.android.internal.R.array.config_locationProviderPackageNames,
378 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700379 if (networkProvider != null) {
380 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
381 mProxyProviders.add(networkProvider);
382 addProviderLocked(networkProvider);
383 } else {
384 Slog.w(TAG, "no network location provider found");
385 }
386
387 // bind to fused provider
388 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
389 mContext,
390 LocationManager.FUSED_PROVIDER,
391 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700392 com.android.internal.R.bool.config_enableFusedLocationOverlay,
393 com.android.internal.R.string.config_fusedLocationProviderPackageName,
394 com.android.internal.R.array.config_locationProviderPackageNames,
395 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700396 if (fusedLocationProvider != null) {
397 addProviderLocked(fusedLocationProvider);
398 mProxyProviders.add(fusedLocationProvider);
399 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700400 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700401 } else {
402 Slog.e(TAG, "no fused location provider found",
403 new IllegalStateException("Location service needs a fused location provider"));
404 }
405
406 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700407 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
408 com.android.internal.R.bool.config_enableGeocoderOverlay,
409 com.android.internal.R.string.config_geocoderProviderPackageName,
410 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800411 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700412 if (mGeocodeProvider == null) {
413 Slog.e(TAG, "no geocoder provider found");
414 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700415
416 // bind to geofence provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700417 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext,
418 com.android.internal.R.bool.config_enableGeofenceOverlay,
419 com.android.internal.R.string.config_geofenceProviderPackageName,
420 com.android.internal.R.array.config_locationProviderPackageNames,
421 mLocationHandler,
422 gpsProvider.getGpsGeofenceProxy());
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700423 if (provider == null) {
424 Slog.e(TAG, "no geofence provider found");
425 }
426
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700427 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700430 * Called when the device's active user changes.
431 * @param userId the new active user's UserId
432 */
433 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700434 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800435 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700436 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700437 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700438 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700439 for (LocationProviderInterface p : mProviders) {
440 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700441 }
Victoria Lease38389b62012-09-30 11:44:22 -0700442 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700443 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700444 }
445 }
446
447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
449 * location updates.
450 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700451 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700452 final int mUid; // uid of receiver
453 final int mPid; // pid of receiver
454 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700455 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 final ILocationListener mListener;
458 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700460
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400461 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700462
Mike Lockwood48f17512009-04-23 09:12:08 -0700463 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700464 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700466 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
467 String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700470 if (listener != null) {
471 mKey = listener.asBinder();
472 } else {
473 mKey = intent;
474 }
Victoria Lease37425c32012-10-16 16:08:48 -0700475 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700476 mUid = uid;
477 mPid = pid;
478 mPackageName = packageName;
Victoria Lease0aa28602013-05-29 15:28:26 -0700479
480 // construct/configure wakelock
481 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
482 mWakeLock.setWorkSource(new WorkSource(mUid, mPackageName));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
484
485 @Override
486 public boolean equals(Object otherObj) {
487 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700488 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490 return false;
491 }
492
493 @Override
494 public int hashCode() {
495 return mKey.hashCode();
496 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 @Override
499 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700500 StringBuilder s = new StringBuilder();
501 s.append("Reciever[");
502 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700504 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700506 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700508 for (String p : mUpdateRecords.keySet()) {
509 s.append(" ").append(mUpdateRecords.get(p).toString());
510 }
511 s.append("]");
512 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 public boolean isListener() {
516 return mListener != null;
517 }
518
519 public boolean isPendingIntent() {
520 return mPendingIntent != null;
521 }
522
523 public ILocationListener getListener() {
524 if (mListener != null) {
525 return mListener;
526 }
527 throw new IllegalStateException("Request for non-existent listener");
528 }
529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
531 if (mListener != null) {
532 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700533 synchronized (this) {
534 // synchronize to ensure incrementPendingBroadcastsLocked()
535 // is called before decrementPendingBroadcasts()
536 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700537 // call this after broadcasting so we do not increment
538 // if we throw an exeption.
539 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 } catch (RemoteException e) {
542 return false;
543 }
544 } else {
545 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800546 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
548 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700549 synchronized (this) {
550 // synchronize to ensure incrementPendingBroadcastsLocked()
551 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700552 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700553 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700554 // call this after broadcasting so we do not increment
555 // if we throw an exeption.
556 incrementPendingBroadcastsLocked();
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 } catch (PendingIntent.CanceledException e) {
559 return false;
560 }
561 }
562 return true;
563 }
564
565 public boolean callLocationChangedLocked(Location location) {
566 if (mListener != null) {
567 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700568 synchronized (this) {
569 // synchronize to ensure incrementPendingBroadcastsLocked()
570 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800571 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700572 // call this after broadcasting so we do not increment
573 // if we throw an exeption.
574 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 } catch (RemoteException e) {
577 return false;
578 }
579 } else {
580 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800581 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700583 synchronized (this) {
584 // synchronize to ensure incrementPendingBroadcastsLocked()
585 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700586 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700587 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700588 // call this after broadcasting so we do not increment
589 // if we throw an exeption.
590 incrementPendingBroadcastsLocked();
591 }
592 } catch (PendingIntent.CanceledException e) {
593 return false;
594 }
595 }
596 return true;
597 }
598
599 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
600 if (mListener != null) {
601 try {
602 synchronized (this) {
603 // synchronize to ensure incrementPendingBroadcastsLocked()
604 // is called before decrementPendingBroadcasts()
605 if (enabled) {
606 mListener.onProviderEnabled(provider);
607 } else {
608 mListener.onProviderDisabled(provider);
609 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700610 // call this after broadcasting so we do not increment
611 // if we throw an exeption.
612 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700613 }
614 } catch (RemoteException e) {
615 return false;
616 }
617 } else {
618 Intent providerIntent = new Intent();
619 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
620 try {
621 synchronized (this) {
622 // synchronize to ensure incrementPendingBroadcastsLocked()
623 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700624 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700625 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700626 // call this after broadcasting so we do not increment
627 // if we throw an exeption.
628 incrementPendingBroadcastsLocked();
629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 } catch (PendingIntent.CanceledException e) {
631 return false;
632 }
633 }
634 return true;
635 }
636
Nick Pellyf1be6862012-05-15 10:53:42 -0700637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700639 if (D) Log.d(TAG, "Location listener died");
640
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400641 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 removeUpdatesLocked(this);
643 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700644 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700645 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700646 }
647 }
648
Nick Pellye0fd6932012-07-11 10:26:13 -0700649 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700650 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
651 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400652 synchronized (this) {
653 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700654 }
655 }
656
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400657 // this must be called while synchronized by caller in a synchronized block
658 // containing the sending of the broadcaset
659 private void incrementPendingBroadcastsLocked() {
660 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700661 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400662 }
663 }
664
665 private void decrementPendingBroadcastsLocked() {
666 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700667 if (mWakeLock.isHeld()) {
668 mWakeLock.release();
669 }
670 }
671 }
672
673 public void clearPendingBroadcastsLocked() {
674 if (mPendingBroadcasts > 0) {
675 mPendingBroadcasts = 0;
676 if (mWakeLock.isHeld()) {
677 mWakeLock.release();
678 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700679 }
680 }
681 }
682
Nick Pellye0fd6932012-07-11 10:26:13 -0700683 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700684 public void locationCallbackFinished(ILocationListener listener) {
Joshua Bartel080b61b2009-10-05 12:44:46 -0400685 //Do not use getReceiver here as that will add the ILocationListener to
686 //the receiver list if it is not found. If it is not found then the
687 //LocationListener was removed when it had a pending broadcast and should
688 //not be added back.
689 IBinder binder = listener.asBinder();
690 Receiver receiver = mReceivers.get(binder);
Mike Lockwood48f17512009-04-23 09:12:08 -0700691 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400692 synchronized (receiver) {
693 // so wakelock calls will succeed
694 long identity = Binder.clearCallingIdentity();
695 receiver.decrementPendingBroadcastsLocked();
696 Binder.restoreCallingIdentity(identity);
697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699 }
700
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700701 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400702 mProviders.add(provider);
703 mProvidersByName.put(provider.getName(), provider);
704 }
705
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700706 private void removeProviderLocked(LocationProviderInterface provider) {
707 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400708 mProviders.remove(provider);
709 mProvidersByName.remove(provider.getName());
710 }
711
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800712 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800713 * Returns "true" if access to the specified location provider is allowed by the current
714 * user's settings. Access to all location providers is forbidden to non-location-provider
715 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800716 *
717 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800718 * @return
719 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800720 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 if (mEnabledProviders.contains(provider)) {
722 return true;
723 }
724 if (mDisabledProviders.contains(provider)) {
725 return false;
726 }
727 // Use system settings
728 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729
Victoria Leaseb711d572012-10-02 13:14:11 -0700730 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700733 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800734 * Returns "true" if access to the specified location provider is allowed by the specified
735 * user's settings. Access to all location providers is forbidden to non-location-provider
736 * processes belonging to background users.
737 *
738 * @param provider the name of the location provider
739 * @param uid the requestor's UID
740 * @return
741 */
742 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
743 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
744 return false;
745 }
746 return isAllowedByCurrentUserSettingsLocked(provider);
747 }
748
749 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700750 * Returns the permission string associated with the specified resolution level.
751 *
752 * @param resolutionLevel the resolution level
753 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700754 */
Victoria Lease37425c32012-10-16 16:08:48 -0700755 private String getResolutionPermission(int resolutionLevel) {
756 switch (resolutionLevel) {
757 case RESOLUTION_LEVEL_FINE:
758 return android.Manifest.permission.ACCESS_FINE_LOCATION;
759 case RESOLUTION_LEVEL_COARSE:
760 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
761 default:
762 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700764 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700765
Victoria Leaseda479c52012-10-15 15:24:16 -0700766 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700767 * Returns the resolution level allowed to the given PID/UID pair.
768 *
769 * @param pid the PID
770 * @param uid the UID
771 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700772 */
Victoria Lease37425c32012-10-16 16:08:48 -0700773 private int getAllowedResolutionLevel(int pid, int uid) {
774 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
775 pid, uid) == PackageManager.PERMISSION_GRANTED) {
776 return RESOLUTION_LEVEL_FINE;
777 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
778 pid, uid) == PackageManager.PERMISSION_GRANTED) {
779 return RESOLUTION_LEVEL_COARSE;
780 } else {
781 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700782 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700783 }
784
785 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700786 * Returns the resolution level allowed to the caller
787 *
788 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700789 */
Victoria Lease37425c32012-10-16 16:08:48 -0700790 private int getCallerAllowedResolutionLevel() {
791 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
792 }
793
794 /**
795 * Throw SecurityException if specified resolution level is insufficient to use geofences.
796 *
797 * @param allowedResolutionLevel resolution level allowed to caller
798 */
799 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
800 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700801 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
Victoria Lease37425c32012-10-16 16:08:48 -0700805 /**
806 * Return the minimum resolution level required to use the specified location provider.
807 *
808 * @param provider the name of the location provider
809 * @return minimum resolution level required for provider
810 */
811 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700812 if (LocationManager.GPS_PROVIDER.equals(provider) ||
813 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
814 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700815 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700816 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
817 LocationManager.FUSED_PROVIDER.equals(provider)) {
818 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700819 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700820 } else {
821 // mock providers
822 LocationProviderInterface lp = mMockProviders.get(provider);
823 if (lp != null) {
824 ProviderProperties properties = lp.getProperties();
825 if (properties != null) {
826 if (properties.mRequiresSatellite) {
827 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700828 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700829 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
830 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700831 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700832 }
833 }
834 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700835 }
Victoria Lease37425c32012-10-16 16:08:48 -0700836 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700837 }
838
Victoria Lease37425c32012-10-16 16:08:48 -0700839 /**
840 * Throw SecurityException if specified resolution level is insufficient to use the named
841 * location provider.
842 *
843 * @param allowedResolutionLevel resolution level allowed to caller
844 * @param providerName the name of the location provider
845 */
846 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
847 String providerName) {
848 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
849 if (allowedResolutionLevel < requiredResolutionLevel) {
850 switch (requiredResolutionLevel) {
851 case RESOLUTION_LEVEL_FINE:
852 throw new SecurityException("\"" + providerName + "\" location provider " +
853 "requires ACCESS_FINE_LOCATION permission.");
854 case RESOLUTION_LEVEL_COARSE:
855 throw new SecurityException("\"" + providerName + "\" location provider " +
856 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
857 default:
858 throw new SecurityException("Insufficient permission for \"" + providerName +
859 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700860 }
861 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700862 }
863
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800864 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800865 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
866 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800867 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800868 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800869 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800870 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800871 }
872 return -1;
873 }
874
875 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
876 int op = resolutionLevelToOp(allowedResolutionLevel);
877 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800878 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
879 return false;
880 }
881 }
882 return true;
883 }
884
885 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800886 int op = resolutionLevelToOp(allowedResolutionLevel);
887 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800888 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
889 return false;
890 }
891 }
892 return true;
893 }
894
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700895 /**
896 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700897 * fused, also including ones that are not permitted to
898 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700899 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700900 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700902 ArrayList<String> out;
903 synchronized (mLock) {
904 out = new ArrayList<String>(mProviders.size());
905 for (LocationProviderInterface provider : mProviders) {
906 String name = provider.getName();
907 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700908 continue;
909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 out.add(name);
911 }
912 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700913
914 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 return out;
916 }
917
Mike Lockwood03ca2162010-04-01 08:10:09 -0700918 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700919 * Return all providers by name, that match criteria and are optionally
920 * enabled.
921 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700922 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700923 @Override
924 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -0700925 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700926 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800927 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -0700928 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -0700929 try {
930 synchronized (mLock) {
931 out = new ArrayList<String>(mProviders.size());
932 for (LocationProviderInterface provider : mProviders) {
933 String name = provider.getName();
934 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700935 continue;
936 }
Victoria Lease37425c32012-10-16 16:08:48 -0700937 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -0800938 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700939 continue;
940 }
941 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
942 name, provider.getProperties(), criteria)) {
943 continue;
944 }
945 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700946 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700947 }
Mike Lockwood03ca2162010-04-01 08:10:09 -0700948 }
Victoria Leaseb711d572012-10-02 13:14:11 -0700949 } finally {
950 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700951 }
952
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700953 if (D) Log.d(TAG, "getProviders()=" + out);
954 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700955 }
956
957 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700958 * Return the name of the best provider given a Criteria object.
959 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -0700960 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700961 * has been deprecated as well. So this method now uses
962 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700963 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700964 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700965 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700966 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700967
968 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700969 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700970 result = pickBest(providers);
971 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
972 return result;
973 }
974 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700975 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700976 result = pickBest(providers);
977 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
978 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700979 }
980
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700981 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700982 return null;
983 }
984
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700985 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -0700986 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700987 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -0700988 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
989 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700990 } else {
991 return providers.get(0);
992 }
993 }
994
Nick Pellye0fd6932012-07-11 10:26:13 -0700995 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700996 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
997 LocationProviderInterface p = mProvidersByName.get(provider);
998 if (p == null) {
999 throw new IllegalArgumentException("provider=" + provider);
1000 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001001
1002 boolean result = LocationProvider.propertiesMeetCriteria(
1003 p.getName(), p.getProperties(), criteria);
1004 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1005 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001006 }
1007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001009 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001010 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001011 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 boolean isEnabled = p.isEnabled();
1013 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001014 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001016 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001017 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001019 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001020 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001022 }
1023 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001024 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1025 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 }
1028
Victoria Leaseb711d572012-10-02 13:14:11 -07001029 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 int listeners = 0;
1031
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001032 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001033 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034
1035 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1038 if (records != null) {
1039 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001040 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001042 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001043 // Sends a notification message to the receiver
1044 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1045 if (deadReceivers == null) {
1046 deadReceivers = new ArrayList<Receiver>();
1047 }
1048 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001050 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 }
1054
1055 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001056 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 removeUpdatesLocked(deadReceivers.get(i));
1058 }
1059 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 if (enabled) {
1062 p.enable();
1063 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001064 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
1066 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001071 private void applyRequirementsLocked(String provider) {
1072 LocationProviderInterface p = mProvidersByName.get(provider);
1073 if (p == null) return;
1074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001076 WorkSource worksource = new WorkSource();
1077 ProviderRequest providerRequest = new ProviderRequest();
1078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001080 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001081 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001082 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1083 record.mReceiver.mAllowedResolutionLevel)) {
1084 LocationRequest locationRequest = record.mRequest;
1085 providerRequest.locationRequests.add(locationRequest);
1086 if (locationRequest.getInterval() < providerRequest.interval) {
1087 providerRequest.reportLocation = true;
1088 providerRequest.interval = locationRequest.getInterval();
1089 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001090 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001091 }
1092 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001093
1094 if (providerRequest.reportLocation) {
1095 // calculate who to blame for power
1096 // This is somewhat arbitrary. We pick a threshold interval
1097 // that is slightly higher that the minimum interval, and
1098 // spread the blame across all applications with a request
1099 // under that threshold.
1100 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1101 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001102 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001103 LocationRequest locationRequest = record.mRequest;
1104 if (locationRequest.getInterval() <= thresholdInterval) {
Dianne Hackborn002a54e2013-01-10 17:34:55 -08001105 worksource.add(record.mReceiver.mUid, record.mReceiver.mPackageName);
Victoria Leaseb711d572012-10-02 13:14:11 -07001106 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001107 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001111
1112 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1113 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115
1116 private class UpdateRecord {
1117 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001118 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001120 Location mLastFixBroadcast;
1121 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122
1123 /**
1124 * Note: must be constructed with lock held.
1125 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001126 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001128 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130
1131 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1132 if (records == null) {
1133 records = new ArrayList<UpdateRecord>();
1134 mRecordsByProvider.put(provider, records);
1135 }
1136 if (!records.contains(this)) {
1137 records.add(this);
1138 }
1139 }
1140
1141 /**
1142 * Method to be called when a record will no longer be used. Calling this multiple times
1143 * must have the same effect as calling it once.
1144 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001145 void disposeLocked(boolean removeReceiver) {
1146 // remove from mRecordsByProvider
1147 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1148 if (globalRecords != null) {
1149 globalRecords.remove(this);
1150 }
1151
1152 if (!removeReceiver) return; // the caller will handle the rest
1153
1154 // remove from Receiver#mUpdateRecords
1155 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1156 if (receiverRecords != null) {
1157 receiverRecords.remove(this.mProvider);
1158
1159 // and also remove the Receiver if it has no more update records
1160 if (removeReceiver && receiverRecords.size() == 0) {
1161 removeUpdatesLocked(mReceiver);
1162 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165
1166 @Override
1167 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001168 StringBuilder s = new StringBuilder();
1169 s.append("UpdateRecord[");
1170 s.append(mProvider);
1171 s.append(' ').append(mReceiver.mPackageName).append('(');
1172 s.append(mReceiver.mUid).append(')');
1173 s.append(' ').append(mRequest);
1174 s.append(']');
1175 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001179 private Receiver getReceiver(ILocationListener listener, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001180 IBinder binder = listener.asBinder();
1181 Receiver receiver = mReceivers.get(binder);
1182 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001183 receiver = new Receiver(listener, null, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001184 mReceivers.put(binder, receiver);
1185
1186 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001187 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001188 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001189 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001190 return null;
1191 }
1192 }
1193 return receiver;
1194 }
1195
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001196 private Receiver getReceiver(PendingIntent intent, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001197 Receiver receiver = mReceivers.get(intent);
1198 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001199 receiver = new Receiver(null, intent, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001200 mReceivers.put(intent, receiver);
1201 }
1202 return receiver;
1203 }
1204
Victoria Lease37425c32012-10-16 16:08:48 -07001205 /**
1206 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1207 * and consistency requirements.
1208 *
1209 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001210 * @return a version of request that meets the given resolution and consistency requirements
1211 * @hide
1212 */
1213 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1214 LocationRequest sanitizedRequest = new LocationRequest(request);
1215 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1216 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001217 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001218 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001219 break;
1220 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001221 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001222 break;
1223 }
1224 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001225 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1226 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001227 }
Victoria Lease37425c32012-10-16 16:08:48 -07001228 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1229 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001230 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001231 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001232 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001233 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001234 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001235 }
Victoria Lease37425c32012-10-16 16:08:48 -07001236 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001237 }
1238
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001239 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001240 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001241 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001242 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001243 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001244 String[] packages = mPackageManager.getPackagesForUid(uid);
1245 if (packages == null) {
1246 throw new SecurityException("invalid UID " + uid);
1247 }
1248 for (String pkg : packages) {
1249 if (packageName.equals(pkg)) return;
1250 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001251 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001252 }
1253
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001254 private void checkPendingIntent(PendingIntent intent) {
1255 if (intent == null) {
1256 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001257 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001258 }
1259
1260 private Receiver checkListenerOrIntent(ILocationListener listener, PendingIntent intent,
1261 int pid, int uid, String packageName) {
1262 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001263 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001264 } else if (intent != null && listener != null) {
1265 throw new IllegalArgumentException("cannot register both listener and intent");
1266 } else if (intent != null) {
1267 checkPendingIntent(intent);
1268 return getReceiver(intent, pid, uid, packageName);
1269 } else {
1270 return getReceiver(listener, pid, uid, packageName);
1271 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001272 }
1273
Nick Pellye0fd6932012-07-11 10:26:13 -07001274 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001275 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1276 PendingIntent intent, String packageName) {
1277 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1278 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001279 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1280 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1281 request.getProvider());
1282 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001284 final int pid = Binder.getCallingPid();
1285 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001286 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 long identity = Binder.clearCallingIdentity();
1288 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001289 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1290 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001291 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001292 Receiver recevier = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1293
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001294 synchronized (mLock) {
Victoria Lease37425c32012-10-16 16:08:48 -07001295 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 } finally {
1298 Binder.restoreCallingIdentity(identity);
1299 }
1300 }
1301
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001302 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1303 int pid, int uid, String packageName) {
1304 // Figure out the provider. Either its explicitly request (legacy use cases), or
1305 // use the fused provider
1306 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1307 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001308 if (name == null) {
1309 throw new IllegalArgumentException("provider name must not be null");
1310 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001311
1312 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1313 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001314 LocationProviderInterface provider = mProvidersByName.get(name);
1315 if (provider == null) {
1316 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1317 }
1318
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001319 UpdateRecord record = new UpdateRecord(name, request, receiver);
1320 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1321 if (oldRecord != null) {
1322 oldRecord.disposeLocked(false);
1323 }
1324
Victoria Lease09eeaec2013-02-05 11:34:13 -08001325 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001326 if (isProviderEnabled) {
1327 applyRequirementsLocked(name);
1328 } else {
1329 // Notify the listener that updates are currently disabled
1330 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332 }
1333
Nick Pellye0fd6932012-07-11 10:26:13 -07001334 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001335 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1336 String packageName) {
1337 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001338
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001339 final int pid = Binder.getCallingPid();
1340 final int uid = Binder.getCallingUid();
1341 Receiver receiver = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1342
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001343 // providers may use public location API's, need to clear identity
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001344 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001346 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001347 removeUpdatesLocked(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001349 } finally {
1350 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352 }
1353
1354 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001355 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001356
1357 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1358 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1359 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001360 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001364 // Record which providers were associated with this listener
1365 HashSet<String> providers = new HashSet<String>();
1366 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1367 if (oldRecords != null) {
1368 // Call dispose() on the obsolete update records.
1369 for (UpdateRecord record : oldRecords.values()) {
1370 record.disposeLocked(false);
1371 }
1372 // Accumulate providers
1373 providers.addAll(oldRecords.keySet());
1374 }
1375
1376 // update provider
1377 for (String provider : providers) {
1378 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001379 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001380 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001383 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385 }
1386
Dianne Hackbornc2293022013-02-06 23:14:49 -08001387 private void applyAllProviderRequirementsLocked() {
1388 for (LocationProviderInterface p : mProviders) {
1389 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001390 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001391 continue;
1392 }
1393
1394 applyRequirementsLocked(p.getName());
1395 }
1396 }
1397
Nick Pellye0fd6932012-07-11 10:26:13 -07001398 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001399 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001400 if (D) Log.d(TAG, "getLastLocation: " + request);
1401 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001402 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001403 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001404 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1405 request.getProvider());
1406 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001407
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001408 final int uid = Binder.getCallingUid();
1409 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001410 try {
1411 if (mBlacklist.isBlacklisted(packageName)) {
1412 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1413 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001414 return null;
1415 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001416
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001417 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1418 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1419 packageName);
1420 return null;
1421 }
1422
Victoria Leaseb711d572012-10-02 13:14:11 -07001423 synchronized (mLock) {
1424 // Figure out the provider. Either its explicitly request (deprecated API's),
1425 // or use the fused provider
1426 String name = request.getProvider();
1427 if (name == null) name = LocationManager.FUSED_PROVIDER;
1428 LocationProviderInterface provider = mProvidersByName.get(name);
1429 if (provider == null) return null;
1430
Victoria Lease09eeaec2013-02-05 11:34:13 -08001431 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001432
David Christie1b9b7b12013-04-15 15:31:11 -07001433 Location location;
1434 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1435 // Make sure that an app with coarse permissions can't get frequent location
1436 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1437 location = mLastLocationCoarseInterval.get(name);
1438 } else {
1439 location = mLastLocation.get(name);
1440 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001441 if (location == null) {
1442 return null;
1443 }
Victoria Lease37425c32012-10-16 16:08:48 -07001444 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001445 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1446 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001447 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001448 }
Victoria Lease37425c32012-10-16 16:08:48 -07001449 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001450 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001451 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001452 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001453 return null;
1454 } finally {
1455 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001456 }
1457 }
1458
1459 @Override
1460 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1461 String packageName) {
1462 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001463 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1464 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001465 checkPendingIntent(intent);
1466 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001467 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1468 request.getProvider());
1469 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001470
Victoria Lease37425c32012-10-16 16:08:48 -07001471 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001472
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001473 // geo-fence manager uses the public location API, need to clear identity
1474 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001475 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1476 // temporary measure until geofences work for secondary users
1477 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1478 return;
1479 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001480 long identity = Binder.clearCallingIdentity();
1481 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001482 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1483 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001484 } finally {
1485 Binder.restoreCallingIdentity(identity);
1486 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001487 }
1488
1489 @Override
1490 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001491 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001492 checkPendingIntent(intent);
1493 checkPackageName(packageName);
1494
1495 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1496
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001497 // geo-fence manager uses the public location API, need to clear identity
1498 long identity = Binder.clearCallingIdentity();
1499 try {
1500 mGeofenceManager.removeFence(geofence, intent);
1501 } finally {
1502 Binder.restoreCallingIdentity(identity);
1503 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001504 }
1505
1506
1507 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001508 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001509 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 return false;
1511 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001512 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1513 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001514 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001516 final int uid = Binder.getCallingUid();
1517 final long ident = Binder.clearCallingIdentity();
1518 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001519 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001520 return false;
1521 }
1522 } finally {
1523 Binder.restoreCallingIdentity(ident);
1524 }
1525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001527 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001529 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 return false;
1531 }
1532 return true;
1533 }
1534
Nick Pellye0fd6932012-07-11 10:26:13 -07001535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001537 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001538 try {
1539 mGpsStatusProvider.removeGpsStatusListener(listener);
1540 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001541 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
1544 }
1545
Nick Pellye0fd6932012-07-11 10:26:13 -07001546 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001548 if (provider == null) {
1549 // throw NullPointerException to remain compatible with previous implementation
1550 throw new NullPointerException();
1551 }
Victoria Lease37425c32012-10-16 16:08:48 -07001552 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1553 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001556 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 != PackageManager.PERMISSION_GRANTED)) {
1558 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1559 }
1560
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001561 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001562 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001563 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001564
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001565 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
1567 }
1568
Nick Pellye0fd6932012-07-11 10:26:13 -07001569 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001570 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001571 if (Binder.getCallingUid() != Process.myUid()) {
1572 throw new SecurityException(
1573 "calling sendNiResponse from outside of the system is not allowed");
1574 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001575 try {
1576 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001577 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001578 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001579 return false;
1580 }
1581 }
1582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001584 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001585 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 * accessed by the caller
1587 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001588 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001589 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001590 if (mProvidersByName.get(provider) == null) {
1591 return null;
1592 }
1593
Victoria Lease37425c32012-10-16 16:08:48 -07001594 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1595 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001597 LocationProviderInterface p;
1598 synchronized (mLock) {
1599 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001602 if (p == null) return null;
1603 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
1605
Nick Pellye0fd6932012-07-11 10:26:13 -07001606 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001608 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1609 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001610 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1611
Victoria Lease09eeaec2013-02-05 11:34:13 -08001612 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001613 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001614 try {
1615 synchronized (mLock) {
1616 LocationProviderInterface p = mProvidersByName.get(provider);
1617 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001618
Victoria Lease09eeaec2013-02-05 11:34:13 -08001619 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001620 }
1621 } finally {
1622 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001623 }
1624 }
1625
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001626 /**
1627 * Returns "true" if the UID belongs to a bound location provider.
1628 *
1629 * @param uid the uid
1630 * @return true if uid belongs to a bound location provider
1631 */
1632 private boolean isUidALocationProvider(int uid) {
1633 if (uid == Process.SYSTEM_UID) {
1634 return true;
1635 }
1636 if (mGeocodeProvider != null) {
1637 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1638 }
1639 for (LocationProviderProxy proxy : mProxyProviders) {
1640 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1641 }
1642 return false;
1643 }
1644
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001645 private void checkCallerIsProvider() {
1646 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1647 == PackageManager.PERMISSION_GRANTED) {
1648 return;
1649 }
1650
1651 // Previously we only used the INSTALL_LOCATION_PROVIDER
1652 // check. But that is system or signature
1653 // protection level which is not flexible enough for
1654 // providers installed oustide the system image. So
1655 // also allow providers with a UID matching the
1656 // currently bound package name
1657
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001658 if (isUidALocationProvider(Binder.getCallingUid())) {
1659 return;
1660 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001661
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001662 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1663 "or UID of a currently bound location provider");
1664 }
1665
1666 private boolean doesPackageHaveUid(int uid, String packageName) {
1667 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 return false;
1669 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001670 try {
1671 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1672 if (appInfo.uid != uid) {
1673 return false;
1674 }
1675 } catch (NameNotFoundException e) {
1676 return false;
1677 }
1678 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680
Nick Pellye0fd6932012-07-11 10:26:13 -07001681 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001682 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001683 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001684
Nick Pelly2eeeec22012-07-18 13:13:37 -07001685 if (!location.isComplete()) {
1686 Log.w(TAG, "Dropping incomplete location: " + location);
1687 return;
1688 }
1689
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001690 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1691 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001692 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001693 mLocationHandler.sendMessageAtFrontOfQueue(m);
1694 }
1695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696
Laurent Tu75defb62012-11-01 16:21:52 -07001697 private static boolean shouldBroadcastSafe(
1698 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 // Always broadcast the first update
1700 if (lastLoc == null) {
1701 return true;
1702 }
1703
Nick Pellyf1be6862012-05-15 10:53:42 -07001704 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001705 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07001706 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
1707 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001708 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 return false;
1710 }
1711
1712 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001713 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 if (minDistance > 0.0) {
1715 if (loc.distanceTo(lastLoc) <= minDistance) {
1716 return false;
1717 }
1718 }
1719
Laurent Tu75defb62012-11-01 16:21:52 -07001720 // Check whether sufficient number of udpates is left
1721 if (record.mRequest.getNumUpdates() <= 0) {
1722 return false;
1723 }
1724
1725 // Check whether the expiry date has passed
1726 if (record.mRequest.getExpireAt() < now) {
1727 return false;
1728 }
1729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 return true;
1731 }
1732
Mike Lockwooda4903f22010-02-17 06:42:23 -05001733 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001734 if (D) Log.d(TAG, "incoming location: " + location);
1735
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001736 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001737 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738
Laurent Tu60ec50a2012-10-04 17:00:10 -07001739 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001740 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001741 if (p == null) return;
1742
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001743 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001744 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1745 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001746 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001747 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001748 lastLocation = new Location(provider);
1749 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001750 } else {
1751 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1752 if (noGPSLocation == null && lastNoGPSLocation != null) {
1753 // New location has no no-GPS location: adopt last no-GPS location. This is set
1754 // directly into location because we do not want to notify COARSE clients.
1755 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1756 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001757 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001758 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759
David Christie1b9b7b12013-04-15 15:31:11 -07001760 // Update last known coarse interval location if enough time has passed.
1761 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
1762 if (lastLocationCoarseInterval == null) {
1763 lastLocationCoarseInterval = new Location(location);
1764 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
1765 }
1766 long timeDiffNanos = location.getElapsedRealtimeNanos()
1767 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
1768 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
1769 lastLocationCoarseInterval.set(location);
1770 }
1771 // Don't ever return a coarse location that is more recent than the allowed update
1772 // interval (i.e. don't allow an app to keep registering and unregistering for
1773 // location updates to overcome the minimum interval).
1774 noGPSLocation =
1775 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1776
Laurent Tu60ec50a2012-10-04 17:00:10 -07001777 // Skip if there are no UpdateRecords for this provider.
1778 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1779 if (records == null || records.size() == 0) return;
1780
Victoria Lease09016ab2012-09-16 12:33:15 -07001781 // Fetch coarse location
1782 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07001783 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001784 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1785 }
1786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 // Fetch latest status update time
1788 long newStatusUpdateTime = p.getStatusUpdateTime();
1789
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001790 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 Bundle extras = new Bundle();
1792 int status = p.getStatus(extras);
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001795 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001798 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001800 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001801
Victoria Lease269518e2012-10-29 08:25:39 -07001802 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Victoria Lease2f5b97c2013-05-07 14:22:02 -07001803 if (receiverUserId != mCurrentUserId && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001804 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001805 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001806 " (current user: " + mCurrentUserId + ", app: " +
1807 receiver.mPackageName + ")");
1808 }
1809 continue;
1810 }
1811
Nick Pelly4035f5a2012-08-17 14:43:49 -07001812 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1813 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1814 receiver.mPackageName);
1815 continue;
1816 }
1817
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001818 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1819 receiver.mAllowedResolutionLevel)) {
1820 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1821 receiver.mPackageName);
1822 continue;
1823 }
1824
Victoria Lease09016ab2012-09-16 12:33:15 -07001825 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001826 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1827 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001828 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001829 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001830 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001831 if (notifyLocation != null) {
1832 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001833 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001834 if (lastLoc == null) {
1835 lastLoc = new Location(notifyLocation);
1836 r.mLastFixBroadcast = lastLoc;
1837 } else {
1838 lastLoc.set(notifyLocation);
1839 }
1840 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1841 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1842 receiverDead = true;
1843 }
Laurent Tu75defb62012-11-01 16:21:52 -07001844 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
1846 }
1847
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001848 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001850 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001852 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001854 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001855 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001856 }
1857 }
1858
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001859 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001860 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001861 if (deadUpdateRecords == null) {
1862 deadUpdateRecords = new ArrayList<UpdateRecord>();
1863 }
1864 deadUpdateRecords.add(r);
1865 }
1866 // track dead receivers
1867 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001868 if (deadReceivers == null) {
1869 deadReceivers = new ArrayList<Receiver>();
1870 }
1871 if (!deadReceivers.contains(receiver)) {
1872 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874 }
1875 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001876
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001877 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001879 for (Receiver receiver : deadReceivers) {
1880 removeUpdatesLocked(receiver);
1881 }
1882 }
1883 if (deadUpdateRecords != null) {
1884 for (UpdateRecord r : deadUpdateRecords) {
1885 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
Victoria Lease8b38b292012-12-04 15:04:43 -08001887 applyRequirementsLocked(provider);
1888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890
1891 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08001892 public LocationWorkerHandler(Looper looper) {
1893 super(looper, null, true);
1894 }
1895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 @Override
1897 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001898 switch (msg.what) {
1899 case MSG_LOCATION_CHANGED:
1900 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
1901 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
1903 }
1904 }
1905
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001906 private boolean isMockProvider(String provider) {
1907 synchronized (mLock) {
1908 return mMockProviders.containsKey(provider);
1909 }
1910 }
1911
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001912 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001913 // create a working copy of the incoming Location so that the service can modify it without
1914 // disturbing the caller's copy
1915 Location myLocation = new Location(location);
1916 String provider = myLocation.getProvider();
1917
1918 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
1919 // bit if location did not come from a mock provider because passive/fused providers can
1920 // forward locations from mock providers, and should not grant them legitimacy in doing so.
1921 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
1922 myLocation.setIsFromMockProvider(true);
1923 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08001924
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001925 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001926 if (isAllowedByCurrentUserSettingsLocked(provider)) {
1927 if (!passive) {
1928 // notify passive provider of the new location
1929 mPassiveProvider.updateLocation(myLocation);
1930 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001931 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935
Mike Lockwoode97ae402010-09-29 15:23:46 -04001936 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
1937 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001938 public void onPackageDisappeared(String packageName, int reason) {
1939 // remove all receivers associated with this package name
1940 synchronized (mLock) {
1941 ArrayList<Receiver> deadReceivers = null;
1942
1943 for (Receiver receiver : mReceivers.values()) {
1944 if (receiver.mPackageName.equals(packageName)) {
1945 if (deadReceivers == null) {
1946 deadReceivers = new ArrayList<Receiver>();
1947 }
1948 deadReceivers.add(receiver);
1949 }
1950 }
1951
1952 // perform removal outside of mReceivers loop
1953 if (deadReceivers != null) {
1954 for (Receiver receiver : deadReceivers) {
1955 removeUpdatesLocked(receiver);
1956 }
1957 }
1958 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001959 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04001960 };
1961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 // Geocoder
1963
Nick Pellye0fd6932012-07-11 10:26:13 -07001964 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04001965 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07001966 return mGeocodeProvider != null;
1967 }
1968
Nick Pellye0fd6932012-07-11 10:26:13 -07001969 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001971 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001972 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001973 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
1974 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001976 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
1978
Mike Lockwooda55c3212009-04-15 11:10:11 -04001979
Nick Pellye0fd6932012-07-11 10:26:13 -07001980 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001982 double lowerLeftLatitude, double lowerLeftLongitude,
1983 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001984 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001985
1986 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001987 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1988 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1989 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001991 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993
1994 // Mock Providers
1995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 private void checkMockPermissionsSafe() {
1997 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1998 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1999 if (!allowMocks) {
2000 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
2001 }
2002
2003 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
2004 PackageManager.PERMISSION_GRANTED) {
2005 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07002006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008
Nick Pellye0fd6932012-07-11 10:26:13 -07002009 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002010 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 checkMockPermissionsSafe();
2012
Mike Lockwooda4903f22010-02-17 06:42:23 -05002013 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2014 throw new IllegalArgumentException("Cannot mock the passive location provider");
2015 }
2016
Mike Lockwood86328a92009-10-23 08:38:25 -04002017 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002018 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002019 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002020 // remove the real provider if we are replacing GPS or network provider
2021 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002022 || LocationManager.NETWORK_PROVIDER.equals(name)
2023 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002024 LocationProviderInterface p = mProvidersByName.get(name);
2025 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002026 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002027 }
2028 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002029 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2031 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002032 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002033 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002034 mLastLocation.put(name, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002035 mLastLocationCoarseInterval.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 updateProvidersLocked();
2037 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002038 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
2040
Nick Pellye0fd6932012-07-11 10:26:13 -07002041 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 public void removeTestProvider(String provider) {
2043 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002044 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002045 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002046 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2048 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002049 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002050 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002051
2052 // reinstate real provider if available
2053 LocationProviderInterface realProvider = mRealProviders.get(provider);
2054 if (realProvider != null) {
2055 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002056 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002057 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002058 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002060 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 }
2063
Nick Pellye0fd6932012-07-11 10:26:13 -07002064 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 public void setTestProviderLocation(String provider, Location loc) {
2066 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002067 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002068 MockProvider mockProvider = mMockProviders.get(provider);
2069 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2071 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002072 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2073 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002074 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002075 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
2077 }
2078
Nick Pellye0fd6932012-07-11 10:26:13 -07002079 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 public void clearTestProviderLocation(String provider) {
2081 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002082 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002083 MockProvider mockProvider = mMockProviders.get(provider);
2084 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2086 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002087 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 }
2090
Nick Pellye0fd6932012-07-11 10:26:13 -07002091 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 public void setTestProviderEnabled(String provider, boolean enabled) {
2093 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002094 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002095 MockProvider mockProvider = mMockProviders.get(provider);
2096 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2098 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002099 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002101 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 mEnabledProviders.add(provider);
2103 mDisabledProviders.remove(provider);
2104 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002105 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 mEnabledProviders.remove(provider);
2107 mDisabledProviders.add(provider);
2108 }
2109 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -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 clearTestProviderEnabled(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 Lockwood86328a92009-10-23 08:38:25 -04002122 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 mEnabledProviders.remove(provider);
2124 mDisabledProviders.remove(provider);
2125 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002126 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
2128 }
2129
Nick Pellye0fd6932012-07-11 10:26:13 -07002130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2132 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002133 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002134 MockProvider mockProvider = mMockProviders.get(provider);
2135 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2137 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002138 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140 }
2141
Nick Pellye0fd6932012-07-11 10:26:13 -07002142 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 public void clearTestProviderStatus(String provider) {
2144 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002145 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002146 MockProvider mockProvider = mMockProviders.get(provider);
2147 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2149 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002150 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
2152 }
2153
2154 private void log(String log) {
2155 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002156 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002159
2160 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2162 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2163 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002164 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 + Binder.getCallingPid()
2166 + ", uid=" + Binder.getCallingUid());
2167 return;
2168 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002169
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002170 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002173 for (Receiver receiver : mReceivers.values()) {
2174 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002177 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2178 pw.println(" " + entry.getKey() + ":");
2179 for (UpdateRecord record : entry.getValue()) {
2180 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
2182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002184 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2185 String provider = entry.getKey();
2186 Location location = entry.getValue();
2187 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002189
David Christie1b9b7b12013-04-15 15:31:11 -07002190 pw.println(" Last Known Locations Coarse Intervals:");
2191 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2192 String provider = entry.getKey();
2193 Location location = entry.getValue();
2194 pw.println(" " + provider + ": " + location);
2195 }
2196
Nick Pellye0fd6932012-07-11 10:26:13 -07002197 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 if (mEnabledProviders.size() > 0) {
2200 pw.println(" Enabled Providers:");
2201 for (String i : mEnabledProviders) {
2202 pw.println(" " + i);
2203 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 }
2206 if (mDisabledProviders.size() > 0) {
2207 pw.println(" Disabled Providers:");
2208 for (String i : mDisabledProviders) {
2209 pw.println(" " + i);
2210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002212 pw.append(" ");
2213 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 if (mMockProviders.size() > 0) {
2215 pw.println(" Mock Providers:");
2216 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002217 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
2219 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002220
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002221 pw.append(" fudger: ");
2222 mLocationFudger.dump(fd, pw, args);
2223
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002224 if (args.length > 0 && "short".equals(args[0])) {
2225 return;
2226 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002227 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002228 pw.print(provider.getName() + " Internal State");
2229 if (provider instanceof LocationProviderProxy) {
2230 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2231 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002232 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002233 pw.println(":");
2234 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 }
2237 }
2238}