blob: 14ddc363f392726eb76d94d70426d9e07a0e5c1e [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;
destradaaa4fa3b52014-07-09 10:46:39 -070024import com.android.server.location.ActivityRecognitionProxy;
destradaaea8a8a62014-06-23 18:19:03 -070025import com.android.server.location.FlpHardwareProvider;
26import com.android.server.location.FusedProxy;
27import com.android.server.location.GeocoderProxy;
28import com.android.server.location.GeofenceManager;
29import com.android.server.location.GeofenceProxy;
Lifu Tang818aa2c2016-02-01 01:52:00 -080030import com.android.server.location.GnssLocationProvider;
31import com.android.server.location.GnssMeasurementsProvider;
32import com.android.server.location.GnssNavigationMessageProvider;
destradaaea8a8a62014-06-23 18:19:03 -070033import com.android.server.location.LocationBlacklist;
34import com.android.server.location.LocationFudger;
35import com.android.server.location.LocationProviderInterface;
36import com.android.server.location.LocationProviderProxy;
37import com.android.server.location.LocationRequestStatistics;
38import com.android.server.location.LocationRequestStatistics.PackageProviderKey;
39import com.android.server.location.LocationRequestStatistics.PackageStatistics;
40import com.android.server.location.MockProvider;
41import com.android.server.location.PassiveProvider;
42
Dianne Hackborna06de0f2012-12-11 16:34:47 -080043import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070045import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.ContentResolver;
47import android.content.Context;
48import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070049import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070050import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050051import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070053import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050054import android.content.pm.ResolveInfo;
55import android.content.pm.Signature;
Amith Yamasanib27528d2014-06-05 15:02:10 -070056import android.content.pm.UserInfo;
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 Tang82f893d2016-01-21 18:15:33 -0800217 private GnssLocationProvider.GpsSystemInfoProvider mGpsSystemInfoProvider;
218
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) {
362 List<UserInfo> profiles = mUserManager.getProfiles(currentUserId);
363 synchronized (mLock) {
364 mCurrentUserProfiles = new int[profiles.size()];
365 for (int i = 0; i < mCurrentUserProfiles.length; i++) {
366 mCurrentUserProfiles[i] = profiles.get(i).id;
367 }
368 }
369 }
370
371 /**
372 * Checks if the specified userId matches any of the current foreground
373 * users stored in mCurrentUserProfiles.
374 */
375 private boolean isCurrentProfile(int userId) {
376 synchronized (mLock) {
377 for (int i = 0; i < mCurrentUserProfiles.length; i++) {
378 if (mCurrentUserProfiles[i] == userId) {
379 return true;
380 }
381 }
382 return false;
383 }
384 }
385
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500386 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
387 PackageManager pm = mContext.getPackageManager();
388 String systemPackageName = mContext.getPackageName();
389 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
390
391 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
392 new Intent(FUSED_LOCATION_SERVICE_ACTION),
393 PackageManager.GET_META_DATA, mCurrentUserId);
394 for (ResolveInfo rInfo : rInfos) {
395 String packageName = rInfo.serviceInfo.packageName;
396
397 // Check that the signature is in the list of supported sigs. If it's not in
398 // this list the standard provider binding logic won't bind to it.
399 try {
400 PackageInfo pInfo;
401 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
402 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
403 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
404 ", but has wrong signature, ignoring");
405 continue;
406 }
407 } catch (NameNotFoundException e) {
408 Log.e(TAG, "missing package: " + packageName);
409 continue;
410 }
411
412 // Get the version info
413 if (rInfo.serviceInfo.metaData == null) {
414 Log.w(TAG, "Found fused provider without metadata: " + packageName);
415 continue;
416 }
417
418 int version = rInfo.serviceInfo.metaData.getInt(
419 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
420 if (version == 0) {
421 // This should be the fallback fused location provider.
422
423 // Make sure it's in the system partition.
424 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
425 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
426 continue;
427 }
428
429 // Check that the fallback is signed the same as the OS
430 // as a proxy for coreApp="true"
431 if (pm.checkSignatures(systemPackageName, packageName)
432 != PackageManager.SIGNATURE_MATCH) {
433 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
434 + packageName);
435 continue;
436 }
437
438 // Found a valid fallback.
439 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
440 return;
441 } else {
442 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
443 }
444 }
445
446 throw new IllegalStateException("Unable to find a fused location provider that is in the "
447 + "system partition with version 0 and signed with the platform certificate. "
448 + "Such a package is needed to provide a default fused location provider in the "
449 + "event that no other fused location provider has been installed or is currently "
450 + "available. For example, coreOnly boot mode when decrypting the data "
451 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
452 }
453
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700454 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700455 // create a passive location provider, which is always enabled
456 PassiveProvider passiveProvider = new PassiveProvider(this);
457 addProviderLocked(passiveProvider);
458 mEnabledProviders.add(passiveProvider.getName());
459 mPassiveProvider = passiveProvider;
460
Lifu Tang30f95a72016-01-07 23:20:38 -0800461 if (GnssLocationProvider.isSupported()) {
Wei Liu5241a4c2015-05-11 14:00:36 -0700462 // Create a gps location provider
Lifu Tang30f95a72016-01-07 23:20:38 -0800463 GnssLocationProvider gnssProvider = new GnssLocationProvider(mContext, this,
Wei Liu5241a4c2015-05-11 14:00:36 -0700464 mLocationHandler.getLooper());
Lifu Tang82f893d2016-01-21 18:15:33 -0800465 mGpsSystemInfoProvider = gnssProvider.getGpsSystemInfoProvider();
Lifu Tang30f95a72016-01-07 23:20:38 -0800466 mGnssStatusProvider = gnssProvider.getGnssStatusProvider();
467 mNetInitiatedListener = gnssProvider.getNetInitiatedListener();
468 addProviderLocked(gnssProvider);
469 mRealProviders.put(LocationManager.GPS_PROVIDER, gnssProvider);
Lifu Tang818aa2c2016-02-01 01:52:00 -0800470 mGnssMeasurementsProvider = gnssProvider.getGnssMeasurementsProvider();
471 mGnssNavigationMessageProvider = gnssProvider.getGnssNavigationMessageProvider();
Lifu Tang30f95a72016-01-07 23:20:38 -0800472 mGpsGeofenceProxy = gnssProvider.getGpsGeofenceProxy();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700473 }
474
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700475 /*
476 Load package name(s) containing location provider support.
477 These packages can contain services implementing location providers:
478 Geocoder Provider, Network Location Provider, and
479 Fused Location Provider. They will each be searched for
480 service components implementing these providers.
481 The location framework also has support for installation
482 of new location providers at run-time. The new package does not
483 have to be explicitly listed here, however it must have a signature
484 that matches the signature of at least one package on this list.
485 */
486 Resources resources = mContext.getResources();
487 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500488 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700489 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500490 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
491 Arrays.toString(pkgs));
492 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
493
494 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700495
496 // bind to network provider
497 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
498 mContext,
499 LocationManager.NETWORK_PROVIDER,
500 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700501 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
502 com.android.internal.R.string.config_networkLocationProviderPackageName,
503 com.android.internal.R.array.config_locationProviderPackageNames,
504 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700505 if (networkProvider != null) {
506 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
507 mProxyProviders.add(networkProvider);
508 addProviderLocked(networkProvider);
509 } else {
510 Slog.w(TAG, "no network location provider found");
511 }
512
513 // bind to fused provider
514 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
515 mContext,
516 LocationManager.FUSED_PROVIDER,
517 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700518 com.android.internal.R.bool.config_enableFusedLocationOverlay,
519 com.android.internal.R.string.config_fusedLocationProviderPackageName,
520 com.android.internal.R.array.config_locationProviderPackageNames,
521 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700522 if (fusedLocationProvider != null) {
523 addProviderLocked(fusedLocationProvider);
524 mProxyProviders.add(fusedLocationProvider);
525 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700526 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700527 } else {
528 Slog.e(TAG, "no fused location provider found",
529 new IllegalStateException("Location service needs a fused location provider"));
530 }
531
532 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700533 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
534 com.android.internal.R.bool.config_enableGeocoderOverlay,
535 com.android.internal.R.string.config_geocoderProviderPackageName,
536 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800537 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700538 if (mGeocodeProvider == null) {
539 Slog.e(TAG, "no geocoder provider found");
540 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700541
destradaaa4fa3b52014-07-09 10:46:39 -0700542 // bind to fused hardware provider if supported
destradaabeea4422014-07-30 18:17:21 -0700543 // in devices without support, requesting an instance of FlpHardwareProvider will raise an
544 // exception, so make sure we only do that when supported
545 FlpHardwareProvider flpHardwareProvider;
destradaa5ce66d82014-05-28 18:24:08 -0700546 if (FlpHardwareProvider.isSupported()) {
destradaabeea4422014-07-30 18:17:21 -0700547 flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
destradaaf9a274c2014-07-25 15:11:56 -0700548 FusedProxy fusedProxy = FusedProxy.createAndBind(
549 mContext,
550 mLocationHandler,
551 flpHardwareProvider.getLocationHardware(),
552 com.android.internal.R.bool.config_enableHardwareFlpOverlay,
553 com.android.internal.R.string.config_hardwareFlpPackageName,
554 com.android.internal.R.array.config_locationProviderPackageNames);
555 if (fusedProxy == null) {
556 Slog.e(TAG, "Unable to bind FusedProxy.");
557 }
destradaacfbdcd22014-04-30 11:29:11 -0700558 } else {
destradaabeea4422014-07-30 18:17:21 -0700559 flpHardwareProvider = null;
destradaaf9a274c2014-07-25 15:11:56 -0700560 Slog.e(TAG, "FLP HAL not supported");
561 }
562
563 // bind to geofence provider
564 GeofenceProxy provider = GeofenceProxy.createAndBind(
565 mContext,com.android.internal.R.bool.config_enableGeofenceOverlay,
566 com.android.internal.R.string.config_geofenceProviderPackageName,
567 com.android.internal.R.array.config_locationProviderPackageNames,
568 mLocationHandler,
Wei Liu5241a4c2015-05-11 14:00:36 -0700569 mGpsGeofenceProxy,
destradaabeea4422014-07-30 18:17:21 -0700570 flpHardwareProvider != null ? flpHardwareProvider.getGeofenceHardware() : null);
destradaaf9a274c2014-07-25 15:11:56 -0700571 if (provider == null) {
572 Slog.e(TAG, "Unable to bind FLP Geofence proxy.");
destradaa0682809a2013-08-12 18:50:30 -0700573 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900574
destradaa6e2fe752015-06-23 17:25:53 -0700575 // bind to hardware activity recognition
576 boolean activityRecognitionHardwareIsSupported = ActivityRecognitionHardware.isSupported();
577 ActivityRecognitionHardware activityRecognitionHardware = null;
578 if (activityRecognitionHardwareIsSupported) {
579 activityRecognitionHardware = ActivityRecognitionHardware.getInstance(mContext);
destradaaa4fa3b52014-07-09 10:46:39 -0700580 } else {
581 Slog.e(TAG, "Hardware Activity-Recognition not supported.");
582 }
destradaa6e2fe752015-06-23 17:25:53 -0700583 ActivityRecognitionProxy proxy = ActivityRecognitionProxy.createAndBind(
584 mContext,
585 mLocationHandler,
586 activityRecognitionHardwareIsSupported,
587 activityRecognitionHardware,
588 com.android.internal.R.bool.config_enableActivityRecognitionHardwareOverlay,
589 com.android.internal.R.string.config_activityRecognitionHardwarePackageName,
590 com.android.internal.R.array.config_locationProviderPackageNames);
591 if (proxy == null) {
592 Slog.e(TAG, "Unable to bind ActivityRecognitionProxy.");
593 }
destradaaa4fa3b52014-07-09 10:46:39 -0700594
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900595 String[] testProviderStrings = resources.getStringArray(
596 com.android.internal.R.array.config_testLocationProviders);
597 for (String testProviderString : testProviderStrings) {
598 String fragments[] = testProviderString.split(",");
599 String name = fragments[0].trim();
600 if (mProvidersByName.get(name) != null) {
601 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
602 }
603 ProviderProperties properties = new ProviderProperties(
604 Boolean.parseBoolean(fragments[1]) /* requiresNetwork */,
605 Boolean.parseBoolean(fragments[2]) /* requiresSatellite */,
606 Boolean.parseBoolean(fragments[3]) /* requiresCell */,
607 Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */,
608 Boolean.parseBoolean(fragments[5]) /* supportsAltitude */,
609 Boolean.parseBoolean(fragments[6]) /* supportsSpeed */,
610 Boolean.parseBoolean(fragments[7]) /* supportsBearing */,
611 Integer.parseInt(fragments[8]) /* powerRequirement */,
612 Integer.parseInt(fragments[9]) /* accuracy */);
613 addTestProviderLocked(name, properties);
614 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700615 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700618 * Called when the device's active user changes.
619 * @param userId the new active user's UserId
620 */
621 private void switchUser(int userId) {
Jianzheng Zhoud5c69462013-10-10 14:02:09 +0800622 if (mCurrentUserId == userId) {
623 return;
624 }
Victoria Lease83762d22012-10-03 13:51:17 -0700625 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800626 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700627 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700628 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700629 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700630 for (LocationProviderInterface p : mProviders) {
Amith Yamasanib27528d2014-06-05 15:02:10 -0700631 updateProviderListenersLocked(p.getName(), false);
Victoria Leaseb711d572012-10-02 13:14:11 -0700632 }
Victoria Lease38389b62012-09-30 11:44:22 -0700633 mCurrentUserId = userId;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700634 updateUserProfiles(userId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700635 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700636 }
637 }
638
639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
641 * location updates.
642 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700643 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700644 final int mUid; // uid of receiver
645 final int mPid; // pid of receiver
646 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700647 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 final ILocationListener mListener;
650 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700651 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700652 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700654
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400655 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700656
David Christie0b837452013-07-29 16:02:13 -0700657 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700658 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700659 // True if app ops has started monitoring this receiver for high power (gps) locations.
660 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700661 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700662 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700664 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700665 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700668 if (listener != null) {
669 mKey = listener.asBinder();
670 } else {
671 mKey = intent;
672 }
Victoria Lease37425c32012-10-16 16:08:48 -0700673 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700674 mUid = uid;
675 mPid = pid;
676 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700677 if (workSource != null && workSource.size() <= 0) {
678 workSource = null;
679 }
680 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700681 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700682
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700683 updateMonitoring(true);
684
Victoria Lease0aa28602013-05-29 15:28:26 -0700685 // construct/configure wakelock
686 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700687 if (workSource == null) {
688 workSource = new WorkSource(mUid, mPackageName);
689 }
690 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692
693 @Override
694 public boolean equals(Object otherObj) {
695 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700696 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698 return false;
699 }
700
701 @Override
702 public int hashCode() {
703 return mKey.hashCode();
704 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 @Override
707 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700708 StringBuilder s = new StringBuilder();
709 s.append("Reciever[");
710 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700712 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700714 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700716 for (String p : mUpdateRecords.keySet()) {
717 s.append(" ").append(mUpdateRecords.get(p).toString());
718 }
719 s.append("]");
720 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
David Christie15b31912013-08-13 15:54:32 -0700723 /**
724 * Update AppOp monitoring for this receiver.
725 *
726 * @param allow If true receiver is currently active, if false it's been removed.
727 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700728 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700729 if (mHideFromAppOps) {
730 return;
731 }
732
David Christie15b31912013-08-13 15:54:32 -0700733 boolean requestingLocation = false;
734 boolean requestingHighPowerLocation = false;
735 if (allow) {
736 // See if receiver has any enabled update records. Also note if any update records
737 // are high power (has a high power provider with an interval under a threshold).
738 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
739 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
740 requestingLocation = true;
741 LocationProviderInterface locationProvider
David Christie2ff96af2014-01-30 16:09:37 -0800742 = mProvidersByName.get(updateRecord.mProvider);
David Christie15b31912013-08-13 15:54:32 -0700743 ProviderProperties properties = locationProvider != null
744 ? locationProvider.getProperties() : null;
745 if (properties != null
746 && properties.mPowerRequirement == Criteria.POWER_HIGH
747 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
748 requestingHighPowerLocation = true;
749 break;
750 }
751 }
752 }
753 }
754
David Christie0b837452013-07-29 16:02:13 -0700755 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700756 mOpMonitoring = updateMonitoring(
757 requestingLocation,
758 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700759 AppOpsManager.OP_MONITOR_LOCATION);
760
761 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700762 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700763 mOpHighPowerMonitoring = updateMonitoring(
764 requestingHighPowerLocation,
765 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700766 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700767 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700768 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700769 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
770 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
771 }
David Christie0b837452013-07-29 16:02:13 -0700772 }
773
774 /**
775 * Update AppOps monitoring for a single location request and op type.
776 *
777 * @param allowMonitoring True if monitoring is allowed for this request/op.
778 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
779 * @param op AppOps code for the op to update.
780 * @return True if monitoring is on for this request/op after updating.
781 */
782 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
783 int op) {
784 if (!currentlyMonitoring) {
785 if (allowMonitoring) {
786 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
787 == AppOpsManager.MODE_ALLOWED;
788 }
789 } else {
790 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
791 != AppOpsManager.MODE_ALLOWED) {
792 mAppOps.finishOp(op, mUid, mPackageName);
793 return false;
794 }
795 }
796
797 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700798 }
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 public boolean isListener() {
801 return mListener != null;
802 }
803
804 public boolean isPendingIntent() {
805 return mPendingIntent != null;
806 }
807
808 public ILocationListener getListener() {
809 if (mListener != null) {
810 return mListener;
811 }
812 throw new IllegalStateException("Request for non-existent listener");
813 }
814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
816 if (mListener != null) {
817 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700818 synchronized (this) {
819 // synchronize to ensure incrementPendingBroadcastsLocked()
820 // is called before decrementPendingBroadcasts()
821 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700822 // call this after broadcasting so we do not increment
823 // if we throw an exeption.
824 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 } catch (RemoteException e) {
827 return false;
828 }
829 } else {
830 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800831 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
833 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700834 synchronized (this) {
835 // synchronize to ensure incrementPendingBroadcastsLocked()
836 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700837 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700838 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700839 // call this after broadcasting so we do not increment
840 // if we throw an exeption.
841 incrementPendingBroadcastsLocked();
842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 } catch (PendingIntent.CanceledException e) {
844 return false;
845 }
846 }
847 return true;
848 }
849
850 public boolean callLocationChangedLocked(Location location) {
851 if (mListener != null) {
852 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700853 synchronized (this) {
854 // synchronize to ensure incrementPendingBroadcastsLocked()
855 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800856 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700857 // call this after broadcasting so we do not increment
858 // if we throw an exeption.
859 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 } catch (RemoteException e) {
862 return false;
863 }
864 } else {
865 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800866 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700868 synchronized (this) {
869 // synchronize to ensure incrementPendingBroadcastsLocked()
870 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700871 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700872 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700873 // call this after broadcasting so we do not increment
874 // if we throw an exeption.
875 incrementPendingBroadcastsLocked();
876 }
877 } catch (PendingIntent.CanceledException e) {
878 return false;
879 }
880 }
881 return true;
882 }
883
884 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -0700885 // First update AppOp monitoring.
886 // An app may get/lose location access as providers are enabled/disabled.
887 updateMonitoring(true);
888
Mike Lockwood48f17512009-04-23 09:12:08 -0700889 if (mListener != null) {
890 try {
891 synchronized (this) {
892 // synchronize to ensure incrementPendingBroadcastsLocked()
893 // is called before decrementPendingBroadcasts()
894 if (enabled) {
895 mListener.onProviderEnabled(provider);
896 } else {
897 mListener.onProviderDisabled(provider);
898 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700899 // call this after broadcasting so we do not increment
900 // if we throw an exeption.
901 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700902 }
903 } catch (RemoteException e) {
904 return false;
905 }
906 } else {
907 Intent providerIntent = new Intent();
908 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
909 try {
910 synchronized (this) {
911 // synchronize to ensure incrementPendingBroadcastsLocked()
912 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700913 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700914 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700915 // call this after broadcasting so we do not increment
916 // if we throw an exeption.
917 incrementPendingBroadcastsLocked();
918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 } catch (PendingIntent.CanceledException e) {
920 return false;
921 }
922 }
923 return true;
924 }
925
Nick Pellyf1be6862012-05-15 10:53:42 -0700926 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700928 if (D) Log.d(TAG, "Location listener died");
929
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400930 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 removeUpdatesLocked(this);
932 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700933 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700934 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700935 }
936 }
937
Nick Pellye0fd6932012-07-11 10:26:13 -0700938 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700939 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
940 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400941 synchronized (this) {
942 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700943 }
944 }
945
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400946 // this must be called while synchronized by caller in a synchronized block
947 // containing the sending of the broadcaset
948 private void incrementPendingBroadcastsLocked() {
949 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700950 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400951 }
952 }
953
954 private void decrementPendingBroadcastsLocked() {
955 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700956 if (mWakeLock.isHeld()) {
957 mWakeLock.release();
958 }
959 }
960 }
961
962 public void clearPendingBroadcastsLocked() {
963 if (mPendingBroadcasts > 0) {
964 mPendingBroadcasts = 0;
965 if (mWakeLock.isHeld()) {
966 mWakeLock.release();
967 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700968 }
969 }
970 }
971
Nick Pellye0fd6932012-07-11 10:26:13 -0700972 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700973 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700974 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400975 //the receiver list if it is not found. If it is not found then the
976 //LocationListener was removed when it had a pending broadcast and should
977 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700978 synchronized (mLock) {
979 IBinder binder = listener.asBinder();
980 Receiver receiver = mReceivers.get(binder);
981 if (receiver != null) {
982 synchronized (receiver) {
983 // so wakelock calls will succeed
984 long identity = Binder.clearCallingIdentity();
985 receiver.decrementPendingBroadcastsLocked();
986 Binder.restoreCallingIdentity(identity);
David Christie2ff96af2014-01-30 16:09:37 -0800987 }
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
990 }
991
Lifu Tang82f893d2016-01-21 18:15:33 -0800992 /**
993 * Returns the system information of the GPS hardware.
994 */
995 @Override
996 public int getGpsYearOfHardware() {
Lifu Tang818aa2c2016-02-01 01:52:00 -0800997 if (mGnssNavigationMessageProvider != null) {
Lifu Tang82f893d2016-01-21 18:15:33 -0800998 return mGpsSystemInfoProvider.getGpsYearOfHardware();
999 } else {
1000 return 0;
1001 }
1002 }
1003
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001004 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001005 mProviders.add(provider);
1006 mProvidersByName.put(provider.getName(), provider);
1007 }
1008
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001009 private void removeProviderLocked(LocationProviderInterface provider) {
1010 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001011 mProviders.remove(provider);
1012 mProvidersByName.remove(provider.getName());
1013 }
1014
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001015 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001016 * Returns "true" if access to the specified location provider is allowed by the current
1017 * user's settings. Access to all location providers is forbidden to non-location-provider
1018 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001019 *
1020 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001021 * @return
1022 */
Victoria Lease09eeaec2013-02-05 11:34:13 -08001023 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 if (mEnabledProviders.contains(provider)) {
1025 return true;
1026 }
1027 if (mDisabledProviders.contains(provider)) {
1028 return false;
1029 }
1030 // Use system settings
1031 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032
Victoria Leaseb711d572012-10-02 13:14:11 -07001033 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001036 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001037 * Returns "true" if access to the specified location provider is allowed by the specified
1038 * user's settings. Access to all location providers is forbidden to non-location-provider
1039 * processes belonging to background users.
1040 *
1041 * @param provider the name of the location provider
1042 * @param uid the requestor's UID
1043 * @return
1044 */
1045 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001046 if (!isCurrentProfile(UserHandle.getUserId(uid)) && !isUidALocationProvider(uid)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001047 return false;
1048 }
1049 return isAllowedByCurrentUserSettingsLocked(provider);
1050 }
1051
1052 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001053 * Returns the permission string associated with the specified resolution level.
1054 *
1055 * @param resolutionLevel the resolution level
1056 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001057 */
Victoria Lease37425c32012-10-16 16:08:48 -07001058 private String getResolutionPermission(int resolutionLevel) {
1059 switch (resolutionLevel) {
1060 case RESOLUTION_LEVEL_FINE:
1061 return android.Manifest.permission.ACCESS_FINE_LOCATION;
1062 case RESOLUTION_LEVEL_COARSE:
1063 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
1064 default:
1065 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
Victoria Leaseda479c52012-10-15 15:24:16 -07001067 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001068
Victoria Leaseda479c52012-10-15 15:24:16 -07001069 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001070 * Returns the resolution level allowed to the given PID/UID pair.
1071 *
1072 * @param pid the PID
1073 * @param uid the UID
1074 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -07001075 */
Victoria Lease37425c32012-10-16 16:08:48 -07001076 private int getAllowedResolutionLevel(int pid, int uid) {
1077 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
1078 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1079 return RESOLUTION_LEVEL_FINE;
1080 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
1081 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1082 return RESOLUTION_LEVEL_COARSE;
1083 } else {
1084 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -07001085 }
Victoria Lease4fab68b2012-09-13 13:20:59 -07001086 }
1087
1088 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001089 * Returns the resolution level allowed to the caller
1090 *
1091 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -07001092 */
Victoria Lease37425c32012-10-16 16:08:48 -07001093 private int getCallerAllowedResolutionLevel() {
1094 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
1095 }
1096
1097 /**
1098 * Throw SecurityException if specified resolution level is insufficient to use geofences.
1099 *
1100 * @param allowedResolutionLevel resolution level allowed to caller
1101 */
1102 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
1103 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -07001104 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
1105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 }
1107
Victoria Lease37425c32012-10-16 16:08:48 -07001108 /**
1109 * Return the minimum resolution level required to use the specified location provider.
1110 *
1111 * @param provider the name of the location provider
1112 * @return minimum resolution level required for provider
1113 */
1114 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001115 if (LocationManager.GPS_PROVIDER.equals(provider) ||
1116 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
1117 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001118 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -07001119 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
1120 LocationManager.FUSED_PROVIDER.equals(provider)) {
1121 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001122 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001123 } else {
1124 // mock providers
1125 LocationProviderInterface lp = mMockProviders.get(provider);
1126 if (lp != null) {
1127 ProviderProperties properties = lp.getProperties();
1128 if (properties != null) {
1129 if (properties.mRequiresSatellite) {
1130 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001131 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -07001132 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
1133 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001134 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001135 }
1136 }
1137 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001138 }
Victoria Lease37425c32012-10-16 16:08:48 -07001139 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -07001140 }
1141
Victoria Lease37425c32012-10-16 16:08:48 -07001142 /**
1143 * Throw SecurityException if specified resolution level is insufficient to use the named
1144 * location provider.
1145 *
1146 * @param allowedResolutionLevel resolution level allowed to caller
1147 * @param providerName the name of the location provider
1148 */
1149 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
1150 String providerName) {
1151 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
1152 if (allowedResolutionLevel < requiredResolutionLevel) {
1153 switch (requiredResolutionLevel) {
1154 case RESOLUTION_LEVEL_FINE:
1155 throw new SecurityException("\"" + providerName + "\" location provider " +
1156 "requires ACCESS_FINE_LOCATION permission.");
1157 case RESOLUTION_LEVEL_COARSE:
1158 throw new SecurityException("\"" + providerName + "\" location provider " +
1159 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
1160 default:
1161 throw new SecurityException("Insufficient permission for \"" + providerName +
1162 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -07001163 }
1164 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001165 }
1166
David Christie82edc9b2013-07-19 11:31:42 -07001167 /**
1168 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
1169 * for battery).
1170 */
David Christie40e57822013-07-30 11:36:48 -07001171 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -07001172 mContext.enforceCallingOrSelfPermission(
1173 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
1174 }
1175
David Christie40e57822013-07-30 11:36:48 -07001176 private void checkUpdateAppOpsAllowed() {
1177 mContext.enforceCallingOrSelfPermission(
1178 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
1179 }
1180
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001181 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001182 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1183 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001184 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001185 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001186 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001187 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001188 }
1189 return -1;
1190 }
1191
David Christieb870dbf2015-06-22 12:42:53 -07001192 boolean reportLocationAccessNoThrow(
1193 int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001194 int op = resolutionLevelToOp(allowedResolutionLevel);
1195 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001196 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1197 return false;
1198 }
1199 }
David Christieb870dbf2015-06-22 12:42:53 -07001200
1201 if (getAllowedResolutionLevel(pid, uid) < allowedResolutionLevel) {
1202 return false;
1203 }
1204
Dianne Hackborn35654b62013-01-14 17:38:02 -08001205 return true;
1206 }
1207
David Christieb870dbf2015-06-22 12:42:53 -07001208 boolean checkLocationAccess(int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001209 int op = resolutionLevelToOp(allowedResolutionLevel);
1210 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001211 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1212 return false;
1213 }
1214 }
David Christieb870dbf2015-06-22 12:42:53 -07001215
1216 if (getAllowedResolutionLevel(pid, uid) < allowedResolutionLevel) {
1217 return false;
1218 }
1219
Dianne Hackborn35654b62013-01-14 17:38:02 -08001220 return true;
1221 }
1222
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001223 /**
1224 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001225 * fused, also including ones that are not permitted to
1226 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001227 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001230 ArrayList<String> out;
1231 synchronized (mLock) {
1232 out = new ArrayList<String>(mProviders.size());
1233 for (LocationProviderInterface provider : mProviders) {
1234 String name = provider.getName();
1235 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001236 continue;
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 out.add(name);
1239 }
1240 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001241
1242 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 return out;
1244 }
1245
Mike Lockwood03ca2162010-04-01 08:10:09 -07001246 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001247 * Return all providers by name, that match criteria and are optionally
1248 * enabled.
1249 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001250 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001251 @Override
1252 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001253 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001254 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001255 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001256 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001257 try {
1258 synchronized (mLock) {
1259 out = new ArrayList<String>(mProviders.size());
1260 for (LocationProviderInterface provider : mProviders) {
1261 String name = provider.getName();
1262 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001263 continue;
1264 }
Victoria Lease37425c32012-10-16 16:08:48 -07001265 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001266 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001267 continue;
1268 }
1269 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1270 name, provider.getProperties(), criteria)) {
1271 continue;
1272 }
1273 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001274 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001275 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001276 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001277 } finally {
1278 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001279 }
1280
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001281 if (D) Log.d(TAG, "getProviders()=" + out);
1282 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001283 }
1284
1285 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001286 * Return the name of the best provider given a Criteria object.
1287 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001288 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001289 * has been deprecated as well. So this method now uses
1290 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001291 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001292 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001293 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001294 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001295
1296 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001297 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001298 result = pickBest(providers);
1299 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1300 return result;
1301 }
1302 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001303 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001304 result = pickBest(providers);
1305 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1306 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001307 }
1308
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001309 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001310 return null;
1311 }
1312
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001313 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001314 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001315 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001316 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1317 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001318 } else {
1319 return providers.get(0);
1320 }
1321 }
1322
Nick Pellye0fd6932012-07-11 10:26:13 -07001323 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001324 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1325 LocationProviderInterface p = mProvidersByName.get(provider);
1326 if (p == null) {
1327 throw new IllegalArgumentException("provider=" + provider);
1328 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001329
1330 boolean result = LocationProvider.propertiesMeetCriteria(
1331 p.getName(), p.getProperties(), criteria);
1332 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1333 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001334 }
1335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001337 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001338 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001339 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 boolean isEnabled = p.isEnabled();
1341 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001342 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 if (isEnabled && !shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001344 updateProviderListenersLocked(name, false);
David Christieb084fef2013-12-18 14:33:57 -08001345 // If any provider has been disabled, clear all last locations for all providers.
1346 // This is to be on the safe side in case a provider has location derived from
1347 // this disabled provider.
1348 mLastLocation.clear();
1349 mLastLocationCoarseInterval.clear();
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001350 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 } else if (!isEnabled && shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001352 updateProviderListenersLocked(name, true);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001353 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001355 }
1356 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001357 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1358 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001359 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1360 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
1362 }
1363
Amith Yamasanib27528d2014-06-05 15:02:10 -07001364 private void updateProviderListenersLocked(String provider, boolean enabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 int listeners = 0;
1366
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001367 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001368 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369
1370 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1373 if (records != null) {
1374 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001375 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 UpdateRecord record = records.get(i);
Amith Yamasanib27528d2014-06-05 15:02:10 -07001377 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001378 // Sends a notification message to the receiver
1379 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1380 if (deadReceivers == null) {
1381 deadReceivers = new ArrayList<Receiver>();
1382 }
1383 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001385 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388 }
1389
1390 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001391 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 removeUpdatesLocked(deadReceivers.get(i));
1393 }
1394 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 if (enabled) {
1397 p.enable();
1398 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001399 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001406 private void applyRequirementsLocked(String provider) {
1407 LocationProviderInterface p = mProvidersByName.get(provider);
1408 if (p == null) return;
1409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001411 WorkSource worksource = new WorkSource();
1412 ProviderRequest providerRequest = new ProviderRequest();
1413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001415 for (UpdateRecord record : records) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001416 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
David Christieb870dbf2015-06-22 12:42:53 -07001417 if (checkLocationAccess(
1418 record.mReceiver.mPid,
1419 record.mReceiver.mUid,
1420 record.mReceiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001421 record.mReceiver.mAllowedResolutionLevel)) {
1422 LocationRequest locationRequest = record.mRequest;
1423 providerRequest.locationRequests.add(locationRequest);
1424 if (locationRequest.getInterval() < providerRequest.interval) {
1425 providerRequest.reportLocation = true;
1426 providerRequest.interval = locationRequest.getInterval();
1427 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001428 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001429 }
1430 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001431
1432 if (providerRequest.reportLocation) {
1433 // calculate who to blame for power
1434 // This is somewhat arbitrary. We pick a threshold interval
1435 // that is slightly higher that the minimum interval, and
1436 // spread the blame across all applications with a request
1437 // under that threshold.
1438 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1439 for (UpdateRecord record : records) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001440 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001441 LocationRequest locationRequest = record.mRequest;
1442 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001443 if (record.mReceiver.mWorkSource != null
1444 && record.mReceiver.mWorkSource.size() > 0
1445 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001446 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001447 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001448 worksource.add(record.mReceiver.mWorkSource);
1449 } else {
1450 // Assign blame to caller.
1451 worksource.add(
1452 record.mReceiver.mUid,
1453 record.mReceiver.mPackageName);
1454 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001455 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001456 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001460
1461 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1462 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 }
1464
1465 private class UpdateRecord {
1466 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001467 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001469 Location mLastFixBroadcast;
1470 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471
1472 /**
1473 * Note: must be constructed with lock held.
1474 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001475 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001477 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479
1480 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1481 if (records == null) {
1482 records = new ArrayList<UpdateRecord>();
1483 mRecordsByProvider.put(provider, records);
1484 }
1485 if (!records.contains(this)) {
1486 records.add(this);
1487 }
David Christie2ff96af2014-01-30 16:09:37 -08001488
1489 // Update statistics for historical location requests by package/provider
1490 mRequestStatistics.startRequesting(
1491 mReceiver.mPackageName, provider, request.getInterval());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
1493
1494 /**
David Christie2ff96af2014-01-30 16:09:37 -08001495 * Method to be called when a record will no longer be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001497 void disposeLocked(boolean removeReceiver) {
David Christie2ff96af2014-01-30 16:09:37 -08001498 mRequestStatistics.stopRequesting(mReceiver.mPackageName, mProvider);
1499
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001500 // remove from mRecordsByProvider
1501 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1502 if (globalRecords != null) {
1503 globalRecords.remove(this);
1504 }
1505
1506 if (!removeReceiver) return; // the caller will handle the rest
1507
1508 // remove from Receiver#mUpdateRecords
1509 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1510 if (receiverRecords != null) {
1511 receiverRecords.remove(this.mProvider);
1512
1513 // and also remove the Receiver if it has no more update records
1514 if (removeReceiver && receiverRecords.size() == 0) {
1515 removeUpdatesLocked(mReceiver);
1516 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
1519
1520 @Override
1521 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001522 StringBuilder s = new StringBuilder();
1523 s.append("UpdateRecord[");
1524 s.append(mProvider);
1525 s.append(' ').append(mReceiver.mPackageName).append('(');
1526 s.append(mReceiver.mUid).append(')');
1527 s.append(' ').append(mRequest);
1528 s.append(']');
1529 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001533 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001534 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001535 IBinder binder = listener.asBinder();
1536 Receiver receiver = mReceivers.get(binder);
1537 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001538 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1539 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001540 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001541 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001542 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001543 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001544 return null;
1545 }
Wen Jingcb3ab222014-03-27 13:42:59 +08001546 mReceivers.put(binder, receiver);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001547 }
1548 return receiver;
1549 }
1550
David Christie82edc9b2013-07-19 11:31:42 -07001551 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001552 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001553 Receiver receiver = mReceivers.get(intent);
1554 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001555 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1556 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001557 mReceivers.put(intent, receiver);
1558 }
1559 return receiver;
1560 }
1561
Victoria Lease37425c32012-10-16 16:08:48 -07001562 /**
1563 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1564 * and consistency requirements.
1565 *
1566 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001567 * @return a version of request that meets the given resolution and consistency requirements
1568 * @hide
1569 */
1570 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1571 LocationRequest sanitizedRequest = new LocationRequest(request);
1572 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1573 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001574 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001575 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001576 break;
1577 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001578 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001579 break;
1580 }
1581 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001582 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1583 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001584 }
Victoria Lease37425c32012-10-16 16:08:48 -07001585 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1586 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001587 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001588 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001589 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001590 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001591 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001592 }
Victoria Lease37425c32012-10-16 16:08:48 -07001593 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001594 }
1595
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001596 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001597 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001598 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001599 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001600 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001601 String[] packages = mPackageManager.getPackagesForUid(uid);
1602 if (packages == null) {
1603 throw new SecurityException("invalid UID " + uid);
1604 }
1605 for (String pkg : packages) {
1606 if (packageName.equals(pkg)) return;
1607 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001608 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001609 }
1610
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001611 private void checkPendingIntent(PendingIntent intent) {
1612 if (intent == null) {
1613 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001614 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001615 }
1616
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001617 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001618 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001619 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001620 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001621 } else if (intent != null && listener != null) {
1622 throw new IllegalArgumentException("cannot register both listener and intent");
1623 } else if (intent != null) {
1624 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001625 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001626 } else {
David Christie40e57822013-07-30 11:36:48 -07001627 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001628 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001629 }
1630
Nick Pellye0fd6932012-07-11 10:26:13 -07001631 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001632 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1633 PendingIntent intent, String packageName) {
1634 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1635 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001636 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1637 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1638 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001639 WorkSource workSource = request.getWorkSource();
1640 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001641 checkDeviceStatsAllowed();
1642 }
1643 boolean hideFromAppOps = request.getHideFromAppOps();
1644 if (hideFromAppOps) {
1645 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001646 }
Victoria Lease37425c32012-10-16 16:08:48 -07001647 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001649 final int pid = Binder.getCallingPid();
1650 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001651 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 long identity = Binder.clearCallingIdentity();
1653 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001654 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1655 // a location.
David Christieb870dbf2015-06-22 12:42:53 -07001656 checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001657
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001658 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001659 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001660 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001661 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 } finally {
1664 Binder.restoreCallingIdentity(identity);
1665 }
1666 }
1667
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001668 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1669 int pid, int uid, String packageName) {
1670 // Figure out the provider. Either its explicitly request (legacy use cases), or
1671 // use the fused provider
1672 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1673 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001674 if (name == null) {
1675 throw new IllegalArgumentException("provider name must not be null");
1676 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001677
1678 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1679 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001680 LocationProviderInterface provider = mProvidersByName.get(name);
1681 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07001682 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001683 }
1684
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001685 UpdateRecord record = new UpdateRecord(name, request, receiver);
1686 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1687 if (oldRecord != null) {
1688 oldRecord.disposeLocked(false);
1689 }
1690
Victoria Lease09eeaec2013-02-05 11:34:13 -08001691 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001692 if (isProviderEnabled) {
1693 applyRequirementsLocked(name);
1694 } else {
1695 // Notify the listener that updates are currently disabled
1696 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
David Christie0b837452013-07-29 16:02:13 -07001698 // Update the monitoring here just in case multiple location requests were added to the
1699 // same receiver (this request may be high power and the initial might not have been).
1700 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702
Nick Pellye0fd6932012-07-11 10:26:13 -07001703 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001704 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1705 String packageName) {
1706 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001707
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001708 final int pid = Binder.getCallingPid();
1709 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001710
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001711 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001712 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001713 boolean hideFromAppOps = false;
1714 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1715 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001716
1717 // providers may use public location API's, need to clear identity
1718 long identity = Binder.clearCallingIdentity();
1719 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001720 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001721 } finally {
1722 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 }
1725 }
1726
1727 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001728 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001729
1730 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1731 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1732 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001733 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001737 receiver.updateMonitoring(false);
1738
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001739 // Record which providers were associated with this listener
1740 HashSet<String> providers = new HashSet<String>();
1741 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1742 if (oldRecords != null) {
1743 // Call dispose() on the obsolete update records.
1744 for (UpdateRecord record : oldRecords.values()) {
David Christie2ff96af2014-01-30 16:09:37 -08001745 // Update statistics for historical location requests by package/provider
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001746 record.disposeLocked(false);
1747 }
1748 // Accumulate providers
1749 providers.addAll(oldRecords.keySet());
1750 }
1751
1752 // update provider
1753 for (String provider : providers) {
1754 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001755 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001756 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001759 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761 }
1762
Dianne Hackbornc2293022013-02-06 23:14:49 -08001763 private void applyAllProviderRequirementsLocked() {
1764 for (LocationProviderInterface p : mProviders) {
1765 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001766 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001767 continue;
1768 }
1769
1770 applyRequirementsLocked(p.getName());
1771 }
1772 }
1773
Nick Pellye0fd6932012-07-11 10:26:13 -07001774 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001775 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001776 if (D) Log.d(TAG, "getLastLocation: " + request);
1777 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001778 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001779 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001780 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1781 request.getProvider());
1782 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001783
David Christieb870dbf2015-06-22 12:42:53 -07001784 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001785 final int uid = Binder.getCallingUid();
1786 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001787 try {
1788 if (mBlacklist.isBlacklisted(packageName)) {
1789 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1790 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001791 return null;
1792 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001793
David Christieb870dbf2015-06-22 12:42:53 -07001794 if (!reportLocationAccessNoThrow(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001795 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1796 packageName);
1797 return null;
1798 }
1799
Victoria Leaseb711d572012-10-02 13:14:11 -07001800 synchronized (mLock) {
1801 // Figure out the provider. Either its explicitly request (deprecated API's),
1802 // or use the fused provider
1803 String name = request.getProvider();
1804 if (name == null) name = LocationManager.FUSED_PROVIDER;
1805 LocationProviderInterface provider = mProvidersByName.get(name);
1806 if (provider == null) return null;
1807
Victoria Lease09eeaec2013-02-05 11:34:13 -08001808 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001809
David Christie1b9b7b12013-04-15 15:31:11 -07001810 Location location;
1811 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1812 // Make sure that an app with coarse permissions can't get frequent location
1813 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1814 location = mLastLocationCoarseInterval.get(name);
1815 } else {
1816 location = mLastLocation.get(name);
1817 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001818 if (location == null) {
1819 return null;
1820 }
Victoria Lease37425c32012-10-16 16:08:48 -07001821 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001822 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1823 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001824 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001825 }
Victoria Lease37425c32012-10-16 16:08:48 -07001826 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001827 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001828 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001829 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001830 return null;
1831 } finally {
1832 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001833 }
1834 }
1835
1836 @Override
1837 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1838 String packageName) {
1839 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001840 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1841 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001842 checkPendingIntent(intent);
1843 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001844 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1845 request.getProvider());
1846 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001847
Victoria Lease37425c32012-10-16 16:08:48 -07001848 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001849
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001850 // geo-fence manager uses the public location API, need to clear identity
1851 int uid = Binder.getCallingUid();
Xiaohui Chena4490622015-09-22 15:29:31 -07001852 // TODO: http://b/23822629
1853 if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) {
Victoria Lease56e675b2012-11-05 19:25:06 -08001854 // temporary measure until geofences work for secondary users
1855 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1856 return;
1857 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001858 long identity = Binder.clearCallingIdentity();
1859 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001860 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1861 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001862 } finally {
1863 Binder.restoreCallingIdentity(identity);
1864 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001865 }
1866
1867 @Override
1868 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001869 checkPendingIntent(intent);
1870 checkPackageName(packageName);
1871
1872 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1873
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001874 // geo-fence manager uses the public location API, need to clear identity
1875 long identity = Binder.clearCallingIdentity();
1876 try {
1877 mGeofenceManager.removeFence(geofence, intent);
1878 } finally {
1879 Binder.restoreCallingIdentity(identity);
1880 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001881 }
1882
1883
1884 @Override
Lifu Tang30f95a72016-01-07 23:20:38 -08001885 public boolean registerGnssStatusCallback(IGnssStatusListener callback, String packageName) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001886 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1887 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001888 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889
David Christieb870dbf2015-06-22 12:42:53 -07001890 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001891 final int uid = Binder.getCallingUid();
1892 final long ident = Binder.clearCallingIdentity();
1893 try {
David Christieb870dbf2015-06-22 12:42:53 -07001894 if (!checkLocationAccess(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001895 return false;
1896 }
1897 } finally {
1898 Binder.restoreCallingIdentity(ident);
1899 }
1900
Lifu Tang30f95a72016-01-07 23:20:38 -08001901 if (mGnssStatusProvider == null) {
Takayuki Hoshib254ab6a2014-10-23 16:46:02 +09001902 return false;
1903 }
1904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 try {
Lifu Tang30f95a72016-01-07 23:20:38 -08001906 mGnssStatusProvider.registerGnssStatusCallback(callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 } catch (RemoteException e) {
Lifu Tang30f95a72016-01-07 23:20:38 -08001908 Slog.e(TAG, "mGpsStatusProvider.registerGnssStatusCallback failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 return false;
1910 }
1911 return true;
1912 }
1913
Nick Pellye0fd6932012-07-11 10:26:13 -07001914 @Override
Lifu Tang30f95a72016-01-07 23:20:38 -08001915 public void unregisterGnssStatusCallback(IGnssStatusListener callback) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001916 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001917 try {
Lifu Tang30f95a72016-01-07 23:20:38 -08001918 mGnssStatusProvider.unregisterGnssStatusCallback(callback);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001919 } catch (Exception e) {
Lifu Tang30f95a72016-01-07 23:20:38 -08001920 Slog.e(TAG, "mGpsStatusProvider.unregisterGnssStatusCallback failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 }
1924
Nick Pellye0fd6932012-07-11 10:26:13 -07001925 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001926 public boolean addGnssMeasurementsListener(
1927 IGnssMeasurementsListener listener,
destradaaea8a8a62014-06-23 18:19:03 -07001928 String packageName) {
1929 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1930 checkResolutionLevelIsSufficientForProviderUse(
1931 allowedResolutionLevel,
1932 LocationManager.GPS_PROVIDER);
1933
David Christieb870dbf2015-06-22 12:42:53 -07001934 int pid = Binder.getCallingPid();
destradaaea8a8a62014-06-23 18:19:03 -07001935 int uid = Binder.getCallingUid();
1936 long identity = Binder.clearCallingIdentity();
1937 boolean hasLocationAccess;
1938 try {
David Christieb870dbf2015-06-22 12:42:53 -07001939 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaaea8a8a62014-06-23 18:19:03 -07001940 } finally {
1941 Binder.restoreCallingIdentity(identity);
1942 }
1943
Lifu Tang818aa2c2016-02-01 01:52:00 -08001944 if (!hasLocationAccess || mGnssMeasurementsProvider == null) {
destradaaea8a8a62014-06-23 18:19:03 -07001945 return false;
1946 }
Lifu Tang818aa2c2016-02-01 01:52:00 -08001947 return mGnssMeasurementsProvider.addListener(listener);
destradaaea8a8a62014-06-23 18:19:03 -07001948 }
1949
1950 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001951 public void removeGnssMeasurementsListener(IGnssMeasurementsListener listener) {
1952 if (mGnssMeasurementsProvider != null) {
1953 mGnssMeasurementsProvider.removeListener(listener);
Wei Liu5241a4c2015-05-11 14:00:36 -07001954 }
destradaaea8a8a62014-06-23 18:19:03 -07001955 }
1956
1957 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001958 public boolean addGnssNavigationMessageListener(
1959 IGnssNavigationMessageListener listener,
destradaa4b3e3932014-07-21 18:01:47 -07001960 String packageName) {
1961 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1962 checkResolutionLevelIsSufficientForProviderUse(
1963 allowedResolutionLevel,
1964 LocationManager.GPS_PROVIDER);
1965
David Christieb870dbf2015-06-22 12:42:53 -07001966 int pid = Binder.getCallingPid();
destradaa4b3e3932014-07-21 18:01:47 -07001967 int uid = Binder.getCallingUid();
1968 long identity = Binder.clearCallingIdentity();
1969 boolean hasLocationAccess;
1970 try {
David Christieb870dbf2015-06-22 12:42:53 -07001971 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaa4b3e3932014-07-21 18:01:47 -07001972 } finally {
1973 Binder.restoreCallingIdentity(identity);
1974 }
1975
Lifu Tang818aa2c2016-02-01 01:52:00 -08001976 if (!hasLocationAccess || mGnssNavigationMessageProvider == null) {
destradaa4b3e3932014-07-21 18:01:47 -07001977 return false;
1978 }
Lifu Tang818aa2c2016-02-01 01:52:00 -08001979 return mGnssNavigationMessageProvider.addListener(listener);
destradaa4b3e3932014-07-21 18:01:47 -07001980 }
1981
1982 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08001983 public void removeGnssNavigationMessageListener(IGnssNavigationMessageListener listener) {
1984 if (mGnssNavigationMessageProvider != null) {
1985 mGnssNavigationMessageProvider.removeListener(listener);
Wei Liu5241a4c2015-05-11 14:00:36 -07001986 }
destradaa4b3e3932014-07-21 18:01:47 -07001987 }
1988
1989 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001991 if (provider == null) {
1992 // throw NullPointerException to remain compatible with previous implementation
1993 throw new NullPointerException();
1994 }
Victoria Lease37425c32012-10-16 16:08:48 -07001995 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1996 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001999 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 != PackageManager.PERMISSION_GRANTED)) {
2001 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
2002 }
2003
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002004 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002005 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002006 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07002007
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002008 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010 }
2011
Nick Pellye0fd6932012-07-11 10:26:13 -07002012 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002013 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07002014 if (Binder.getCallingUid() != Process.myUid()) {
2015 throw new SecurityException(
2016 "calling sendNiResponse from outside of the system is not allowed");
2017 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04002018 try {
2019 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002020 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002021 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04002022 return false;
2023 }
2024 }
2025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002027 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11002028 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 * accessed by the caller
2030 */
Nick Pellye0fd6932012-07-11 10:26:13 -07002031 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002032 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07002033 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08002034 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07002035 }
2036
Victoria Lease37425c32012-10-16 16:08:48 -07002037 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
2038 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002040 LocationProviderInterface p;
2041 synchronized (mLock) {
2042 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
2044
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002045 if (p == null) return null;
2046 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048
Jason Monkb71218a2015-06-17 14:44:39 -04002049 /**
2050 * @return null if the provider does not exist
2051 * @throws SecurityException if the provider is not allowed to be
2052 * accessed by the caller
2053 */
2054 @Override
2055 public String getNetworkProviderPackage() {
2056 LocationProviderInterface p;
2057 synchronized (mLock) {
2058 if (mProvidersByName.get(LocationManager.NETWORK_PROVIDER) == null) {
2059 return null;
2060 }
2061 p = mProvidersByName.get(LocationManager.NETWORK_PROVIDER);
2062 }
2063
2064 if (p instanceof LocationProviderProxy) {
2065 return ((LocationProviderProxy) p).getConnectedPackageName();
2066 }
2067 return null;
2068 }
2069
Nick Pellye0fd6932012-07-11 10:26:13 -07002070 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07002072 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
2073 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002074 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
2075
Victoria Lease09eeaec2013-02-05 11:34:13 -08002076 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07002077 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07002078 try {
2079 synchronized (mLock) {
2080 LocationProviderInterface p = mProvidersByName.get(provider);
2081 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002082
Victoria Lease09eeaec2013-02-05 11:34:13 -08002083 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07002084 }
2085 } finally {
2086 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002087 }
2088 }
2089
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002090 /**
2091 * Returns "true" if the UID belongs to a bound location provider.
2092 *
2093 * @param uid the uid
2094 * @return true if uid belongs to a bound location provider
2095 */
2096 private boolean isUidALocationProvider(int uid) {
2097 if (uid == Process.SYSTEM_UID) {
2098 return true;
2099 }
2100 if (mGeocodeProvider != null) {
David Christie1f141c12014-05-14 15:11:15 -07002101 if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002102 }
2103 for (LocationProviderProxy proxy : mProxyProviders) {
David Christie1f141c12014-05-14 15:11:15 -07002104 if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002105 }
2106 return false;
2107 }
2108
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002109 private void checkCallerIsProvider() {
2110 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
2111 == PackageManager.PERMISSION_GRANTED) {
2112 return;
2113 }
2114
2115 // Previously we only used the INSTALL_LOCATION_PROVIDER
2116 // check. But that is system or signature
2117 // protection level which is not flexible enough for
2118 // providers installed oustide the system image. So
2119 // also allow providers with a UID matching the
2120 // currently bound package name
2121
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002122 if (isUidALocationProvider(Binder.getCallingUid())) {
2123 return;
2124 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002125
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002126 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
2127 "or UID of a currently bound location provider");
2128 }
2129
David Christie1f141c12014-05-14 15:11:15 -07002130 /**
2131 * Returns true if the given package belongs to the given uid.
2132 */
2133 private boolean doesUidHavePackage(int uid, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002134 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 return false;
2136 }
David Christie1f141c12014-05-14 15:11:15 -07002137 String[] packageNames = mPackageManager.getPackagesForUid(uid);
2138 if (packageNames == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002139 return false;
2140 }
David Christie1f141c12014-05-14 15:11:15 -07002141 for (String name : packageNames) {
2142 if (packageName.equals(name)) {
2143 return true;
2144 }
2145 }
2146 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 }
2148
Nick Pellye0fd6932012-07-11 10:26:13 -07002149 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05002150 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002151 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04002152
Nick Pelly2eeeec22012-07-18 13:13:37 -07002153 if (!location.isComplete()) {
2154 Log.w(TAG, "Dropping incomplete location: " + location);
2155 return;
2156 }
2157
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002158 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
2159 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05002160 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002161 mLocationHandler.sendMessageAtFrontOfQueue(m);
2162 }
2163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164
Laurent Tu75defb62012-11-01 16:21:52 -07002165 private static boolean shouldBroadcastSafe(
2166 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 // Always broadcast the first update
2168 if (lastLoc == null) {
2169 return true;
2170 }
2171
Nick Pellyf1be6862012-05-15 10:53:42 -07002172 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002173 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07002174 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
2175 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002176 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 return false;
2178 }
2179
2180 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002181 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 if (minDistance > 0.0) {
2183 if (loc.distanceTo(lastLoc) <= minDistance) {
2184 return false;
2185 }
2186 }
2187
Laurent Tu75defb62012-11-01 16:21:52 -07002188 // Check whether sufficient number of udpates is left
2189 if (record.mRequest.getNumUpdates() <= 0) {
2190 return false;
2191 }
2192
2193 // Check whether the expiry date has passed
2194 if (record.mRequest.getExpireAt() < now) {
2195 return false;
2196 }
2197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 return true;
2199 }
2200
Mike Lockwooda4903f22010-02-17 06:42:23 -05002201 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07002202 if (D) Log.d(TAG, "incoming location: " + location);
2203
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002204 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05002205 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206
Laurent Tu60ec50a2012-10-04 17:00:10 -07002207 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002208 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002209 if (p == null) return;
2210
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002211 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07002212 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2213 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002214 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002215 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002216 lastLocation = new Location(provider);
2217 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07002218 } else {
2219 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2220 if (noGPSLocation == null && lastNoGPSLocation != null) {
2221 // New location has no no-GPS location: adopt last no-GPS location. This is set
2222 // directly into location because we do not want to notify COARSE clients.
2223 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
2224 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07002225 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002226 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227
David Christie1b9b7b12013-04-15 15:31:11 -07002228 // Update last known coarse interval location if enough time has passed.
2229 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
2230 if (lastLocationCoarseInterval == null) {
2231 lastLocationCoarseInterval = new Location(location);
2232 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
2233 }
2234 long timeDiffNanos = location.getElapsedRealtimeNanos()
2235 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
2236 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
2237 lastLocationCoarseInterval.set(location);
2238 }
2239 // Don't ever return a coarse location that is more recent than the allowed update
2240 // interval (i.e. don't allow an app to keep registering and unregistering for
2241 // location updates to overcome the minimum interval).
2242 noGPSLocation =
2243 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2244
Laurent Tu60ec50a2012-10-04 17:00:10 -07002245 // Skip if there are no UpdateRecords for this provider.
2246 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
2247 if (records == null || records.size() == 0) return;
2248
Victoria Lease09016ab2012-09-16 12:33:15 -07002249 // Fetch coarse location
2250 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07002251 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002252 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
2253 }
2254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 // Fetch latest status update time
2256 long newStatusUpdateTime = p.getStatusUpdateTime();
2257
David Christie2ff96af2014-01-30 16:09:37 -08002258 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 Bundle extras = new Bundle();
2260 int status = p.getStatus(extras);
2261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002263 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07002264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002266 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07002268 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07002269
Victoria Lease269518e2012-10-29 08:25:39 -07002270 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Amith Yamasanib27528d2014-06-05 15:02:10 -07002271 if (!isCurrentProfile(receiverUserId) && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002272 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07002273 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07002274 " (current user: " + mCurrentUserId + ", app: " +
2275 receiver.mPackageName + ")");
2276 }
2277 continue;
2278 }
2279
Nick Pelly4035f5a2012-08-17 14:43:49 -07002280 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
2281 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
2282 receiver.mPackageName);
2283 continue;
2284 }
2285
David Christieb870dbf2015-06-22 12:42:53 -07002286 if (!reportLocationAccessNoThrow(receiver.mPid, receiver.mUid, receiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002287 receiver.mAllowedResolutionLevel)) {
2288 if (D) Log.d(TAG, "skipping loc update for no op app: " +
2289 receiver.mPackageName);
2290 continue;
2291 }
2292
Victoria Lease09016ab2012-09-16 12:33:15 -07002293 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07002294 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2295 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002296 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002297 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002298 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002299 if (notifyLocation != null) {
2300 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002301 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002302 if (lastLoc == null) {
2303 lastLoc = new Location(notifyLocation);
2304 r.mLastFixBroadcast = lastLoc;
2305 } else {
2306 lastLoc.set(notifyLocation);
2307 }
2308 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2309 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2310 receiverDead = true;
2311 }
Laurent Tu75defb62012-11-01 16:21:52 -07002312 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
2314 }
2315
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002316 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002318 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002320 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002322 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002323 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002324 }
2325 }
2326
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002327 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002328 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002329 if (deadUpdateRecords == null) {
2330 deadUpdateRecords = new ArrayList<UpdateRecord>();
2331 }
2332 deadUpdateRecords.add(r);
2333 }
2334 // track dead receivers
2335 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002336 if (deadReceivers == null) {
2337 deadReceivers = new ArrayList<Receiver>();
2338 }
2339 if (!deadReceivers.contains(receiver)) {
2340 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 }
2342 }
2343 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002344
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002345 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002347 for (Receiver receiver : deadReceivers) {
2348 removeUpdatesLocked(receiver);
2349 }
2350 }
2351 if (deadUpdateRecords != null) {
2352 for (UpdateRecord r : deadUpdateRecords) {
2353 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002355 applyRequirementsLocked(provider);
2356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 }
2358
2359 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002360 public LocationWorkerHandler(Looper looper) {
2361 super(looper, null, true);
2362 }
2363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 @Override
2365 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002366 switch (msg.what) {
2367 case MSG_LOCATION_CHANGED:
2368 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2369 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
2371 }
2372 }
2373
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002374 private boolean isMockProvider(String provider) {
2375 synchronized (mLock) {
2376 return mMockProviders.containsKey(provider);
2377 }
2378 }
2379
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002380 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002381 // create a working copy of the incoming Location so that the service can modify it without
2382 // disturbing the caller's copy
2383 Location myLocation = new Location(location);
2384 String provider = myLocation.getProvider();
2385
2386 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2387 // bit if location did not come from a mock provider because passive/fused providers can
2388 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2389 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2390 myLocation.setIsFromMockProvider(true);
2391 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002392
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002393 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002394 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2395 if (!passive) {
2396 // notify passive provider of the new location
2397 mPassiveProvider.updateLocation(myLocation);
2398 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002399 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403
Mike Lockwoode97ae402010-09-29 15:23:46 -04002404 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2405 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002406 public void onPackageDisappeared(String packageName, int reason) {
2407 // remove all receivers associated with this package name
2408 synchronized (mLock) {
2409 ArrayList<Receiver> deadReceivers = null;
2410
2411 for (Receiver receiver : mReceivers.values()) {
2412 if (receiver.mPackageName.equals(packageName)) {
2413 if (deadReceivers == null) {
2414 deadReceivers = new ArrayList<Receiver>();
2415 }
2416 deadReceivers.add(receiver);
2417 }
2418 }
2419
2420 // perform removal outside of mReceivers loop
2421 if (deadReceivers != null) {
2422 for (Receiver receiver : deadReceivers) {
2423 removeUpdatesLocked(receiver);
2424 }
2425 }
2426 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002427 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002428 };
2429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 // Geocoder
2431
Nick Pellye0fd6932012-07-11 10:26:13 -07002432 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002433 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002434 return mGeocodeProvider != null;
2435 }
2436
Nick Pellye0fd6932012-07-11 10:26:13 -07002437 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002439 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002440 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002441 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2442 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002444 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 }
2446
Mike Lockwooda55c3212009-04-15 11:10:11 -04002447
Nick Pellye0fd6932012-07-11 10:26:13 -07002448 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002450 double lowerLeftLatitude, double lowerLeftLongitude,
2451 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002452 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002453
2454 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002455 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2456 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2457 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002459 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 }
2461
2462 // Mock Providers
2463
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002464 private boolean canCallerAccessMockLocation(String opPackageName) {
2465 return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
2466 opPackageName) == AppOpsManager.MODE_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468
Nick Pellye0fd6932012-07-11 10:26:13 -07002469 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002470 public void addTestProvider(String name, ProviderProperties properties, String opPackageName) {
2471 if (!canCallerAccessMockLocation(opPackageName)) {
2472 return;
2473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474
Mike Lockwooda4903f22010-02-17 06:42:23 -05002475 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2476 throw new IllegalArgumentException("Cannot mock the passive location provider");
2477 }
2478
Mike Lockwood86328a92009-10-23 08:38:25 -04002479 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002480 synchronized (mLock) {
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002481 // remove the real provider if we are replacing GPS or network provider
2482 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002483 || LocationManager.NETWORK_PROVIDER.equals(name)
2484 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002485 LocationProviderInterface p = mProvidersByName.get(name);
2486 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002487 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002488 }
2489 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002490 addTestProviderLocked(name, properties);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 updateProvidersLocked();
2492 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002493 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 }
2495
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002496 private void addTestProviderLocked(String name, ProviderProperties properties) {
2497 if (mProvidersByName.get(name) != null) {
2498 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2499 }
2500 MockProvider provider = new MockProvider(name, this, properties);
2501 addProviderLocked(provider);
2502 mMockProviders.put(name, provider);
2503 mLastLocation.put(name, null);
2504 mLastLocationCoarseInterval.put(name, null);
2505 }
2506
Nick Pellye0fd6932012-07-11 10:26:13 -07002507 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002508 public void removeTestProvider(String provider, String opPackageName) {
2509 if (!canCallerAccessMockLocation(opPackageName)) {
2510 return;
2511 }
2512
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002513 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002514
2515 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002516 // we don't leave anything dangling.
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002517 clearTestProviderEnabled(provider, opPackageName);
2518 clearTestProviderLocation(provider, opPackageName);
2519 clearTestProviderStatus(provider, opPackageName);
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002520
You Kima6d0b6f2012-10-28 03:58:44 +09002521 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002522 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2524 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002525 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002526 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002527
2528 // reinstate real provider if available
2529 LocationProviderInterface realProvider = mRealProviders.get(provider);
2530 if (realProvider != null) {
2531 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002532 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002533 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002534 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002536 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
2538 }
2539
Nick Pellye0fd6932012-07-11 10:26:13 -07002540 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002541 public void setTestProviderLocation(String provider, Location loc, String opPackageName) {
2542 if (!canCallerAccessMockLocation(opPackageName)) {
2543 return;
2544 }
2545
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002546 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002547 MockProvider mockProvider = mMockProviders.get(provider);
2548 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2550 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002551 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2552 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002553 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002554 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 }
2556 }
2557
Nick Pellye0fd6932012-07-11 10:26:13 -07002558 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002559 public void clearTestProviderLocation(String provider, String opPackageName) {
2560 if (!canCallerAccessMockLocation(opPackageName)) {
2561 return;
2562 }
2563
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002564 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002565 MockProvider mockProvider = mMockProviders.get(provider);
2566 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2568 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002569 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 }
2571 }
2572
Nick Pellye0fd6932012-07-11 10:26:13 -07002573 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002574 public void setTestProviderEnabled(String provider, boolean enabled, String opPackageName) {
2575 if (!canCallerAccessMockLocation(opPackageName)) {
2576 return;
2577 }
2578
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002579 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002580 MockProvider mockProvider = mMockProviders.get(provider);
2581 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2583 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002584 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002586 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 mEnabledProviders.add(provider);
2588 mDisabledProviders.remove(provider);
2589 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002590 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 mEnabledProviders.remove(provider);
2592 mDisabledProviders.add(provider);
2593 }
2594 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002595 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
2597 }
2598
Nick Pellye0fd6932012-07-11 10:26:13 -07002599 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002600 public void clearTestProviderEnabled(String provider, String opPackageName) {
2601 if (!canCallerAccessMockLocation(opPackageName)) {
2602 return;
2603 }
2604
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002605 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002606 MockProvider mockProvider = mMockProviders.get(provider);
2607 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2609 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002610 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 mEnabledProviders.remove(provider);
2612 mDisabledProviders.remove(provider);
2613 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002614 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 }
2616 }
2617
Nick Pellye0fd6932012-07-11 10:26:13 -07002618 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002619 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime,
2620 String opPackageName) {
2621 if (!canCallerAccessMockLocation(opPackageName)) {
2622 return;
2623 }
2624
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002625 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002626 MockProvider mockProvider = mMockProviders.get(provider);
2627 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2629 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002630 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 }
2632 }
2633
Nick Pellye0fd6932012-07-11 10:26:13 -07002634 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002635 public void clearTestProviderStatus(String provider, String opPackageName) {
2636 if (!canCallerAccessMockLocation(opPackageName)) {
2637 return;
2638 }
2639
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002640 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002641 MockProvider mockProvider = mMockProviders.get(provider);
2642 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2644 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002645 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
2647 }
2648
2649 private void log(String log) {
2650 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002651 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 }
2653 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002654
2655 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2657 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2658 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002659 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 + Binder.getCallingPid()
2661 + ", uid=" + Binder.getCallingUid());
2662 return;
2663 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002664
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002665 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002668 for (Receiver receiver : mReceivers.values()) {
2669 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 }
David Christie2ff96af2014-01-30 16:09:37 -08002671 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002672 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2673 pw.println(" " + entry.getKey() + ":");
2674 for (UpdateRecord record : entry.getValue()) {
2675 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 }
2677 }
David Christie2ff96af2014-01-30 16:09:37 -08002678 pw.println(" Historical Records by Provider:");
2679 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
2680 : mRequestStatistics.statistics.entrySet()) {
2681 PackageProviderKey key = entry.getKey();
2682 PackageStatistics stats = entry.getValue();
2683 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002686 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2687 String provider = entry.getKey();
2688 Location location = entry.getValue();
2689 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002691
David Christie1b9b7b12013-04-15 15:31:11 -07002692 pw.println(" Last Known Locations Coarse Intervals:");
2693 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2694 String provider = entry.getKey();
2695 Location location = entry.getValue();
2696 pw.println(" " + provider + ": " + location);
2697 }
2698
Nick Pellye0fd6932012-07-11 10:26:13 -07002699 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 if (mEnabledProviders.size() > 0) {
2702 pw.println(" Enabled Providers:");
2703 for (String i : mEnabledProviders) {
2704 pw.println(" " + i);
2705 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
2708 if (mDisabledProviders.size() > 0) {
2709 pw.println(" Disabled Providers:");
2710 for (String i : mDisabledProviders) {
2711 pw.println(" " + i);
2712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002714 pw.append(" ");
2715 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 if (mMockProviders.size() > 0) {
2717 pw.println(" Mock Providers:");
2718 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002719 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
2721 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002722
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002723 pw.append(" fudger: ");
2724 mLocationFudger.dump(fd, pw, args);
2725
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002726 if (args.length > 0 && "short".equals(args[0])) {
2727 return;
2728 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002729 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002730 pw.print(provider.getName() + " Internal State");
2731 if (provider instanceof LocationProviderProxy) {
2732 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2733 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002734 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002735 pw.println(":");
2736 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 }
2739 }
2740}