blob: 78699fb10951734d522fce4bd155bbf6750b5400 [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;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080057import android.os.ServiceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070058import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070059import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070060import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080063import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Dianne Hackborna06de0f2012-12-11 16:34:47 -080065import com.android.internal.app.IAppOpsService;
Mike Lockwoode97ae402010-09-29 15:23:46 -040066import com.android.internal.content.PackageMonitor;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070067import com.android.internal.location.ProviderProperties;
68import com.android.internal.location.ProviderRequest;
Mike Lockwood43e33f22010-03-26 10:41:48 -040069import com.android.server.location.GeocoderProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070070import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040071import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070072import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070073import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040074import com.android.server.location.LocationProviderInterface;
75import com.android.server.location.LocationProviderProxy;
76import com.android.server.location.MockProvider;
77import com.android.server.location.PassiveProvider;
78
79import java.io.FileDescriptor;
80import java.io.PrintWriter;
81import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070082import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040083import java.util.HashMap;
84import java.util.HashSet;
85import java.util.List;
86import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040087import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/**
90 * The service class that manages LocationProviders and issues location
91 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080093public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080095 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070096
97 private static final String WAKELOCK_KEY = TAG;
98 private static final String THREAD_NAME = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Victoria Lease37425c32012-10-16 16:08:48 -0700100 // Location resolution level: no location data whatsoever
101 private static final int RESOLUTION_LEVEL_NONE = 0;
102 // Location resolution level: coarse location data only
103 private static final int RESOLUTION_LEVEL_COARSE = 1;
104 // Location resolution level: fine location data
105 private static final int RESOLUTION_LEVEL_FINE = 2;
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700108 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700110 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400111 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700112 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
113
114 private static final String NETWORK_LOCATION_SERVICE_ACTION =
115 "com.android.location.service.v2.NetworkLocationProvider";
116 private static final String FUSED_LOCATION_SERVICE_ACTION =
117 "com.android.location.service.FusedLocationProvider";
118
119 private static final int MSG_LOCATION_CHANGED = 1;
120
Nick Pellyf1be6862012-05-15 10:53:42 -0700121 // Location Providers may sometimes deliver location updates
122 // slightly faster that requested - provide grace period so
123 // we don't unnecessarily filter events that are otherwise on
124 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700125 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700126
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700127 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
128
129 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800130 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700131
132 // used internally for synchronization
133 private final Object mLock = new Object();
134
Victoria Lease5cd731a2012-12-19 15:04:21 -0800135 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700136 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700137 private GeofenceManager mGeofenceManager;
138 private PowerManager.WakeLock mWakeLock;
139 private PackageManager mPackageManager;
140 private GeocoderProxy mGeocodeProvider;
141 private IGpsStatusProvider mGpsStatusProvider;
142 private INetInitiatedListener mNetInitiatedListener;
143 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700144 private PassiveProvider mPassiveProvider; // track passive provider for special cases
145 private LocationBlacklist mBlacklist;
Victoria Lease5cd731a2012-12-19 15:04:21 -0800146 private HandlerThread mHandlerThread;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700147
148 // --- fields below are protected by mWakeLock ---
149 private int mPendingBroadcasts;
150
151 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 // Set of providers that are explicitly enabled
153 private final Set<String> mEnabledProviders = new HashSet<String>();
154
155 // Set of providers that are explicitly disabled
156 private final Set<String> mDisabledProviders = new HashSet<String>();
157
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700158 // Mock (test) providers
159 private final HashMap<String, MockProvider> mMockProviders =
160 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700162 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400163 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500166 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400168
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700169 // real providers, saved here when mocked out
170 private final HashMap<String, LocationProviderInterface> mRealProviders =
171 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700173 // mapping from provider name to provider
174 private final HashMap<String, LocationProviderInterface> mProvidersByName =
175 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700177 // mapping from provider name to all its UpdateRecords
178 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
179 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700180
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700181 // mapping from provider name to last known location
182 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700184 // all providers that operate over proxy, for authorizing incoming location
185 private final ArrayList<LocationProviderProxy> mProxyProviders =
186 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Victoria Lease38389b62012-09-30 11:44:22 -0700188 // current active user on the device - other users are denied location data
189 private int mCurrentUserId = UserHandle.USER_OWNER;
190
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700191 public LocationManagerService(Context context) {
192 super();
193 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800194 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800195
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700196 if (D) Log.d(TAG, "Constructed");
197
198 // most startup is deferred until systemReady()
199 }
200
201 public void systemReady() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700202 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800203 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700204
Victoria Lease5cd731a2012-12-19 15:04:21 -0800205 // fetch package manager
206 mPackageManager = mContext.getPackageManager();
207
208 // prepare wake lock
209 PowerManager powerManager =
210 (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
211 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
212
213 // prepare worker thread
214 mHandlerThread = new HandlerThread(THREAD_NAME, Process.THREAD_PRIORITY_BACKGROUND);
215 mHandlerThread.start();
216 mLocationHandler = new LocationWorkerHandler(mHandlerThread.getLooper());
217
218 // prepare mLocationHandler's dependents
219 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
220 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
221 mBlacklist.init();
222 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
223
Dianne Hackbornc2293022013-02-06 23:14:49 -0800224 // Monitor for app ops mode changes.
225 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
226 public void opChanged(int op, String packageName) {
227 synchronized (mLock) {
228 applyAllProviderRequirementsLocked();
229 }
230 }
231 };
232 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
233
Victoria Lease5cd731a2012-12-19 15:04:21 -0800234 // prepare providers
235 loadProvidersLocked();
236 updateProvidersLocked();
237 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700238
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700239 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700240 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700241 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700242 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800243 @Override
244 public void onChange(boolean selfChange) {
245 synchronized (mLock) {
246 updateProvidersLocked();
247 }
248 }
249 }, UserHandle.USER_ALL);
250 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700251
Victoria Lease38389b62012-09-30 11:44:22 -0700252 // listen for user change
253 IntentFilter intentFilter = new IntentFilter();
254 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
255
256 mContext.registerReceiverAsUser(new BroadcastReceiver() {
257 @Override
258 public void onReceive(Context context, Intent intent) {
259 String action = intent.getAction();
260 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
261 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
262 }
263 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800264 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700265 }
266
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500267 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
268 PackageManager pm = mContext.getPackageManager();
269 String systemPackageName = mContext.getPackageName();
270 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
271
272 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
273 new Intent(FUSED_LOCATION_SERVICE_ACTION),
274 PackageManager.GET_META_DATA, mCurrentUserId);
275 for (ResolveInfo rInfo : rInfos) {
276 String packageName = rInfo.serviceInfo.packageName;
277
278 // Check that the signature is in the list of supported sigs. If it's not in
279 // this list the standard provider binding logic won't bind to it.
280 try {
281 PackageInfo pInfo;
282 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
283 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
284 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
285 ", but has wrong signature, ignoring");
286 continue;
287 }
288 } catch (NameNotFoundException e) {
289 Log.e(TAG, "missing package: " + packageName);
290 continue;
291 }
292
293 // Get the version info
294 if (rInfo.serviceInfo.metaData == null) {
295 Log.w(TAG, "Found fused provider without metadata: " + packageName);
296 continue;
297 }
298
299 int version = rInfo.serviceInfo.metaData.getInt(
300 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
301 if (version == 0) {
302 // This should be the fallback fused location provider.
303
304 // Make sure it's in the system partition.
305 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
306 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
307 continue;
308 }
309
310 // Check that the fallback is signed the same as the OS
311 // as a proxy for coreApp="true"
312 if (pm.checkSignatures(systemPackageName, packageName)
313 != PackageManager.SIGNATURE_MATCH) {
314 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
315 + packageName);
316 continue;
317 }
318
319 // Found a valid fallback.
320 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
321 return;
322 } else {
323 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
324 }
325 }
326
327 throw new IllegalStateException("Unable to find a fused location provider that is in the "
328 + "system partition with version 0 and signed with the platform certificate. "
329 + "Such a package is needed to provide a default fused location provider in the "
330 + "event that no other fused location provider has been installed or is currently "
331 + "available. For example, coreOnly boot mode when decrypting the data "
332 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
333 }
334
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700335 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700336 // create a passive location provider, which is always enabled
337 PassiveProvider passiveProvider = new PassiveProvider(this);
338 addProviderLocked(passiveProvider);
339 mEnabledProviders.add(passiveProvider.getName());
340 mPassiveProvider = passiveProvider;
341
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700342 if (GpsLocationProvider.isSupported()) {
343 // Create a gps location provider
Victoria Lease5cd731a2012-12-19 15:04:21 -0800344 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
345 mLocationHandler.getLooper());
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700346 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
347 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
348 addProviderLocked(gpsProvider);
349 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
350 }
351
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700352 /*
353 Load package name(s) containing location provider support.
354 These packages can contain services implementing location providers:
355 Geocoder Provider, Network Location Provider, and
356 Fused Location Provider. They will each be searched for
357 service components implementing these providers.
358 The location framework also has support for installation
359 of new location providers at run-time. The new package does not
360 have to be explicitly listed here, however it must have a signature
361 that matches the signature of at least one package on this list.
362 */
363 Resources resources = mContext.getResources();
364 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500365 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700366 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500367 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
368 Arrays.toString(pkgs));
369 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
370
371 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700372
373 // bind to network provider
374 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
375 mContext,
376 LocationManager.NETWORK_PROVIDER,
377 NETWORK_LOCATION_SERVICE_ACTION,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800378 providerPackageNames, 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,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800392 providerPackageNames, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700393 if (fusedLocationProvider != null) {
394 addProviderLocked(fusedLocationProvider);
395 mProxyProviders.add(fusedLocationProvider);
396 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700397 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700398 } else {
399 Slog.e(TAG, "no fused location provider found",
400 new IllegalStateException("Location service needs a fused location provider"));
401 }
402
403 // bind to geocoder provider
Victoria Leaseb711d572012-10-02 13:14:11 -0700404 mGeocodeProvider = GeocoderProxy.createAndBind(mContext, providerPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800405 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700406 if (mGeocodeProvider == null) {
407 Slog.e(TAG, "no geocoder provider found");
408 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700409 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700412 * Called when the device's active user changes.
413 * @param userId the new active user's UserId
414 */
415 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700416 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800417 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700418 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700419 mLastLocation.clear();
420 for (LocationProviderInterface p : mProviders) {
421 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700422 }
Victoria Lease38389b62012-09-30 11:44:22 -0700423 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700424 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700425 }
426 }
427
428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
430 * location updates.
431 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700432 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700433 final int mUid; // uid of receiver
434 final int mPid; // pid of receiver
435 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700436 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 final ILocationListener mListener;
439 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700441
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400442 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700443
Mike Lockwood48f17512009-04-23 09:12:08 -0700444 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700446 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
447 String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700450 if (listener != null) {
451 mKey = listener.asBinder();
452 } else {
453 mKey = intent;
454 }
Victoria Lease37425c32012-10-16 16:08:48 -0700455 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700456 mUid = uid;
457 mPid = pid;
458 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460
461 @Override
462 public boolean equals(Object otherObj) {
463 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700464 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466 return false;
467 }
468
469 @Override
470 public int hashCode() {
471 return mKey.hashCode();
472 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 @Override
475 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700476 StringBuilder s = new StringBuilder();
477 s.append("Reciever[");
478 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700480 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700482 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700484 for (String p : mUpdateRecords.keySet()) {
485 s.append(" ").append(mUpdateRecords.get(p).toString());
486 }
487 s.append("]");
488 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490
491 public boolean isListener() {
492 return mListener != null;
493 }
494
495 public boolean isPendingIntent() {
496 return mPendingIntent != null;
497 }
498
499 public ILocationListener getListener() {
500 if (mListener != null) {
501 return mListener;
502 }
503 throw new IllegalStateException("Request for non-existent listener");
504 }
505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
507 if (mListener != null) {
508 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700509 synchronized (this) {
510 // synchronize to ensure incrementPendingBroadcastsLocked()
511 // is called before decrementPendingBroadcasts()
512 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700513 // call this after broadcasting so we do not increment
514 // if we throw an exeption.
515 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 } catch (RemoteException e) {
518 return false;
519 }
520 } else {
521 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800522 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
524 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700525 synchronized (this) {
526 // synchronize to ensure incrementPendingBroadcastsLocked()
527 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700528 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700529 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700530 // call this after broadcasting so we do not increment
531 // if we throw an exeption.
532 incrementPendingBroadcastsLocked();
533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 } catch (PendingIntent.CanceledException e) {
535 return false;
536 }
537 }
538 return true;
539 }
540
541 public boolean callLocationChangedLocked(Location location) {
542 if (mListener != null) {
543 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700544 synchronized (this) {
545 // synchronize to ensure incrementPendingBroadcastsLocked()
546 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800547 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700548 // call this after broadcasting so we do not increment
549 // if we throw an exeption.
550 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 } catch (RemoteException e) {
553 return false;
554 }
555 } else {
556 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800557 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700559 synchronized (this) {
560 // synchronize to ensure incrementPendingBroadcastsLocked()
561 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700562 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700563 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700564 // call this after broadcasting so we do not increment
565 // if we throw an exeption.
566 incrementPendingBroadcastsLocked();
567 }
568 } catch (PendingIntent.CanceledException e) {
569 return false;
570 }
571 }
572 return true;
573 }
574
575 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
576 if (mListener != null) {
577 try {
578 synchronized (this) {
579 // synchronize to ensure incrementPendingBroadcastsLocked()
580 // is called before decrementPendingBroadcasts()
581 if (enabled) {
582 mListener.onProviderEnabled(provider);
583 } else {
584 mListener.onProviderDisabled(provider);
585 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700586 // call this after broadcasting so we do not increment
587 // if we throw an exeption.
588 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700589 }
590 } catch (RemoteException e) {
591 return false;
592 }
593 } else {
594 Intent providerIntent = new Intent();
595 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
596 try {
597 synchronized (this) {
598 // synchronize to ensure incrementPendingBroadcastsLocked()
599 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700600 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700601 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700602 // call this after broadcasting so we do not increment
603 // if we throw an exeption.
604 incrementPendingBroadcastsLocked();
605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 } catch (PendingIntent.CanceledException e) {
607 return false;
608 }
609 }
610 return true;
611 }
612
Nick Pellyf1be6862012-05-15 10:53:42 -0700613 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700615 if (D) Log.d(TAG, "Location listener died");
616
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400617 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 removeUpdatesLocked(this);
619 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700620 synchronized (this) {
621 if (mPendingBroadcasts > 0) {
622 LocationManagerService.this.decrementPendingBroadcasts();
623 mPendingBroadcasts = 0;
624 }
625 }
626 }
627
Nick Pellye0fd6932012-07-11 10:26:13 -0700628 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700629 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
630 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400631 synchronized (this) {
632 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700633 }
634 }
635
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400636 // this must be called while synchronized by caller in a synchronized block
637 // containing the sending of the broadcaset
638 private void incrementPendingBroadcastsLocked() {
639 if (mPendingBroadcasts++ == 0) {
640 LocationManagerService.this.incrementPendingBroadcasts();
641 }
642 }
643
644 private void decrementPendingBroadcastsLocked() {
645 if (--mPendingBroadcasts == 0) {
646 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700647 }
648 }
649 }
650
Nick Pellye0fd6932012-07-11 10:26:13 -0700651 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700652 public void locationCallbackFinished(ILocationListener listener) {
Joshua Bartel080b61b2009-10-05 12:44:46 -0400653 //Do not use getReceiver here as that will add the ILocationListener to
654 //the receiver list if it is not found. If it is not found then the
655 //LocationListener was removed when it had a pending broadcast and should
656 //not be added back.
657 IBinder binder = listener.asBinder();
658 Receiver receiver = mReceivers.get(binder);
Mike Lockwood48f17512009-04-23 09:12:08 -0700659 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400660 synchronized (receiver) {
661 // so wakelock calls will succeed
662 long identity = Binder.clearCallingIdentity();
663 receiver.decrementPendingBroadcastsLocked();
664 Binder.restoreCallingIdentity(identity);
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667 }
668
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700669 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400670 mProviders.add(provider);
671 mProvidersByName.put(provider.getName(), provider);
672 }
673
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700674 private void removeProviderLocked(LocationProviderInterface provider) {
675 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400676 mProviders.remove(provider);
677 mProvidersByName.remove(provider.getName());
678 }
679
Mike Lockwood3d12b512009-04-21 23:25:35 -0700680
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800681 /**
682 * Returns "true" if access to the specified location provider is allowed by the current user's
683 * settings. Access to all location providers is forbidden to non-location-provider processes
684 * belonging to background users.
685 *
686 * @param provider the name of the location provider
687 * @param uid the requestor's UID
688 * @return
689 */
690 private boolean isAllowedBySettingsLocked(String provider, int uid) {
691 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700692 return false;
693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 if (mEnabledProviders.contains(provider)) {
695 return true;
696 }
697 if (mDisabledProviders.contains(provider)) {
698 return false;
699 }
700 // Use system settings
701 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702
Victoria Leaseb711d572012-10-02 13:14:11 -0700703 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700706 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700707 * Returns the permission string associated with the specified resolution level.
708 *
709 * @param resolutionLevel the resolution level
710 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700711 */
Victoria Lease37425c32012-10-16 16:08:48 -0700712 private String getResolutionPermission(int resolutionLevel) {
713 switch (resolutionLevel) {
714 case RESOLUTION_LEVEL_FINE:
715 return android.Manifest.permission.ACCESS_FINE_LOCATION;
716 case RESOLUTION_LEVEL_COARSE:
717 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
718 default:
719 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700721 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700722
Victoria Leaseda479c52012-10-15 15:24:16 -0700723 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700724 * Returns the resolution level allowed to the given PID/UID pair.
725 *
726 * @param pid the PID
727 * @param uid the UID
728 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700729 */
Victoria Lease37425c32012-10-16 16:08:48 -0700730 private int getAllowedResolutionLevel(int pid, int uid) {
731 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
732 pid, uid) == PackageManager.PERMISSION_GRANTED) {
733 return RESOLUTION_LEVEL_FINE;
734 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
735 pid, uid) == PackageManager.PERMISSION_GRANTED) {
736 return RESOLUTION_LEVEL_COARSE;
737 } else {
738 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700739 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700740 }
741
742 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700743 * Returns the resolution level allowed to the caller
744 *
745 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700746 */
Victoria Lease37425c32012-10-16 16:08:48 -0700747 private int getCallerAllowedResolutionLevel() {
748 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
749 }
750
751 /**
752 * Throw SecurityException if specified resolution level is insufficient to use geofences.
753 *
754 * @param allowedResolutionLevel resolution level allowed to caller
755 */
756 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
757 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700758 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761
Victoria Lease37425c32012-10-16 16:08:48 -0700762 /**
763 * Return the minimum resolution level required to use the specified location provider.
764 *
765 * @param provider the name of the location provider
766 * @return minimum resolution level required for provider
767 */
768 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700769 if (LocationManager.GPS_PROVIDER.equals(provider) ||
770 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
771 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700772 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700773 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
774 LocationManager.FUSED_PROVIDER.equals(provider)) {
775 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700776 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700777 } else {
778 // mock providers
779 LocationProviderInterface lp = mMockProviders.get(provider);
780 if (lp != null) {
781 ProviderProperties properties = lp.getProperties();
782 if (properties != null) {
783 if (properties.mRequiresSatellite) {
784 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700785 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700786 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
787 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700788 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700789 }
790 }
791 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700792 }
Victoria Lease37425c32012-10-16 16:08:48 -0700793 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700794 }
795
Victoria Lease37425c32012-10-16 16:08:48 -0700796 /**
797 * Throw SecurityException if specified resolution level is insufficient to use the named
798 * location provider.
799 *
800 * @param allowedResolutionLevel resolution level allowed to caller
801 * @param providerName the name of the location provider
802 */
803 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
804 String providerName) {
805 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
806 if (allowedResolutionLevel < requiredResolutionLevel) {
807 switch (requiredResolutionLevel) {
808 case RESOLUTION_LEVEL_FINE:
809 throw new SecurityException("\"" + providerName + "\" location provider " +
810 "requires ACCESS_FINE_LOCATION permission.");
811 case RESOLUTION_LEVEL_COARSE:
812 throw new SecurityException("\"" + providerName + "\" location provider " +
813 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
814 default:
815 throw new SecurityException("Insufficient permission for \"" + providerName +
816 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700817 }
818 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700819 }
820
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800821 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800822 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
823 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800824 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800825 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800826 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800827 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800828 }
829 return -1;
830 }
831
832 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
833 int op = resolutionLevelToOp(allowedResolutionLevel);
834 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800835 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
836 return false;
837 }
838 }
839 return true;
840 }
841
842 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800843 int op = resolutionLevelToOp(allowedResolutionLevel);
844 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800845 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
846 return false;
847 }
848 }
849 return true;
850 }
851
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700852 /**
853 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700854 * fused, also including ones that are not permitted to
855 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700856 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700857 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700859 ArrayList<String> out;
860 synchronized (mLock) {
861 out = new ArrayList<String>(mProviders.size());
862 for (LocationProviderInterface provider : mProviders) {
863 String name = provider.getName();
864 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700865 continue;
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 out.add(name);
868 }
869 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700870
871 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 return out;
873 }
874
Mike Lockwood03ca2162010-04-01 08:10:09 -0700875 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700876 * Return all providers by name, that match criteria and are optionally
877 * enabled.
878 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700879 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700880 @Override
881 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -0700882 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700883 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800884 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -0700885 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -0700886 try {
887 synchronized (mLock) {
888 out = new ArrayList<String>(mProviders.size());
889 for (LocationProviderInterface provider : mProviders) {
890 String name = provider.getName();
891 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700892 continue;
893 }
Victoria Lease37425c32012-10-16 16:08:48 -0700894 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800895 if (enabledOnly && !isAllowedBySettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700896 continue;
897 }
898 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
899 name, provider.getProperties(), criteria)) {
900 continue;
901 }
902 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700903 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700904 }
Mike Lockwood03ca2162010-04-01 08:10:09 -0700905 }
Victoria Leaseb711d572012-10-02 13:14:11 -0700906 } finally {
907 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700908 }
909
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700910 if (D) Log.d(TAG, "getProviders()=" + out);
911 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700912 }
913
914 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700915 * Return the name of the best provider given a Criteria object.
916 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -0700917 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700918 * has been deprecated as well. So this method now uses
919 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700920 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700921 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700922 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700923 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700924
925 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700926 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700927 result = pickBest(providers);
928 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
929 return result;
930 }
931 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700932 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700933 result = pickBest(providers);
934 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
935 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700936 }
937
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700938 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700939 return null;
940 }
941
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700942 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -0700943 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700944 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -0700945 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
946 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700947 } else {
948 return providers.get(0);
949 }
950 }
951
Nick Pellye0fd6932012-07-11 10:26:13 -0700952 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700953 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
954 LocationProviderInterface p = mProvidersByName.get(provider);
955 if (p == null) {
956 throw new IllegalArgumentException("provider=" + provider);
957 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700958
959 boolean result = LocationProvider.propertiesMeetCriteria(
960 p.getName(), p.getProperties(), criteria);
961 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
962 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700963 }
964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700966 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400967 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500968 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 boolean isEnabled = p.isEnabled();
970 String name = p.getName();
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800971 boolean shouldBeEnabled = isAllowedBySettingsLocked(name,
972 UserHandle.getUid(mCurrentUserId, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700974 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700975 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700977 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700978 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700980 }
981 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700982 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
983 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 }
986
Victoria Leaseb711d572012-10-02 13:14:11 -0700987 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 int listeners = 0;
989
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500990 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700991 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992
993 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -0700994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
996 if (records != null) {
997 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700998 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001000 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001001 // Sends a notification message to the receiver
1002 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1003 if (deadReceivers == null) {
1004 deadReceivers = new ArrayList<Receiver>();
1005 }
1006 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001008 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 }
1011 }
1012
1013 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001014 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 removeUpdatesLocked(deadReceivers.get(i));
1016 }
1017 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 if (enabled) {
1020 p.enable();
1021 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001022 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001029 private void applyRequirementsLocked(String provider) {
1030 LocationProviderInterface p = mProvidersByName.get(provider);
1031 if (p == null) return;
1032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001034 WorkSource worksource = new WorkSource();
1035 ProviderRequest providerRequest = new ProviderRequest();
1036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001038 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001039 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001040 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1041 record.mReceiver.mAllowedResolutionLevel)) {
1042 LocationRequest locationRequest = record.mRequest;
1043 providerRequest.locationRequests.add(locationRequest);
1044 if (locationRequest.getInterval() < providerRequest.interval) {
1045 providerRequest.reportLocation = true;
1046 providerRequest.interval = locationRequest.getInterval();
1047 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001048 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001049 }
1050 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001051
1052 if (providerRequest.reportLocation) {
1053 // calculate who to blame for power
1054 // This is somewhat arbitrary. We pick a threshold interval
1055 // that is slightly higher that the minimum interval, and
1056 // spread the blame across all applications with a request
1057 // under that threshold.
1058 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1059 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001060 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001061 LocationRequest locationRequest = record.mRequest;
1062 if (locationRequest.getInterval() <= thresholdInterval) {
Dianne Hackborn002a54e2013-01-10 17:34:55 -08001063 worksource.add(record.mReceiver.mUid, record.mReceiver.mPackageName);
Victoria Leaseb711d572012-10-02 13:14:11 -07001064 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001065 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001069
1070 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1071 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073
1074 private class UpdateRecord {
1075 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001076 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001078 Location mLastFixBroadcast;
1079 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080
1081 /**
1082 * Note: must be constructed with lock held.
1083 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001084 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001086 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088
1089 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1090 if (records == null) {
1091 records = new ArrayList<UpdateRecord>();
1092 mRecordsByProvider.put(provider, records);
1093 }
1094 if (!records.contains(this)) {
1095 records.add(this);
1096 }
1097 }
1098
1099 /**
1100 * Method to be called when a record will no longer be used. Calling this multiple times
1101 * must have the same effect as calling it once.
1102 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001103 void disposeLocked(boolean removeReceiver) {
1104 // remove from mRecordsByProvider
1105 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1106 if (globalRecords != null) {
1107 globalRecords.remove(this);
1108 }
1109
1110 if (!removeReceiver) return; // the caller will handle the rest
1111
1112 // remove from Receiver#mUpdateRecords
1113 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1114 if (receiverRecords != null) {
1115 receiverRecords.remove(this.mProvider);
1116
1117 // and also remove the Receiver if it has no more update records
1118 if (removeReceiver && receiverRecords.size() == 0) {
1119 removeUpdatesLocked(mReceiver);
1120 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123
1124 @Override
1125 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001126 StringBuilder s = new StringBuilder();
1127 s.append("UpdateRecord[");
1128 s.append(mProvider);
1129 s.append(' ').append(mReceiver.mPackageName).append('(');
1130 s.append(mReceiver.mUid).append(')');
1131 s.append(' ').append(mRequest);
1132 s.append(']');
1133 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001137 private Receiver getReceiver(ILocationListener listener, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001138 IBinder binder = listener.asBinder();
1139 Receiver receiver = mReceivers.get(binder);
1140 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001141 receiver = new Receiver(listener, null, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001142 mReceivers.put(binder, receiver);
1143
1144 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001145 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001146 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001147 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001148 return null;
1149 }
1150 }
1151 return receiver;
1152 }
1153
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001154 private Receiver getReceiver(PendingIntent intent, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001155 Receiver receiver = mReceivers.get(intent);
1156 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001157 receiver = new Receiver(null, intent, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001158 mReceivers.put(intent, receiver);
1159 }
1160 return receiver;
1161 }
1162
Victoria Lease37425c32012-10-16 16:08:48 -07001163 /**
1164 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1165 * and consistency requirements.
1166 *
1167 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001168 * @return a version of request that meets the given resolution and consistency requirements
1169 * @hide
1170 */
1171 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1172 LocationRequest sanitizedRequest = new LocationRequest(request);
1173 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1174 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001175 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001176 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001177 break;
1178 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001179 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001180 break;
1181 }
1182 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001183 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1184 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001185 }
Victoria Lease37425c32012-10-16 16:08:48 -07001186 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1187 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001188 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001189 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001190 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001191 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001192 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001193 }
Victoria Lease37425c32012-10-16 16:08:48 -07001194 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001195 }
1196
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001197 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001198 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001199 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001200 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001201 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001202 String[] packages = mPackageManager.getPackagesForUid(uid);
1203 if (packages == null) {
1204 throw new SecurityException("invalid UID " + uid);
1205 }
1206 for (String pkg : packages) {
1207 if (packageName.equals(pkg)) return;
1208 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001209 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001210 }
1211
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001212 private void checkPendingIntent(PendingIntent intent) {
1213 if (intent == null) {
1214 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001215 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001216 }
1217
1218 private Receiver checkListenerOrIntent(ILocationListener listener, PendingIntent intent,
1219 int pid, int uid, String packageName) {
1220 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001221 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001222 } else if (intent != null && listener != null) {
1223 throw new IllegalArgumentException("cannot register both listener and intent");
1224 } else if (intent != null) {
1225 checkPendingIntent(intent);
1226 return getReceiver(intent, pid, uid, packageName);
1227 } else {
1228 return getReceiver(listener, pid, uid, packageName);
1229 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001230 }
1231
Nick Pellye0fd6932012-07-11 10:26:13 -07001232 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001233 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1234 PendingIntent intent, String packageName) {
1235 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1236 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001237 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1238 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1239 request.getProvider());
1240 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001242 final int pid = Binder.getCallingPid();
1243 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001244 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 long identity = Binder.clearCallingIdentity();
1246 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001247 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1248 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001249 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001250 Receiver recevier = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1251
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001252 synchronized (mLock) {
Victoria Lease37425c32012-10-16 16:08:48 -07001253 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 } finally {
1256 Binder.restoreCallingIdentity(identity);
1257 }
1258 }
1259
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001260 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1261 int pid, int uid, String packageName) {
1262 // Figure out the provider. Either its explicitly request (legacy use cases), or
1263 // use the fused provider
1264 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1265 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001266 if (name == null) {
1267 throw new IllegalArgumentException("provider name must not be null");
1268 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001269 LocationProviderInterface provider = mProvidersByName.get(name);
1270 if (provider == null) {
1271 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1272 }
1273
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001274 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1275 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001276
1277 UpdateRecord record = new UpdateRecord(name, request, receiver);
1278 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1279 if (oldRecord != null) {
1280 oldRecord.disposeLocked(false);
1281 }
1282
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001283 boolean isProviderEnabled = isAllowedBySettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001284 if (isProviderEnabled) {
1285 applyRequirementsLocked(name);
1286 } else {
1287 // Notify the listener that updates are currently disabled
1288 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290 }
1291
Nick Pellye0fd6932012-07-11 10:26:13 -07001292 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001293 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1294 String packageName) {
1295 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001296
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001297 final int pid = Binder.getCallingPid();
1298 final int uid = Binder.getCallingUid();
1299 Receiver receiver = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1300
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001301 // providers may use public location API's, need to clear identity
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001302 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001304 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001305 removeUpdatesLocked(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001307 } finally {
1308 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310 }
1311
1312 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001313 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001314
1315 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1316 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1317 synchronized (receiver) {
1318 if (receiver.mPendingBroadcasts > 0) {
1319 decrementPendingBroadcasts();
1320 receiver.mPendingBroadcasts = 0;
1321 }
1322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001325 // Record which providers were associated with this listener
1326 HashSet<String> providers = new HashSet<String>();
1327 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1328 if (oldRecords != null) {
1329 // Call dispose() on the obsolete update records.
1330 for (UpdateRecord record : oldRecords.values()) {
1331 record.disposeLocked(false);
1332 }
1333 // Accumulate providers
1334 providers.addAll(oldRecords.keySet());
1335 }
1336
1337 // update provider
1338 for (String provider : providers) {
1339 // If provider is already disabled, don't need to do anything
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001340 if (!isAllowedBySettingsLocked(provider, UserHandle.getUid(mCurrentUserId, 0))) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001341 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001344 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346 }
1347
Dianne Hackbornc2293022013-02-06 23:14:49 -08001348 private void applyAllProviderRequirementsLocked() {
1349 for (LocationProviderInterface p : mProviders) {
1350 // If provider is already disabled, don't need to do anything
1351 if (!isAllowedBySettingsLocked(p.getName(), UserHandle.getUid(mCurrentUserId, 0))) {
1352 continue;
1353 }
1354
1355 applyRequirementsLocked(p.getName());
1356 }
1357 }
1358
Nick Pellye0fd6932012-07-11 10:26:13 -07001359 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001360 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001361 if (D) Log.d(TAG, "getLastLocation: " + request);
1362 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001363 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001364 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001365 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1366 request.getProvider());
1367 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001368
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001369 final int uid = Binder.getCallingUid();
1370 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001371 try {
1372 if (mBlacklist.isBlacklisted(packageName)) {
1373 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1374 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001375 return null;
1376 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001377
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001378 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1379 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1380 packageName);
1381 return null;
1382 }
1383
Victoria Leaseb711d572012-10-02 13:14:11 -07001384 synchronized (mLock) {
1385 // Figure out the provider. Either its explicitly request (deprecated API's),
1386 // or use the fused provider
1387 String name = request.getProvider();
1388 if (name == null) name = LocationManager.FUSED_PROVIDER;
1389 LocationProviderInterface provider = mProvidersByName.get(name);
1390 if (provider == null) return null;
1391
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001392 if (!isAllowedBySettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001393
1394 Location location = mLastLocation.get(name);
1395 if (location == null) {
1396 return null;
1397 }
Victoria Lease37425c32012-10-16 16:08:48 -07001398 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001399 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1400 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001401 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001402 }
Victoria Lease37425c32012-10-16 16:08:48 -07001403 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001404 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001405 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001406 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001407 return null;
1408 } finally {
1409 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001410 }
1411 }
1412
1413 @Override
1414 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1415 String packageName) {
1416 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001417 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1418 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001419 checkPendingIntent(intent);
1420 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001421 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1422 request.getProvider());
1423 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001424
Victoria Lease37425c32012-10-16 16:08:48 -07001425 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001426
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001427 // geo-fence manager uses the public location API, need to clear identity
1428 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001429 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1430 // temporary measure until geofences work for secondary users
1431 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1432 return;
1433 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001434 long identity = Binder.clearCallingIdentity();
1435 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001436 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1437 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001438 } finally {
1439 Binder.restoreCallingIdentity(identity);
1440 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001441 }
1442
1443 @Override
1444 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001445 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001446 checkPendingIntent(intent);
1447 checkPackageName(packageName);
1448
1449 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1450
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001451 // geo-fence manager uses the public location API, need to clear identity
1452 long identity = Binder.clearCallingIdentity();
1453 try {
1454 mGeofenceManager.removeFence(geofence, intent);
1455 } finally {
1456 Binder.restoreCallingIdentity(identity);
1457 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001458 }
1459
1460
1461 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001462 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001463 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 return false;
1465 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001466 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1467 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001468 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001470 final int uid = Binder.getCallingUid();
1471 final long ident = Binder.clearCallingIdentity();
1472 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001473 if (checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001474 return false;
1475 }
1476 } finally {
1477 Binder.restoreCallingIdentity(ident);
1478 }
1479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001481 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 return false;
1485 }
1486 return true;
1487 }
1488
Nick Pellye0fd6932012-07-11 10:26:13 -07001489 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001491 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001492 try {
1493 mGpsStatusProvider.removeGpsStatusListener(listener);
1494 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001495 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
1498 }
1499
Nick Pellye0fd6932012-07-11 10:26:13 -07001500 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001502 if (provider == null) {
1503 // throw NullPointerException to remain compatible with previous implementation
1504 throw new NullPointerException();
1505 }
Victoria Lease37425c32012-10-16 16:08:48 -07001506 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1507 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001510 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 != PackageManager.PERMISSION_GRANTED)) {
1512 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1513 }
1514
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001515 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001516 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001517 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001518
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001519 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
1521 }
1522
Nick Pellye0fd6932012-07-11 10:26:13 -07001523 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001524 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001525 if (Binder.getCallingUid() != Process.myUid()) {
1526 throw new SecurityException(
1527 "calling sendNiResponse from outside of the system is not allowed");
1528 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001529 try {
1530 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001531 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001532 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001533 return false;
1534 }
1535 }
1536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001538 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001539 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * accessed by the caller
1541 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001542 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001543 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001544 if (mProvidersByName.get(provider) == null) {
1545 return null;
1546 }
1547
Victoria Lease37425c32012-10-16 16:08:48 -07001548 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1549 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001551 LocationProviderInterface p;
1552 synchronized (mLock) {
1553 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001556 if (p == null) return null;
1557 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559
Nick Pellye0fd6932012-07-11 10:26:13 -07001560 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001562 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1563 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001564 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1565
Victoria Lease269518e2012-10-29 08:25:39 -07001566 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001567 try {
1568 synchronized (mLock) {
1569 LocationProviderInterface p = mProvidersByName.get(provider);
1570 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001571
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001572 return isAllowedBySettingsLocked(provider, UserHandle.getUid(mCurrentUserId, 0));
Victoria Leaseb711d572012-10-02 13:14:11 -07001573 }
1574 } finally {
1575 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001576 }
1577 }
1578
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001579 /**
1580 * Returns "true" if the UID belongs to a bound location provider.
1581 *
1582 * @param uid the uid
1583 * @return true if uid belongs to a bound location provider
1584 */
1585 private boolean isUidALocationProvider(int uid) {
1586 if (uid == Process.SYSTEM_UID) {
1587 return true;
1588 }
1589 if (mGeocodeProvider != null) {
1590 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1591 }
1592 for (LocationProviderProxy proxy : mProxyProviders) {
1593 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1594 }
1595 return false;
1596 }
1597
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001598 private void checkCallerIsProvider() {
1599 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1600 == PackageManager.PERMISSION_GRANTED) {
1601 return;
1602 }
1603
1604 // Previously we only used the INSTALL_LOCATION_PROVIDER
1605 // check. But that is system or signature
1606 // protection level which is not flexible enough for
1607 // providers installed oustide the system image. So
1608 // also allow providers with a UID matching the
1609 // currently bound package name
1610
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001611 if (isUidALocationProvider(Binder.getCallingUid())) {
1612 return;
1613 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001614
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001615 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1616 "or UID of a currently bound location provider");
1617 }
1618
1619 private boolean doesPackageHaveUid(int uid, String packageName) {
1620 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 return false;
1622 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001623 try {
1624 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1625 if (appInfo.uid != uid) {
1626 return false;
1627 }
1628 } catch (NameNotFoundException e) {
1629 return false;
1630 }
1631 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633
Nick Pellye0fd6932012-07-11 10:26:13 -07001634 @Override
Mike Lockwooda4903f252010-02-17 06:42:23 -05001635 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001636 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001637
Nick Pelly2eeeec22012-07-18 13:13:37 -07001638 if (!location.isComplete()) {
1639 Log.w(TAG, "Dropping incomplete location: " + location);
1640 return;
1641 }
1642
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001643 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1644 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f252010-02-17 06:42:23 -05001645 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001646 mLocationHandler.sendMessageAtFrontOfQueue(m);
1647 }
1648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649
Laurent Tu75defb62012-11-01 16:21:52 -07001650 private static boolean shouldBroadcastSafe(
1651 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 // Always broadcast the first update
1653 if (lastLoc == null) {
1654 return true;
1655 }
1656
Nick Pellyf1be6862012-05-15 10:53:42 -07001657 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001658 long minTime = record.mRequest.getFastestInterval();
Philip Milne41180122012-09-26 11:29:25 -07001659 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos()) / 1000000L;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001660 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 return false;
1662 }
1663
1664 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001665 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 if (minDistance > 0.0) {
1667 if (loc.distanceTo(lastLoc) <= minDistance) {
1668 return false;
1669 }
1670 }
1671
Laurent Tu75defb62012-11-01 16:21:52 -07001672 // Check whether sufficient number of udpates is left
1673 if (record.mRequest.getNumUpdates() <= 0) {
1674 return false;
1675 }
1676
1677 // Check whether the expiry date has passed
1678 if (record.mRequest.getExpireAt() < now) {
1679 return false;
1680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 return true;
1683 }
1684
Mike Lockwooda4903f252010-02-17 06:42:23 -05001685 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001686 if (D) Log.d(TAG, "incoming location: " + location);
1687
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001688 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f252010-02-17 06:42:23 -05001689 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690
Laurent Tu60ec50a2012-10-04 17:00:10 -07001691 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001692 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001693 if (p == null) return;
1694
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001695 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001696 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1697 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001698 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001699 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001700 lastLocation = new Location(provider);
1701 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001702 } else {
1703 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1704 if (noGPSLocation == null && lastNoGPSLocation != null) {
1705 // New location has no no-GPS location: adopt last no-GPS location. This is set
1706 // directly into location because we do not want to notify COARSE clients.
1707 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1708 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001709 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001710 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711
Laurent Tu60ec50a2012-10-04 17:00:10 -07001712 // Skip if there are no UpdateRecords for this provider.
1713 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1714 if (records == null || records.size() == 0) return;
1715
Victoria Lease09016ab2012-09-16 12:33:15 -07001716 // Fetch coarse location
1717 Location coarseLocation = null;
1718 if (noGPSLocation != null && !noGPSLocation.equals(lastNoGPSLocation)) {
1719 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1720 }
1721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 // Fetch latest status update time
1723 long newStatusUpdateTime = p.getStatusUpdateTime();
1724
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001725 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 Bundle extras = new Bundle();
1727 int status = p.getStatus(extras);
1728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001730 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001733 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001735 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001736
Victoria Lease269518e2012-10-29 08:25:39 -07001737 int receiverUserId = UserHandle.getUserId(receiver.mUid);
1738 if (receiverUserId != mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001739 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001740 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001741 " (current user: " + mCurrentUserId + ", app: " +
1742 receiver.mPackageName + ")");
1743 }
1744 continue;
1745 }
1746
Nick Pelly4035f5a2012-08-17 14:43:49 -07001747 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1748 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1749 receiver.mPackageName);
1750 continue;
1751 }
1752
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001753 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1754 receiver.mAllowedResolutionLevel)) {
1755 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1756 receiver.mPackageName);
1757 continue;
1758 }
1759
Victoria Lease09016ab2012-09-16 12:33:15 -07001760 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001761 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1762 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001763 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001764 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001765 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001766 if (notifyLocation != null) {
1767 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001768 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001769 if (lastLoc == null) {
1770 lastLoc = new Location(notifyLocation);
1771 r.mLastFixBroadcast = lastLoc;
1772 } else {
1773 lastLoc.set(notifyLocation);
1774 }
1775 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1776 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1777 receiverDead = true;
1778 }
Laurent Tu75defb62012-11-01 16:21:52 -07001779 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
1781 }
1782
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001783 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001785 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001787 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001789 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001790 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001791 }
1792 }
1793
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001794 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001795 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001796 if (deadUpdateRecords == null) {
1797 deadUpdateRecords = new ArrayList<UpdateRecord>();
1798 }
1799 deadUpdateRecords.add(r);
1800 }
1801 // track dead receivers
1802 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001803 if (deadReceivers == null) {
1804 deadReceivers = new ArrayList<Receiver>();
1805 }
1806 if (!deadReceivers.contains(receiver)) {
1807 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
1809 }
1810 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001811
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001812 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001814 for (Receiver receiver : deadReceivers) {
1815 removeUpdatesLocked(receiver);
1816 }
1817 }
1818 if (deadUpdateRecords != null) {
1819 for (UpdateRecord r : deadUpdateRecords) {
1820 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
Victoria Lease8b38b292012-12-04 15:04:43 -08001822 applyRequirementsLocked(provider);
1823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825
1826 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08001827 public LocationWorkerHandler(Looper looper) {
1828 super(looper, null, true);
1829 }
1830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 @Override
1832 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001833 switch (msg.what) {
1834 case MSG_LOCATION_CHANGED:
1835 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
1836 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838 }
1839 }
1840
Victoria Lease54ca7aef22013-01-08 09:39:50 -08001841 private boolean isMockProvider(String provider) {
1842 synchronized (mLock) {
1843 return mMockProviders.containsKey(provider);
1844 }
1845 }
1846
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001847 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7aef22013-01-08 09:39:50 -08001848 // create a working copy of the incoming Location so that the service can modify it without
1849 // disturbing the caller's copy
1850 Location myLocation = new Location(location);
1851 String provider = myLocation.getProvider();
1852
1853 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
1854 // bit if location did not come from a mock provider because passive/fused providers can
1855 // forward locations from mock providers, and should not grant them legitimacy in doing so.
1856 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
1857 myLocation.setIsFromMockProvider(true);
1858 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08001859
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001860 if (!passive) {
1861 // notify passive provider of the new location
Victoria Lease54ca7aef22013-01-08 09:39:50 -08001862 mPassiveProvider.updateLocation(myLocation);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001865 synchronized (mLock) {
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001866 if (isAllowedBySettingsLocked(provider, UserHandle.getUid(mCurrentUserId, 0))) {
Victoria Lease54ca7aef22013-01-08 09:39:50 -08001867 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871
Mike Lockwoode97ae402010-09-29 15:23:46 -04001872 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
1873 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001874 public void onPackageDisappeared(String packageName, int reason) {
1875 // remove all receivers associated with this package name
1876 synchronized (mLock) {
1877 ArrayList<Receiver> deadReceivers = null;
1878
1879 for (Receiver receiver : mReceivers.values()) {
1880 if (receiver.mPackageName.equals(packageName)) {
1881 if (deadReceivers == null) {
1882 deadReceivers = new ArrayList<Receiver>();
1883 }
1884 deadReceivers.add(receiver);
1885 }
1886 }
1887
1888 // perform removal outside of mReceivers loop
1889 if (deadReceivers != null) {
1890 for (Receiver receiver : deadReceivers) {
1891 removeUpdatesLocked(receiver);
1892 }
1893 }
1894 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001895 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04001896 };
1897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 // Wake locks
1899
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001900 private void incrementPendingBroadcasts() {
1901 synchronized (mWakeLock) {
1902 if (mPendingBroadcasts++ == 0) {
1903 try {
1904 mWakeLock.acquire();
1905 log("Acquired wakelock");
1906 } catch (Exception e) {
1907 // This is to catch a runtime exception thrown when we try to release an
1908 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001909 Slog.e(TAG, "exception in acquireWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001910 }
1911 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001912 }
1913 }
1914
1915 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001916 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001917 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001918 try {
1919 // Release wake lock
1920 if (mWakeLock.isHeld()) {
1921 mWakeLock.release();
1922 log("Released wakelock");
1923 } else {
1924 log("Can't release wakelock again!");
1925 }
1926 } catch (Exception e) {
1927 // This is to catch a runtime exception thrown when we try to release an
1928 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001929 Slog.e(TAG, "exception in releaseWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001930 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001931 }
1932 }
1933 }
1934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 // Geocoder
1936
Nick Pellye0fd6932012-07-11 10:26:13 -07001937 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04001938 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07001939 return mGeocodeProvider != null;
1940 }
1941
Nick Pellye0fd6932012-07-11 10:26:13 -07001942 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001944 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001945 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001946 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
1947 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001949 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
1951
Mike Lockwooda55c3212009-04-15 11:10:11 -04001952
Nick Pellye0fd6932012-07-11 10:26:13 -07001953 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001955 double lowerLeftLatitude, double lowerLeftLongitude,
1956 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001957 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001958
1959 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001960 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1961 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1962 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001964 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966
1967 // Mock Providers
1968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 private void checkMockPermissionsSafe() {
1970 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1971 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1972 if (!allowMocks) {
1973 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1974 }
1975
1976 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1977 PackageManager.PERMISSION_GRANTED) {
1978 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07001979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
1981
Nick Pellye0fd6932012-07-11 10:26:13 -07001982 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001983 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 checkMockPermissionsSafe();
1985
Mike Lockwooda4903f252010-02-17 06:42:23 -05001986 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
1987 throw new IllegalArgumentException("Cannot mock the passive location provider");
1988 }
1989
Mike Lockwood86328a92009-10-23 08:38:25 -04001990 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001991 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001992 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07001993 // remove the real provider if we are replacing GPS or network provider
1994 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07001995 || LocationManager.NETWORK_PROVIDER.equals(name)
1996 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001997 LocationProviderInterface p = mProvidersByName.get(name);
1998 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001999 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002000 }
2001 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002002 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2004 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002005 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002006 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002007 mLastLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 updateProvidersLocked();
2009 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002010 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
2012
Nick Pellye0fd6932012-07-11 10:26:13 -07002013 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 public void removeTestProvider(String provider) {
2015 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002016 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002017 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002018 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2020 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002021 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002022 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002023
2024 // reinstate real provider if available
2025 LocationProviderInterface realProvider = mRealProviders.get(provider);
2026 if (realProvider != null) {
2027 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002028 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002029 mLastLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002031 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
2033 }
2034
Nick Pellye0fd6932012-07-11 10:26:13 -07002035 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 public void setTestProviderLocation(String provider, Location loc) {
2037 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002038 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002039 MockProvider mockProvider = mMockProviders.get(provider);
2040 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2042 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002043 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2044 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002045 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002046 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048 }
2049
Nick Pellye0fd6932012-07-11 10:26:13 -07002050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 public void clearTestProviderLocation(String provider) {
2052 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002053 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002054 MockProvider mockProvider = mMockProviders.get(provider);
2055 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2057 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002058 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
2060 }
2061
Nick Pellye0fd6932012-07-11 10:26:13 -07002062 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 public void setTestProviderEnabled(String provider, boolean enabled) {
2064 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002065 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002066 MockProvider mockProvider = mMockProviders.get(provider);
2067 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2069 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002070 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002072 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 mEnabledProviders.add(provider);
2074 mDisabledProviders.remove(provider);
2075 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002076 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 mEnabledProviders.remove(provider);
2078 mDisabledProviders.add(provider);
2079 }
2080 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002081 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 }
2084
Nick Pellye0fd6932012-07-11 10:26:13 -07002085 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 public void clearTestProviderEnabled(String provider) {
2087 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002088 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002089 MockProvider mockProvider = mMockProviders.get(provider);
2090 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2092 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002093 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 mEnabledProviders.remove(provider);
2095 mDisabledProviders.remove(provider);
2096 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002097 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099 }
2100
Nick Pellye0fd6932012-07-11 10:26:13 -07002101 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2103 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002104 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002105 MockProvider mockProvider = mMockProviders.get(provider);
2106 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2108 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002109 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111 }
2112
Nick Pellye0fd6932012-07-11 10:26:13 -07002113 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 public void clearTestProviderStatus(String provider) {
2115 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002116 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002117 MockProvider mockProvider = mMockProviders.get(provider);
2118 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2120 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002121 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123 }
2124
2125 private void log(String log) {
2126 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002127 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002130
2131 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2133 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2134 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002135 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 + Binder.getCallingPid()
2137 + ", uid=" + Binder.getCallingUid());
2138 return;
2139 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002140
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002141 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002144 for (Receiver receiver : mReceivers.values()) {
2145 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002148 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2149 pw.println(" " + entry.getKey() + ":");
2150 for (UpdateRecord record : entry.getValue()) {
2151 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 }
2153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002155 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2156 String provider = entry.getKey();
2157 Location location = entry.getValue();
2158 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002160
Nick Pellye0fd6932012-07-11 10:26:13 -07002161 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 if (mEnabledProviders.size() > 0) {
2164 pw.println(" Enabled Providers:");
2165 for (String i : mEnabledProviders) {
2166 pw.println(" " + i);
2167 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 if (mDisabledProviders.size() > 0) {
2171 pw.println(" Disabled Providers:");
2172 for (String i : mDisabledProviders) {
2173 pw.println(" " + i);
2174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002176 pw.append(" ");
2177 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 if (mMockProviders.size() > 0) {
2179 pw.println(" Mock Providers:");
2180 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002181 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002184
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002185 pw.append(" fudger: ");
2186 mLocationFudger.dump(fd, pw, args);
2187
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002188 if (args.length > 0 && "short".equals(args[0])) {
2189 return;
2190 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002191 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002192 pw.print(provider.getName() + " Internal State");
2193 if (provider instanceof LocationProviderProxy) {
2194 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2195 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002196 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002197 pw.println(":");
2198 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
2201 }
2202}