blob: 36ec2eb9a192f446f78da467dd74aa57e7410f36 [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
Svet Ganovadc1cf42015-06-15 16:36:24 -070019import android.content.pm.PackageManagerInternal;
destradaaea8a8a62014-06-23 18:19:03 -070020import com.android.internal.content.PackageMonitor;
21import com.android.internal.location.ProviderProperties;
22import com.android.internal.location.ProviderRequest;
23import com.android.internal.os.BackgroundThread;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070024import com.android.internal.util.ArrayUtils;
destradaaa4fa3b52014-07-09 10:46:39 -070025import com.android.server.location.ActivityRecognitionProxy;
destradaaea8a8a62014-06-23 18:19:03 -070026import com.android.server.location.FlpHardwareProvider;
27import com.android.server.location.FusedProxy;
28import com.android.server.location.GeocoderProxy;
29import com.android.server.location.GeofenceManager;
30import com.android.server.location.GeofenceProxy;
Lifu Tang818aa2c2016-02-01 01:52:00 -080031import com.android.server.location.GnssLocationProvider;
32import com.android.server.location.GnssMeasurementsProvider;
33import com.android.server.location.GnssNavigationMessageProvider;
destradaaea8a8a62014-06-23 18:19:03 -070034import com.android.server.location.LocationBlacklist;
35import com.android.server.location.LocationFudger;
36import com.android.server.location.LocationProviderInterface;
37import com.android.server.location.LocationProviderProxy;
38import com.android.server.location.LocationRequestStatistics;
39import com.android.server.location.LocationRequestStatistics.PackageProviderKey;
40import com.android.server.location.LocationRequestStatistics.PackageStatistics;
41import com.android.server.location.MockProvider;
42import com.android.server.location.PassiveProvider;
43
Dianne Hackborna06de0f2012-12-11 16:34:47 -080044import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070046import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.ContentResolver;
48import android.content.Context;
49import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070050import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070051import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050052import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070054import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050055import android.content.pm.ResolveInfo;
56import android.content.pm.Signature;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050057import android.content.res.Resources;
Brian Muramatsubb95cb92012-08-29 10:43:21 -070058import android.database.ContentObserver;
destradaaa4fa3b52014-07-09 10:46:39 -070059import android.hardware.location.ActivityRecognitionHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.location.Address;
Mike Lockwood03ca2162010-04-01 08:10:09 -070061import android.location.Criteria;
Mike Lockwood34901402010-01-04 12:14:21 -050062import android.location.GeocoderParams;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070063import android.location.Geofence;
Lifu Tang818aa2c2016-02-01 01:52:00 -080064import android.location.IGnssMeasurementsListener;
Lifu Tang30f95a72016-01-07 23:20:38 -080065import android.location.IGnssStatusListener;
66import android.location.IGnssStatusProvider;
Wei Liu5241a4c2015-05-11 14:00:36 -070067import android.location.IGpsGeofenceHardware;
Lifu Tang818aa2c2016-02-01 01:52:00 -080068import android.location.IGnssNavigationMessageListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.location.ILocationListener;
70import android.location.ILocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040071import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.location.Location;
73import android.location.LocationManager;
74import android.location.LocationProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070075import android.location.LocationRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Binder;
77import android.os.Bundle;
78import android.os.Handler;
79import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070080import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Message;
82import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070083import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070085import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070086import android.os.UserHandle;
Amith Yamasanib27528d2014-06-05 15:02:10 -070087import android.os.UserManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070088import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080091import android.util.Slog;
Amith Yamasanib27528d2014-06-05 15:02:10 -070092
Mike Lockwood43e33f22010-03-26 10:41:48 -040093import java.io.FileDescriptor;
94import java.io.PrintWriter;
95import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070096import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -040097import java.util.HashMap;
98import java.util.HashSet;
99import java.util.List;
100import java.util.Map;
Mike Lockwood43e33f22010-03-26 10:41:48 -0400101import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103/**
104 * The service class that manages LocationProviders and issues location
105 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 */
Victoria Lease5cd731a2012-12-19 15:04:21 -0800107public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -0800109 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700110
111 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Victoria Lease37425c32012-10-16 16:08:48 -0700113 // Location resolution level: no location data whatsoever
114 private static final int RESOLUTION_LEVEL_NONE = 0;
115 // Location resolution level: coarse location data only
116 private static final int RESOLUTION_LEVEL_COARSE = 1;
117 // Location resolution level: fine location data
118 private static final int RESOLUTION_LEVEL_FINE = 2;
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700121 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700123 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400124 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700125 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
126
127 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700128 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700129 private static final String FUSED_LOCATION_SERVICE_ACTION =
130 "com.android.location.service.FusedLocationProvider";
131
132 private static final int MSG_LOCATION_CHANGED = 1;
133
David Christie1b9b7b12013-04-15 15:31:11 -0700134 private static final long NANOS_PER_MILLI = 1000000L;
135
David Christie0b837452013-07-29 16:02:13 -0700136 // The maximum interval a location request can have and still be considered "high power".
137 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
138
Nick Pellyf1be6862012-05-15 10:53:42 -0700139 // Location Providers may sometimes deliver location updates
140 // slightly faster that requested - provide grace period so
141 // we don't unnecessarily filter events that are otherwise on
142 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700143 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700144
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700145 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
146
147 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800148 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700149
150 // used internally for synchronization
151 private final Object mLock = new Object();
152
Victoria Lease5cd731a2012-12-19 15:04:21 -0800153 // --- fields below are final after systemReady() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700154 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700155 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700156 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700157 private PowerManager mPowerManager;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700158 private UserManager mUserManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700159 private GeocoderProxy mGeocodeProvider;
Lifu Tang30f95a72016-01-07 23:20:38 -0800160 private IGnssStatusProvider mGnssStatusProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700161 private INetInitiatedListener mNetInitiatedListener;
162 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700163 private PassiveProvider mPassiveProvider; // track passive provider for special cases
164 private LocationBlacklist mBlacklist;
Lifu Tang818aa2c2016-02-01 01:52:00 -0800165 private GnssMeasurementsProvider mGnssMeasurementsProvider;
166 private GnssNavigationMessageProvider mGnssNavigationMessageProvider;
Wei Liu5241a4c2015-05-11 14:00:36 -0700167 private IGpsGeofenceHardware mGpsGeofenceProxy;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700168
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700169 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 // Set of providers that are explicitly enabled
171 private final Set<String> mEnabledProviders = new HashSet<String>();
172
173 // Set of providers that are explicitly disabled
174 private final Set<String> mDisabledProviders = new HashSet<String>();
175
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700176 // Mock (test) providers
177 private final HashMap<String, MockProvider> mMockProviders =
178 new HashMap<String, MockProvider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700180 // all receivers
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400181 private final HashMap<Object, Receiver> mReceivers = new HashMap<Object, Receiver>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700183 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500184 private final ArrayList<LocationProviderInterface> mProviders =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700185 new ArrayList<LocationProviderInterface>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400186
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700187 // real providers, saved here when mocked out
188 private final HashMap<String, LocationProviderInterface> mRealProviders =
189 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700191 // mapping from provider name to provider
192 private final HashMap<String, LocationProviderInterface> mProvidersByName =
193 new HashMap<String, LocationProviderInterface>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700195 // mapping from provider name to all its UpdateRecords
196 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
197 new HashMap<String, ArrayList<UpdateRecord>>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700198
David Christie2ff96af2014-01-30 16:09:37 -0800199 private final LocationRequestStatistics mRequestStatistics = new LocationRequestStatistics();
200
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700201 // mapping from provider name to last known location
202 private final HashMap<String, Location> mLastLocation = new HashMap<String, Location>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
David Christie1b9b7b12013-04-15 15:31:11 -0700204 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
205 // locations stored here are not fudged for coarse permissions.
206 private final HashMap<String, Location> mLastLocationCoarseInterval =
207 new HashMap<String, Location>();
208
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700209 // all providers that operate over proxy, for authorizing incoming location
210 private final ArrayList<LocationProviderProxy> mProxyProviders =
211 new ArrayList<LocationProviderProxy>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Victoria Lease38389b62012-09-30 11:44:22 -0700213 // current active user on the device - other users are denied location data
Xiaohui Chena4490622015-09-22 15:29:31 -0700214 private int mCurrentUserId = UserHandle.USER_SYSTEM;
215 private int[] mCurrentUserProfiles = new int[] { UserHandle.USER_SYSTEM };
Victoria Lease38389b62012-09-30 11:44:22 -0700216
Lifu Tang9363b942016-02-16 18:07:00 -0800217 private GnssLocationProvider.GnssSystemInfoProvider mGnssSystemInfoProvider;
Lifu Tang82f893d2016-01-21 18:15:33 -0800218
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700219 public LocationManagerService(Context context) {
220 super();
221 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800222 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800223
Svet Ganovadc1cf42015-06-15 16:36:24 -0700224 // Let the package manager query which are the default location
225 // providers as they get certain permissions granted by default.
226 PackageManagerInternal packageManagerInternal = LocalServices.getService(
227 PackageManagerInternal.class);
228 packageManagerInternal.setLocationPackagesProvider(
229 new PackageManagerInternal.PackagesProvider() {
230 @Override
231 public String[] getPackages(int userId) {
232 return mContext.getResources().getStringArray(
233 com.android.internal.R.array.config_locationProviderPackageNames);
234 }
235 });
236
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700237 if (D) Log.d(TAG, "Constructed");
238
239 // most startup is deferred until systemReady()
240 }
241
Svetoslav Ganova0027152013-06-25 14:59:53 -0700242 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700243 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800244 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700245
Victoria Lease5cd731a2012-12-19 15:04:21 -0800246 // fetch package manager
247 mPackageManager = mContext.getPackageManager();
248
Victoria Lease0aa28602013-05-29 15:28:26 -0700249 // fetch power manager
250 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800251
252 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700253 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800254
255 // prepare mLocationHandler's dependents
256 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
257 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
258 mBlacklist.init();
259 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
260
Dianne Hackbornc2293022013-02-06 23:14:49 -0800261 // Monitor for app ops mode changes.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700262 AppOpsManager.OnOpChangedListener callback
263 = new AppOpsManager.OnOpChangedInternalListener() {
264 public void onOpChanged(int op, String packageName) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800265 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700266 for (Receiver receiver : mReceivers.values()) {
267 receiver.updateMonitoring(true);
268 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800269 applyAllProviderRequirementsLocked();
270 }
271 }
272 };
273 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
274
David Christieb870dbf2015-06-22 12:42:53 -0700275 PackageManager.OnPermissionsChangedListener permissionListener
276 = new PackageManager.OnPermissionsChangedListener() {
277 @Override
278 public void onPermissionsChanged(final int uid) {
279 synchronized (mLock) {
280 applyAllProviderRequirementsLocked();
281 }
282 }
283 };
284 mPackageManager.addOnPermissionsChangeListener(permissionListener);
285
Amith Yamasanib27528d2014-06-05 15:02:10 -0700286 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
287 updateUserProfiles(mCurrentUserId);
288
Victoria Lease5cd731a2012-12-19 15:04:21 -0800289 // prepare providers
290 loadProvidersLocked();
291 updateProvidersLocked();
292 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700293
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700294 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700295 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700296 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700297 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800298 @Override
299 public void onChange(boolean selfChange) {
300 synchronized (mLock) {
301 updateProvidersLocked();
302 }
303 }
304 }, UserHandle.USER_ALL);
305 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700306
Victoria Lease38389b62012-09-30 11:44:22 -0700307 // listen for user change
308 IntentFilter intentFilter = new IntentFilter();
309 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700310 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
311 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
destradaab9026982015-08-27 17:34:54 -0700312 intentFilter.addAction(Intent.ACTION_SHUTDOWN);
Victoria Lease38389b62012-09-30 11:44:22 -0700313
314 mContext.registerReceiverAsUser(new BroadcastReceiver() {
315 @Override
316 public void onReceive(Context context, Intent intent) {
317 String action = intent.getAction();
318 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
319 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
Amith Yamasanib27528d2014-06-05 15:02:10 -0700320 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
321 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
322 updateUserProfiles(mCurrentUserId);
destradaab9026982015-08-27 17:34:54 -0700323 } else if (Intent.ACTION_SHUTDOWN.equals(action)) {
324 shutdownComponents();
Victoria Lease38389b62012-09-30 11:44:22 -0700325 }
326 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800327 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700328 }
329
Amith Yamasanib27528d2014-06-05 15:02:10 -0700330 /**
destradaab9026982015-08-27 17:34:54 -0700331 * Provides a way for components held by the {@link LocationManagerService} to clean-up
332 * gracefully on system's shutdown.
333 *
334 * NOTES:
335 * 1) Only provides a chance to clean-up on an opt-in basis. This guarantees back-compat
336 * support for components that do not wish to handle such event.
337 */
338 private void shutdownComponents() {
339 if(D) Log.d(TAG, "Shutting down components...");
340
341 LocationProviderInterface gpsProvider = mProvidersByName.get(LocationManager.GPS_PROVIDER);
342 if (gpsProvider != null && gpsProvider.isEnabled()) {
343 gpsProvider.disable();
344 }
345
destradaa2e385072015-10-14 16:45:58 -0700346 // it is needed to check if FLP HW provider is supported before accessing the instance, this
347 // avoids an exception to be thrown by the singleton factory method
348 if (FlpHardwareProvider.isSupported()) {
349 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
destradaab9026982015-08-27 17:34:54 -0700350 flpHardwareProvider.cleanup();
351 }
352 }
353
354 /**
Amith Yamasanib27528d2014-06-05 15:02:10 -0700355 * Makes a list of userids that are related to the current user. This is
356 * relevant when using managed profiles. Otherwise the list only contains
357 * the current user.
358 *
359 * @param currentUserId the current user, who might have an alter-ego.
360 */
361 void updateUserProfiles(int currentUserId) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700362 int[] profileIds = mUserManager.getProfileIdsWithDisabled(currentUserId);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700363 synchronized (mLock) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700364 mCurrentUserProfiles = profileIds;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700365 }
366 }
367
368 /**
369 * Checks if the specified userId matches any of the current foreground
370 * users stored in mCurrentUserProfiles.
371 */
372 private boolean isCurrentProfile(int userId) {
373 synchronized (mLock) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700374 return ArrayUtils.contains(mCurrentUserProfiles, userId);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700375 }
376 }
377
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500378 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
379 PackageManager pm = mContext.getPackageManager();
380 String systemPackageName = mContext.getPackageName();
381 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
382
383 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
384 new Intent(FUSED_LOCATION_SERVICE_ACTION),
385 PackageManager.GET_META_DATA, mCurrentUserId);
386 for (ResolveInfo rInfo : rInfos) {
387 String packageName = rInfo.serviceInfo.packageName;
388
389 // Check that the signature is in the list of supported sigs. If it's not in
390 // this list the standard provider binding logic won't bind to it.
391 try {
392 PackageInfo pInfo;
393 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
394 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
395 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
396 ", but has wrong signature, ignoring");
397 continue;
398 }
399 } catch (NameNotFoundException e) {
400 Log.e(TAG, "missing package: " + packageName);
401 continue;
402 }
403
404 // Get the version info
405 if (rInfo.serviceInfo.metaData == null) {
406 Log.w(TAG, "Found fused provider without metadata: " + packageName);
407 continue;
408 }
409
410 int version = rInfo.serviceInfo.metaData.getInt(
411 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
412 if (version == 0) {
413 // This should be the fallback fused location provider.
414
415 // Make sure it's in the system partition.
416 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
417 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
418 continue;
419 }
420
421 // Check that the fallback is signed the same as the OS
422 // as a proxy for coreApp="true"
423 if (pm.checkSignatures(systemPackageName, packageName)
424 != PackageManager.SIGNATURE_MATCH) {
425 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
426 + packageName);
427 continue;
428 }
429
430 // Found a valid fallback.
431 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
432 return;
433 } else {
434 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
435 }
436 }
437
438 throw new IllegalStateException("Unable to find a fused location provider that is in the "
439 + "system partition with version 0 and signed with the platform certificate. "
440 + "Such a package is needed to provide a default fused location provider in the "
441 + "event that no other fused location provider has been installed or is currently "
442 + "available. For example, coreOnly boot mode when decrypting the data "
443 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
444 }
445
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700446 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700447 // create a passive location provider, which is always enabled
448 PassiveProvider passiveProvider = new PassiveProvider(this);
449 addProviderLocked(passiveProvider);
450 mEnabledProviders.add(passiveProvider.getName());
451 mPassiveProvider = passiveProvider;
452
Lifu Tang30f95a72016-01-07 23:20:38 -0800453 if (GnssLocationProvider.isSupported()) {
Wei Liu5241a4c2015-05-11 14:00:36 -0700454 // Create a gps location provider
Lifu Tang30f95a72016-01-07 23:20:38 -0800455 GnssLocationProvider gnssProvider = new GnssLocationProvider(mContext, this,
Wei Liu5241a4c2015-05-11 14:00:36 -0700456 mLocationHandler.getLooper());
Lifu Tang9363b942016-02-16 18:07:00 -0800457 mGnssSystemInfoProvider = gnssProvider.getGnssSystemInfoProvider();
Lifu Tang30f95a72016-01-07 23:20:38 -0800458 mGnssStatusProvider = gnssProvider.getGnssStatusProvider();
459 mNetInitiatedListener = gnssProvider.getNetInitiatedListener();
460 addProviderLocked(gnssProvider);
461 mRealProviders.put(LocationManager.GPS_PROVIDER, gnssProvider);
Lifu Tang818aa2c2016-02-01 01:52:00 -0800462 mGnssMeasurementsProvider = gnssProvider.getGnssMeasurementsProvider();
463 mGnssNavigationMessageProvider = gnssProvider.getGnssNavigationMessageProvider();
Lifu Tang30f95a72016-01-07 23:20:38 -0800464 mGpsGeofenceProxy = gnssProvider.getGpsGeofenceProxy();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700465 }
466
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700467 /*
468 Load package name(s) containing location provider support.
469 These packages can contain services implementing location providers:
470 Geocoder Provider, Network Location Provider, and
471 Fused Location Provider. They will each be searched for
472 service components implementing these providers.
473 The location framework also has support for installation
474 of new location providers at run-time. The new package does not
475 have to be explicitly listed here, however it must have a signature
476 that matches the signature of at least one package on this list.
477 */
478 Resources resources = mContext.getResources();
479 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500480 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700481 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500482 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
483 Arrays.toString(pkgs));
484 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
485
486 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700487
488 // bind to network provider
489 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
490 mContext,
491 LocationManager.NETWORK_PROVIDER,
492 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700493 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
494 com.android.internal.R.string.config_networkLocationProviderPackageName,
495 com.android.internal.R.array.config_locationProviderPackageNames,
496 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700497 if (networkProvider != null) {
498 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
499 mProxyProviders.add(networkProvider);
500 addProviderLocked(networkProvider);
501 } else {
502 Slog.w(TAG, "no network location provider found");
503 }
504
505 // bind to fused provider
506 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
507 mContext,
508 LocationManager.FUSED_PROVIDER,
509 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700510 com.android.internal.R.bool.config_enableFusedLocationOverlay,
511 com.android.internal.R.string.config_fusedLocationProviderPackageName,
512 com.android.internal.R.array.config_locationProviderPackageNames,
513 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700514 if (fusedLocationProvider != null) {
515 addProviderLocked(fusedLocationProvider);
516 mProxyProviders.add(fusedLocationProvider);
517 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700518 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700519 } else {
520 Slog.e(TAG, "no fused location provider found",
521 new IllegalStateException("Location service needs a fused location provider"));
522 }
523
524 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700525 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
526 com.android.internal.R.bool.config_enableGeocoderOverlay,
527 com.android.internal.R.string.config_geocoderProviderPackageName,
528 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800529 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700530 if (mGeocodeProvider == null) {
531 Slog.e(TAG, "no geocoder provider found");
532 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700533
destradaaa4fa3b52014-07-09 10:46:39 -0700534 // bind to fused hardware provider if supported
destradaabeea4422014-07-30 18:17:21 -0700535 // in devices without support, requesting an instance of FlpHardwareProvider will raise an
536 // exception, so make sure we only do that when supported
537 FlpHardwareProvider flpHardwareProvider;
destradaa5ce66d82014-05-28 18:24:08 -0700538 if (FlpHardwareProvider.isSupported()) {
destradaabeea4422014-07-30 18:17:21 -0700539 flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
destradaaf9a274c2014-07-25 15:11:56 -0700540 FusedProxy fusedProxy = FusedProxy.createAndBind(
541 mContext,
542 mLocationHandler,
543 flpHardwareProvider.getLocationHardware(),
544 com.android.internal.R.bool.config_enableHardwareFlpOverlay,
545 com.android.internal.R.string.config_hardwareFlpPackageName,
546 com.android.internal.R.array.config_locationProviderPackageNames);
547 if (fusedProxy == null) {
destradaa6b4893a2016-05-03 15:33:43 -0700548 Slog.d(TAG, "Unable to bind FusedProxy.");
destradaaf9a274c2014-07-25 15:11:56 -0700549 }
destradaacfbdcd22014-04-30 11:29:11 -0700550 } else {
destradaabeea4422014-07-30 18:17:21 -0700551 flpHardwareProvider = null;
destradaa6b4893a2016-05-03 15:33:43 -0700552 Slog.d(TAG, "FLP HAL not supported");
destradaaf9a274c2014-07-25 15:11:56 -0700553 }
554
555 // bind to geofence provider
556 GeofenceProxy provider = GeofenceProxy.createAndBind(
557 mContext,com.android.internal.R.bool.config_enableGeofenceOverlay,
558 com.android.internal.R.string.config_geofenceProviderPackageName,
559 com.android.internal.R.array.config_locationProviderPackageNames,
560 mLocationHandler,
Wei Liu5241a4c2015-05-11 14:00:36 -0700561 mGpsGeofenceProxy,
destradaabeea4422014-07-30 18:17:21 -0700562 flpHardwareProvider != null ? flpHardwareProvider.getGeofenceHardware() : null);
destradaaf9a274c2014-07-25 15:11:56 -0700563 if (provider == null) {
destradaa6b4893a2016-05-03 15:33:43 -0700564 Slog.d(TAG, "Unable to bind FLP Geofence proxy.");
destradaa0682809a2013-08-12 18:50:30 -0700565 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900566
destradaa6e2fe752015-06-23 17:25:53 -0700567 // bind to hardware activity recognition
568 boolean activityRecognitionHardwareIsSupported = ActivityRecognitionHardware.isSupported();
569 ActivityRecognitionHardware activityRecognitionHardware = null;
570 if (activityRecognitionHardwareIsSupported) {
571 activityRecognitionHardware = ActivityRecognitionHardware.getInstance(mContext);
destradaaa4fa3b52014-07-09 10:46:39 -0700572 } else {
destradaa6b4893a2016-05-03 15:33:43 -0700573 Slog.d(TAG, "Hardware Activity-Recognition not supported.");
destradaaa4fa3b52014-07-09 10:46:39 -0700574 }
destradaa6e2fe752015-06-23 17:25:53 -0700575 ActivityRecognitionProxy proxy = ActivityRecognitionProxy.createAndBind(
576 mContext,
577 mLocationHandler,
578 activityRecognitionHardwareIsSupported,
579 activityRecognitionHardware,
580 com.android.internal.R.bool.config_enableActivityRecognitionHardwareOverlay,
581 com.android.internal.R.string.config_activityRecognitionHardwarePackageName,
582 com.android.internal.R.array.config_locationProviderPackageNames);
583 if (proxy == null) {
destradaa6b4893a2016-05-03 15:33:43 -0700584 Slog.d(TAG, "Unable to bind ActivityRecognitionProxy.");
destradaa6e2fe752015-06-23 17:25:53 -0700585 }
destradaaa4fa3b52014-07-09 10:46:39 -0700586
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900587 String[] testProviderStrings = resources.getStringArray(
588 com.android.internal.R.array.config_testLocationProviders);
589 for (String testProviderString : testProviderStrings) {
590 String fragments[] = testProviderString.split(",");
591 String name = fragments[0].trim();
592 if (mProvidersByName.get(name) != null) {
593 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
594 }
595 ProviderProperties properties = new ProviderProperties(
596 Boolean.parseBoolean(fragments[1]) /* requiresNetwork */,
597 Boolean.parseBoolean(fragments[2]) /* requiresSatellite */,
598 Boolean.parseBoolean(fragments[3]) /* requiresCell */,
599 Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */,
600 Boolean.parseBoolean(fragments[5]) /* supportsAltitude */,
601 Boolean.parseBoolean(fragments[6]) /* supportsSpeed */,
602 Boolean.parseBoolean(fragments[7]) /* supportsBearing */,
603 Integer.parseInt(fragments[8]) /* powerRequirement */,
604 Integer.parseInt(fragments[9]) /* accuracy */);
605 addTestProviderLocked(name, properties);
606 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700607 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700610 * Called when the device's active user changes.
611 * @param userId the new active user's UserId
612 */
613 private void switchUser(int userId) {
Jianzheng Zhoud5c69462013-10-10 14:02:09 +0800614 if (mCurrentUserId == userId) {
615 return;
616 }
Victoria Lease83762d22012-10-03 13:51:17 -0700617 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800618 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700619 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700620 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700621 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700622 for (LocationProviderInterface p : mProviders) {
Amith Yamasanib27528d2014-06-05 15:02:10 -0700623 updateProviderListenersLocked(p.getName(), false);
Victoria Leaseb711d572012-10-02 13:14:11 -0700624 }
Victoria Lease38389b62012-09-30 11:44:22 -0700625 mCurrentUserId = userId;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700626 updateUserProfiles(userId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700627 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700628 }
629 }
630
631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
633 * location updates.
634 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700635 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700636 final int mUid; // uid of receiver
637 final int mPid; // pid of receiver
638 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700639 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 final ILocationListener mListener;
642 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700643 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700644 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700646
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400647 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700648
David Christie0b837452013-07-29 16:02:13 -0700649 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700650 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700651 // True if app ops has started monitoring this receiver for high power (gps) locations.
652 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700653 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700654 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700656 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700657 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700660 if (listener != null) {
661 mKey = listener.asBinder();
662 } else {
663 mKey = intent;
664 }
Victoria Lease37425c32012-10-16 16:08:48 -0700665 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700666 mUid = uid;
667 mPid = pid;
668 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700669 if (workSource != null && workSource.size() <= 0) {
670 workSource = null;
671 }
672 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700673 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700674
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700675 updateMonitoring(true);
676
Victoria Lease0aa28602013-05-29 15:28:26 -0700677 // construct/configure wakelock
678 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700679 if (workSource == null) {
680 workSource = new WorkSource(mUid, mPackageName);
681 }
682 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684
685 @Override
686 public boolean equals(Object otherObj) {
687 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700688 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
690 return false;
691 }
692
693 @Override
694 public int hashCode() {
695 return mKey.hashCode();
696 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 @Override
699 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700700 StringBuilder s = new StringBuilder();
701 s.append("Reciever[");
702 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700704 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700706 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700708 for (String p : mUpdateRecords.keySet()) {
709 s.append(" ").append(mUpdateRecords.get(p).toString());
710 }
711 s.append("]");
712 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
David Christie15b31912013-08-13 15:54:32 -0700715 /**
716 * Update AppOp monitoring for this receiver.
717 *
718 * @param allow If true receiver is currently active, if false it's been removed.
719 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700720 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700721 if (mHideFromAppOps) {
722 return;
723 }
724
David Christie15b31912013-08-13 15:54:32 -0700725 boolean requestingLocation = false;
726 boolean requestingHighPowerLocation = false;
727 if (allow) {
728 // See if receiver has any enabled update records. Also note if any update records
729 // are high power (has a high power provider with an interval under a threshold).
730 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
731 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
732 requestingLocation = true;
733 LocationProviderInterface locationProvider
David Christie2ff96af2014-01-30 16:09:37 -0800734 = mProvidersByName.get(updateRecord.mProvider);
David Christie15b31912013-08-13 15:54:32 -0700735 ProviderProperties properties = locationProvider != null
736 ? locationProvider.getProperties() : null;
737 if (properties != null
738 && properties.mPowerRequirement == Criteria.POWER_HIGH
739 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
740 requestingHighPowerLocation = true;
741 break;
742 }
743 }
744 }
745 }
746
David Christie0b837452013-07-29 16:02:13 -0700747 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700748 mOpMonitoring = updateMonitoring(
749 requestingLocation,
750 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700751 AppOpsManager.OP_MONITOR_LOCATION);
752
753 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700754 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700755 mOpHighPowerMonitoring = updateMonitoring(
756 requestingHighPowerLocation,
757 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700758 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700759 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700760 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700761 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
762 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
763 }
David Christie0b837452013-07-29 16:02:13 -0700764 }
765
766 /**
767 * Update AppOps monitoring for a single location request and op type.
768 *
769 * @param allowMonitoring True if monitoring is allowed for this request/op.
770 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
771 * @param op AppOps code for the op to update.
772 * @return True if monitoring is on for this request/op after updating.
773 */
774 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
775 int op) {
776 if (!currentlyMonitoring) {
777 if (allowMonitoring) {
778 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
779 == AppOpsManager.MODE_ALLOWED;
780 }
781 } else {
782 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
783 != AppOpsManager.MODE_ALLOWED) {
784 mAppOps.finishOp(op, mUid, mPackageName);
785 return false;
786 }
787 }
788
789 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700790 }
791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 public boolean isListener() {
793 return mListener != null;
794 }
795
796 public boolean isPendingIntent() {
797 return mPendingIntent != null;
798 }
799
800 public ILocationListener getListener() {
801 if (mListener != null) {
802 return mListener;
803 }
804 throw new IllegalStateException("Request for non-existent listener");
805 }
806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
808 if (mListener != null) {
809 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700810 synchronized (this) {
811 // synchronize to ensure incrementPendingBroadcastsLocked()
812 // is called before decrementPendingBroadcasts()
813 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700814 // call this after broadcasting so we do not increment
815 // if we throw an exeption.
816 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 } catch (RemoteException e) {
819 return false;
820 }
821 } else {
822 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800823 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
825 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700826 synchronized (this) {
827 // synchronize to ensure incrementPendingBroadcastsLocked()
828 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700829 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700830 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700831 // call this after broadcasting so we do not increment
832 // if we throw an exeption.
833 incrementPendingBroadcastsLocked();
834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 } catch (PendingIntent.CanceledException e) {
836 return false;
837 }
838 }
839 return true;
840 }
841
842 public boolean callLocationChangedLocked(Location location) {
843 if (mListener != null) {
844 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700845 synchronized (this) {
846 // synchronize to ensure incrementPendingBroadcastsLocked()
847 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800848 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700849 // call this after broadcasting so we do not increment
850 // if we throw an exeption.
851 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 } catch (RemoteException e) {
854 return false;
855 }
856 } else {
857 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800858 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700860 synchronized (this) {
861 // synchronize to ensure incrementPendingBroadcastsLocked()
862 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700863 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700864 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700865 // call this after broadcasting so we do not increment
866 // if we throw an exeption.
867 incrementPendingBroadcastsLocked();
868 }
869 } catch (PendingIntent.CanceledException e) {
870 return false;
871 }
872 }
873 return true;
874 }
875
876 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -0700877 // First update AppOp monitoring.
878 // An app may get/lose location access as providers are enabled/disabled.
879 updateMonitoring(true);
880
Mike Lockwood48f17512009-04-23 09:12:08 -0700881 if (mListener != null) {
882 try {
883 synchronized (this) {
884 // synchronize to ensure incrementPendingBroadcastsLocked()
885 // is called before decrementPendingBroadcasts()
886 if (enabled) {
887 mListener.onProviderEnabled(provider);
888 } else {
889 mListener.onProviderDisabled(provider);
890 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700891 // call this after broadcasting so we do not increment
892 // if we throw an exeption.
893 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700894 }
895 } catch (RemoteException e) {
896 return false;
897 }
898 } else {
899 Intent providerIntent = new Intent();
900 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
901 try {
902 synchronized (this) {
903 // synchronize to ensure incrementPendingBroadcastsLocked()
904 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700905 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700906 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700907 // call this after broadcasting so we do not increment
908 // if we throw an exeption.
909 incrementPendingBroadcastsLocked();
910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 } catch (PendingIntent.CanceledException e) {
912 return false;
913 }
914 }
915 return true;
916 }
917
Nick Pellyf1be6862012-05-15 10:53:42 -0700918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700920 if (D) Log.d(TAG, "Location listener died");
921
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400922 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 removeUpdatesLocked(this);
924 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700925 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700926 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700927 }
928 }
929
Nick Pellye0fd6932012-07-11 10:26:13 -0700930 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700931 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
932 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400933 synchronized (this) {
934 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700935 }
936 }
937
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400938 // this must be called while synchronized by caller in a synchronized block
939 // containing the sending of the broadcaset
940 private void incrementPendingBroadcastsLocked() {
941 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700942 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400943 }
944 }
945
946 private void decrementPendingBroadcastsLocked() {
947 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700948 if (mWakeLock.isHeld()) {
949 mWakeLock.release();
950 }
951 }
952 }
953
954 public void clearPendingBroadcastsLocked() {
955 if (mPendingBroadcasts > 0) {
956 mPendingBroadcasts = 0;
957 if (mWakeLock.isHeld()) {
958 mWakeLock.release();
959 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700960 }
961 }
962 }
963
Nick Pellye0fd6932012-07-11 10:26:13 -0700964 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700965 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700966 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400967 //the receiver list if it is not found. If it is not found then the
968 //LocationListener was removed when it had a pending broadcast and should
969 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700970 synchronized (mLock) {
971 IBinder binder = listener.asBinder();
972 Receiver receiver = mReceivers.get(binder);
973 if (receiver != null) {
974 synchronized (receiver) {
975 // so wakelock calls will succeed
976 long identity = Binder.clearCallingIdentity();
977 receiver.decrementPendingBroadcastsLocked();
978 Binder.restoreCallingIdentity(identity);
David Christie2ff96af2014-01-30 16:09:37 -0800979 }
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
983
Lifu Tang82f893d2016-01-21 18:15:33 -0800984 /**
Lifu Tang9363b942016-02-16 18:07:00 -0800985 * Returns the system information of the GNSS hardware.
Lifu Tang82f893d2016-01-21 18:15:33 -0800986 */
987 @Override
Lifu Tang9363b942016-02-16 18:07:00 -0800988 public int getGnssYearOfHardware() {
Lifu Tang818aa2c2016-02-01 01:52:00 -0800989 if (mGnssNavigationMessageProvider != null) {
Lifu Tang9363b942016-02-16 18:07:00 -0800990 return mGnssSystemInfoProvider.getGnssYearOfHardware();
Lifu Tang82f893d2016-01-21 18:15:33 -0800991 } else {
992 return 0;
993 }
994 }
995
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700996 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400997 mProviders.add(provider);
998 mProvidersByName.put(provider.getName(), provider);
999 }
1000
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001001 private void removeProviderLocked(LocationProviderInterface provider) {
1002 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001003 mProviders.remove(provider);
1004 mProvidersByName.remove(provider.getName());
1005 }
1006
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001007 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001008 * Returns "true" if access to the specified location provider is allowed by the current
1009 * user's settings. Access to all location providers is forbidden to non-location-provider
1010 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001011 *
1012 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001013 * @return
1014 */
Victoria Lease09eeaec2013-02-05 11:34:13 -08001015 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 if (mEnabledProviders.contains(provider)) {
1017 return true;
1018 }
1019 if (mDisabledProviders.contains(provider)) {
1020 return false;
1021 }
1022 // Use system settings
1023 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024
Victoria Leaseb711d572012-10-02 13:14:11 -07001025 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001028 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001029 * Returns "true" if access to the specified location provider is allowed by the specified
1030 * user's settings. Access to all location providers is forbidden to non-location-provider
1031 * processes belonging to background users.
1032 *
1033 * @param provider the name of the location provider
1034 * @param uid the requestor's UID
1035 * @return
1036 */
1037 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001038 if (!isCurrentProfile(UserHandle.getUserId(uid)) && !isUidALocationProvider(uid)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001039 return false;
1040 }
1041 return isAllowedByCurrentUserSettingsLocked(provider);
1042 }
1043
1044 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001045 * Returns the permission string associated with the specified resolution level.
1046 *
1047 * @param resolutionLevel the resolution level
1048 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001049 */
Victoria Lease37425c32012-10-16 16:08:48 -07001050 private String getResolutionPermission(int resolutionLevel) {
1051 switch (resolutionLevel) {
1052 case RESOLUTION_LEVEL_FINE:
1053 return android.Manifest.permission.ACCESS_FINE_LOCATION;
1054 case RESOLUTION_LEVEL_COARSE:
1055 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
1056 default:
1057 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
Victoria Leaseda479c52012-10-15 15:24:16 -07001059 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001060
Victoria Leaseda479c52012-10-15 15:24:16 -07001061 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001062 * Returns the resolution level allowed to the given PID/UID pair.
1063 *
1064 * @param pid the PID
1065 * @param uid the UID
1066 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -07001067 */
Victoria Lease37425c32012-10-16 16:08:48 -07001068 private int getAllowedResolutionLevel(int pid, int uid) {
1069 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
1070 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1071 return RESOLUTION_LEVEL_FINE;
1072 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
1073 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1074 return RESOLUTION_LEVEL_COARSE;
1075 } else {
1076 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -07001077 }
Victoria Lease4fab68b2012-09-13 13:20:59 -07001078 }
1079
1080 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001081 * Returns the resolution level allowed to the caller
1082 *
1083 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -07001084 */
Victoria Lease37425c32012-10-16 16:08:48 -07001085 private int getCallerAllowedResolutionLevel() {
1086 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
1087 }
1088
1089 /**
1090 * Throw SecurityException if specified resolution level is insufficient to use geofences.
1091 *
1092 * @param allowedResolutionLevel resolution level allowed to caller
1093 */
1094 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
1095 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -07001096 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
1097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099
Victoria Lease37425c32012-10-16 16:08:48 -07001100 /**
1101 * Return the minimum resolution level required to use the specified location provider.
1102 *
1103 * @param provider the name of the location provider
1104 * @return minimum resolution level required for provider
1105 */
1106 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001107 if (LocationManager.GPS_PROVIDER.equals(provider) ||
1108 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
1109 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001110 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -07001111 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
1112 LocationManager.FUSED_PROVIDER.equals(provider)) {
1113 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001114 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001115 } else {
1116 // mock providers
1117 LocationProviderInterface lp = mMockProviders.get(provider);
1118 if (lp != null) {
1119 ProviderProperties properties = lp.getProperties();
1120 if (properties != null) {
1121 if (properties.mRequiresSatellite) {
1122 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001123 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -07001124 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
1125 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001126 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001127 }
1128 }
1129 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001130 }
Victoria Lease37425c32012-10-16 16:08:48 -07001131 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -07001132 }
1133
Victoria Lease37425c32012-10-16 16:08:48 -07001134 /**
1135 * Throw SecurityException if specified resolution level is insufficient to use the named
1136 * location provider.
1137 *
1138 * @param allowedResolutionLevel resolution level allowed to caller
1139 * @param providerName the name of the location provider
1140 */
1141 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
1142 String providerName) {
1143 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
1144 if (allowedResolutionLevel < requiredResolutionLevel) {
1145 switch (requiredResolutionLevel) {
1146 case RESOLUTION_LEVEL_FINE:
1147 throw new SecurityException("\"" + providerName + "\" location provider " +
1148 "requires ACCESS_FINE_LOCATION permission.");
1149 case RESOLUTION_LEVEL_COARSE:
1150 throw new SecurityException("\"" + providerName + "\" location provider " +
1151 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
1152 default:
1153 throw new SecurityException("Insufficient permission for \"" + providerName +
1154 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -07001155 }
1156 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001157 }
1158
David Christie82edc9b2013-07-19 11:31:42 -07001159 /**
1160 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
1161 * for battery).
1162 */
David Christie40e57822013-07-30 11:36:48 -07001163 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -07001164 mContext.enforceCallingOrSelfPermission(
1165 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
1166 }
1167
David Christie40e57822013-07-30 11:36:48 -07001168 private void checkUpdateAppOpsAllowed() {
1169 mContext.enforceCallingOrSelfPermission(
1170 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
1171 }
1172
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001173 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001174 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1175 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001176 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001177 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001178 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001179 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001180 }
1181 return -1;
1182 }
1183
David Christieb870dbf2015-06-22 12:42:53 -07001184 boolean reportLocationAccessNoThrow(
1185 int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001186 int op = resolutionLevelToOp(allowedResolutionLevel);
1187 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001188 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1189 return false;
1190 }
1191 }
David Christieb870dbf2015-06-22 12:42:53 -07001192
1193 if (getAllowedResolutionLevel(pid, uid) < allowedResolutionLevel) {
1194 return false;
1195 }
1196
Dianne Hackborn35654b62013-01-14 17:38:02 -08001197 return true;
1198 }
1199
David Christieb870dbf2015-06-22 12:42:53 -07001200 boolean checkLocationAccess(int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001201 int op = resolutionLevelToOp(allowedResolutionLevel);
1202 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001203 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1204 return false;
1205 }
1206 }
David Christieb870dbf2015-06-22 12:42:53 -07001207
1208 if (getAllowedResolutionLevel(pid, uid) < allowedResolutionLevel) {
1209 return false;
1210 }
1211
Dianne Hackborn35654b62013-01-14 17:38:02 -08001212 return true;
1213 }
1214
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001215 /**
1216 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001217 * fused, also including ones that are not permitted to
1218 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001219 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001220 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001222 ArrayList<String> out;
1223 synchronized (mLock) {
1224 out = new ArrayList<String>(mProviders.size());
1225 for (LocationProviderInterface provider : mProviders) {
1226 String name = provider.getName();
1227 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001228 continue;
1229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 out.add(name);
1231 }
1232 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001233
1234 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 return out;
1236 }
1237
Mike Lockwood03ca2162010-04-01 08:10:09 -07001238 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001239 * Return all providers by name, that match criteria and are optionally
1240 * enabled.
1241 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001242 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001243 @Override
1244 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001245 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001246 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001247 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001248 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001249 try {
1250 synchronized (mLock) {
1251 out = new ArrayList<String>(mProviders.size());
1252 for (LocationProviderInterface provider : mProviders) {
1253 String name = provider.getName();
1254 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001255 continue;
1256 }
Victoria Lease37425c32012-10-16 16:08:48 -07001257 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001258 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001259 continue;
1260 }
1261 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1262 name, provider.getProperties(), criteria)) {
1263 continue;
1264 }
1265 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001266 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001267 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001268 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001269 } finally {
1270 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001271 }
1272
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001273 if (D) Log.d(TAG, "getProviders()=" + out);
1274 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001275 }
1276
1277 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001278 * Return the name of the best provider given a Criteria object.
1279 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001280 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001281 * has been deprecated as well. So this method now uses
1282 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001283 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001284 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001285 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001286 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001287
1288 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001289 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001290 result = pickBest(providers);
1291 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1292 return result;
1293 }
1294 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001295 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001296 result = pickBest(providers);
1297 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1298 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001299 }
1300
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001301 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001302 return null;
1303 }
1304
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001305 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001306 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001307 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001308 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1309 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001310 } else {
1311 return providers.get(0);
1312 }
1313 }
1314
Nick Pellye0fd6932012-07-11 10:26:13 -07001315 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001316 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1317 LocationProviderInterface p = mProvidersByName.get(provider);
1318 if (p == null) {
1319 throw new IllegalArgumentException("provider=" + provider);
1320 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001321
1322 boolean result = LocationProvider.propertiesMeetCriteria(
1323 p.getName(), p.getProperties(), criteria);
1324 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1325 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001326 }
1327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001329 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001330 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001331 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 boolean isEnabled = p.isEnabled();
1333 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001334 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 if (isEnabled && !shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001336 updateProviderListenersLocked(name, false);
David Christieb084fef2013-12-18 14:33:57 -08001337 // If any provider has been disabled, clear all last locations for all providers.
1338 // This is to be on the safe side in case a provider has location derived from
1339 // this disabled provider.
1340 mLastLocation.clear();
1341 mLastLocationCoarseInterval.clear();
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001342 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 } else if (!isEnabled && shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001344 updateProviderListenersLocked(name, true);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001345 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001347 }
1348 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001349 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1350 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001351 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1352 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354 }
1355
Amith Yamasanib27528d2014-06-05 15:02:10 -07001356 private void updateProviderListenersLocked(String provider, boolean enabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 int listeners = 0;
1358
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001359 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001360 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361
1362 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1365 if (records != null) {
1366 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001367 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 UpdateRecord record = records.get(i);
Amith Yamasanib27528d2014-06-05 15:02:10 -07001369 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001370 // Sends a notification message to the receiver
1371 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1372 if (deadReceivers == null) {
1373 deadReceivers = new ArrayList<Receiver>();
1374 }
1375 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001377 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380 }
1381
1382 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001383 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 removeUpdatesLocked(deadReceivers.get(i));
1385 }
1386 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 if (enabled) {
1389 p.enable();
1390 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001391 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
1397
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001398 private void applyRequirementsLocked(String provider) {
1399 LocationProviderInterface p = mProvidersByName.get(provider);
1400 if (p == null) return;
1401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001403 WorkSource worksource = new WorkSource();
1404 ProviderRequest providerRequest = new ProviderRequest();
1405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001407 for (UpdateRecord record : records) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001408 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
David Christieb870dbf2015-06-22 12:42:53 -07001409 if (checkLocationAccess(
1410 record.mReceiver.mPid,
1411 record.mReceiver.mUid,
1412 record.mReceiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001413 record.mReceiver.mAllowedResolutionLevel)) {
1414 LocationRequest locationRequest = record.mRequest;
1415 providerRequest.locationRequests.add(locationRequest);
1416 if (locationRequest.getInterval() < providerRequest.interval) {
1417 providerRequest.reportLocation = true;
1418 providerRequest.interval = locationRequest.getInterval();
1419 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001420 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001421 }
1422 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001423
1424 if (providerRequest.reportLocation) {
1425 // calculate who to blame for power
1426 // This is somewhat arbitrary. We pick a threshold interval
1427 // that is slightly higher that the minimum interval, and
1428 // spread the blame across all applications with a request
1429 // under that threshold.
1430 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1431 for (UpdateRecord record : records) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001432 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001433 LocationRequest locationRequest = record.mRequest;
Svet Ganove998c732016-06-10 00:12:38 -07001434
1435 // Don't assign battery blame for update records whose
1436 // client has no permission to receive location data.
1437 if (!providerRequest.locationRequests.contains(locationRequest)) {
1438 continue;
1439 }
1440
Victoria Leaseb711d572012-10-02 13:14:11 -07001441 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001442 if (record.mReceiver.mWorkSource != null
1443 && record.mReceiver.mWorkSource.size() > 0
1444 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001445 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001446 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001447 worksource.add(record.mReceiver.mWorkSource);
1448 } else {
1449 // Assign blame to caller.
1450 worksource.add(
1451 record.mReceiver.mUid,
1452 record.mReceiver.mPackageName);
1453 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001454 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001455 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
1458 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001459
1460 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1461 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463
1464 private class UpdateRecord {
1465 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001466 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001468 Location mLastFixBroadcast;
1469 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470
1471 /**
1472 * Note: must be constructed with lock held.
1473 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001474 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001476 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478
1479 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1480 if (records == null) {
1481 records = new ArrayList<UpdateRecord>();
1482 mRecordsByProvider.put(provider, records);
1483 }
1484 if (!records.contains(this)) {
1485 records.add(this);
1486 }
David Christie2ff96af2014-01-30 16:09:37 -08001487
1488 // Update statistics for historical location requests by package/provider
1489 mRequestStatistics.startRequesting(
1490 mReceiver.mPackageName, provider, request.getInterval());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492
1493 /**
David Christie2ff96af2014-01-30 16:09:37 -08001494 * Method to be called when a record will no longer be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001496 void disposeLocked(boolean removeReceiver) {
David Christie2ff96af2014-01-30 16:09:37 -08001497 mRequestStatistics.stopRequesting(mReceiver.mPackageName, mProvider);
1498
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001499 // remove from mRecordsByProvider
1500 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1501 if (globalRecords != null) {
1502 globalRecords.remove(this);
1503 }
1504
1505 if (!removeReceiver) return; // the caller will handle the rest
1506
1507 // remove from Receiver#mUpdateRecords
1508 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1509 if (receiverRecords != null) {
1510 receiverRecords.remove(this.mProvider);
1511
1512 // and also remove the Receiver if it has no more update records
1513 if (removeReceiver && receiverRecords.size() == 0) {
1514 removeUpdatesLocked(mReceiver);
1515 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518
1519 @Override
1520 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001521 StringBuilder s = new StringBuilder();
1522 s.append("UpdateRecord[");
1523 s.append(mProvider);
1524 s.append(' ').append(mReceiver.mPackageName).append('(');
1525 s.append(mReceiver.mUid).append(')');
1526 s.append(' ').append(mRequest);
1527 s.append(']');
1528 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
1531
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001532 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001533 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001534 IBinder binder = listener.asBinder();
1535 Receiver receiver = mReceivers.get(binder);
1536 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001537 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1538 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001539 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001540 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001541 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001542 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001543 return null;
1544 }
Wen Jingcb3ab222014-03-27 13:42:59 +08001545 mReceivers.put(binder, receiver);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001546 }
1547 return receiver;
1548 }
1549
David Christie82edc9b2013-07-19 11:31:42 -07001550 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001551 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001552 Receiver receiver = mReceivers.get(intent);
1553 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001554 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1555 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001556 mReceivers.put(intent, receiver);
1557 }
1558 return receiver;
1559 }
1560
Victoria Lease37425c32012-10-16 16:08:48 -07001561 /**
1562 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1563 * and consistency requirements.
1564 *
1565 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001566 * @return a version of request that meets the given resolution and consistency requirements
1567 * @hide
1568 */
1569 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1570 LocationRequest sanitizedRequest = new LocationRequest(request);
1571 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1572 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001573 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001574 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001575 break;
1576 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001577 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001578 break;
1579 }
1580 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001581 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1582 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001583 }
Victoria Lease37425c32012-10-16 16:08:48 -07001584 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1585 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001586 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001587 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001588 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001589 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001590 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001591 }
Victoria Lease37425c32012-10-16 16:08:48 -07001592 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001593 }
1594
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001595 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001596 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001597 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001598 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001599 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001600 String[] packages = mPackageManager.getPackagesForUid(uid);
1601 if (packages == null) {
1602 throw new SecurityException("invalid UID " + uid);
1603 }
1604 for (String pkg : packages) {
1605 if (packageName.equals(pkg)) return;
1606 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001607 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001608 }
1609
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001610 private void checkPendingIntent(PendingIntent intent) {
1611 if (intent == null) {
1612 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001613 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001614 }
1615
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001616 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001617 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001618 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001619 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001620 } else if (intent != null && listener != null) {
1621 throw new IllegalArgumentException("cannot register both listener and intent");
1622 } else if (intent != null) {
1623 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001624 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001625 } else {
David Christie40e57822013-07-30 11:36:48 -07001626 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001627 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001628 }
1629
Nick Pellye0fd6932012-07-11 10:26:13 -07001630 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001631 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1632 PendingIntent intent, String packageName) {
1633 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1634 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001635 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1636 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1637 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001638 WorkSource workSource = request.getWorkSource();
1639 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001640 checkDeviceStatsAllowed();
1641 }
1642 boolean hideFromAppOps = request.getHideFromAppOps();
1643 if (hideFromAppOps) {
1644 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001645 }
Victoria Lease37425c32012-10-16 16:08:48 -07001646 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001648 final int pid = Binder.getCallingPid();
1649 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001650 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 long identity = Binder.clearCallingIdentity();
1652 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001653 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1654 // a location.
David Christieb870dbf2015-06-22 12:42:53 -07001655 checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001656
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001657 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001658 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001659 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001660 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 } finally {
1663 Binder.restoreCallingIdentity(identity);
1664 }
1665 }
1666
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001667 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1668 int pid, int uid, String packageName) {
1669 // Figure out the provider. Either its explicitly request (legacy use cases), or
1670 // use the fused provider
1671 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1672 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001673 if (name == null) {
1674 throw new IllegalArgumentException("provider name must not be null");
1675 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001676
1677 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1678 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001679 LocationProviderInterface provider = mProvidersByName.get(name);
1680 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07001681 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001682 }
1683
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001684 UpdateRecord record = new UpdateRecord(name, request, receiver);
1685 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1686 if (oldRecord != null) {
1687 oldRecord.disposeLocked(false);
1688 }
1689
Victoria Lease09eeaec2013-02-05 11:34:13 -08001690 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001691 if (isProviderEnabled) {
1692 applyRequirementsLocked(name);
1693 } else {
1694 // Notify the listener that updates are currently disabled
1695 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
David Christie0b837452013-07-29 16:02:13 -07001697 // Update the monitoring here just in case multiple location requests were added to the
1698 // same receiver (this request may be high power and the initial might not have been).
1699 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701
Nick Pellye0fd6932012-07-11 10:26:13 -07001702 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001703 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1704 String packageName) {
1705 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001706
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001707 final int pid = Binder.getCallingPid();
1708 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001709
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001710 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001711 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001712 boolean hideFromAppOps = false;
1713 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1714 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001715
1716 // providers may use public location API's, need to clear identity
1717 long identity = Binder.clearCallingIdentity();
1718 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001719 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001720 } finally {
1721 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 }
1725
1726 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001727 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001728
1729 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1730 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1731 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001732 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 }
1735
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001736 receiver.updateMonitoring(false);
1737
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001738 // Record which providers were associated with this listener
1739 HashSet<String> providers = new HashSet<String>();
1740 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1741 if (oldRecords != null) {
1742 // Call dispose() on the obsolete update records.
1743 for (UpdateRecord record : oldRecords.values()) {
David Christie2ff96af2014-01-30 16:09:37 -08001744 // Update statistics for historical location requests by package/provider
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001745 record.disposeLocked(false);
1746 }
1747 // Accumulate providers
1748 providers.addAll(oldRecords.keySet());
1749 }
1750
1751 // update provider
1752 for (String provider : providers) {
1753 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001754 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001755 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001758 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
1761
Dianne Hackbornc2293022013-02-06 23:14:49 -08001762 private void applyAllProviderRequirementsLocked() {
1763 for (LocationProviderInterface p : mProviders) {
1764 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001765 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001766 continue;
1767 }
1768
1769 applyRequirementsLocked(p.getName());
1770 }
1771 }
1772
Nick Pellye0fd6932012-07-11 10:26:13 -07001773 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001774 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001775 if (D) Log.d(TAG, "getLastLocation: " + request);
1776 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001777 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001778 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001779 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1780 request.getProvider());
1781 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001782
David Christieb870dbf2015-06-22 12:42:53 -07001783 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001784 final int uid = Binder.getCallingUid();
1785 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001786 try {
1787 if (mBlacklist.isBlacklisted(packageName)) {
1788 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1789 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001790 return null;
1791 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001792
David Christieb870dbf2015-06-22 12:42:53 -07001793 if (!reportLocationAccessNoThrow(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001794 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1795 packageName);
1796 return null;
1797 }
1798
Victoria Leaseb711d572012-10-02 13:14:11 -07001799 synchronized (mLock) {
1800 // Figure out the provider. Either its explicitly request (deprecated API's),
1801 // or use the fused provider
1802 String name = request.getProvider();
1803 if (name == null) name = LocationManager.FUSED_PROVIDER;
1804 LocationProviderInterface provider = mProvidersByName.get(name);
1805 if (provider == null) return null;
1806
Victoria Lease09eeaec2013-02-05 11:34:13 -08001807 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001808
David Christie1b9b7b12013-04-15 15:31:11 -07001809 Location location;
1810 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1811 // Make sure that an app with coarse permissions can't get frequent location
1812 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1813 location = mLastLocationCoarseInterval.get(name);
1814 } else {
1815 location = mLastLocation.get(name);
1816 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001817 if (location == null) {
1818 return null;
1819 }
Victoria Lease37425c32012-10-16 16:08:48 -07001820 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001821 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1822 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001823 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001824 }
Victoria Lease37425c32012-10-16 16:08:48 -07001825 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001826 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001827 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001828 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001829 return null;
1830 } finally {
1831 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001832 }
1833 }
1834
1835 @Override
1836 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1837 String packageName) {
1838 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001839 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1840 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001841 checkPendingIntent(intent);
1842 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001843 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1844 request.getProvider());
1845 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001846
Victoria Lease37425c32012-10-16 16:08:48 -07001847 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001848
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001849 // geo-fence manager uses the public location API, need to clear identity
1850 int uid = Binder.getCallingUid();
Xiaohui Chena4490622015-09-22 15:29:31 -07001851 // TODO: http://b/23822629
1852 if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) {
Victoria Lease56e675b2012-11-05 19:25:06 -08001853 // temporary measure until geofences work for secondary users
1854 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1855 return;
1856 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001857 long identity = Binder.clearCallingIdentity();
1858 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001859 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1860 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001861 } finally {
1862 Binder.restoreCallingIdentity(identity);
1863 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001864 }
1865
1866 @Override
1867 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001868 checkPendingIntent(intent);
1869 checkPackageName(packageName);
1870
1871 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1872
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001873 // geo-fence manager uses the public location API, need to clear identity
1874 long identity = Binder.clearCallingIdentity();
1875 try {
1876 mGeofenceManager.removeFence(geofence, intent);
1877 } finally {
1878 Binder.restoreCallingIdentity(identity);
1879 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001880 }
1881
1882
1883 @Override
Lifu Tang30f95a72016-01-07 23:20:38 -08001884 public boolean registerGnssStatusCallback(IGnssStatusListener callback, String packageName) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001885 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1886 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001887 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888
David Christieb870dbf2015-06-22 12:42:53 -07001889 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001890 final int uid = Binder.getCallingUid();
1891 final long ident = Binder.clearCallingIdentity();
1892 try {
David Christieb870dbf2015-06-22 12:42:53 -07001893 if (!checkLocationAccess(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001894 return false;
1895 }
1896 } finally {
1897 Binder.restoreCallingIdentity(ident);
1898 }
1899
Lifu Tang30f95a72016-01-07 23:20:38 -08001900 if (mGnssStatusProvider == null) {
Takayuki Hoshib254ab6a2014-10-23 16:46:02 +09001901 return false;
1902 }
1903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 try {
Lifu Tang30f95a72016-01-07 23:20:38 -08001905 mGnssStatusProvider.registerGnssStatusCallback(callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 } catch (RemoteException e) {
Lifu Tang30f95a72016-01-07 23:20:38 -08001907 Slog.e(TAG, "mGpsStatusProvider.registerGnssStatusCallback failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 return false;
1909 }
1910 return true;
1911 }
1912
Nick Pellye0fd6932012-07-11 10:26:13 -07001913 @Override
Lifu Tang30f95a72016-01-07 23:20:38 -08001914 public void unregisterGnssStatusCallback(IGnssStatusListener callback) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001915 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001916 try {
Lifu Tang30f95a72016-01-07 23:20:38 -08001917 mGnssStatusProvider.unregisterGnssStatusCallback(callback);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001918 } catch (Exception e) {
Lifu Tang30f95a72016-01-07 23:20:38 -08001919 Slog.e(TAG, "mGpsStatusProvider.unregisterGnssStatusCallback failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 }
1922 }
1923
Nick Pellye0fd6932012-07-11 10:26:13 -07001924 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001925 public boolean addGnssMeasurementsListener(
1926 IGnssMeasurementsListener listener,
destradaaea8a8a62014-06-23 18:19:03 -07001927 String packageName) {
1928 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1929 checkResolutionLevelIsSufficientForProviderUse(
1930 allowedResolutionLevel,
1931 LocationManager.GPS_PROVIDER);
1932
David Christieb870dbf2015-06-22 12:42:53 -07001933 int pid = Binder.getCallingPid();
destradaaea8a8a62014-06-23 18:19:03 -07001934 int uid = Binder.getCallingUid();
1935 long identity = Binder.clearCallingIdentity();
1936 boolean hasLocationAccess;
1937 try {
David Christieb870dbf2015-06-22 12:42:53 -07001938 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaaea8a8a62014-06-23 18:19:03 -07001939 } finally {
1940 Binder.restoreCallingIdentity(identity);
1941 }
1942
Lifu Tang818aa2c2016-02-01 01:52:00 -08001943 if (!hasLocationAccess || mGnssMeasurementsProvider == null) {
destradaaea8a8a62014-06-23 18:19:03 -07001944 return false;
1945 }
Lifu Tang818aa2c2016-02-01 01:52:00 -08001946 return mGnssMeasurementsProvider.addListener(listener);
destradaaea8a8a62014-06-23 18:19:03 -07001947 }
1948
1949 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001950 public void removeGnssMeasurementsListener(IGnssMeasurementsListener listener) {
1951 if (mGnssMeasurementsProvider != null) {
1952 mGnssMeasurementsProvider.removeListener(listener);
Wei Liu5241a4c2015-05-11 14:00:36 -07001953 }
destradaaea8a8a62014-06-23 18:19:03 -07001954 }
1955
1956 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001957 public boolean addGnssNavigationMessageListener(
1958 IGnssNavigationMessageListener listener,
destradaa4b3e3932014-07-21 18:01:47 -07001959 String packageName) {
1960 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1961 checkResolutionLevelIsSufficientForProviderUse(
1962 allowedResolutionLevel,
1963 LocationManager.GPS_PROVIDER);
1964
David Christieb870dbf2015-06-22 12:42:53 -07001965 int pid = Binder.getCallingPid();
destradaa4b3e3932014-07-21 18:01:47 -07001966 int uid = Binder.getCallingUid();
1967 long identity = Binder.clearCallingIdentity();
1968 boolean hasLocationAccess;
1969 try {
David Christieb870dbf2015-06-22 12:42:53 -07001970 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaa4b3e3932014-07-21 18:01:47 -07001971 } finally {
1972 Binder.restoreCallingIdentity(identity);
1973 }
1974
Lifu Tang818aa2c2016-02-01 01:52:00 -08001975 if (!hasLocationAccess || mGnssNavigationMessageProvider == null) {
destradaa4b3e3932014-07-21 18:01:47 -07001976 return false;
1977 }
Lifu Tang818aa2c2016-02-01 01:52:00 -08001978 return mGnssNavigationMessageProvider.addListener(listener);
destradaa4b3e3932014-07-21 18:01:47 -07001979 }
1980
1981 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001982 public void removeGnssNavigationMessageListener(IGnssNavigationMessageListener listener) {
1983 if (mGnssNavigationMessageProvider != null) {
1984 mGnssNavigationMessageProvider.removeListener(listener);
Wei Liu5241a4c2015-05-11 14:00:36 -07001985 }
destradaa4b3e3932014-07-21 18:01:47 -07001986 }
1987
1988 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001990 if (provider == null) {
1991 // throw NullPointerException to remain compatible with previous implementation
1992 throw new NullPointerException();
1993 }
Victoria Lease37425c32012-10-16 16:08:48 -07001994 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1995 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001998 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 != PackageManager.PERMISSION_GRANTED)) {
2000 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
2001 }
2002
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002003 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002004 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002005 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07002006
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002007 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
2009 }
2010
Nick Pellye0fd6932012-07-11 10:26:13 -07002011 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002012 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07002013 if (Binder.getCallingUid() != Process.myUid()) {
2014 throw new SecurityException(
2015 "calling sendNiResponse from outside of the system is not allowed");
2016 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04002017 try {
2018 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002019 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002020 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04002021 return false;
2022 }
2023 }
2024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002026 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11002027 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 * accessed by the caller
2029 */
Nick Pellye0fd6932012-07-11 10:26:13 -07002030 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002031 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07002032 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08002033 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07002034 }
2035
Victoria Lease37425c32012-10-16 16:08:48 -07002036 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
2037 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002039 LocationProviderInterface p;
2040 synchronized (mLock) {
2041 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002044 if (p == null) return null;
2045 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
2047
Jason Monkb71218a2015-06-17 14:44:39 -04002048 /**
2049 * @return null if the provider does not exist
2050 * @throws SecurityException if the provider is not allowed to be
2051 * accessed by the caller
2052 */
2053 @Override
2054 public String getNetworkProviderPackage() {
2055 LocationProviderInterface p;
2056 synchronized (mLock) {
2057 if (mProvidersByName.get(LocationManager.NETWORK_PROVIDER) == null) {
2058 return null;
2059 }
2060 p = mProvidersByName.get(LocationManager.NETWORK_PROVIDER);
2061 }
2062
2063 if (p instanceof LocationProviderProxy) {
2064 return ((LocationProviderProxy) p).getConnectedPackageName();
2065 }
2066 return null;
2067 }
2068
Nick Pellye0fd6932012-07-11 10:26:13 -07002069 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07002071 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
2072 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002073 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
2074
Victoria Lease09eeaec2013-02-05 11:34:13 -08002075 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07002076 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07002077 try {
2078 synchronized (mLock) {
2079 LocationProviderInterface p = mProvidersByName.get(provider);
2080 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002081
Victoria Lease09eeaec2013-02-05 11:34:13 -08002082 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07002083 }
2084 } finally {
2085 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002086 }
2087 }
2088
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002089 /**
2090 * Returns "true" if the UID belongs to a bound location provider.
2091 *
2092 * @param uid the uid
2093 * @return true if uid belongs to a bound location provider
2094 */
2095 private boolean isUidALocationProvider(int uid) {
2096 if (uid == Process.SYSTEM_UID) {
2097 return true;
2098 }
2099 if (mGeocodeProvider != null) {
David Christie1f141c12014-05-14 15:11:15 -07002100 if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002101 }
2102 for (LocationProviderProxy proxy : mProxyProviders) {
David Christie1f141c12014-05-14 15:11:15 -07002103 if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002104 }
2105 return false;
2106 }
2107
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002108 private void checkCallerIsProvider() {
2109 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
2110 == PackageManager.PERMISSION_GRANTED) {
2111 return;
2112 }
2113
2114 // Previously we only used the INSTALL_LOCATION_PROVIDER
2115 // check. But that is system or signature
2116 // protection level which is not flexible enough for
2117 // providers installed oustide the system image. So
2118 // also allow providers with a UID matching the
2119 // currently bound package name
2120
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002121 if (isUidALocationProvider(Binder.getCallingUid())) {
2122 return;
2123 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002124
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002125 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
2126 "or UID of a currently bound location provider");
2127 }
2128
David Christie1f141c12014-05-14 15:11:15 -07002129 /**
2130 * Returns true if the given package belongs to the given uid.
2131 */
2132 private boolean doesUidHavePackage(int uid, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002133 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 return false;
2135 }
David Christie1f141c12014-05-14 15:11:15 -07002136 String[] packageNames = mPackageManager.getPackagesForUid(uid);
2137 if (packageNames == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002138 return false;
2139 }
David Christie1f141c12014-05-14 15:11:15 -07002140 for (String name : packageNames) {
2141 if (packageName.equals(name)) {
2142 return true;
2143 }
2144 }
2145 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147
Nick Pellye0fd6932012-07-11 10:26:13 -07002148 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05002149 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002150 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04002151
Nick Pelly2eeeec22012-07-18 13:13:37 -07002152 if (!location.isComplete()) {
2153 Log.w(TAG, "Dropping incomplete location: " + location);
2154 return;
2155 }
2156
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002157 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
2158 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05002159 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002160 mLocationHandler.sendMessageAtFrontOfQueue(m);
2161 }
2162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163
Laurent Tu75defb62012-11-01 16:21:52 -07002164 private static boolean shouldBroadcastSafe(
2165 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 // Always broadcast the first update
2167 if (lastLoc == null) {
2168 return true;
2169 }
2170
Nick Pellyf1be6862012-05-15 10:53:42 -07002171 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002172 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07002173 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
2174 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002175 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 return false;
2177 }
2178
2179 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002180 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 if (minDistance > 0.0) {
2182 if (loc.distanceTo(lastLoc) <= minDistance) {
2183 return false;
2184 }
2185 }
2186
Laurent Tu75defb62012-11-01 16:21:52 -07002187 // Check whether sufficient number of udpates is left
2188 if (record.mRequest.getNumUpdates() <= 0) {
2189 return false;
2190 }
2191
2192 // Check whether the expiry date has passed
2193 if (record.mRequest.getExpireAt() < now) {
2194 return false;
2195 }
2196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 return true;
2198 }
2199
Mike Lockwooda4903f22010-02-17 06:42:23 -05002200 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07002201 if (D) Log.d(TAG, "incoming location: " + location);
2202
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002203 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05002204 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205
Laurent Tu60ec50a2012-10-04 17:00:10 -07002206 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002207 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002208 if (p == null) return;
2209
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002210 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07002211 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2212 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002213 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002214 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002215 lastLocation = new Location(provider);
2216 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07002217 } else {
2218 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2219 if (noGPSLocation == null && lastNoGPSLocation != null) {
2220 // New location has no no-GPS location: adopt last no-GPS location. This is set
2221 // directly into location because we do not want to notify COARSE clients.
2222 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
2223 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07002224 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002225 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226
David Christie1b9b7b12013-04-15 15:31:11 -07002227 // Update last known coarse interval location if enough time has passed.
2228 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
2229 if (lastLocationCoarseInterval == null) {
2230 lastLocationCoarseInterval = new Location(location);
2231 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
2232 }
2233 long timeDiffNanos = location.getElapsedRealtimeNanos()
2234 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
2235 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
2236 lastLocationCoarseInterval.set(location);
2237 }
2238 // Don't ever return a coarse location that is more recent than the allowed update
2239 // interval (i.e. don't allow an app to keep registering and unregistering for
2240 // location updates to overcome the minimum interval).
2241 noGPSLocation =
2242 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2243
Laurent Tu60ec50a2012-10-04 17:00:10 -07002244 // Skip if there are no UpdateRecords for this provider.
2245 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
2246 if (records == null || records.size() == 0) return;
2247
Victoria Lease09016ab2012-09-16 12:33:15 -07002248 // Fetch coarse location
2249 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07002250 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002251 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
2252 }
2253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 // Fetch latest status update time
2255 long newStatusUpdateTime = p.getStatusUpdateTime();
2256
David Christie2ff96af2014-01-30 16:09:37 -08002257 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 Bundle extras = new Bundle();
2259 int status = p.getStatus(extras);
2260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002262 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07002263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002265 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07002267 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07002268
Victoria Lease269518e2012-10-29 08:25:39 -07002269 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Amith Yamasanib27528d2014-06-05 15:02:10 -07002270 if (!isCurrentProfile(receiverUserId) && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002271 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07002272 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07002273 " (current user: " + mCurrentUserId + ", app: " +
2274 receiver.mPackageName + ")");
2275 }
2276 continue;
2277 }
2278
Nick Pelly4035f5a2012-08-17 14:43:49 -07002279 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
2280 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
2281 receiver.mPackageName);
2282 continue;
2283 }
2284
David Christieb870dbf2015-06-22 12:42:53 -07002285 if (!reportLocationAccessNoThrow(receiver.mPid, receiver.mUid, receiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002286 receiver.mAllowedResolutionLevel)) {
2287 if (D) Log.d(TAG, "skipping loc update for no op app: " +
2288 receiver.mPackageName);
2289 continue;
2290 }
2291
Victoria Lease09016ab2012-09-16 12:33:15 -07002292 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07002293 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2294 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002295 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002296 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002297 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002298 if (notifyLocation != null) {
2299 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002300 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002301 if (lastLoc == null) {
2302 lastLoc = new Location(notifyLocation);
2303 r.mLastFixBroadcast = lastLoc;
2304 } else {
2305 lastLoc.set(notifyLocation);
2306 }
2307 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2308 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2309 receiverDead = true;
2310 }
Laurent Tu75defb62012-11-01 16:21:52 -07002311 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
2313 }
2314
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002315 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002317 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002319 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002321 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002322 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002323 }
2324 }
2325
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002326 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002327 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002328 if (deadUpdateRecords == null) {
2329 deadUpdateRecords = new ArrayList<UpdateRecord>();
2330 }
2331 deadUpdateRecords.add(r);
2332 }
2333 // track dead receivers
2334 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002335 if (deadReceivers == null) {
2336 deadReceivers = new ArrayList<Receiver>();
2337 }
2338 if (!deadReceivers.contains(receiver)) {
2339 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
2341 }
2342 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002343
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002344 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002346 for (Receiver receiver : deadReceivers) {
2347 removeUpdatesLocked(receiver);
2348 }
2349 }
2350 if (deadUpdateRecords != null) {
2351 for (UpdateRecord r : deadUpdateRecords) {
2352 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002354 applyRequirementsLocked(provider);
2355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
2357
2358 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002359 public LocationWorkerHandler(Looper looper) {
2360 super(looper, null, true);
2361 }
2362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 @Override
2364 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002365 switch (msg.what) {
2366 case MSG_LOCATION_CHANGED:
2367 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2368 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370 }
2371 }
2372
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002373 private boolean isMockProvider(String provider) {
2374 synchronized (mLock) {
2375 return mMockProviders.containsKey(provider);
2376 }
2377 }
2378
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002379 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002380 // create a working copy of the incoming Location so that the service can modify it without
2381 // disturbing the caller's copy
2382 Location myLocation = new Location(location);
2383 String provider = myLocation.getProvider();
2384
2385 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2386 // bit if location did not come from a mock provider because passive/fused providers can
2387 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2388 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2389 myLocation.setIsFromMockProvider(true);
2390 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002391
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002392 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002393 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2394 if (!passive) {
2395 // notify passive provider of the new location
2396 mPassiveProvider.updateLocation(myLocation);
2397 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002398 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402
Mike Lockwoode97ae402010-09-29 15:23:46 -04002403 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2404 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002405 public void onPackageDisappeared(String packageName, int reason) {
2406 // remove all receivers associated with this package name
2407 synchronized (mLock) {
2408 ArrayList<Receiver> deadReceivers = null;
2409
2410 for (Receiver receiver : mReceivers.values()) {
2411 if (receiver.mPackageName.equals(packageName)) {
2412 if (deadReceivers == null) {
2413 deadReceivers = new ArrayList<Receiver>();
2414 }
2415 deadReceivers.add(receiver);
2416 }
2417 }
2418
2419 // perform removal outside of mReceivers loop
2420 if (deadReceivers != null) {
2421 for (Receiver receiver : deadReceivers) {
2422 removeUpdatesLocked(receiver);
2423 }
2424 }
2425 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002426 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002427 };
2428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 // Geocoder
2430
Nick Pellye0fd6932012-07-11 10:26:13 -07002431 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002432 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002433 return mGeocodeProvider != null;
2434 }
2435
Nick Pellye0fd6932012-07-11 10:26:13 -07002436 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002438 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002439 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002440 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2441 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002443 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
2445
Mike Lockwooda55c3212009-04-15 11:10:11 -04002446
Nick Pellye0fd6932012-07-11 10:26:13 -07002447 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002449 double lowerLeftLatitude, double lowerLeftLongitude,
2450 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002451 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002452
2453 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002454 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2455 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2456 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002458 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 }
2460
2461 // Mock Providers
2462
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002463 private boolean canCallerAccessMockLocation(String opPackageName) {
2464 return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
2465 opPackageName) == AppOpsManager.MODE_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
2467
Nick Pellye0fd6932012-07-11 10:26:13 -07002468 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002469 public void addTestProvider(String name, ProviderProperties properties, String opPackageName) {
2470 if (!canCallerAccessMockLocation(opPackageName)) {
2471 return;
2472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473
Mike Lockwooda4903f22010-02-17 06:42:23 -05002474 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2475 throw new IllegalArgumentException("Cannot mock the passive location provider");
2476 }
2477
Mike Lockwood86328a92009-10-23 08:38:25 -04002478 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002479 synchronized (mLock) {
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002480 // remove the real provider if we are replacing GPS or network provider
2481 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002482 || LocationManager.NETWORK_PROVIDER.equals(name)
2483 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002484 LocationProviderInterface p = mProvidersByName.get(name);
2485 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002486 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002487 }
2488 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002489 addTestProviderLocked(name, properties);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 updateProvidersLocked();
2491 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002492 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002495 private void addTestProviderLocked(String name, ProviderProperties properties) {
2496 if (mProvidersByName.get(name) != null) {
2497 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2498 }
2499 MockProvider provider = new MockProvider(name, this, properties);
2500 addProviderLocked(provider);
2501 mMockProviders.put(name, provider);
2502 mLastLocation.put(name, null);
2503 mLastLocationCoarseInterval.put(name, null);
2504 }
2505
Nick Pellye0fd6932012-07-11 10:26:13 -07002506 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002507 public void removeTestProvider(String provider, String opPackageName) {
2508 if (!canCallerAccessMockLocation(opPackageName)) {
2509 return;
2510 }
2511
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002512 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002513
2514 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002515 // we don't leave anything dangling.
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002516 clearTestProviderEnabled(provider, opPackageName);
2517 clearTestProviderLocation(provider, opPackageName);
2518 clearTestProviderStatus(provider, opPackageName);
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002519
You Kima6d0b6f2012-10-28 03:58:44 +09002520 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002521 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2523 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002524 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002525 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002526
2527 // reinstate real provider if available
2528 LocationProviderInterface realProvider = mRealProviders.get(provider);
2529 if (realProvider != null) {
2530 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002531 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002532 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002533 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002535 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 }
2537 }
2538
Nick Pellye0fd6932012-07-11 10:26:13 -07002539 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002540 public void setTestProviderLocation(String provider, Location loc, String opPackageName) {
2541 if (!canCallerAccessMockLocation(opPackageName)) {
2542 return;
2543 }
2544
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002545 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002546 MockProvider mockProvider = mMockProviders.get(provider);
2547 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2549 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002550 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2551 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002552 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002553 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
2555 }
2556
Nick Pellye0fd6932012-07-11 10:26:13 -07002557 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002558 public void clearTestProviderLocation(String provider, String opPackageName) {
2559 if (!canCallerAccessMockLocation(opPackageName)) {
2560 return;
2561 }
2562
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002563 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002564 MockProvider mockProvider = mMockProviders.get(provider);
2565 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2567 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002568 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
2570 }
2571
Nick Pellye0fd6932012-07-11 10:26:13 -07002572 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002573 public void setTestProviderEnabled(String provider, boolean enabled, String opPackageName) {
2574 if (!canCallerAccessMockLocation(opPackageName)) {
2575 return;
2576 }
2577
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002578 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002579 MockProvider mockProvider = mMockProviders.get(provider);
2580 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2582 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002583 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002585 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 mEnabledProviders.add(provider);
2587 mDisabledProviders.remove(provider);
2588 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002589 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 mEnabledProviders.remove(provider);
2591 mDisabledProviders.add(provider);
2592 }
2593 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002594 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 }
2596 }
2597
Nick Pellye0fd6932012-07-11 10:26:13 -07002598 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002599 public void clearTestProviderEnabled(String provider, String opPackageName) {
2600 if (!canCallerAccessMockLocation(opPackageName)) {
2601 return;
2602 }
2603
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002604 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002605 MockProvider mockProvider = mMockProviders.get(provider);
2606 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2608 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002609 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 mEnabledProviders.remove(provider);
2611 mDisabledProviders.remove(provider);
2612 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002613 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
2615 }
2616
Nick Pellye0fd6932012-07-11 10:26:13 -07002617 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002618 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime,
2619 String opPackageName) {
2620 if (!canCallerAccessMockLocation(opPackageName)) {
2621 return;
2622 }
2623
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002624 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002625 MockProvider mockProvider = mMockProviders.get(provider);
2626 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2628 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002629 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 }
2631 }
2632
Nick Pellye0fd6932012-07-11 10:26:13 -07002633 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002634 public void clearTestProviderStatus(String provider, String opPackageName) {
2635 if (!canCallerAccessMockLocation(opPackageName)) {
2636 return;
2637 }
2638
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002639 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002640 MockProvider mockProvider = mMockProviders.get(provider);
2641 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2643 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002644 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
2646 }
2647
2648 private void log(String log) {
2649 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002650 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 }
2652 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002653
2654 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2656 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2657 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002658 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 + Binder.getCallingPid()
2660 + ", uid=" + Binder.getCallingUid());
2661 return;
2662 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002663
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002664 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002667 for (Receiver receiver : mReceivers.values()) {
2668 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 }
David Christie2ff96af2014-01-30 16:09:37 -08002670 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002671 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2672 pw.println(" " + entry.getKey() + ":");
2673 for (UpdateRecord record : entry.getValue()) {
2674 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
2676 }
David Christie2ff96af2014-01-30 16:09:37 -08002677 pw.println(" Historical Records by Provider:");
2678 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
2679 : mRequestStatistics.statistics.entrySet()) {
2680 PackageProviderKey key = entry.getKey();
2681 PackageStatistics stats = entry.getValue();
2682 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
2683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002685 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2686 String provider = entry.getKey();
2687 Location location = entry.getValue();
2688 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002690
David Christie1b9b7b12013-04-15 15:31:11 -07002691 pw.println(" Last Known Locations Coarse Intervals:");
2692 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2693 String provider = entry.getKey();
2694 Location location = entry.getValue();
2695 pw.println(" " + provider + ": " + location);
2696 }
2697
Nick Pellye0fd6932012-07-11 10:26:13 -07002698 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 if (mEnabledProviders.size() > 0) {
2701 pw.println(" Enabled Providers:");
2702 for (String i : mEnabledProviders) {
2703 pw.println(" " + i);
2704 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707 if (mDisabledProviders.size() > 0) {
2708 pw.println(" Disabled Providers:");
2709 for (String i : mDisabledProviders) {
2710 pw.println(" " + i);
2711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002713 pw.append(" ");
2714 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 if (mMockProviders.size() > 0) {
2716 pw.println(" Mock Providers:");
2717 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002718 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 }
2720 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002721
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002722 pw.append(" fudger: ");
2723 mLocationFudger.dump(fd, pw, args);
2724
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002725 if (args.length > 0 && "short".equals(args[0])) {
2726 return;
2727 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002728 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002729 pw.print(provider.getName() + " Internal State");
2730 if (provider instanceof LocationProviderProxy) {
2731 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2732 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002733 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002734 pw.println(":");
2735 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 }
2738 }
2739}