blob: 1e1cf5a548cb1d9d10eb4b1206759a5eef16164e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070021import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070025import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070026import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050027import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070029import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050030import android.content.pm.ResolveInfo;
31import android.content.pm.Signature;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050032import android.content.res.Resources;
Brian Muramatsubb95cb92012-08-29 10:43:21 -070033import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.location.Address;
Mike Lockwood03ca2162010-04-01 08:10:09 -070035import android.location.Criteria;
Mike Lockwood34901402010-01-04 12:14:21 -050036import android.location.GeocoderParams;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070037import android.location.Geofence;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040039import android.location.IGpsStatusProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.location.ILocationListener;
41import android.location.ILocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040042import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.location.Location;
44import android.location.LocationManager;
45import android.location.LocationProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070046import android.location.LocationRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
48import android.os.Bundle;
49import android.os.Handler;
Victoria Lease5cd731a2012-12-19 15:04:21 -080050import android.os.HandlerThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070052import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Message;
54import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070055import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070057import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070058import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070059import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080062import android.util.Slog;
Mike Lockwoode97ae402010-09-29 15:23:46 -040063import com.android.internal.content.PackageMonitor;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070064import com.android.internal.location.ProviderProperties;
65import com.android.internal.location.ProviderRequest;
Mike Lockwood43e33f22010-03-26 10:41:48 -040066import com.android.server.location.GeocoderProxy;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070067import com.android.server.location.GeofenceProxy;
Nick Pellye0fd6932012-07-11 10:26:13 -070068import com.android.server.location.GeofenceManager;
Mike Lockwood43e33f22010-03-26 10:41:48 -040069import com.android.server.location.GpsLocationProvider;
Nick Pelly4035f5a2012-08-17 14:43:49 -070070import com.android.server.location.LocationBlacklist;
Nick Pelly74fa7ea2012-08-13 19:36:38 -070071import com.android.server.location.LocationFudger;
Mike Lockwood43e33f22010-03-26 10:41:48 -040072import com.android.server.location.LocationProviderInterface;
73import com.android.server.location.LocationProviderProxy;
74import com.android.server.location.MockProvider;
75import com.android.server.location.PassiveProvider;
76
77import java.io.FileDescriptor;
78import java.io.PrintWriter;
79import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070080import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040081import java.util.HashMap;
82import java.util.HashSet;
83import java.util.List;
84import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -040085import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * The service class that manages LocationProviders and issues location
89 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Victoria Lease5cd731a2012-12-19 15:04:21 -080091public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -080093 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070094
95 private static final String WAKELOCK_KEY = TAG;
96 private static final String THREAD_NAME = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Victoria Lease37425c32012-10-16 16:08:48 -070098 // Location resolution level: no location data whatsoever
99 private static final int RESOLUTION_LEVEL_NONE = 0;
100 // Location resolution level: coarse location data only
101 private static final int RESOLUTION_LEVEL_COARSE = 1;
102 // Location resolution level: fine location data
103 private static final int RESOLUTION_LEVEL_FINE = 2;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700106 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700108 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400109 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700110 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
111
112 private static final String NETWORK_LOCATION_SERVICE_ACTION =
113 "com.android.location.service.v2.NetworkLocationProvider";
114 private static final String FUSED_LOCATION_SERVICE_ACTION =
115 "com.android.location.service.FusedLocationProvider";
116
117 private static final int MSG_LOCATION_CHANGED = 1;
118
Nick Pellyf1be6862012-05-15 10:53:42 -0700119 // Location Providers may sometimes deliver location updates
120 // slightly faster that requested - provide grace period so
121 // we don't unnecessarily filter events that are otherwise on
122 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700123 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700124
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700125 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
126
127 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800128 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700129
130 // used internally for synchronization
131 private final Object mLock = new Object();
132
Victoria Lease5cd731a2012-12-19 15:04:21 -0800133 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700134 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700135 private GeofenceManager mGeofenceManager;
136 private PowerManager.WakeLock mWakeLock;
137 private PackageManager mPackageManager;
138 private GeocoderProxy mGeocodeProvider;
139 private IGpsStatusProvider mGpsStatusProvider;
140 private INetInitiatedListener mNetInitiatedListener;
141 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700142 private PassiveProvider mPassiveProvider; // track passive provider for special cases
143 private LocationBlacklist mBlacklist;
Victoria Lease5cd731a2012-12-19 15:04:21 -0800144 private HandlerThread mHandlerThread;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700145
146 // --- fields below are protected by mWakeLock ---
147 private int mPendingBroadcasts;
148
149 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // Set of providers that are explicitly enabled
151 private final Set<String> mEnabledProviders = new HashSet<String>();
152
153 // Set of providers that are explicitly disabled
154 private final Set<String> mDisabledProviders = new HashSet<String>();
155
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700156 // Mock (test) providers
157 private final HashMap<String, MockProvider> mMockProviders =
158 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700160 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400161 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700163 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500164 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400166
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700167 // real providers, saved here when mocked out
168 private final HashMap<String, LocationProviderInterface> mRealProviders =
169 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700171 // mapping from provider name to provider
172 private final HashMap<String, LocationProviderInterface> mProvidersByName =
173 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700175 // mapping from provider name to all its UpdateRecords
176 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
177 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700178
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700179 // mapping from provider name to last known location
180 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700182 // all providers that operate over proxy, for authorizing incoming location
183 private final ArrayList<LocationProviderProxy> mProxyProviders =
184 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Victoria Lease38389b62012-09-30 11:44:22 -0700186 // current active user on the device - other users are denied location data
187 private int mCurrentUserId = UserHandle.USER_OWNER;
188
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700189 public LocationManagerService(Context context) {
190 super();
191 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800192 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800193
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700194 if (D) Log.d(TAG, "Constructed");
195
196 // most startup is deferred until systemReady()
197 }
198
199 public void systemReady() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700200 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800201 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700202
Victoria Lease5cd731a2012-12-19 15:04:21 -0800203 // fetch package manager
204 mPackageManager = mContext.getPackageManager();
205
206 // prepare wake lock
207 PowerManager powerManager =
208 (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
209 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
210
211 // prepare worker thread
212 mHandlerThread = new HandlerThread(THREAD_NAME, Process.THREAD_PRIORITY_BACKGROUND);
213 mHandlerThread.start();
214 mLocationHandler = new LocationWorkerHandler(mHandlerThread.getLooper());
215
216 // prepare mLocationHandler's dependents
217 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
218 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
219 mBlacklist.init();
220 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
221
Dianne Hackbornc2293022013-02-06 23:14:49 -0800222 // Monitor for app ops mode changes.
223 AppOpsManager.Callback callback = new AppOpsManager.Callback() {
224 public void opChanged(int op, String packageName) {
225 synchronized (mLock) {
226 applyAllProviderRequirementsLocked();
227 }
228 }
229 };
230 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
231
Victoria Lease5cd731a2012-12-19 15:04:21 -0800232 // prepare providers
233 loadProvidersLocked();
234 updateProvidersLocked();
235 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700236
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700237 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700238 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700239 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700240 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800241 @Override
242 public void onChange(boolean selfChange) {
243 synchronized (mLock) {
244 updateProvidersLocked();
245 }
246 }
247 }, UserHandle.USER_ALL);
248 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700249
Victoria Lease38389b62012-09-30 11:44:22 -0700250 // listen for user change
251 IntentFilter intentFilter = new IntentFilter();
252 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
253
254 mContext.registerReceiverAsUser(new BroadcastReceiver() {
255 @Override
256 public void onReceive(Context context, Intent intent) {
257 String action = intent.getAction();
258 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
259 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
260 }
261 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800262 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700263 }
264
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500265 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
266 PackageManager pm = mContext.getPackageManager();
267 String systemPackageName = mContext.getPackageName();
268 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
269
270 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
271 new Intent(FUSED_LOCATION_SERVICE_ACTION),
272 PackageManager.GET_META_DATA, mCurrentUserId);
273 for (ResolveInfo rInfo : rInfos) {
274 String packageName = rInfo.serviceInfo.packageName;
275
276 // Check that the signature is in the list of supported sigs. If it's not in
277 // this list the standard provider binding logic won't bind to it.
278 try {
279 PackageInfo pInfo;
280 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
281 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
282 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
283 ", but has wrong signature, ignoring");
284 continue;
285 }
286 } catch (NameNotFoundException e) {
287 Log.e(TAG, "missing package: " + packageName);
288 continue;
289 }
290
291 // Get the version info
292 if (rInfo.serviceInfo.metaData == null) {
293 Log.w(TAG, "Found fused provider without metadata: " + packageName);
294 continue;
295 }
296
297 int version = rInfo.serviceInfo.metaData.getInt(
298 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
299 if (version == 0) {
300 // This should be the fallback fused location provider.
301
302 // Make sure it's in the system partition.
303 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
304 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
305 continue;
306 }
307
308 // Check that the fallback is signed the same as the OS
309 // as a proxy for coreApp="true"
310 if (pm.checkSignatures(systemPackageName, packageName)
311 != PackageManager.SIGNATURE_MATCH) {
312 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
313 + packageName);
314 continue;
315 }
316
317 // Found a valid fallback.
318 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
319 return;
320 } else {
321 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
322 }
323 }
324
325 throw new IllegalStateException("Unable to find a fused location provider that is in the "
326 + "system partition with version 0 and signed with the platform certificate. "
327 + "Such a package is needed to provide a default fused location provider in the "
328 + "event that no other fused location provider has been installed or is currently "
329 + "available. For example, coreOnly boot mode when decrypting the data "
330 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
331 }
332
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700333 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700334 // create a passive location provider, which is always enabled
335 PassiveProvider passiveProvider = new PassiveProvider(this);
336 addProviderLocked(passiveProvider);
337 mEnabledProviders.add(passiveProvider.getName());
338 mPassiveProvider = passiveProvider;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700339 // Create a gps location provider
340 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
341 mLocationHandler.getLooper());
Victoria Lease5c24fd02012-10-01 11:00:50 -0700342
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700343 if (GpsLocationProvider.isSupported()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700344 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
345 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
346 addProviderLocked(gpsProvider);
347 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
348 }
349
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700350 /*
351 Load package name(s) containing location provider support.
352 These packages can contain services implementing location providers:
353 Geocoder Provider, Network Location Provider, and
354 Fused Location Provider. They will each be searched for
355 service components implementing these providers.
356 The location framework also has support for installation
357 of new location providers at run-time. The new package does not
358 have to be explicitly listed here, however it must have a signature
359 that matches the signature of at least one package on this list.
360 */
361 Resources resources = mContext.getResources();
362 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500363 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700364 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500365 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
366 Arrays.toString(pkgs));
367 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
368
369 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700370
371 // bind to network provider
372 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
373 mContext,
374 LocationManager.NETWORK_PROVIDER,
375 NETWORK_LOCATION_SERVICE_ACTION,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800376 providerPackageNames, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700377 if (networkProvider != null) {
378 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
379 mProxyProviders.add(networkProvider);
380 addProviderLocked(networkProvider);
381 } else {
382 Slog.w(TAG, "no network location provider found");
383 }
384
385 // bind to fused provider
386 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
387 mContext,
388 LocationManager.FUSED_PROVIDER,
389 FUSED_LOCATION_SERVICE_ACTION,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800390 providerPackageNames, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700391 if (fusedLocationProvider != null) {
392 addProviderLocked(fusedLocationProvider);
393 mProxyProviders.add(fusedLocationProvider);
394 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700395 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700396 } else {
397 Slog.e(TAG, "no fused location provider found",
398 new IllegalStateException("Location service needs a fused location provider"));
399 }
400
401 // bind to geocoder provider
Victoria Leaseb711d572012-10-02 13:14:11 -0700402 mGeocodeProvider = GeocoderProxy.createAndBind(mContext, providerPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800403 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700404 if (mGeocodeProvider == null) {
405 Slog.e(TAG, "no geocoder provider found");
406 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700407
408 // bind to geofence provider
409 GeofenceProxy provider = GeofenceProxy.createAndBind(mContext, providerPackageNames,
410 mLocationHandler, gpsProvider.getGpsGeofenceProxy());
411 if (provider == null) {
412 Slog.e(TAG, "no geofence provider found");
413 }
414
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700415 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700418 * Called when the device's active user changes.
419 * @param userId the new active user's UserId
420 */
421 private void switchUser(int userId) {
Victoria Lease83762d22012-10-03 13:51:17 -0700422 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800423 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700424 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700425 mLastLocation.clear();
426 for (LocationProviderInterface p : mProviders) {
427 updateProviderListenersLocked(p.getName(), false, mCurrentUserId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700428 }
Victoria Lease38389b62012-09-30 11:44:22 -0700429 mCurrentUserId = userId;
Victoria Leaseb711d572012-10-02 13:14:11 -0700430 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700431 }
432 }
433
434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
436 * location updates.
437 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700438 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700439 final int mUid; // uid of receiver
440 final int mPid; // pid of receiver
441 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700442 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 final ILocationListener mListener;
445 final PendingIntent mPendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700447
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400448 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700449
Mike Lockwood48f17512009-04-23 09:12:08 -0700450 int mPendingBroadcasts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700452 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
453 String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700456 if (listener != null) {
457 mKey = listener.asBinder();
458 } else {
459 mKey = intent;
460 }
Victoria Lease37425c32012-10-16 16:08:48 -0700461 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700462 mUid = uid;
463 mPid = pid;
464 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
467 @Override
468 public boolean equals(Object otherObj) {
469 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700470 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472 return false;
473 }
474
475 @Override
476 public int hashCode() {
477 return mKey.hashCode();
478 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 @Override
481 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700482 StringBuilder s = new StringBuilder();
483 s.append("Reciever[");
484 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700486 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700488 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700490 for (String p : mUpdateRecords.keySet()) {
491 s.append(" ").append(mUpdateRecords.get(p).toString());
492 }
493 s.append("]");
494 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
497 public boolean isListener() {
498 return mListener != null;
499 }
500
501 public boolean isPendingIntent() {
502 return mPendingIntent != null;
503 }
504
505 public ILocationListener getListener() {
506 if (mListener != null) {
507 return mListener;
508 }
509 throw new IllegalStateException("Request for non-existent listener");
510 }
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
513 if (mListener != null) {
514 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700515 synchronized (this) {
516 // synchronize to ensure incrementPendingBroadcastsLocked()
517 // is called before decrementPendingBroadcasts()
518 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700519 // call this after broadcasting so we do not increment
520 // if we throw an exeption.
521 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 } catch (RemoteException e) {
524 return false;
525 }
526 } else {
527 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800528 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
530 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700531 synchronized (this) {
532 // synchronize to ensure incrementPendingBroadcastsLocked()
533 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700534 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700535 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700536 // call this after broadcasting so we do not increment
537 // if we throw an exeption.
538 incrementPendingBroadcastsLocked();
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 } catch (PendingIntent.CanceledException e) {
541 return false;
542 }
543 }
544 return true;
545 }
546
547 public boolean callLocationChangedLocked(Location location) {
548 if (mListener != null) {
549 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700550 synchronized (this) {
551 // synchronize to ensure incrementPendingBroadcastsLocked()
552 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800553 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700554 // call this after broadcasting so we do not increment
555 // if we throw an exeption.
556 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 } catch (RemoteException e) {
559 return false;
560 }
561 } else {
562 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800563 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700565 synchronized (this) {
566 // synchronize to ensure incrementPendingBroadcastsLocked()
567 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700568 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700569 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700570 // call this after broadcasting so we do not increment
571 // if we throw an exeption.
572 incrementPendingBroadcastsLocked();
573 }
574 } catch (PendingIntent.CanceledException e) {
575 return false;
576 }
577 }
578 return true;
579 }
580
581 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
582 if (mListener != null) {
583 try {
584 synchronized (this) {
585 // synchronize to ensure incrementPendingBroadcastsLocked()
586 // is called before decrementPendingBroadcasts()
587 if (enabled) {
588 mListener.onProviderEnabled(provider);
589 } else {
590 mListener.onProviderDisabled(provider);
591 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700592 // call this after broadcasting so we do not increment
593 // if we throw an exeption.
594 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700595 }
596 } catch (RemoteException e) {
597 return false;
598 }
599 } else {
600 Intent providerIntent = new Intent();
601 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
602 try {
603 synchronized (this) {
604 // synchronize to ensure incrementPendingBroadcastsLocked()
605 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700606 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700607 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700608 // call this after broadcasting so we do not increment
609 // if we throw an exeption.
610 incrementPendingBroadcastsLocked();
611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 } catch (PendingIntent.CanceledException e) {
613 return false;
614 }
615 }
616 return true;
617 }
618
Nick Pellyf1be6862012-05-15 10:53:42 -0700619 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700621 if (D) Log.d(TAG, "Location listener died");
622
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400623 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 removeUpdatesLocked(this);
625 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700626 synchronized (this) {
627 if (mPendingBroadcasts > 0) {
628 LocationManagerService.this.decrementPendingBroadcasts();
629 mPendingBroadcasts = 0;
630 }
631 }
632 }
633
Nick Pellye0fd6932012-07-11 10:26:13 -0700634 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700635 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
636 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400637 synchronized (this) {
638 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700639 }
640 }
641
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400642 // this must be called while synchronized by caller in a synchronized block
643 // containing the sending of the broadcaset
644 private void incrementPendingBroadcastsLocked() {
645 if (mPendingBroadcasts++ == 0) {
646 LocationManagerService.this.incrementPendingBroadcasts();
647 }
648 }
649
650 private void decrementPendingBroadcastsLocked() {
651 if (--mPendingBroadcasts == 0) {
652 LocationManagerService.this.decrementPendingBroadcasts();
Mike Lockwood48f17512009-04-23 09:12:08 -0700653 }
654 }
655 }
656
Nick Pellye0fd6932012-07-11 10:26:13 -0700657 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700658 public void locationCallbackFinished(ILocationListener listener) {
Joshua Bartel080b61b2009-10-05 12:44:46 -0400659 //Do not use getReceiver here as that will add the ILocationListener to
660 //the receiver list if it is not found. If it is not found then the
661 //LocationListener was removed when it had a pending broadcast and should
662 //not be added back.
663 IBinder binder = listener.asBinder();
664 Receiver receiver = mReceivers.get(binder);
Mike Lockwood48f17512009-04-23 09:12:08 -0700665 if (receiver != null) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400666 synchronized (receiver) {
667 // so wakelock calls will succeed
668 long identity = Binder.clearCallingIdentity();
669 receiver.decrementPendingBroadcastsLocked();
670 Binder.restoreCallingIdentity(identity);
671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 }
674
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700675 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400676 mProviders.add(provider);
677 mProvidersByName.put(provider.getName(), provider);
678 }
679
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700680 private void removeProviderLocked(LocationProviderInterface provider) {
681 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400682 mProviders.remove(provider);
683 mProvidersByName.remove(provider.getName());
684 }
685
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800686 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800687 * Returns "true" if access to the specified location provider is allowed by the current
688 * user's settings. Access to all location providers is forbidden to non-location-provider
689 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800690 *
691 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800692 * @return
693 */
Victoria Lease09eeaec2013-02-05 11:34:13 -0800694 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 if (mEnabledProviders.contains(provider)) {
696 return true;
697 }
698 if (mDisabledProviders.contains(provider)) {
699 return false;
700 }
701 // Use system settings
702 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703
Victoria Leaseb711d572012-10-02 13:14:11 -0700704 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700707 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800708 * Returns "true" if access to the specified location provider is allowed by the specified
709 * user's settings. Access to all location providers is forbidden to non-location-provider
710 * processes belonging to background users.
711 *
712 * @param provider the name of the location provider
713 * @param uid the requestor's UID
714 * @return
715 */
716 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
717 if (UserHandle.getUserId(uid) != mCurrentUserId && !isUidALocationProvider(uid)) {
718 return false;
719 }
720 return isAllowedByCurrentUserSettingsLocked(provider);
721 }
722
723 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700724 * Returns the permission string associated with the specified resolution level.
725 *
726 * @param resolutionLevel the resolution level
727 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700728 */
Victoria Lease37425c32012-10-16 16:08:48 -0700729 private String getResolutionPermission(int resolutionLevel) {
730 switch (resolutionLevel) {
731 case RESOLUTION_LEVEL_FINE:
732 return android.Manifest.permission.ACCESS_FINE_LOCATION;
733 case RESOLUTION_LEVEL_COARSE:
734 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
735 default:
736 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
Victoria Leaseda479c52012-10-15 15:24:16 -0700738 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700739
Victoria Leaseda479c52012-10-15 15:24:16 -0700740 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700741 * Returns the resolution level allowed to the given PID/UID pair.
742 *
743 * @param pid the PID
744 * @param uid the UID
745 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -0700746 */
Victoria Lease37425c32012-10-16 16:08:48 -0700747 private int getAllowedResolutionLevel(int pid, int uid) {
748 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
749 pid, uid) == PackageManager.PERMISSION_GRANTED) {
750 return RESOLUTION_LEVEL_FINE;
751 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
752 pid, uid) == PackageManager.PERMISSION_GRANTED) {
753 return RESOLUTION_LEVEL_COARSE;
754 } else {
755 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -0700756 }
Victoria Lease4fab68b2012-09-13 13:20:59 -0700757 }
758
759 /**
Victoria Lease37425c32012-10-16 16:08:48 -0700760 * Returns the resolution level allowed to the caller
761 *
762 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -0700763 */
Victoria Lease37425c32012-10-16 16:08:48 -0700764 private int getCallerAllowedResolutionLevel() {
765 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
766 }
767
768 /**
769 * Throw SecurityException if specified resolution level is insufficient to use geofences.
770 *
771 * @param allowedResolutionLevel resolution level allowed to caller
772 */
773 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
774 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -0700775 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
778
Victoria Lease37425c32012-10-16 16:08:48 -0700779 /**
780 * Return the minimum resolution level required to use the specified location provider.
781 *
782 * @param provider the name of the location provider
783 * @return minimum resolution level required for provider
784 */
785 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700786 if (LocationManager.GPS_PROVIDER.equals(provider) ||
787 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
788 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700789 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -0700790 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
791 LocationManager.FUSED_PROVIDER.equals(provider)) {
792 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700793 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700794 } else {
795 // mock providers
796 LocationProviderInterface lp = mMockProviders.get(provider);
797 if (lp != null) {
798 ProviderProperties properties = lp.getProperties();
799 if (properties != null) {
800 if (properties.mRequiresSatellite) {
801 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -0700802 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -0700803 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
804 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -0700805 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -0700806 }
807 }
808 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700809 }
Victoria Lease37425c32012-10-16 16:08:48 -0700810 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -0700811 }
812
Victoria Lease37425c32012-10-16 16:08:48 -0700813 /**
814 * Throw SecurityException if specified resolution level is insufficient to use the named
815 * location provider.
816 *
817 * @param allowedResolutionLevel resolution level allowed to caller
818 * @param providerName the name of the location provider
819 */
820 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
821 String providerName) {
822 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
823 if (allowedResolutionLevel < requiredResolutionLevel) {
824 switch (requiredResolutionLevel) {
825 case RESOLUTION_LEVEL_FINE:
826 throw new SecurityException("\"" + providerName + "\" location provider " +
827 "requires ACCESS_FINE_LOCATION permission.");
828 case RESOLUTION_LEVEL_COARSE:
829 throw new SecurityException("\"" + providerName + "\" location provider " +
830 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
831 default:
832 throw new SecurityException("Insufficient permission for \"" + providerName +
833 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -0700834 }
835 }
Victoria Lease8dbb6342012-09-21 16:55:53 -0700836 }
837
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800838 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800839 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
840 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800841 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800842 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800843 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800844 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800845 }
846 return -1;
847 }
848
849 boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
850 int op = resolutionLevelToOp(allowedResolutionLevel);
851 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800852 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
853 return false;
854 }
855 }
856 return true;
857 }
858
859 boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800860 int op = resolutionLevelToOp(allowedResolutionLevel);
861 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800862 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
863 return false;
864 }
865 }
866 return true;
867 }
868
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700869 /**
870 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -0700871 * fused, also including ones that are not permitted to
872 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700873 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700874 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700876 ArrayList<String> out;
877 synchronized (mLock) {
878 out = new ArrayList<String>(mProviders.size());
879 for (LocationProviderInterface provider : mProviders) {
880 String name = provider.getName();
881 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -0700882 continue;
883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 out.add(name);
885 }
886 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700887
888 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 return out;
890 }
891
Mike Lockwood03ca2162010-04-01 08:10:09 -0700892 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700893 * Return all providers by name, that match criteria and are optionally
894 * enabled.
895 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700896 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700897 @Override
898 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -0700899 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700900 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800901 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -0700902 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -0700903 try {
904 synchronized (mLock) {
905 out = new ArrayList<String>(mProviders.size());
906 for (LocationProviderInterface provider : mProviders) {
907 String name = provider.getName();
908 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -0700909 continue;
910 }
Victoria Lease37425c32012-10-16 16:08:48 -0700911 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -0800912 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700913 continue;
914 }
915 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
916 name, provider.getProperties(), criteria)) {
917 continue;
918 }
919 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700920 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700921 }
Mike Lockwood03ca2162010-04-01 08:10:09 -0700922 }
Victoria Leaseb711d572012-10-02 13:14:11 -0700923 } finally {
924 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700925 }
926
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700927 if (D) Log.d(TAG, "getProviders()=" + out);
928 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700929 }
930
931 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700932 * Return the name of the best provider given a Criteria object.
933 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -0700934 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700935 * has been deprecated as well. So this method now uses
936 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -0700937 */
Nick Pellye0fd6932012-07-11 10:26:13 -0700938 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700939 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700940 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700941
942 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700943 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700944 result = pickBest(providers);
945 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
946 return result;
947 }
948 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -0700949 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700950 result = pickBest(providers);
951 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
952 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700953 }
954
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700955 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -0700956 return null;
957 }
958
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700959 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -0700960 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700961 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -0700962 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
963 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700964 } else {
965 return providers.get(0);
966 }
967 }
968
Nick Pellye0fd6932012-07-11 10:26:13 -0700969 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -0700970 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
971 LocationProviderInterface p = mProvidersByName.get(provider);
972 if (p == null) {
973 throw new IllegalArgumentException("provider=" + provider);
974 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700975
976 boolean result = LocationProvider.propertiesMeetCriteria(
977 p.getName(), p.getProperties(), criteria);
978 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
979 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -0700980 }
981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700983 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400984 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500985 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 boolean isEnabled = p.isEnabled();
987 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -0800988 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 if (isEnabled && !shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700990 updateProviderListenersLocked(name, false, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700991 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 } else if (!isEnabled && shouldBeEnabled) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700993 updateProviderListenersLocked(name, true, mCurrentUserId);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700994 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -0700996 }
997 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700998 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
999 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001 }
1002
Victoria Leaseb711d572012-10-02 13:14:11 -07001003 private void updateProviderListenersLocked(String provider, boolean enabled, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 int listeners = 0;
1005
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001006 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001007 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008
1009 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1012 if (records != null) {
1013 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001014 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 UpdateRecord record = records.get(i);
Victoria Lease269518e2012-10-29 08:25:39 -07001016 if (UserHandle.getUserId(record.mReceiver.mUid) == userId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001017 // Sends a notification message to the receiver
1018 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1019 if (deadReceivers == null) {
1020 deadReceivers = new ArrayList<Receiver>();
1021 }
1022 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001024 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 }
1028
1029 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001030 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 removeUpdatesLocked(deadReceivers.get(i));
1032 }
1033 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 if (enabled) {
1036 p.enable();
1037 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001038 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001045 private void applyRequirementsLocked(String provider) {
1046 LocationProviderInterface p = mProvidersByName.get(provider);
1047 if (p == null) return;
1048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001050 WorkSource worksource = new WorkSource();
1051 ProviderRequest providerRequest = new ProviderRequest();
1052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001054 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001055 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001056 if (checkLocationAccess(record.mReceiver.mUid, record.mReceiver.mPackageName,
1057 record.mReceiver.mAllowedResolutionLevel)) {
1058 LocationRequest locationRequest = record.mRequest;
1059 providerRequest.locationRequests.add(locationRequest);
1060 if (locationRequest.getInterval() < providerRequest.interval) {
1061 providerRequest.reportLocation = true;
1062 providerRequest.interval = locationRequest.getInterval();
1063 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001064 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001065 }
1066 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001067
1068 if (providerRequest.reportLocation) {
1069 // calculate who to blame for power
1070 // This is somewhat arbitrary. We pick a threshold interval
1071 // that is slightly higher that the minimum interval, and
1072 // spread the blame across all applications with a request
1073 // under that threshold.
1074 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1075 for (UpdateRecord record : records) {
Victoria Lease269518e2012-10-29 08:25:39 -07001076 if (UserHandle.getUserId(record.mReceiver.mUid) == mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001077 LocationRequest locationRequest = record.mRequest;
1078 if (locationRequest.getInterval() <= thresholdInterval) {
Dianne Hackborn002a54e2013-01-10 17:34:55 -08001079 worksource.add(record.mReceiver.mUid, record.mReceiver.mPackageName);
Victoria Leaseb711d572012-10-02 13:14:11 -07001080 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001081 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001085
1086 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1087 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089
1090 private class UpdateRecord {
1091 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001092 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001094 Location mLastFixBroadcast;
1095 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096
1097 /**
1098 * Note: must be constructed with lock held.
1099 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001100 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001102 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104
1105 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1106 if (records == null) {
1107 records = new ArrayList<UpdateRecord>();
1108 mRecordsByProvider.put(provider, records);
1109 }
1110 if (!records.contains(this)) {
1111 records.add(this);
1112 }
1113 }
1114
1115 /**
1116 * Method to be called when a record will no longer be used. Calling this multiple times
1117 * must have the same effect as calling it once.
1118 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001119 void disposeLocked(boolean removeReceiver) {
1120 // remove from mRecordsByProvider
1121 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1122 if (globalRecords != null) {
1123 globalRecords.remove(this);
1124 }
1125
1126 if (!removeReceiver) return; // the caller will handle the rest
1127
1128 // remove from Receiver#mUpdateRecords
1129 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1130 if (receiverRecords != null) {
1131 receiverRecords.remove(this.mProvider);
1132
1133 // and also remove the Receiver if it has no more update records
1134 if (removeReceiver && receiverRecords.size() == 0) {
1135 removeUpdatesLocked(mReceiver);
1136 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139
1140 @Override
1141 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001142 StringBuilder s = new StringBuilder();
1143 s.append("UpdateRecord[");
1144 s.append(mProvider);
1145 s.append(' ').append(mReceiver.mPackageName).append('(');
1146 s.append(mReceiver.mUid).append(')');
1147 s.append(' ').append(mRequest);
1148 s.append(']');
1149 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001153 private Receiver getReceiver(ILocationListener listener, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001154 IBinder binder = listener.asBinder();
1155 Receiver receiver = mReceivers.get(binder);
1156 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001157 receiver = new Receiver(listener, null, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001158 mReceivers.put(binder, receiver);
1159
1160 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001161 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001162 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001163 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001164 return null;
1165 }
1166 }
1167 return receiver;
1168 }
1169
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001170 private Receiver getReceiver(PendingIntent intent, int pid, int uid, String packageName) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001171 Receiver receiver = mReceivers.get(intent);
1172 if (receiver == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001173 receiver = new Receiver(null, intent, pid, uid, packageName);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001174 mReceivers.put(intent, receiver);
1175 }
1176 return receiver;
1177 }
1178
Victoria Lease37425c32012-10-16 16:08:48 -07001179 /**
1180 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1181 * and consistency requirements.
1182 *
1183 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001184 * @return a version of request that meets the given resolution and consistency requirements
1185 * @hide
1186 */
1187 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1188 LocationRequest sanitizedRequest = new LocationRequest(request);
1189 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1190 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001191 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001192 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001193 break;
1194 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001195 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001196 break;
1197 }
1198 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001199 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1200 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001201 }
Victoria Lease37425c32012-10-16 16:08:48 -07001202 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1203 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001204 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001205 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001206 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001207 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001208 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001209 }
Victoria Lease37425c32012-10-16 16:08:48 -07001210 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001211 }
1212
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001213 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001214 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001215 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001216 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001217 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001218 String[] packages = mPackageManager.getPackagesForUid(uid);
1219 if (packages == null) {
1220 throw new SecurityException("invalid UID " + uid);
1221 }
1222 for (String pkg : packages) {
1223 if (packageName.equals(pkg)) return;
1224 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001225 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001226 }
1227
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001228 private void checkPendingIntent(PendingIntent intent) {
1229 if (intent == null) {
1230 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001231 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001232 }
1233
1234 private Receiver checkListenerOrIntent(ILocationListener listener, PendingIntent intent,
1235 int pid, int uid, String packageName) {
1236 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001237 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001238 } else if (intent != null && listener != null) {
1239 throw new IllegalArgumentException("cannot register both listener and intent");
1240 } else if (intent != null) {
1241 checkPendingIntent(intent);
1242 return getReceiver(intent, pid, uid, packageName);
1243 } else {
1244 return getReceiver(listener, pid, uid, packageName);
1245 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001246 }
1247
Nick Pellye0fd6932012-07-11 10:26:13 -07001248 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001249 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1250 PendingIntent intent, String packageName) {
1251 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1252 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001253 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1254 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1255 request.getProvider());
1256 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001258 final int pid = Binder.getCallingPid();
1259 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001260 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 long identity = Binder.clearCallingIdentity();
1262 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001263 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1264 // a location.
Dianne Hackborn35654b62013-01-14 17:38:02 -08001265 checkLocationAccess(uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001266 Receiver recevier = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1267
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001268 synchronized (mLock) {
Victoria Lease37425c32012-10-16 16:08:48 -07001269 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 } finally {
1272 Binder.restoreCallingIdentity(identity);
1273 }
1274 }
1275
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001276 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1277 int pid, int uid, String packageName) {
1278 // Figure out the provider. Either its explicitly request (legacy use cases), or
1279 // use the fused provider
1280 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1281 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001282 if (name == null) {
1283 throw new IllegalArgumentException("provider name must not be null");
1284 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001285 LocationProviderInterface provider = mProvidersByName.get(name);
1286 if (provider == null) {
1287 throw new IllegalArgumentException("provider doesn't exisit: " + provider);
1288 }
1289
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001290 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1291 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001292
1293 UpdateRecord record = new UpdateRecord(name, request, receiver);
1294 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1295 if (oldRecord != null) {
1296 oldRecord.disposeLocked(false);
1297 }
1298
Victoria Lease09eeaec2013-02-05 11:34:13 -08001299 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001300 if (isProviderEnabled) {
1301 applyRequirementsLocked(name);
1302 } else {
1303 // Notify the listener that updates are currently disabled
1304 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306 }
1307
Nick Pellye0fd6932012-07-11 10:26:13 -07001308 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001309 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1310 String packageName) {
1311 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001312
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001313 final int pid = Binder.getCallingPid();
1314 final int uid = Binder.getCallingUid();
1315 Receiver receiver = checkListenerOrIntent(listener, intent, pid, uid, packageName);
1316
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001317 // providers may use public location API's, need to clear identity
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001318 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 try {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001320 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001321 removeUpdatesLocked(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001323 } finally {
1324 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326 }
1327
1328 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001329 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001330
1331 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1332 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1333 synchronized (receiver) {
1334 if (receiver.mPendingBroadcasts > 0) {
1335 decrementPendingBroadcasts();
1336 receiver.mPendingBroadcasts = 0;
1337 }
1338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001341 // Record which providers were associated with this listener
1342 HashSet<String> providers = new HashSet<String>();
1343 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1344 if (oldRecords != null) {
1345 // Call dispose() on the obsolete update records.
1346 for (UpdateRecord record : oldRecords.values()) {
1347 record.disposeLocked(false);
1348 }
1349 // Accumulate providers
1350 providers.addAll(oldRecords.keySet());
1351 }
1352
1353 // update provider
1354 for (String provider : providers) {
1355 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001356 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001357 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001360 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
1362 }
1363
Dianne Hackbornc2293022013-02-06 23:14:49 -08001364 private void applyAllProviderRequirementsLocked() {
1365 for (LocationProviderInterface p : mProviders) {
1366 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001367 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001368 continue;
1369 }
1370
1371 applyRequirementsLocked(p.getName());
1372 }
1373 }
1374
Nick Pellye0fd6932012-07-11 10:26:13 -07001375 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001376 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001377 if (D) Log.d(TAG, "getLastLocation: " + request);
1378 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001379 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001380 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001381 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1382 request.getProvider());
1383 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001384
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001385 final int uid = Binder.getCallingUid();
1386 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001387 try {
1388 if (mBlacklist.isBlacklisted(packageName)) {
1389 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1390 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001391 return null;
1392 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001393
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001394 if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
1395 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1396 packageName);
1397 return null;
1398 }
1399
Victoria Leaseb711d572012-10-02 13:14:11 -07001400 synchronized (mLock) {
1401 // Figure out the provider. Either its explicitly request (deprecated API's),
1402 // or use the fused provider
1403 String name = request.getProvider();
1404 if (name == null) name = LocationManager.FUSED_PROVIDER;
1405 LocationProviderInterface provider = mProvidersByName.get(name);
1406 if (provider == null) return null;
1407
Victoria Lease09eeaec2013-02-05 11:34:13 -08001408 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001409
1410 Location location = mLastLocation.get(name);
1411 if (location == null) {
1412 return null;
1413 }
Victoria Lease37425c32012-10-16 16:08:48 -07001414 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001415 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1416 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001417 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001418 }
Victoria Lease37425c32012-10-16 16:08:48 -07001419 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001420 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001421 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001422 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001423 return null;
1424 } finally {
1425 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001426 }
1427 }
1428
1429 @Override
1430 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1431 String packageName) {
1432 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001433 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1434 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001435 checkPendingIntent(intent);
1436 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001437 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1438 request.getProvider());
1439 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001440
Victoria Lease37425c32012-10-16 16:08:48 -07001441 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001442
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001443 // geo-fence manager uses the public location API, need to clear identity
1444 int uid = Binder.getCallingUid();
Victoria Lease56e675b2012-11-05 19:25:06 -08001445 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1446 // temporary measure until geofences work for secondary users
1447 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1448 return;
1449 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001450 long identity = Binder.clearCallingIdentity();
1451 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001452 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1453 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001454 } finally {
1455 Binder.restoreCallingIdentity(identity);
1456 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001457 }
1458
1459 @Override
1460 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Victoria Lease37425c32012-10-16 16:08:48 -07001461 checkResolutionLevelIsSufficientForGeofenceUse(getCallerAllowedResolutionLevel());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001462 checkPendingIntent(intent);
1463 checkPackageName(packageName);
1464
1465 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1466
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001467 // geo-fence manager uses the public location API, need to clear identity
1468 long identity = Binder.clearCallingIdentity();
1469 try {
1470 mGeofenceManager.removeFence(geofence, intent);
1471 } finally {
1472 Binder.restoreCallingIdentity(identity);
1473 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001474 }
1475
1476
1477 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001478 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001479 if (mGpsStatusProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 return false;
1481 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001482 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1483 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001484 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001486 final int uid = Binder.getCallingUid();
1487 final long ident = Binder.clearCallingIdentity();
1488 try {
Victoria Lease3d5173d2013-02-05 16:07:32 -08001489 if (!checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001490 return false;
1491 }
1492 } finally {
1493 Binder.restoreCallingIdentity(ident);
1494 }
1495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001497 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 return false;
1501 }
1502 return true;
1503 }
1504
Nick Pellye0fd6932012-07-11 10:26:13 -07001505 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001507 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001508 try {
1509 mGpsStatusProvider.removeGpsStatusListener(listener);
1510 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001511 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
1514 }
1515
Nick Pellye0fd6932012-07-11 10:26:13 -07001516 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001518 if (provider == null) {
1519 // throw NullPointerException to remain compatible with previous implementation
1520 throw new NullPointerException();
1521 }
Victoria Lease37425c32012-10-16 16:08:48 -07001522 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1523 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001526 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 != PackageManager.PERMISSION_GRANTED)) {
1528 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1529 }
1530
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001531 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001532 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001533 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001534
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001535 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 }
1537 }
1538
Nick Pellye0fd6932012-07-11 10:26:13 -07001539 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001540 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001541 if (Binder.getCallingUid() != Process.myUid()) {
1542 throw new SecurityException(
1543 "calling sendNiResponse from outside of the system is not allowed");
1544 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04001545 try {
1546 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001547 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001548 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04001549 return false;
1550 }
1551 }
1552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001554 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11001555 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * accessed by the caller
1557 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001558 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001559 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07001560 if (mProvidersByName.get(provider) == null) {
1561 return null;
1562 }
1563
Victoria Lease37425c32012-10-16 16:08:48 -07001564 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1565 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001567 LocationProviderInterface p;
1568 synchronized (mLock) {
1569 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001572 if (p == null) return null;
1573 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575
Nick Pellye0fd6932012-07-11 10:26:13 -07001576 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 public boolean isProviderEnabled(String provider) {
Victoria Lease37425c32012-10-16 16:08:48 -07001578 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1579 provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001580 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
1581
Victoria Lease09eeaec2013-02-05 11:34:13 -08001582 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001583 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001584 try {
1585 synchronized (mLock) {
1586 LocationProviderInterface p = mProvidersByName.get(provider);
1587 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001588
Victoria Lease09eeaec2013-02-05 11:34:13 -08001589 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07001590 }
1591 } finally {
1592 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001593 }
1594 }
1595
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001596 /**
1597 * Returns "true" if the UID belongs to a bound location provider.
1598 *
1599 * @param uid the uid
1600 * @return true if uid belongs to a bound location provider
1601 */
1602 private boolean isUidALocationProvider(int uid) {
1603 if (uid == Process.SYSTEM_UID) {
1604 return true;
1605 }
1606 if (mGeocodeProvider != null) {
1607 if (doesPackageHaveUid(uid, mGeocodeProvider.getConnectedPackageName())) return true;
1608 }
1609 for (LocationProviderProxy proxy : mProxyProviders) {
1610 if (doesPackageHaveUid(uid, proxy.getConnectedPackageName())) return true;
1611 }
1612 return false;
1613 }
1614
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001615 private void checkCallerIsProvider() {
1616 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
1617 == PackageManager.PERMISSION_GRANTED) {
1618 return;
1619 }
1620
1621 // Previously we only used the INSTALL_LOCATION_PROVIDER
1622 // check. But that is system or signature
1623 // protection level which is not flexible enough for
1624 // providers installed oustide the system image. So
1625 // also allow providers with a UID matching the
1626 // currently bound package name
1627
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001628 if (isUidALocationProvider(Binder.getCallingUid())) {
1629 return;
1630 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001631
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001632 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
1633 "or UID of a currently bound location provider");
1634 }
1635
1636 private boolean doesPackageHaveUid(int uid, String packageName) {
1637 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 return false;
1639 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001640 try {
1641 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
1642 if (appInfo.uid != uid) {
1643 return false;
1644 }
1645 } catch (NameNotFoundException e) {
1646 return false;
1647 }
1648 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
1650
Nick Pellye0fd6932012-07-11 10:26:13 -07001651 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05001652 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001653 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04001654
Nick Pelly2eeeec22012-07-18 13:13:37 -07001655 if (!location.isComplete()) {
1656 Log.w(TAG, "Dropping incomplete location: " + location);
1657 return;
1658 }
1659
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001660 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
1661 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05001662 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001663 mLocationHandler.sendMessageAtFrontOfQueue(m);
1664 }
1665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666
Laurent Tu75defb62012-11-01 16:21:52 -07001667 private static boolean shouldBroadcastSafe(
1668 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // Always broadcast the first update
1670 if (lastLoc == null) {
1671 return true;
1672 }
1673
Nick Pellyf1be6862012-05-15 10:53:42 -07001674 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001675 long minTime = record.mRequest.getFastestInterval();
Philip Milne41180122012-09-26 11:29:25 -07001676 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos()) / 1000000L;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001677 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 return false;
1679 }
1680
1681 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001682 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 if (minDistance > 0.0) {
1684 if (loc.distanceTo(lastLoc) <= minDistance) {
1685 return false;
1686 }
1687 }
1688
Laurent Tu75defb62012-11-01 16:21:52 -07001689 // Check whether sufficient number of udpates is left
1690 if (record.mRequest.getNumUpdates() <= 0) {
1691 return false;
1692 }
1693
1694 // Check whether the expiry date has passed
1695 if (record.mRequest.getExpireAt() < now) {
1696 return false;
1697 }
1698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 return true;
1700 }
1701
Mike Lockwooda4903f22010-02-17 06:42:23 -05001702 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001703 if (D) Log.d(TAG, "incoming location: " + location);
1704
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001705 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05001706 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707
Laurent Tu60ec50a2012-10-04 17:00:10 -07001708 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001709 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001710 if (p == null) return;
1711
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001712 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07001713 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1714 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001715 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07001716 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001717 lastLocation = new Location(provider);
1718 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07001719 } else {
1720 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1721 if (noGPSLocation == null && lastNoGPSLocation != null) {
1722 // New location has no no-GPS location: adopt last no-GPS location. This is set
1723 // directly into location because we do not want to notify COARSE clients.
1724 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
1725 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07001726 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001727 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728
Laurent Tu60ec50a2012-10-04 17:00:10 -07001729 // Skip if there are no UpdateRecords for this provider.
1730 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1731 if (records == null || records.size() == 0) return;
1732
Victoria Lease09016ab2012-09-16 12:33:15 -07001733 // Fetch coarse location
1734 Location coarseLocation = null;
1735 if (noGPSLocation != null && !noGPSLocation.equals(lastNoGPSLocation)) {
1736 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
1737 }
1738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 // Fetch latest status update time
1740 long newStatusUpdateTime = p.getStatusUpdateTime();
1741
Mike Lockwood7ec434e2009-03-27 07:46:48 -07001742 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 Bundle extras = new Bundle();
1744 int status = p.getStatus(extras);
1745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001747 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001750 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001752 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07001753
Victoria Lease269518e2012-10-29 08:25:39 -07001754 int receiverUserId = UserHandle.getUserId(receiver.mUid);
1755 if (receiverUserId != mCurrentUserId) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001756 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07001757 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07001758 " (current user: " + mCurrentUserId + ", app: " +
1759 receiver.mPackageName + ")");
1760 }
1761 continue;
1762 }
1763
Nick Pelly4035f5a2012-08-17 14:43:49 -07001764 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
1765 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
1766 receiver.mPackageName);
1767 continue;
1768 }
1769
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001770 if (!reportLocationAccessNoThrow(receiver.mUid, receiver.mPackageName,
1771 receiver.mAllowedResolutionLevel)) {
1772 if (D) Log.d(TAG, "skipping loc update for no op app: " +
1773 receiver.mPackageName);
1774 continue;
1775 }
1776
Victoria Lease09016ab2012-09-16 12:33:15 -07001777 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07001778 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1779 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001780 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07001781 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001782 }
Victoria Lease09016ab2012-09-16 12:33:15 -07001783 if (notifyLocation != null) {
1784 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07001785 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001786 if (lastLoc == null) {
1787 lastLoc = new Location(notifyLocation);
1788 r.mLastFixBroadcast = lastLoc;
1789 } else {
1790 lastLoc.set(notifyLocation);
1791 }
1792 if (!receiver.callLocationChangedLocked(notifyLocation)) {
1793 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
1794 receiverDead = true;
1795 }
Laurent Tu75defb62012-11-01 16:21:52 -07001796 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798 }
1799
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001800 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07001802 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001804 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001806 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001807 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001808 }
1809 }
1810
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001811 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07001812 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001813 if (deadUpdateRecords == null) {
1814 deadUpdateRecords = new ArrayList<UpdateRecord>();
1815 }
1816 deadUpdateRecords.add(r);
1817 }
1818 // track dead receivers
1819 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001820 if (deadReceivers == null) {
1821 deadReceivers = new ArrayList<Receiver>();
1822 }
1823 if (!deadReceivers.contains(receiver)) {
1824 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826 }
1827 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001828
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001829 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001831 for (Receiver receiver : deadReceivers) {
1832 removeUpdatesLocked(receiver);
1833 }
1834 }
1835 if (deadUpdateRecords != null) {
1836 for (UpdateRecord r : deadUpdateRecords) {
1837 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
Victoria Lease8b38b292012-12-04 15:04:43 -08001839 applyRequirementsLocked(provider);
1840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842
1843 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08001844 public LocationWorkerHandler(Looper looper) {
1845 super(looper, null, true);
1846 }
1847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 @Override
1849 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001850 switch (msg.what) {
1851 case MSG_LOCATION_CHANGED:
1852 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
1853 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855 }
1856 }
1857
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001858 private boolean isMockProvider(String provider) {
1859 synchronized (mLock) {
1860 return mMockProviders.containsKey(provider);
1861 }
1862 }
1863
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001864 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001865 // create a working copy of the incoming Location so that the service can modify it without
1866 // disturbing the caller's copy
1867 Location myLocation = new Location(location);
1868 String provider = myLocation.getProvider();
1869
1870 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
1871 // bit if location did not come from a mock provider because passive/fused providers can
1872 // forward locations from mock providers, and should not grant them legitimacy in doing so.
1873 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
1874 myLocation.setIsFromMockProvider(true);
1875 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08001876
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001877 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001878 if (isAllowedByCurrentUserSettingsLocked(provider)) {
1879 if (!passive) {
1880 // notify passive provider of the new location
1881 mPassiveProvider.updateLocation(myLocation);
1882 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08001883 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887
Mike Lockwoode97ae402010-09-29 15:23:46 -04001888 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
1889 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001890 public void onPackageDisappeared(String packageName, int reason) {
1891 // remove all receivers associated with this package name
1892 synchronized (mLock) {
1893 ArrayList<Receiver> deadReceivers = null;
1894
1895 for (Receiver receiver : mReceivers.values()) {
1896 if (receiver.mPackageName.equals(packageName)) {
1897 if (deadReceivers == null) {
1898 deadReceivers = new ArrayList<Receiver>();
1899 }
1900 deadReceivers.add(receiver);
1901 }
1902 }
1903
1904 // perform removal outside of mReceivers loop
1905 if (deadReceivers != null) {
1906 for (Receiver receiver : deadReceivers) {
1907 removeUpdatesLocked(receiver);
1908 }
1909 }
1910 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001911 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04001912 };
1913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 // Wake locks
1915
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001916 private void incrementPendingBroadcasts() {
1917 synchronized (mWakeLock) {
1918 if (mPendingBroadcasts++ == 0) {
1919 try {
1920 mWakeLock.acquire();
1921 log("Acquired wakelock");
1922 } catch (Exception e) {
1923 // This is to catch a runtime exception thrown when we try to release an
1924 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001925 Slog.e(TAG, "exception in acquireWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001926 }
1927 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001928 }
1929 }
1930
1931 private void decrementPendingBroadcasts() {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001932 synchronized (mWakeLock) {
Mike Lockwood48f17512009-04-23 09:12:08 -07001933 if (--mPendingBroadcasts == 0) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001934 try {
1935 // Release wake lock
1936 if (mWakeLock.isHeld()) {
1937 mWakeLock.release();
1938 log("Released wakelock");
1939 } else {
1940 log("Can't release wakelock again!");
1941 }
1942 } catch (Exception e) {
1943 // This is to catch a runtime exception thrown when we try to release an
1944 // already released lock.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001945 Slog.e(TAG, "exception in releaseWakeLock()", e);
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001946 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001947 }
1948 }
1949 }
1950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 // Geocoder
1952
Nick Pellye0fd6932012-07-11 10:26:13 -07001953 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04001954 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07001955 return mGeocodeProvider != null;
1956 }
1957
Nick Pellye0fd6932012-07-11 10:26:13 -07001958 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001960 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001961 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001962 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
1963 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001965 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967
Mike Lockwooda55c3212009-04-15 11:10:11 -04001968
Nick Pellye0fd6932012-07-11 10:26:13 -07001969 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04001971 double lowerLeftLatitude, double lowerLeftLongitude,
1972 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05001973 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04001974
1975 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001976 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
1977 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
1978 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04001980 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
1982
1983 // Mock Providers
1984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 private void checkMockPermissionsSafe() {
1986 boolean allowMocks = Settings.Secure.getInt(mContext.getContentResolver(),
1987 Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 1;
1988 if (!allowMocks) {
1989 throw new SecurityException("Requires ACCESS_MOCK_LOCATION secure setting");
1990 }
1991
1992 if (mContext.checkCallingPermission(ACCESS_MOCK_LOCATION) !=
1993 PackageManager.PERMISSION_GRANTED) {
1994 throw new SecurityException("Requires ACCESS_MOCK_LOCATION permission");
Nick Pellye0fd6932012-07-11 10:26:13 -07001995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997
Nick Pellye0fd6932012-07-11 10:26:13 -07001998 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001999 public void addTestProvider(String name, ProviderProperties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 checkMockPermissionsSafe();
2001
Mike Lockwooda4903f22010-02-17 06:42:23 -05002002 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2003 throw new IllegalArgumentException("Cannot mock the passive location provider");
2004 }
2005
Mike Lockwood86328a92009-10-23 08:38:25 -04002006 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002007 synchronized (mLock) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002008 MockProvider provider = new MockProvider(name, this, properties);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002009 // remove the real provider if we are replacing GPS or network provider
2010 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002011 || LocationManager.NETWORK_PROVIDER.equals(name)
2012 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002013 LocationProviderInterface p = mProvidersByName.get(name);
2014 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002015 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002016 }
2017 }
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002018 if (mProvidersByName.get(name) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2020 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002021 addProviderLocked(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002022 mMockProviders.put(name, provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002023 mLastLocation.put(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 updateProvidersLocked();
2025 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002026 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028
Nick Pellye0fd6932012-07-11 10:26:13 -07002029 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 public void removeTestProvider(String provider) {
2031 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002032 synchronized (mLock) {
You Kima6d0b6f2012-10-28 03:58:44 +09002033 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002034 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2036 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002037 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002038 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002039
2040 // reinstate real provider if available
2041 LocationProviderInterface realProvider = mRealProviders.get(provider);
2042 if (realProvider != null) {
2043 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002044 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002045 mLastLocation.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002047 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049 }
2050
Nick Pellye0fd6932012-07-11 10:26:13 -07002051 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 public void setTestProviderLocation(String provider, Location loc) {
2053 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002054 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002055 MockProvider mockProvider = mMockProviders.get(provider);
2056 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2058 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002059 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2060 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002061 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002062 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
2064 }
2065
Nick Pellye0fd6932012-07-11 10:26:13 -07002066 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 public void clearTestProviderLocation(String provider) {
2068 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002069 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002070 MockProvider mockProvider = mMockProviders.get(provider);
2071 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2073 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002074 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076 }
2077
Nick Pellye0fd6932012-07-11 10:26:13 -07002078 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 public void setTestProviderEnabled(String provider, boolean enabled) {
2080 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002081 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002082 MockProvider mockProvider = mMockProviders.get(provider);
2083 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2085 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002086 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002088 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 mEnabledProviders.add(provider);
2090 mDisabledProviders.remove(provider);
2091 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002092 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 mEnabledProviders.remove(provider);
2094 mDisabledProviders.add(provider);
2095 }
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 clearTestProviderEnabled(String provider) {
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 Lockwood86328a92009-10-23 08:38:25 -04002109 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 mEnabledProviders.remove(provider);
2111 mDisabledProviders.remove(provider);
2112 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002113 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115 }
2116
Nick Pellye0fd6932012-07-11 10:26:13 -07002117 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
2119 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002120 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002121 MockProvider mockProvider = mMockProviders.get(provider);
2122 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2124 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002125 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127 }
2128
Nick Pellye0fd6932012-07-11 10:26:13 -07002129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 public void clearTestProviderStatus(String provider) {
2131 checkMockPermissionsSafe();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002132 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002133 MockProvider mockProvider = mMockProviders.get(provider);
2134 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2136 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002137 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
2139 }
2140
2141 private void log(String log) {
2142 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002143 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002146
2147 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2149 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2150 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002151 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 + Binder.getCallingPid()
2153 + ", uid=" + Binder.getCallingUid());
2154 return;
2155 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002156
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002157 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002160 for (Receiver receiver : mReceivers.values()) {
2161 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 pw.println(" Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002164 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2165 pw.println(" " + entry.getKey() + ":");
2166 for (UpdateRecord record : entry.getValue()) {
2167 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
2169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002171 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2172 String provider = entry.getKey();
2173 Location location = entry.getValue();
2174 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002176
Nick Pellye0fd6932012-07-11 10:26:13 -07002177 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 if (mEnabledProviders.size() > 0) {
2180 pw.println(" Enabled Providers:");
2181 for (String i : mEnabledProviders) {
2182 pw.println(" " + i);
2183 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186 if (mDisabledProviders.size() > 0) {
2187 pw.println(" Disabled Providers:");
2188 for (String i : mDisabledProviders) {
2189 pw.println(" " + i);
2190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002192 pw.append(" ");
2193 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 if (mMockProviders.size() > 0) {
2195 pw.println(" Mock Providers:");
2196 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002197 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
2199 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002200
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002201 pw.append(" fudger: ");
2202 mLocationFudger.dump(fd, pw, args);
2203
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002204 if (args.length > 0 && "short".equals(args[0])) {
2205 return;
2206 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002207 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002208 pw.print(provider.getName() + " Internal State");
2209 if (provider instanceof LocationProviderProxy) {
2210 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2211 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002212 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002213 pw.println(":");
2214 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
2217 }
2218}