blob: 087ddd6fd8820228c847e2acc967529428975f4f [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;
30import com.android.server.location.GpsLocationProvider;
31import com.android.server.location.GpsMeasurementsProvider;
destradaa4b3e3932014-07-21 18:01:47 -070032import com.android.server.location.GpsNavigationMessageProvider;
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;
Wei Liu5241a4c2015-05-11 14:00:36 -070064import android.location.IGpsGeofenceHardware;
destradaaea8a8a62014-06-23 18:19:03 -070065import android.location.IGpsMeasurementsListener;
destradaa4b3e3932014-07-21 18:01:47 -070066import android.location.IGpsNavigationMessageListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.location.IGpsStatusListener;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -040068import android.location.IGpsStatusProvider;
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;
160 private IGpsStatusProvider mGpsStatusProvider;
161 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;
destradaaea8a8a62014-06-23 18:19:03 -0700165 private GpsMeasurementsProvider mGpsMeasurementsProvider;
destradaa4b3e3932014-07-21 18:01:47 -0700166 private GpsNavigationMessageProvider mGpsNavigationMessageProvider;
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
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700217 public LocationManagerService(Context context) {
218 super();
219 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800220 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800221
Svet Ganovadc1cf42015-06-15 16:36:24 -0700222 // Let the package manager query which are the default location
223 // providers as they get certain permissions granted by default.
224 PackageManagerInternal packageManagerInternal = LocalServices.getService(
225 PackageManagerInternal.class);
226 packageManagerInternal.setLocationPackagesProvider(
227 new PackageManagerInternal.PackagesProvider() {
228 @Override
229 public String[] getPackages(int userId) {
230 return mContext.getResources().getStringArray(
231 com.android.internal.R.array.config_locationProviderPackageNames);
232 }
233 });
234
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700235 if (D) Log.d(TAG, "Constructed");
236
237 // most startup is deferred until systemReady()
238 }
239
Svetoslav Ganova0027152013-06-25 14:59:53 -0700240 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700241 synchronized (mLock) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800242 if (D) Log.d(TAG, "systemReady()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700243
Victoria Lease5cd731a2012-12-19 15:04:21 -0800244 // fetch package manager
245 mPackageManager = mContext.getPackageManager();
246
Victoria Lease0aa28602013-05-29 15:28:26 -0700247 // fetch power manager
248 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800249
250 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700251 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800252
253 // prepare mLocationHandler's dependents
254 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
255 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
256 mBlacklist.init();
257 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
258
Dianne Hackbornc2293022013-02-06 23:14:49 -0800259 // Monitor for app ops mode changes.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700260 AppOpsManager.OnOpChangedListener callback
261 = new AppOpsManager.OnOpChangedInternalListener() {
262 public void onOpChanged(int op, String packageName) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800263 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700264 for (Receiver receiver : mReceivers.values()) {
265 receiver.updateMonitoring(true);
266 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800267 applyAllProviderRequirementsLocked();
268 }
269 }
270 };
271 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
272
David Christieb870dbf2015-06-22 12:42:53 -0700273 PackageManager.OnPermissionsChangedListener permissionListener
274 = new PackageManager.OnPermissionsChangedListener() {
275 @Override
276 public void onPermissionsChanged(final int uid) {
277 synchronized (mLock) {
278 applyAllProviderRequirementsLocked();
279 }
280 }
281 };
282 mPackageManager.addOnPermissionsChangeListener(permissionListener);
283
Amith Yamasanib27528d2014-06-05 15:02:10 -0700284 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
285 updateUserProfiles(mCurrentUserId);
286
Victoria Lease5cd731a2012-12-19 15:04:21 -0800287 // prepare providers
288 loadProvidersLocked();
289 updateProvidersLocked();
290 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700291
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700292 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700293 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700294 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700295 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800296 @Override
297 public void onChange(boolean selfChange) {
298 synchronized (mLock) {
299 updateProvidersLocked();
300 }
301 }
302 }, UserHandle.USER_ALL);
303 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700304
Victoria Lease38389b62012-09-30 11:44:22 -0700305 // listen for user change
306 IntentFilter intentFilter = new IntentFilter();
307 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700308 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
309 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
destradaab9026982015-08-27 17:34:54 -0700310 intentFilter.addAction(Intent.ACTION_SHUTDOWN);
Victoria Lease38389b62012-09-30 11:44:22 -0700311
312 mContext.registerReceiverAsUser(new BroadcastReceiver() {
313 @Override
314 public void onReceive(Context context, Intent intent) {
315 String action = intent.getAction();
316 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
317 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
Amith Yamasanib27528d2014-06-05 15:02:10 -0700318 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
319 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
320 updateUserProfiles(mCurrentUserId);
destradaab9026982015-08-27 17:34:54 -0700321 } else if (Intent.ACTION_SHUTDOWN.equals(action)) {
322 shutdownComponents();
Victoria Lease38389b62012-09-30 11:44:22 -0700323 }
324 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800325 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700326 }
327
Amith Yamasanib27528d2014-06-05 15:02:10 -0700328 /**
destradaab9026982015-08-27 17:34:54 -0700329 * Provides a way for components held by the {@link LocationManagerService} to clean-up
330 * gracefully on system's shutdown.
331 *
332 * NOTES:
333 * 1) Only provides a chance to clean-up on an opt-in basis. This guarantees back-compat
334 * support for components that do not wish to handle such event.
335 */
336 private void shutdownComponents() {
337 if(D) Log.d(TAG, "Shutting down components...");
338
339 LocationProviderInterface gpsProvider = mProvidersByName.get(LocationManager.GPS_PROVIDER);
340 if (gpsProvider != null && gpsProvider.isEnabled()) {
341 gpsProvider.disable();
342 }
343
344 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
345 if (FlpHardwareProvider.isSupported() && flpHardwareProvider != null) {
346 flpHardwareProvider.cleanup();
347 }
348 }
349
350 /**
Amith Yamasanib27528d2014-06-05 15:02:10 -0700351 * Makes a list of userids that are related to the current user. This is
352 * relevant when using managed profiles. Otherwise the list only contains
353 * the current user.
354 *
355 * @param currentUserId the current user, who might have an alter-ego.
356 */
357 void updateUserProfiles(int currentUserId) {
358 List<UserInfo> profiles = mUserManager.getProfiles(currentUserId);
359 synchronized (mLock) {
360 mCurrentUserProfiles = new int[profiles.size()];
361 for (int i = 0; i < mCurrentUserProfiles.length; i++) {
362 mCurrentUserProfiles[i] = profiles.get(i).id;
363 }
364 }
365 }
366
367 /**
368 * Checks if the specified userId matches any of the current foreground
369 * users stored in mCurrentUserProfiles.
370 */
371 private boolean isCurrentProfile(int userId) {
372 synchronized (mLock) {
373 for (int i = 0; i < mCurrentUserProfiles.length; i++) {
374 if (mCurrentUserProfiles[i] == userId) {
375 return true;
376 }
377 }
378 return false;
379 }
380 }
381
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500382 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
383 PackageManager pm = mContext.getPackageManager();
384 String systemPackageName = mContext.getPackageName();
385 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
386
387 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
388 new Intent(FUSED_LOCATION_SERVICE_ACTION),
389 PackageManager.GET_META_DATA, mCurrentUserId);
390 for (ResolveInfo rInfo : rInfos) {
391 String packageName = rInfo.serviceInfo.packageName;
392
393 // Check that the signature is in the list of supported sigs. If it's not in
394 // this list the standard provider binding logic won't bind to it.
395 try {
396 PackageInfo pInfo;
397 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
398 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
399 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
400 ", but has wrong signature, ignoring");
401 continue;
402 }
403 } catch (NameNotFoundException e) {
404 Log.e(TAG, "missing package: " + packageName);
405 continue;
406 }
407
408 // Get the version info
409 if (rInfo.serviceInfo.metaData == null) {
410 Log.w(TAG, "Found fused provider without metadata: " + packageName);
411 continue;
412 }
413
414 int version = rInfo.serviceInfo.metaData.getInt(
415 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
416 if (version == 0) {
417 // This should be the fallback fused location provider.
418
419 // Make sure it's in the system partition.
420 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
421 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
422 continue;
423 }
424
425 // Check that the fallback is signed the same as the OS
426 // as a proxy for coreApp="true"
427 if (pm.checkSignatures(systemPackageName, packageName)
428 != PackageManager.SIGNATURE_MATCH) {
429 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
430 + packageName);
431 continue;
432 }
433
434 // Found a valid fallback.
435 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
436 return;
437 } else {
438 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
439 }
440 }
441
442 throw new IllegalStateException("Unable to find a fused location provider that is in the "
443 + "system partition with version 0 and signed with the platform certificate. "
444 + "Such a package is needed to provide a default fused location provider in the "
445 + "event that no other fused location provider has been installed or is currently "
446 + "available. For example, coreOnly boot mode when decrypting the data "
447 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
448 }
449
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700450 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700451 // create a passive location provider, which is always enabled
452 PassiveProvider passiveProvider = new PassiveProvider(this);
453 addProviderLocked(passiveProvider);
454 mEnabledProviders.add(passiveProvider.getName());
455 mPassiveProvider = passiveProvider;
456
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700457 if (GpsLocationProvider.isSupported()) {
Wei Liu5241a4c2015-05-11 14:00:36 -0700458 // Create a gps location provider
459 GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
460 mLocationHandler.getLooper());
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700461 mGpsStatusProvider = gpsProvider.getGpsStatusProvider();
462 mNetInitiatedListener = gpsProvider.getNetInitiatedListener();
463 addProviderLocked(gpsProvider);
464 mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
Wei Liu5241a4c2015-05-11 14:00:36 -0700465 mGpsMeasurementsProvider = gpsProvider.getGpsMeasurementsProvider();
466 mGpsNavigationMessageProvider = gpsProvider.getGpsNavigationMessageProvider();
467 mGpsGeofenceProxy = gpsProvider.getGpsGeofenceProxy();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700468 }
469
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700470 /*
471 Load package name(s) containing location provider support.
472 These packages can contain services implementing location providers:
473 Geocoder Provider, Network Location Provider, and
474 Fused Location Provider. They will each be searched for
475 service components implementing these providers.
476 The location framework also has support for installation
477 of new location providers at run-time. The new package does not
478 have to be explicitly listed here, however it must have a signature
479 that matches the signature of at least one package on this list.
480 */
481 Resources resources = mContext.getResources();
482 ArrayList<String> providerPackageNames = new ArrayList<String>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500483 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700484 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500485 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
486 Arrays.toString(pkgs));
487 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
488
489 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700490
491 // bind to network provider
492 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
493 mContext,
494 LocationManager.NETWORK_PROVIDER,
495 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700496 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
497 com.android.internal.R.string.config_networkLocationProviderPackageName,
498 com.android.internal.R.array.config_locationProviderPackageNames,
499 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700500 if (networkProvider != null) {
501 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
502 mProxyProviders.add(networkProvider);
503 addProviderLocked(networkProvider);
504 } else {
505 Slog.w(TAG, "no network location provider found");
506 }
507
508 // bind to fused provider
509 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
510 mContext,
511 LocationManager.FUSED_PROVIDER,
512 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700513 com.android.internal.R.bool.config_enableFusedLocationOverlay,
514 com.android.internal.R.string.config_fusedLocationProviderPackageName,
515 com.android.internal.R.array.config_locationProviderPackageNames,
516 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700517 if (fusedLocationProvider != null) {
518 addProviderLocked(fusedLocationProvider);
519 mProxyProviders.add(fusedLocationProvider);
520 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700521 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700522 } else {
523 Slog.e(TAG, "no fused location provider found",
524 new IllegalStateException("Location service needs a fused location provider"));
525 }
526
527 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700528 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
529 com.android.internal.R.bool.config_enableGeocoderOverlay,
530 com.android.internal.R.string.config_geocoderProviderPackageName,
531 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800532 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700533 if (mGeocodeProvider == null) {
534 Slog.e(TAG, "no geocoder provider found");
535 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700536
destradaaa4fa3b52014-07-09 10:46:39 -0700537 // bind to fused hardware provider if supported
destradaabeea4422014-07-30 18:17:21 -0700538 // in devices without support, requesting an instance of FlpHardwareProvider will raise an
539 // exception, so make sure we only do that when supported
540 FlpHardwareProvider flpHardwareProvider;
destradaa5ce66d82014-05-28 18:24:08 -0700541 if (FlpHardwareProvider.isSupported()) {
destradaabeea4422014-07-30 18:17:21 -0700542 flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
destradaaf9a274c2014-07-25 15:11:56 -0700543 FusedProxy fusedProxy = FusedProxy.createAndBind(
544 mContext,
545 mLocationHandler,
546 flpHardwareProvider.getLocationHardware(),
547 com.android.internal.R.bool.config_enableHardwareFlpOverlay,
548 com.android.internal.R.string.config_hardwareFlpPackageName,
549 com.android.internal.R.array.config_locationProviderPackageNames);
550 if (fusedProxy == null) {
551 Slog.e(TAG, "Unable to bind FusedProxy.");
552 }
destradaacfbdcd22014-04-30 11:29:11 -0700553 } else {
destradaabeea4422014-07-30 18:17:21 -0700554 flpHardwareProvider = null;
destradaaf9a274c2014-07-25 15:11:56 -0700555 Slog.e(TAG, "FLP HAL not supported");
556 }
557
558 // bind to geofence provider
559 GeofenceProxy provider = GeofenceProxy.createAndBind(
560 mContext,com.android.internal.R.bool.config_enableGeofenceOverlay,
561 com.android.internal.R.string.config_geofenceProviderPackageName,
562 com.android.internal.R.array.config_locationProviderPackageNames,
563 mLocationHandler,
Wei Liu5241a4c2015-05-11 14:00:36 -0700564 mGpsGeofenceProxy,
destradaabeea4422014-07-30 18:17:21 -0700565 flpHardwareProvider != null ? flpHardwareProvider.getGeofenceHardware() : null);
destradaaf9a274c2014-07-25 15:11:56 -0700566 if (provider == null) {
567 Slog.e(TAG, "Unable to bind FLP Geofence proxy.");
destradaa0682809a2013-08-12 18:50:30 -0700568 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900569
destradaa6e2fe752015-06-23 17:25:53 -0700570 // bind to hardware activity recognition
571 boolean activityRecognitionHardwareIsSupported = ActivityRecognitionHardware.isSupported();
572 ActivityRecognitionHardware activityRecognitionHardware = null;
573 if (activityRecognitionHardwareIsSupported) {
574 activityRecognitionHardware = ActivityRecognitionHardware.getInstance(mContext);
destradaaa4fa3b52014-07-09 10:46:39 -0700575 } else {
576 Slog.e(TAG, "Hardware Activity-Recognition not supported.");
577 }
destradaa6e2fe752015-06-23 17:25:53 -0700578 ActivityRecognitionProxy proxy = ActivityRecognitionProxy.createAndBind(
579 mContext,
580 mLocationHandler,
581 activityRecognitionHardwareIsSupported,
582 activityRecognitionHardware,
583 com.android.internal.R.bool.config_enableActivityRecognitionHardwareOverlay,
584 com.android.internal.R.string.config_activityRecognitionHardwarePackageName,
585 com.android.internal.R.array.config_locationProviderPackageNames);
586 if (proxy == null) {
587 Slog.e(TAG, "Unable to bind ActivityRecognitionProxy.");
588 }
destradaaa4fa3b52014-07-09 10:46:39 -0700589
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900590 String[] testProviderStrings = resources.getStringArray(
591 com.android.internal.R.array.config_testLocationProviders);
592 for (String testProviderString : testProviderStrings) {
593 String fragments[] = testProviderString.split(",");
594 String name = fragments[0].trim();
595 if (mProvidersByName.get(name) != null) {
596 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
597 }
598 ProviderProperties properties = new ProviderProperties(
599 Boolean.parseBoolean(fragments[1]) /* requiresNetwork */,
600 Boolean.parseBoolean(fragments[2]) /* requiresSatellite */,
601 Boolean.parseBoolean(fragments[3]) /* requiresCell */,
602 Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */,
603 Boolean.parseBoolean(fragments[5]) /* supportsAltitude */,
604 Boolean.parseBoolean(fragments[6]) /* supportsSpeed */,
605 Boolean.parseBoolean(fragments[7]) /* supportsBearing */,
606 Integer.parseInt(fragments[8]) /* powerRequirement */,
607 Integer.parseInt(fragments[9]) /* accuracy */);
608 addTestProviderLocked(name, properties);
609 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700610 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700613 * Called when the device's active user changes.
614 * @param userId the new active user's UserId
615 */
616 private void switchUser(int userId) {
Jianzheng Zhoud5c69462013-10-10 14:02:09 +0800617 if (mCurrentUserId == userId) {
618 return;
619 }
Victoria Lease83762d22012-10-03 13:51:17 -0700620 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800621 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700622 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700623 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700624 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700625 for (LocationProviderInterface p : mProviders) {
Amith Yamasanib27528d2014-06-05 15:02:10 -0700626 updateProviderListenersLocked(p.getName(), false);
Victoria Leaseb711d572012-10-02 13:14:11 -0700627 }
Victoria Lease38389b62012-09-30 11:44:22 -0700628 mCurrentUserId = userId;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700629 updateUserProfiles(userId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700630 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700631 }
632 }
633
634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
636 * location updates.
637 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700638 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700639 final int mUid; // uid of receiver
640 final int mPid; // pid of receiver
641 final String mPackageName; // package name of receiver
Victoria Lease37425c32012-10-16 16:08:48 -0700642 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 final ILocationListener mListener;
645 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700646 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700647 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700649
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400650 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<String,UpdateRecord>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700651
David Christie0b837452013-07-29 16:02:13 -0700652 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700653 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700654 // True if app ops has started monitoring this receiver for high power (gps) locations.
655 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700656 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700657 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700659 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700660 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700663 if (listener != null) {
664 mKey = listener.asBinder();
665 } else {
666 mKey = intent;
667 }
Victoria Lease37425c32012-10-16 16:08:48 -0700668 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700669 mUid = uid;
670 mPid = pid;
671 mPackageName = packageName;
David Christie82edc9b2013-07-19 11:31:42 -0700672 if (workSource != null && workSource.size() <= 0) {
673 workSource = null;
674 }
675 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700676 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700677
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700678 updateMonitoring(true);
679
Victoria Lease0aa28602013-05-29 15:28:26 -0700680 // construct/configure wakelock
681 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700682 if (workSource == null) {
683 workSource = new WorkSource(mUid, mPackageName);
684 }
685 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687
688 @Override
689 public boolean equals(Object otherObj) {
690 if (otherObj instanceof Receiver) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700691 return mKey.equals(((Receiver)otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 return false;
694 }
695
696 @Override
697 public int hashCode() {
698 return mKey.hashCode();
699 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 @Override
702 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700703 StringBuilder s = new StringBuilder();
704 s.append("Reciever[");
705 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700707 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700709 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700711 for (String p : mUpdateRecords.keySet()) {
712 s.append(" ").append(mUpdateRecords.get(p).toString());
713 }
714 s.append("]");
715 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
David Christie15b31912013-08-13 15:54:32 -0700718 /**
719 * Update AppOp monitoring for this receiver.
720 *
721 * @param allow If true receiver is currently active, if false it's been removed.
722 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700723 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700724 if (mHideFromAppOps) {
725 return;
726 }
727
David Christie15b31912013-08-13 15:54:32 -0700728 boolean requestingLocation = false;
729 boolean requestingHighPowerLocation = false;
730 if (allow) {
731 // See if receiver has any enabled update records. Also note if any update records
732 // are high power (has a high power provider with an interval under a threshold).
733 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
734 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
735 requestingLocation = true;
736 LocationProviderInterface locationProvider
David Christie2ff96af2014-01-30 16:09:37 -0800737 = mProvidersByName.get(updateRecord.mProvider);
David Christie15b31912013-08-13 15:54:32 -0700738 ProviderProperties properties = locationProvider != null
739 ? locationProvider.getProperties() : null;
740 if (properties != null
741 && properties.mPowerRequirement == Criteria.POWER_HIGH
742 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
743 requestingHighPowerLocation = true;
744 break;
745 }
746 }
747 }
748 }
749
David Christie0b837452013-07-29 16:02:13 -0700750 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700751 mOpMonitoring = updateMonitoring(
752 requestingLocation,
753 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700754 AppOpsManager.OP_MONITOR_LOCATION);
755
756 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700757 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700758 mOpHighPowerMonitoring = updateMonitoring(
759 requestingHighPowerLocation,
760 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700761 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700762 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700763 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700764 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
765 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
766 }
David Christie0b837452013-07-29 16:02:13 -0700767 }
768
769 /**
770 * Update AppOps monitoring for a single location request and op type.
771 *
772 * @param allowMonitoring True if monitoring is allowed for this request/op.
773 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
774 * @param op AppOps code for the op to update.
775 * @return True if monitoring is on for this request/op after updating.
776 */
777 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
778 int op) {
779 if (!currentlyMonitoring) {
780 if (allowMonitoring) {
781 return mAppOps.startOpNoThrow(op, mUid, mPackageName)
782 == AppOpsManager.MODE_ALLOWED;
783 }
784 } else {
785 if (!allowMonitoring || mAppOps.checkOpNoThrow(op, mUid, mPackageName)
786 != AppOpsManager.MODE_ALLOWED) {
787 mAppOps.finishOp(op, mUid, mPackageName);
788 return false;
789 }
790 }
791
792 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700793 }
794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 public boolean isListener() {
796 return mListener != null;
797 }
798
799 public boolean isPendingIntent() {
800 return mPendingIntent != null;
801 }
802
803 public ILocationListener getListener() {
804 if (mListener != null) {
805 return mListener;
806 }
807 throw new IllegalStateException("Request for non-existent listener");
808 }
809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
811 if (mListener != null) {
812 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700813 synchronized (this) {
814 // synchronize to ensure incrementPendingBroadcastsLocked()
815 // is called before decrementPendingBroadcasts()
816 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700817 // call this after broadcasting so we do not increment
818 // if we throw an exeption.
819 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 } catch (RemoteException e) {
822 return false;
823 }
824 } else {
825 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800826 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
828 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700829 synchronized (this) {
830 // synchronize to ensure incrementPendingBroadcastsLocked()
831 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700832 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700833 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700834 // call this after broadcasting so we do not increment
835 // if we throw an exeption.
836 incrementPendingBroadcastsLocked();
837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 } catch (PendingIntent.CanceledException e) {
839 return false;
840 }
841 }
842 return true;
843 }
844
845 public boolean callLocationChangedLocked(Location location) {
846 if (mListener != null) {
847 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700848 synchronized (this) {
849 // synchronize to ensure incrementPendingBroadcastsLocked()
850 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800851 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700852 // call this after broadcasting so we do not increment
853 // if we throw an exeption.
854 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 } catch (RemoteException e) {
857 return false;
858 }
859 } else {
860 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800861 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700863 synchronized (this) {
864 // synchronize to ensure incrementPendingBroadcastsLocked()
865 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700866 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700867 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700868 // call this after broadcasting so we do not increment
869 // if we throw an exeption.
870 incrementPendingBroadcastsLocked();
871 }
872 } catch (PendingIntent.CanceledException e) {
873 return false;
874 }
875 }
876 return true;
877 }
878
879 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -0700880 // First update AppOp monitoring.
881 // An app may get/lose location access as providers are enabled/disabled.
882 updateMonitoring(true);
883
Mike Lockwood48f17512009-04-23 09:12:08 -0700884 if (mListener != null) {
885 try {
886 synchronized (this) {
887 // synchronize to ensure incrementPendingBroadcastsLocked()
888 // is called before decrementPendingBroadcasts()
889 if (enabled) {
890 mListener.onProviderEnabled(provider);
891 } else {
892 mListener.onProviderDisabled(provider);
893 }
Nick Pellye0fd6932012-07-11 10:26:13 -0700894 // call this after broadcasting so we do not increment
895 // if we throw an exeption.
896 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700897 }
898 } catch (RemoteException e) {
899 return false;
900 }
901 } else {
902 Intent providerIntent = new Intent();
903 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
904 try {
905 synchronized (this) {
906 // synchronize to ensure incrementPendingBroadcastsLocked()
907 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700908 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700909 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700910 // call this after broadcasting so we do not increment
911 // if we throw an exeption.
912 incrementPendingBroadcastsLocked();
913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 } catch (PendingIntent.CanceledException e) {
915 return false;
916 }
917 }
918 return true;
919 }
920
Nick Pellyf1be6862012-05-15 10:53:42 -0700921 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700923 if (D) Log.d(TAG, "Location listener died");
924
Mike Lockwood2f82c4e2009-04-17 08:24:10 -0400925 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 removeUpdatesLocked(this);
927 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700928 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700929 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700930 }
931 }
932
Nick Pellye0fd6932012-07-11 10:26:13 -0700933 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700934 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
935 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400936 synchronized (this) {
937 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700938 }
939 }
940
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400941 // this must be called while synchronized by caller in a synchronized block
942 // containing the sending of the broadcaset
943 private void incrementPendingBroadcastsLocked() {
944 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700945 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -0400946 }
947 }
948
949 private void decrementPendingBroadcastsLocked() {
950 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -0700951 if (mWakeLock.isHeld()) {
952 mWakeLock.release();
953 }
954 }
955 }
956
957 public void clearPendingBroadcastsLocked() {
958 if (mPendingBroadcasts > 0) {
959 mPendingBroadcasts = 0;
960 if (mWakeLock.isHeld()) {
961 mWakeLock.release();
962 }
Mike Lockwood48f17512009-04-23 09:12:08 -0700963 }
964 }
965 }
966
Nick Pellye0fd6932012-07-11 10:26:13 -0700967 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -0700968 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700969 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -0400970 //the receiver list if it is not found. If it is not found then the
971 //LocationListener was removed when it had a pending broadcast and should
972 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700973 synchronized (mLock) {
974 IBinder binder = listener.asBinder();
975 Receiver receiver = mReceivers.get(binder);
976 if (receiver != null) {
977 synchronized (receiver) {
978 // so wakelock calls will succeed
979 long identity = Binder.clearCallingIdentity();
980 receiver.decrementPendingBroadcastsLocked();
981 Binder.restoreCallingIdentity(identity);
David Christie2ff96af2014-01-30 16:09:37 -0800982 }
Dianne Hackbornf5fdca92013-06-05 14:53:33 -0700983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 }
986
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700987 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400988 mProviders.add(provider);
989 mProvidersByName.put(provider.getName(), provider);
990 }
991
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700992 private void removeProviderLocked(LocationProviderInterface provider) {
993 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400994 mProviders.remove(provider);
995 mProvidersByName.remove(provider.getName());
996 }
997
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800998 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -0800999 * Returns "true" if access to the specified location provider is allowed by the current
1000 * user's settings. Access to all location providers is forbidden to non-location-provider
1001 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001002 *
1003 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001004 * @return
1005 */
Victoria Lease09eeaec2013-02-05 11:34:13 -08001006 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (mEnabledProviders.contains(provider)) {
1008 return true;
1009 }
1010 if (mDisabledProviders.contains(provider)) {
1011 return false;
1012 }
1013 // Use system settings
1014 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015
Victoria Leaseb711d572012-10-02 13:14:11 -07001016 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
1018
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001019 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001020 * Returns "true" if access to the specified location provider is allowed by the specified
1021 * user's settings. Access to all location providers is forbidden to non-location-provider
1022 * processes belonging to background users.
1023 *
1024 * @param provider the name of the location provider
1025 * @param uid the requestor's UID
1026 * @return
1027 */
1028 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001029 if (!isCurrentProfile(UserHandle.getUserId(uid)) && !isUidALocationProvider(uid)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001030 return false;
1031 }
1032 return isAllowedByCurrentUserSettingsLocked(provider);
1033 }
1034
1035 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001036 * Returns the permission string associated with the specified resolution level.
1037 *
1038 * @param resolutionLevel the resolution level
1039 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001040 */
Victoria Lease37425c32012-10-16 16:08:48 -07001041 private String getResolutionPermission(int resolutionLevel) {
1042 switch (resolutionLevel) {
1043 case RESOLUTION_LEVEL_FINE:
1044 return android.Manifest.permission.ACCESS_FINE_LOCATION;
1045 case RESOLUTION_LEVEL_COARSE:
1046 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
1047 default:
1048 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
Victoria Leaseda479c52012-10-15 15:24:16 -07001050 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001051
Victoria Leaseda479c52012-10-15 15:24:16 -07001052 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001053 * Returns the resolution level allowed to the given PID/UID pair.
1054 *
1055 * @param pid the PID
1056 * @param uid the UID
1057 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -07001058 */
Victoria Lease37425c32012-10-16 16:08:48 -07001059 private int getAllowedResolutionLevel(int pid, int uid) {
1060 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
1061 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1062 return RESOLUTION_LEVEL_FINE;
1063 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
1064 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1065 return RESOLUTION_LEVEL_COARSE;
1066 } else {
1067 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -07001068 }
Victoria Lease4fab68b2012-09-13 13:20:59 -07001069 }
1070
1071 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001072 * Returns the resolution level allowed to the caller
1073 *
1074 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -07001075 */
Victoria Lease37425c32012-10-16 16:08:48 -07001076 private int getCallerAllowedResolutionLevel() {
1077 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
1078 }
1079
1080 /**
1081 * Throw SecurityException if specified resolution level is insufficient to use geofences.
1082 *
1083 * @param allowedResolutionLevel resolution level allowed to caller
1084 */
1085 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
1086 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -07001087 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
1088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
Victoria Lease37425c32012-10-16 16:08:48 -07001091 /**
1092 * Return the minimum resolution level required to use the specified location provider.
1093 *
1094 * @param provider the name of the location provider
1095 * @return minimum resolution level required for provider
1096 */
1097 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001098 if (LocationManager.GPS_PROVIDER.equals(provider) ||
1099 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
1100 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001101 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -07001102 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
1103 LocationManager.FUSED_PROVIDER.equals(provider)) {
1104 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001105 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001106 } else {
1107 // mock providers
1108 LocationProviderInterface lp = mMockProviders.get(provider);
1109 if (lp != null) {
1110 ProviderProperties properties = lp.getProperties();
1111 if (properties != null) {
1112 if (properties.mRequiresSatellite) {
1113 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001114 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -07001115 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
1116 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001117 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001118 }
1119 }
1120 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001121 }
Victoria Lease37425c32012-10-16 16:08:48 -07001122 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -07001123 }
1124
Victoria Lease37425c32012-10-16 16:08:48 -07001125 /**
1126 * Throw SecurityException if specified resolution level is insufficient to use the named
1127 * location provider.
1128 *
1129 * @param allowedResolutionLevel resolution level allowed to caller
1130 * @param providerName the name of the location provider
1131 */
1132 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
1133 String providerName) {
1134 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
1135 if (allowedResolutionLevel < requiredResolutionLevel) {
1136 switch (requiredResolutionLevel) {
1137 case RESOLUTION_LEVEL_FINE:
1138 throw new SecurityException("\"" + providerName + "\" location provider " +
1139 "requires ACCESS_FINE_LOCATION permission.");
1140 case RESOLUTION_LEVEL_COARSE:
1141 throw new SecurityException("\"" + providerName + "\" location provider " +
1142 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
1143 default:
1144 throw new SecurityException("Insufficient permission for \"" + providerName +
1145 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -07001146 }
1147 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001148 }
1149
David Christie82edc9b2013-07-19 11:31:42 -07001150 /**
1151 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
1152 * for battery).
1153 */
David Christie40e57822013-07-30 11:36:48 -07001154 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -07001155 mContext.enforceCallingOrSelfPermission(
1156 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
1157 }
1158
David Christie40e57822013-07-30 11:36:48 -07001159 private void checkUpdateAppOpsAllowed() {
1160 mContext.enforceCallingOrSelfPermission(
1161 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
1162 }
1163
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001164 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001165 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1166 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001167 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001168 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001169 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001170 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001171 }
1172 return -1;
1173 }
1174
David Christieb870dbf2015-06-22 12:42:53 -07001175 boolean reportLocationAccessNoThrow(
1176 int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001177 int op = resolutionLevelToOp(allowedResolutionLevel);
1178 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001179 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1180 return false;
1181 }
1182 }
David Christieb870dbf2015-06-22 12:42:53 -07001183
1184 if (getAllowedResolutionLevel(pid, uid) < allowedResolutionLevel) {
1185 return false;
1186 }
1187
Dianne Hackborn35654b62013-01-14 17:38:02 -08001188 return true;
1189 }
1190
David Christieb870dbf2015-06-22 12:42:53 -07001191 boolean checkLocationAccess(int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001192 int op = resolutionLevelToOp(allowedResolutionLevel);
1193 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001194 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1195 return false;
1196 }
1197 }
David Christieb870dbf2015-06-22 12:42:53 -07001198
1199 if (getAllowedResolutionLevel(pid, uid) < allowedResolutionLevel) {
1200 return false;
1201 }
1202
Dianne Hackborn35654b62013-01-14 17:38:02 -08001203 return true;
1204 }
1205
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001206 /**
1207 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001208 * fused, also including ones that are not permitted to
1209 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001210 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001211 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001213 ArrayList<String> out;
1214 synchronized (mLock) {
1215 out = new ArrayList<String>(mProviders.size());
1216 for (LocationProviderInterface provider : mProviders) {
1217 String name = provider.getName();
1218 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001219 continue;
1220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 out.add(name);
1222 }
1223 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001224
1225 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 return out;
1227 }
1228
Mike Lockwood03ca2162010-04-01 08:10:09 -07001229 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001230 * Return all providers by name, that match criteria and are optionally
1231 * enabled.
1232 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001233 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001234 @Override
1235 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001236 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001237 ArrayList<String> out;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001238 int uid = Binder.getCallingUid();;
Victoria Lease269518e2012-10-29 08:25:39 -07001239 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001240 try {
1241 synchronized (mLock) {
1242 out = new ArrayList<String>(mProviders.size());
1243 for (LocationProviderInterface provider : mProviders) {
1244 String name = provider.getName();
1245 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001246 continue;
1247 }
Victoria Lease37425c32012-10-16 16:08:48 -07001248 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001249 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001250 continue;
1251 }
1252 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1253 name, provider.getProperties(), criteria)) {
1254 continue;
1255 }
1256 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001257 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001258 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001259 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001260 } finally {
1261 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001262 }
1263
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001264 if (D) Log.d(TAG, "getProviders()=" + out);
1265 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001266 }
1267
1268 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001269 * Return the name of the best provider given a Criteria object.
1270 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001271 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001272 * has been deprecated as well. So this method now uses
1273 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001274 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001275 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001276 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001277 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001278
1279 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001280 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001281 result = pickBest(providers);
1282 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1283 return result;
1284 }
1285 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001286 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001287 result = pickBest(providers);
1288 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1289 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001290 }
1291
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001292 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001293 return null;
1294 }
1295
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001296 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001297 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001298 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001299 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1300 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001301 } else {
1302 return providers.get(0);
1303 }
1304 }
1305
Nick Pellye0fd6932012-07-11 10:26:13 -07001306 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001307 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1308 LocationProviderInterface p = mProvidersByName.get(provider);
1309 if (p == null) {
1310 throw new IllegalArgumentException("provider=" + provider);
1311 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001312
1313 boolean result = LocationProvider.propertiesMeetCriteria(
1314 p.getName(), p.getProperties(), criteria);
1315 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1316 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001317 }
1318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001320 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001321 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001322 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 boolean isEnabled = p.isEnabled();
1324 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001325 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 if (isEnabled && !shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001327 updateProviderListenersLocked(name, false);
David Christieb084fef2013-12-18 14:33:57 -08001328 // If any provider has been disabled, clear all last locations for all providers.
1329 // This is to be on the safe side in case a provider has location derived from
1330 // this disabled provider.
1331 mLastLocation.clear();
1332 mLastLocationCoarseInterval.clear();
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001333 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 } else if (!isEnabled && shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001335 updateProviderListenersLocked(name, true);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001336 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001338 }
1339 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001340 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1341 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001342 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1343 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 }
1345 }
1346
Amith Yamasanib27528d2014-06-05 15:02:10 -07001347 private void updateProviderListenersLocked(String provider, boolean enabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 int listeners = 0;
1349
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001350 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001351 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
1353 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1356 if (records != null) {
1357 final int N = records.size();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001358 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 UpdateRecord record = records.get(i);
Amith Yamasanib27528d2014-06-05 15:02:10 -07001360 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001361 // Sends a notification message to the receiver
1362 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1363 if (deadReceivers == null) {
1364 deadReceivers = new ArrayList<Receiver>();
1365 }
1366 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001368 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 }
1372
1373 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001374 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 removeUpdatesLocked(deadReceivers.get(i));
1376 }
1377 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 if (enabled) {
1380 p.enable();
1381 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001382 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
1384 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001389 private void applyRequirementsLocked(String provider) {
1390 LocationProviderInterface p = mProvidersByName.get(provider);
1391 if (p == null) return;
1392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001394 WorkSource worksource = new WorkSource();
1395 ProviderRequest providerRequest = new ProviderRequest();
1396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001398 for (UpdateRecord record : records) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001399 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
David Christieb870dbf2015-06-22 12:42:53 -07001400 if (checkLocationAccess(
1401 record.mReceiver.mPid,
1402 record.mReceiver.mUid,
1403 record.mReceiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001404 record.mReceiver.mAllowedResolutionLevel)) {
1405 LocationRequest locationRequest = record.mRequest;
1406 providerRequest.locationRequests.add(locationRequest);
1407 if (locationRequest.getInterval() < providerRequest.interval) {
1408 providerRequest.reportLocation = true;
1409 providerRequest.interval = locationRequest.getInterval();
1410 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001411 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001412 }
1413 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001414
1415 if (providerRequest.reportLocation) {
1416 // calculate who to blame for power
1417 // This is somewhat arbitrary. We pick a threshold interval
1418 // that is slightly higher that the minimum interval, and
1419 // spread the blame across all applications with a request
1420 // under that threshold.
1421 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1422 for (UpdateRecord record : records) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001423 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001424 LocationRequest locationRequest = record.mRequest;
1425 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001426 if (record.mReceiver.mWorkSource != null
1427 && record.mReceiver.mWorkSource.size() > 0
1428 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001429 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001430 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001431 worksource.add(record.mReceiver.mWorkSource);
1432 } else {
1433 // Assign blame to caller.
1434 worksource.add(
1435 record.mReceiver.mUid,
1436 record.mReceiver.mPackageName);
1437 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001438 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001439 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001443
1444 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1445 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 }
1447
1448 private class UpdateRecord {
1449 final String mProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001450 final LocationRequest mRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 final Receiver mReceiver;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001452 Location mLastFixBroadcast;
1453 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454
1455 /**
1456 * Note: must be constructed with lock held.
1457 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001458 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 mProvider = provider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001460 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 mReceiver = receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462
1463 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1464 if (records == null) {
1465 records = new ArrayList<UpdateRecord>();
1466 mRecordsByProvider.put(provider, records);
1467 }
1468 if (!records.contains(this)) {
1469 records.add(this);
1470 }
David Christie2ff96af2014-01-30 16:09:37 -08001471
1472 // Update statistics for historical location requests by package/provider
1473 mRequestStatistics.startRequesting(
1474 mReceiver.mPackageName, provider, request.getInterval());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 }
1476
1477 /**
David Christie2ff96af2014-01-30 16:09:37 -08001478 * Method to be called when a record will no longer be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001480 void disposeLocked(boolean removeReceiver) {
David Christie2ff96af2014-01-30 16:09:37 -08001481 mRequestStatistics.stopRequesting(mReceiver.mPackageName, mProvider);
1482
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001483 // remove from mRecordsByProvider
1484 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1485 if (globalRecords != null) {
1486 globalRecords.remove(this);
1487 }
1488
1489 if (!removeReceiver) return; // the caller will handle the rest
1490
1491 // remove from Receiver#mUpdateRecords
1492 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1493 if (receiverRecords != null) {
1494 receiverRecords.remove(this.mProvider);
1495
1496 // and also remove the Receiver if it has no more update records
1497 if (removeReceiver && receiverRecords.size() == 0) {
1498 removeUpdatesLocked(mReceiver);
1499 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
1502
1503 @Override
1504 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001505 StringBuilder s = new StringBuilder();
1506 s.append("UpdateRecord[");
1507 s.append(mProvider);
1508 s.append(' ').append(mReceiver.mPackageName).append('(');
1509 s.append(mReceiver.mUid).append(')');
1510 s.append(' ').append(mRequest);
1511 s.append(']');
1512 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001516 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001517 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001518 IBinder binder = listener.asBinder();
1519 Receiver receiver = mReceivers.get(binder);
1520 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001521 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1522 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001523 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001524 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001525 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001526 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001527 return null;
1528 }
Wen Jingcb3ab222014-03-27 13:42:59 +08001529 mReceivers.put(binder, receiver);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001530 }
1531 return receiver;
1532 }
1533
David Christie82edc9b2013-07-19 11:31:42 -07001534 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001535 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001536 Receiver receiver = mReceivers.get(intent);
1537 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001538 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1539 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001540 mReceivers.put(intent, receiver);
1541 }
1542 return receiver;
1543 }
1544
Victoria Lease37425c32012-10-16 16:08:48 -07001545 /**
1546 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1547 * and consistency requirements.
1548 *
1549 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001550 * @return a version of request that meets the given resolution and consistency requirements
1551 * @hide
1552 */
1553 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1554 LocationRequest sanitizedRequest = new LocationRequest(request);
1555 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1556 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001557 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001558 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001559 break;
1560 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001561 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001562 break;
1563 }
1564 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001565 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1566 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001567 }
Victoria Lease37425c32012-10-16 16:08:48 -07001568 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1569 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001570 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001571 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001572 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001573 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001574 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001575 }
Victoria Lease37425c32012-10-16 16:08:48 -07001576 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001577 }
1578
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001579 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001580 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001581 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001582 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001583 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001584 String[] packages = mPackageManager.getPackagesForUid(uid);
1585 if (packages == null) {
1586 throw new SecurityException("invalid UID " + uid);
1587 }
1588 for (String pkg : packages) {
1589 if (packageName.equals(pkg)) return;
1590 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001591 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001592 }
1593
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001594 private void checkPendingIntent(PendingIntent intent) {
1595 if (intent == null) {
1596 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001597 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001598 }
1599
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001600 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001601 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001602 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001603 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001604 } else if (intent != null && listener != null) {
1605 throw new IllegalArgumentException("cannot register both listener and intent");
1606 } else if (intent != null) {
1607 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001608 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001609 } else {
David Christie40e57822013-07-30 11:36:48 -07001610 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001611 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001612 }
1613
Nick Pellye0fd6932012-07-11 10:26:13 -07001614 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001615 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
1616 PendingIntent intent, String packageName) {
1617 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1618 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001619 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1620 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1621 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07001622 WorkSource workSource = request.getWorkSource();
1623 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07001624 checkDeviceStatsAllowed();
1625 }
1626 boolean hideFromAppOps = request.getHideFromAppOps();
1627 if (hideFromAppOps) {
1628 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07001629 }
Victoria Lease37425c32012-10-16 16:08:48 -07001630 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001632 final int pid = Binder.getCallingPid();
1633 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001634 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 long identity = Binder.clearCallingIdentity();
1636 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001637 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
1638 // a location.
David Christieb870dbf2015-06-22 12:42:53 -07001639 checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001640
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001641 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001642 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07001643 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07001644 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04001645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 } finally {
1647 Binder.restoreCallingIdentity(identity);
1648 }
1649 }
1650
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001651 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
1652 int pid, int uid, String packageName) {
1653 // Figure out the provider. Either its explicitly request (legacy use cases), or
1654 // use the fused provider
1655 if (request == null) request = DEFAULT_LOCATION_REQUEST;
1656 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07001657 if (name == null) {
1658 throw new IllegalArgumentException("provider name must not be null");
1659 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07001660
1661 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
1662 + " " + name + " " + request + " from " + packageName + "(" + uid + ")");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001663 LocationProviderInterface provider = mProvidersByName.get(name);
1664 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07001665 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001666 }
1667
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001668 UpdateRecord record = new UpdateRecord(name, request, receiver);
1669 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
1670 if (oldRecord != null) {
1671 oldRecord.disposeLocked(false);
1672 }
1673
Victoria Lease09eeaec2013-02-05 11:34:13 -08001674 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001675 if (isProviderEnabled) {
1676 applyRequirementsLocked(name);
1677 } else {
1678 // Notify the listener that updates are currently disabled
1679 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
David Christie0b837452013-07-29 16:02:13 -07001681 // Update the monitoring here just in case multiple location requests were added to the
1682 // same receiver (this request may be high power and the initial might not have been).
1683 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685
Nick Pellye0fd6932012-07-11 10:26:13 -07001686 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001687 public void removeUpdates(ILocationListener listener, PendingIntent intent,
1688 String packageName) {
1689 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001690
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001691 final int pid = Binder.getCallingPid();
1692 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001693
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001694 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07001695 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07001696 boolean hideFromAppOps = false;
1697 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
1698 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001699
1700 // providers may use public location API's, need to clear identity
1701 long identity = Binder.clearCallingIdentity();
1702 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001703 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001704 } finally {
1705 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
1708 }
1709
1710 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08001711 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001712
1713 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
1714 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
1715 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001716 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001720 receiver.updateMonitoring(false);
1721
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001722 // Record which providers were associated with this listener
1723 HashSet<String> providers = new HashSet<String>();
1724 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
1725 if (oldRecords != null) {
1726 // Call dispose() on the obsolete update records.
1727 for (UpdateRecord record : oldRecords.values()) {
David Christie2ff96af2014-01-30 16:09:37 -08001728 // Update statistics for historical location requests by package/provider
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001729 record.disposeLocked(false);
1730 }
1731 // Accumulate providers
1732 providers.addAll(oldRecords.keySet());
1733 }
1734
1735 // update provider
1736 for (String provider : providers) {
1737 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08001738 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001739 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
1741
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001742 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 }
1745
Dianne Hackbornc2293022013-02-06 23:14:49 -08001746 private void applyAllProviderRequirementsLocked() {
1747 for (LocationProviderInterface p : mProviders) {
1748 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08001749 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001750 continue;
1751 }
1752
1753 applyRequirementsLocked(p.getName());
1754 }
1755 }
1756
Nick Pellye0fd6932012-07-11 10:26:13 -07001757 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07001758 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001759 if (D) Log.d(TAG, "getLastLocation: " + request);
1760 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001761 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07001762 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001763 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1764 request.getProvider());
1765 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07001766
David Christieb870dbf2015-06-22 12:42:53 -07001767 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001768 final int uid = Binder.getCallingUid();
1769 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001770 try {
1771 if (mBlacklist.isBlacklisted(packageName)) {
1772 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
1773 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07001774 return null;
1775 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001776
David Christieb870dbf2015-06-22 12:42:53 -07001777 if (!reportLocationAccessNoThrow(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001778 if (D) Log.d(TAG, "not returning last loc for no op app: " +
1779 packageName);
1780 return null;
1781 }
1782
Victoria Leaseb711d572012-10-02 13:14:11 -07001783 synchronized (mLock) {
1784 // Figure out the provider. Either its explicitly request (deprecated API's),
1785 // or use the fused provider
1786 String name = request.getProvider();
1787 if (name == null) name = LocationManager.FUSED_PROVIDER;
1788 LocationProviderInterface provider = mProvidersByName.get(name);
1789 if (provider == null) return null;
1790
Victoria Lease09eeaec2013-02-05 11:34:13 -08001791 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07001792
David Christie1b9b7b12013-04-15 15:31:11 -07001793 Location location;
1794 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
1795 // Make sure that an app with coarse permissions can't get frequent location
1796 // updates by calling LocationManager.getLastKnownLocation repeatedly.
1797 location = mLastLocationCoarseInterval.get(name);
1798 } else {
1799 location = mLastLocation.get(name);
1800 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001801 if (location == null) {
1802 return null;
1803 }
Victoria Lease37425c32012-10-16 16:08:48 -07001804 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001805 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
1806 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001807 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07001808 }
Victoria Lease37425c32012-10-16 16:08:48 -07001809 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001810 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07001811 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001812 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001813 return null;
1814 } finally {
1815 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001816 }
1817 }
1818
1819 @Override
1820 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
1821 String packageName) {
1822 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07001823 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1824 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001825 checkPendingIntent(intent);
1826 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07001827 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
1828 request.getProvider());
1829 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001830
Victoria Lease37425c32012-10-16 16:08:48 -07001831 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001832
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001833 // geo-fence manager uses the public location API, need to clear identity
1834 int uid = Binder.getCallingUid();
Xiaohui Chena4490622015-09-22 15:29:31 -07001835 // TODO: http://b/23822629
1836 if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) {
Victoria Lease56e675b2012-11-05 19:25:06 -08001837 // temporary measure until geofences work for secondary users
1838 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1839 return;
1840 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001841 long identity = Binder.clearCallingIdentity();
1842 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001843 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1844 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001845 } finally {
1846 Binder.restoreCallingIdentity(identity);
1847 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001848 }
1849
1850 @Override
1851 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001852 checkPendingIntent(intent);
1853 checkPackageName(packageName);
1854
1855 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1856
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001857 // geo-fence manager uses the public location API, need to clear identity
1858 long identity = Binder.clearCallingIdentity();
1859 try {
1860 mGeofenceManager.removeFence(geofence, intent);
1861 } finally {
1862 Binder.restoreCallingIdentity(identity);
1863 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001864 }
1865
1866
1867 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001868 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001869 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1870 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001871 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872
David Christieb870dbf2015-06-22 12:42:53 -07001873 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001874 final int uid = Binder.getCallingUid();
1875 final long ident = Binder.clearCallingIdentity();
1876 try {
David Christieb870dbf2015-06-22 12:42:53 -07001877 if (!checkLocationAccess(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001878 return false;
1879 }
1880 } finally {
1881 Binder.restoreCallingIdentity(ident);
1882 }
1883
Takayuki Hoshib254ab6a2014-10-23 16:46:02 +09001884 if (mGpsStatusProvider == null) {
1885 return false;
1886 }
1887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001889 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001891 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 return false;
1893 }
1894 return true;
1895 }
1896
Nick Pellye0fd6932012-07-11 10:26:13 -07001897 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001899 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001900 try {
1901 mGpsStatusProvider.removeGpsStatusListener(listener);
1902 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001903 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
1906 }
1907
Nick Pellye0fd6932012-07-11 10:26:13 -07001908 @Override
destradaaea8a8a62014-06-23 18:19:03 -07001909 public boolean addGpsMeasurementsListener(
1910 IGpsMeasurementsListener listener,
1911 String packageName) {
1912 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1913 checkResolutionLevelIsSufficientForProviderUse(
1914 allowedResolutionLevel,
1915 LocationManager.GPS_PROVIDER);
1916
David Christieb870dbf2015-06-22 12:42:53 -07001917 int pid = Binder.getCallingPid();
destradaaea8a8a62014-06-23 18:19:03 -07001918 int uid = Binder.getCallingUid();
1919 long identity = Binder.clearCallingIdentity();
1920 boolean hasLocationAccess;
1921 try {
David Christieb870dbf2015-06-22 12:42:53 -07001922 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaaea8a8a62014-06-23 18:19:03 -07001923 } finally {
1924 Binder.restoreCallingIdentity(identity);
1925 }
1926
Wei Liu5241a4c2015-05-11 14:00:36 -07001927 if (!hasLocationAccess || mGpsMeasurementsProvider == null) {
destradaaea8a8a62014-06-23 18:19:03 -07001928 return false;
1929 }
destradaaea8a8a62014-06-23 18:19:03 -07001930 return mGpsMeasurementsProvider.addListener(listener);
1931 }
1932
1933 @Override
destradaa6568d702014-10-27 12:47:41 -07001934 public void removeGpsMeasurementsListener(IGpsMeasurementsListener listener) {
Wei Liu5241a4c2015-05-11 14:00:36 -07001935 if (mGpsMeasurementsProvider != null) {
1936 mGpsMeasurementsProvider.removeListener(listener);
1937 }
destradaaea8a8a62014-06-23 18:19:03 -07001938 }
1939
1940 @Override
destradaa4b3e3932014-07-21 18:01:47 -07001941 public boolean addGpsNavigationMessageListener(
1942 IGpsNavigationMessageListener listener,
1943 String packageName) {
1944 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1945 checkResolutionLevelIsSufficientForProviderUse(
1946 allowedResolutionLevel,
1947 LocationManager.GPS_PROVIDER);
1948
David Christieb870dbf2015-06-22 12:42:53 -07001949 int pid = Binder.getCallingPid();
destradaa4b3e3932014-07-21 18:01:47 -07001950 int uid = Binder.getCallingUid();
1951 long identity = Binder.clearCallingIdentity();
1952 boolean hasLocationAccess;
1953 try {
David Christieb870dbf2015-06-22 12:42:53 -07001954 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaa4b3e3932014-07-21 18:01:47 -07001955 } finally {
1956 Binder.restoreCallingIdentity(identity);
1957 }
1958
Wei Liu5241a4c2015-05-11 14:00:36 -07001959 if (!hasLocationAccess || mGpsNavigationMessageProvider == null) {
destradaa4b3e3932014-07-21 18:01:47 -07001960 return false;
1961 }
1962 return mGpsNavigationMessageProvider.addListener(listener);
1963 }
1964
1965 @Override
destradaa6568d702014-10-27 12:47:41 -07001966 public void removeGpsNavigationMessageListener(IGpsNavigationMessageListener listener) {
Wei Liu5241a4c2015-05-11 14:00:36 -07001967 if (mGpsNavigationMessageProvider != null) {
1968 mGpsNavigationMessageProvider.removeListener(listener);
1969 }
destradaa4b3e3932014-07-21 18:01:47 -07001970 }
1971
1972 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001974 if (provider == null) {
1975 // throw NullPointerException to remain compatible with previous implementation
1976 throw new NullPointerException();
1977 }
Victoria Lease37425c32012-10-16 16:08:48 -07001978 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1979 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001982 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 != PackageManager.PERMISSION_GRANTED)) {
1984 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1985 }
1986
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001987 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001988 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001989 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001990
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001991 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 }
1994
Nick Pellye0fd6932012-07-11 10:26:13 -07001995 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001996 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001997 if (Binder.getCallingUid() != Process.myUid()) {
1998 throw new SecurityException(
1999 "calling sendNiResponse from outside of the system is not allowed");
2000 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04002001 try {
2002 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002003 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002004 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04002005 return false;
2006 }
2007 }
2008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002010 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11002011 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 * accessed by the caller
2013 */
Nick Pellye0fd6932012-07-11 10:26:13 -07002014 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002015 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07002016 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08002017 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07002018 }
2019
Victoria Lease37425c32012-10-16 16:08:48 -07002020 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
2021 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002023 LocationProviderInterface p;
2024 synchronized (mLock) {
2025 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002028 if (p == null) return null;
2029 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
2031
Jason Monkb71218a2015-06-17 14:44:39 -04002032 /**
2033 * @return null if the provider does not exist
2034 * @throws SecurityException if the provider is not allowed to be
2035 * accessed by the caller
2036 */
2037 @Override
2038 public String getNetworkProviderPackage() {
2039 LocationProviderInterface p;
2040 synchronized (mLock) {
2041 if (mProvidersByName.get(LocationManager.NETWORK_PROVIDER) == null) {
2042 return null;
2043 }
2044 p = mProvidersByName.get(LocationManager.NETWORK_PROVIDER);
2045 }
2046
2047 if (p instanceof LocationProviderProxy) {
2048 return ((LocationProviderProxy) p).getConnectedPackageName();
2049 }
2050 return null;
2051 }
2052
Nick Pellye0fd6932012-07-11 10:26:13 -07002053 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07002055 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
2056 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002057 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
2058
Victoria Lease09eeaec2013-02-05 11:34:13 -08002059 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07002060 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07002061 try {
2062 synchronized (mLock) {
2063 LocationProviderInterface p = mProvidersByName.get(provider);
2064 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002065
Victoria Lease09eeaec2013-02-05 11:34:13 -08002066 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07002067 }
2068 } finally {
2069 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002070 }
2071 }
2072
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002073 /**
2074 * Returns "true" if the UID belongs to a bound location provider.
2075 *
2076 * @param uid the uid
2077 * @return true if uid belongs to a bound location provider
2078 */
2079 private boolean isUidALocationProvider(int uid) {
2080 if (uid == Process.SYSTEM_UID) {
2081 return true;
2082 }
2083 if (mGeocodeProvider != null) {
David Christie1f141c12014-05-14 15:11:15 -07002084 if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002085 }
2086 for (LocationProviderProxy proxy : mProxyProviders) {
David Christie1f141c12014-05-14 15:11:15 -07002087 if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002088 }
2089 return false;
2090 }
2091
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002092 private void checkCallerIsProvider() {
2093 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
2094 == PackageManager.PERMISSION_GRANTED) {
2095 return;
2096 }
2097
2098 // Previously we only used the INSTALL_LOCATION_PROVIDER
2099 // check. But that is system or signature
2100 // protection level which is not flexible enough for
2101 // providers installed oustide the system image. So
2102 // also allow providers with a UID matching the
2103 // currently bound package name
2104
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002105 if (isUidALocationProvider(Binder.getCallingUid())) {
2106 return;
2107 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002108
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002109 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
2110 "or UID of a currently bound location provider");
2111 }
2112
David Christie1f141c12014-05-14 15:11:15 -07002113 /**
2114 * Returns true if the given package belongs to the given uid.
2115 */
2116 private boolean doesUidHavePackage(int uid, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002117 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 return false;
2119 }
David Christie1f141c12014-05-14 15:11:15 -07002120 String[] packageNames = mPackageManager.getPackagesForUid(uid);
2121 if (packageNames == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002122 return false;
2123 }
David Christie1f141c12014-05-14 15:11:15 -07002124 for (String name : packageNames) {
2125 if (packageName.equals(name)) {
2126 return true;
2127 }
2128 }
2129 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
2131
Nick Pellye0fd6932012-07-11 10:26:13 -07002132 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05002133 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002134 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04002135
Nick Pelly2eeeec22012-07-18 13:13:37 -07002136 if (!location.isComplete()) {
2137 Log.w(TAG, "Dropping incomplete location: " + location);
2138 return;
2139 }
2140
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002141 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
2142 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05002143 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002144 mLocationHandler.sendMessageAtFrontOfQueue(m);
2145 }
2146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147
Laurent Tu75defb62012-11-01 16:21:52 -07002148 private static boolean shouldBroadcastSafe(
2149 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 // Always broadcast the first update
2151 if (lastLoc == null) {
2152 return true;
2153 }
2154
Nick Pellyf1be6862012-05-15 10:53:42 -07002155 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002156 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07002157 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
2158 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002159 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 return false;
2161 }
2162
2163 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002164 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 if (minDistance > 0.0) {
2166 if (loc.distanceTo(lastLoc) <= minDistance) {
2167 return false;
2168 }
2169 }
2170
Laurent Tu75defb62012-11-01 16:21:52 -07002171 // Check whether sufficient number of udpates is left
2172 if (record.mRequest.getNumUpdates() <= 0) {
2173 return false;
2174 }
2175
2176 // Check whether the expiry date has passed
2177 if (record.mRequest.getExpireAt() < now) {
2178 return false;
2179 }
2180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 return true;
2182 }
2183
Mike Lockwooda4903f22010-02-17 06:42:23 -05002184 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07002185 if (D) Log.d(TAG, "incoming location: " + location);
2186
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002187 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05002188 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189
Laurent Tu60ec50a2012-10-04 17:00:10 -07002190 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002191 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002192 if (p == null) return;
2193
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002194 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07002195 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2196 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002197 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002198 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002199 lastLocation = new Location(provider);
2200 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07002201 } else {
2202 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2203 if (noGPSLocation == null && lastNoGPSLocation != null) {
2204 // New location has no no-GPS location: adopt last no-GPS location. This is set
2205 // directly into location because we do not want to notify COARSE clients.
2206 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
2207 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07002208 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002209 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210
David Christie1b9b7b12013-04-15 15:31:11 -07002211 // Update last known coarse interval location if enough time has passed.
2212 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
2213 if (lastLocationCoarseInterval == null) {
2214 lastLocationCoarseInterval = new Location(location);
2215 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
2216 }
2217 long timeDiffNanos = location.getElapsedRealtimeNanos()
2218 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
2219 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
2220 lastLocationCoarseInterval.set(location);
2221 }
2222 // Don't ever return a coarse location that is more recent than the allowed update
2223 // interval (i.e. don't allow an app to keep registering and unregistering for
2224 // location updates to overcome the minimum interval).
2225 noGPSLocation =
2226 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2227
Laurent Tu60ec50a2012-10-04 17:00:10 -07002228 // Skip if there are no UpdateRecords for this provider.
2229 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
2230 if (records == null || records.size() == 0) return;
2231
Victoria Lease09016ab2012-09-16 12:33:15 -07002232 // Fetch coarse location
2233 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07002234 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002235 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
2236 }
2237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 // Fetch latest status update time
2239 long newStatusUpdateTime = p.getStatusUpdateTime();
2240
David Christie2ff96af2014-01-30 16:09:37 -08002241 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 Bundle extras = new Bundle();
2243 int status = p.getStatus(extras);
2244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002246 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07002247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002249 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07002251 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07002252
Victoria Lease269518e2012-10-29 08:25:39 -07002253 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Amith Yamasanib27528d2014-06-05 15:02:10 -07002254 if (!isCurrentProfile(receiverUserId) && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002255 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07002256 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07002257 " (current user: " + mCurrentUserId + ", app: " +
2258 receiver.mPackageName + ")");
2259 }
2260 continue;
2261 }
2262
Nick Pelly4035f5a2012-08-17 14:43:49 -07002263 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
2264 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
2265 receiver.mPackageName);
2266 continue;
2267 }
2268
David Christieb870dbf2015-06-22 12:42:53 -07002269 if (!reportLocationAccessNoThrow(receiver.mPid, receiver.mUid, receiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002270 receiver.mAllowedResolutionLevel)) {
2271 if (D) Log.d(TAG, "skipping loc update for no op app: " +
2272 receiver.mPackageName);
2273 continue;
2274 }
2275
Victoria Lease09016ab2012-09-16 12:33:15 -07002276 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07002277 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2278 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002279 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002280 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002281 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002282 if (notifyLocation != null) {
2283 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002284 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002285 if (lastLoc == null) {
2286 lastLoc = new Location(notifyLocation);
2287 r.mLastFixBroadcast = lastLoc;
2288 } else {
2289 lastLoc.set(notifyLocation);
2290 }
2291 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2292 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2293 receiverDead = true;
2294 }
Laurent Tu75defb62012-11-01 16:21:52 -07002295 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 }
2297 }
2298
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002299 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002301 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002303 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002305 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002306 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002307 }
2308 }
2309
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002310 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002311 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002312 if (deadUpdateRecords == null) {
2313 deadUpdateRecords = new ArrayList<UpdateRecord>();
2314 }
2315 deadUpdateRecords.add(r);
2316 }
2317 // track dead receivers
2318 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002319 if (deadReceivers == null) {
2320 deadReceivers = new ArrayList<Receiver>();
2321 }
2322 if (!deadReceivers.contains(receiver)) {
2323 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
2325 }
2326 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002327
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002328 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002330 for (Receiver receiver : deadReceivers) {
2331 removeUpdatesLocked(receiver);
2332 }
2333 }
2334 if (deadUpdateRecords != null) {
2335 for (UpdateRecord r : deadUpdateRecords) {
2336 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002338 applyRequirementsLocked(provider);
2339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
2341
2342 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002343 public LocationWorkerHandler(Looper looper) {
2344 super(looper, null, true);
2345 }
2346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 @Override
2348 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002349 switch (msg.what) {
2350 case MSG_LOCATION_CHANGED:
2351 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2352 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 }
2355 }
2356
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002357 private boolean isMockProvider(String provider) {
2358 synchronized (mLock) {
2359 return mMockProviders.containsKey(provider);
2360 }
2361 }
2362
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002363 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002364 // create a working copy of the incoming Location so that the service can modify it without
2365 // disturbing the caller's copy
2366 Location myLocation = new Location(location);
2367 String provider = myLocation.getProvider();
2368
2369 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2370 // bit if location did not come from a mock provider because passive/fused providers can
2371 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2372 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2373 myLocation.setIsFromMockProvider(true);
2374 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002375
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002376 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002377 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2378 if (!passive) {
2379 // notify passive provider of the new location
2380 mPassiveProvider.updateLocation(myLocation);
2381 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002382 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386
Mike Lockwoode97ae402010-09-29 15:23:46 -04002387 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2388 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002389 public void onPackageDisappeared(String packageName, int reason) {
2390 // remove all receivers associated with this package name
2391 synchronized (mLock) {
2392 ArrayList<Receiver> deadReceivers = null;
2393
2394 for (Receiver receiver : mReceivers.values()) {
2395 if (receiver.mPackageName.equals(packageName)) {
2396 if (deadReceivers == null) {
2397 deadReceivers = new ArrayList<Receiver>();
2398 }
2399 deadReceivers.add(receiver);
2400 }
2401 }
2402
2403 // perform removal outside of mReceivers loop
2404 if (deadReceivers != null) {
2405 for (Receiver receiver : deadReceivers) {
2406 removeUpdatesLocked(receiver);
2407 }
2408 }
2409 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002410 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002411 };
2412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 // Geocoder
2414
Nick Pellye0fd6932012-07-11 10:26:13 -07002415 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002416 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002417 return mGeocodeProvider != null;
2418 }
2419
Nick Pellye0fd6932012-07-11 10:26:13 -07002420 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002422 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002423 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002424 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2425 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002427 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 }
2429
Mike Lockwooda55c3212009-04-15 11:10:11 -04002430
Nick Pellye0fd6932012-07-11 10:26:13 -07002431 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002433 double lowerLeftLatitude, double lowerLeftLongitude,
2434 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002435 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002436
2437 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002438 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2439 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2440 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002442 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
2444
2445 // Mock Providers
2446
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002447 private boolean canCallerAccessMockLocation(String opPackageName) {
2448 return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
2449 opPackageName) == AppOpsManager.MODE_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451
Nick Pellye0fd6932012-07-11 10:26:13 -07002452 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002453 public void addTestProvider(String name, ProviderProperties properties, String opPackageName) {
2454 if (!canCallerAccessMockLocation(opPackageName)) {
2455 return;
2456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457
Mike Lockwooda4903f22010-02-17 06:42:23 -05002458 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2459 throw new IllegalArgumentException("Cannot mock the passive location provider");
2460 }
2461
Mike Lockwood86328a92009-10-23 08:38:25 -04002462 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002463 synchronized (mLock) {
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002464 // remove the real provider if we are replacing GPS or network provider
2465 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002466 || LocationManager.NETWORK_PROVIDER.equals(name)
2467 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002468 LocationProviderInterface p = mProvidersByName.get(name);
2469 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002470 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002471 }
2472 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002473 addTestProviderLocked(name, properties);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 updateProvidersLocked();
2475 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002476 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 }
2478
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002479 private void addTestProviderLocked(String name, ProviderProperties properties) {
2480 if (mProvidersByName.get(name) != null) {
2481 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2482 }
2483 MockProvider provider = new MockProvider(name, this, properties);
2484 addProviderLocked(provider);
2485 mMockProviders.put(name, provider);
2486 mLastLocation.put(name, null);
2487 mLastLocationCoarseInterval.put(name, null);
2488 }
2489
Nick Pellye0fd6932012-07-11 10:26:13 -07002490 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002491 public void removeTestProvider(String provider, String opPackageName) {
2492 if (!canCallerAccessMockLocation(opPackageName)) {
2493 return;
2494 }
2495
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002496 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002497
2498 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002499 // we don't leave anything dangling.
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002500 clearTestProviderEnabled(provider, opPackageName);
2501 clearTestProviderLocation(provider, opPackageName);
2502 clearTestProviderStatus(provider, opPackageName);
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002503
You Kima6d0b6f2012-10-28 03:58:44 +09002504 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002505 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2507 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002508 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002509 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002510
2511 // reinstate real provider if available
2512 LocationProviderInterface realProvider = mRealProviders.get(provider);
2513 if (realProvider != null) {
2514 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002515 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002516 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002517 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002519 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
2521 }
2522
Nick Pellye0fd6932012-07-11 10:26:13 -07002523 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002524 public void setTestProviderLocation(String provider, Location loc, String opPackageName) {
2525 if (!canCallerAccessMockLocation(opPackageName)) {
2526 return;
2527 }
2528
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002529 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002530 MockProvider mockProvider = mMockProviders.get(provider);
2531 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2533 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002534 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2535 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002536 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002537 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
2539 }
2540
Nick Pellye0fd6932012-07-11 10:26:13 -07002541 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002542 public void clearTestProviderLocation(String provider, String opPackageName) {
2543 if (!canCallerAccessMockLocation(opPackageName)) {
2544 return;
2545 }
2546
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002547 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002548 MockProvider mockProvider = mMockProviders.get(provider);
2549 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2551 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002552 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
2554 }
2555
Nick Pellye0fd6932012-07-11 10:26:13 -07002556 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002557 public void setTestProviderEnabled(String provider, boolean enabled, String opPackageName) {
2558 if (!canCallerAccessMockLocation(opPackageName)) {
2559 return;
2560 }
2561
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002562 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002563 MockProvider mockProvider = mMockProviders.get(provider);
2564 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2566 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002567 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002569 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 mEnabledProviders.add(provider);
2571 mDisabledProviders.remove(provider);
2572 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002573 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 mEnabledProviders.remove(provider);
2575 mDisabledProviders.add(provider);
2576 }
2577 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002578 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 }
2580 }
2581
Nick Pellye0fd6932012-07-11 10:26:13 -07002582 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002583 public void clearTestProviderEnabled(String provider, String opPackageName) {
2584 if (!canCallerAccessMockLocation(opPackageName)) {
2585 return;
2586 }
2587
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002588 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002589 MockProvider mockProvider = mMockProviders.get(provider);
2590 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2592 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002593 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 mEnabledProviders.remove(provider);
2595 mDisabledProviders.remove(provider);
2596 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002597 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
2599 }
2600
Nick Pellye0fd6932012-07-11 10:26:13 -07002601 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002602 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime,
2603 String opPackageName) {
2604 if (!canCallerAccessMockLocation(opPackageName)) {
2605 return;
2606 }
2607
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002608 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002609 MockProvider mockProvider = mMockProviders.get(provider);
2610 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2612 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002613 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
2615 }
2616
Nick Pellye0fd6932012-07-11 10:26:13 -07002617 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002618 public void clearTestProviderStatus(String provider, String opPackageName) {
2619 if (!canCallerAccessMockLocation(opPackageName)) {
2620 return;
2621 }
2622
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002623 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002624 MockProvider mockProvider = mMockProviders.get(provider);
2625 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2627 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002628 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 }
2630 }
2631
2632 private void log(String log) {
2633 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002634 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 }
2636 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002637
2638 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2640 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2641 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002642 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 + Binder.getCallingPid()
2644 + ", uid=" + Binder.getCallingUid());
2645 return;
2646 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002647
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002648 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002651 for (Receiver receiver : mReceivers.values()) {
2652 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 }
David Christie2ff96af2014-01-30 16:09:37 -08002654 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002655 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2656 pw.println(" " + entry.getKey() + ":");
2657 for (UpdateRecord record : entry.getValue()) {
2658 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 }
2660 }
David Christie2ff96af2014-01-30 16:09:37 -08002661 pw.println(" Historical Records by Provider:");
2662 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
2663 : mRequestStatistics.statistics.entrySet()) {
2664 PackageProviderKey key = entry.getKey();
2665 PackageStatistics stats = entry.getValue();
2666 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
2667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002669 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2670 String provider = entry.getKey();
2671 Location location = entry.getValue();
2672 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002674
David Christie1b9b7b12013-04-15 15:31:11 -07002675 pw.println(" Last Known Locations Coarse Intervals:");
2676 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2677 String provider = entry.getKey();
2678 Location location = entry.getValue();
2679 pw.println(" " + provider + ": " + location);
2680 }
2681
Nick Pellye0fd6932012-07-11 10:26:13 -07002682 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 if (mEnabledProviders.size() > 0) {
2685 pw.println(" Enabled Providers:");
2686 for (String i : mEnabledProviders) {
2687 pw.println(" " + i);
2688 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691 if (mDisabledProviders.size() > 0) {
2692 pw.println(" Disabled Providers:");
2693 for (String i : mDisabledProviders) {
2694 pw.println(" " + i);
2695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002697 pw.append(" ");
2698 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 if (mMockProviders.size() > 0) {
2700 pw.println(" Mock Providers:");
2701 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002702 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 }
2704 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002705
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002706 pw.append(" fudger: ");
2707 mLocationFudger.dump(fd, pw, args);
2708
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002709 if (args.length > 0 && "short".equals(args[0])) {
2710 return;
2711 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002712 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002713 pw.print(provider.getName() + " Internal State");
2714 if (provider instanceof LocationProviderProxy) {
2715 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2716 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002717 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002718 pw.println(":");
2719 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 }
2722 }
2723}