blob: 885c765f3f4b21a0cf7750b4afabdca60542df85 [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
214 private int mCurrentUserId = UserHandle.USER_OWNER;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700215 private int[] mCurrentUserProfiles = new int[] { UserHandle.USER_OWNER };
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();
Victoria Lease56e675b2012-11-05 19:25:06 -08001835 if (UserHandle.getUserId(uid) != UserHandle.USER_OWNER) {
1836 // temporary measure until geofences work for secondary users
1837 Log.w(TAG, "proximity alerts are currently available only to the primary user");
1838 return;
1839 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001840 long identity = Binder.clearCallingIdentity();
1841 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001842 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
1843 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001844 } finally {
1845 Binder.restoreCallingIdentity(identity);
1846 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001847 }
1848
1849 @Override
1850 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001851 checkPendingIntent(intent);
1852 checkPackageName(packageName);
1853
1854 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
1855
Nick Pelly2b7a0d02012-08-17 15:09:44 -07001856 // geo-fence manager uses the public location API, need to clear identity
1857 long identity = Binder.clearCallingIdentity();
1858 try {
1859 mGeofenceManager.removeFence(geofence, intent);
1860 } finally {
1861 Binder.restoreCallingIdentity(identity);
1862 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001863 }
1864
1865
1866 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001867 public boolean addGpsStatusListener(IGpsStatusListener listener, String packageName) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001868 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1869 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
Victoria Lease37425c32012-10-16 16:08:48 -07001870 LocationManager.GPS_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871
David Christieb870dbf2015-06-22 12:42:53 -07001872 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001873 final int uid = Binder.getCallingUid();
1874 final long ident = Binder.clearCallingIdentity();
1875 try {
David Christieb870dbf2015-06-22 12:42:53 -07001876 if (!checkLocationAccess(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001877 return false;
1878 }
1879 } finally {
1880 Binder.restoreCallingIdentity(ident);
1881 }
1882
Takayuki Hoshib254ab6a2014-10-23 16:46:02 +09001883 if (mGpsStatusProvider == null) {
1884 return false;
1885 }
1886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 try {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001888 mGpsStatusProvider.addGpsStatusListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001890 Slog.e(TAG, "mGpsStatusProvider.addGpsStatusListener failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 return false;
1892 }
1893 return true;
1894 }
1895
Nick Pellye0fd6932012-07-11 10:26:13 -07001896 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 public void removeGpsStatusListener(IGpsStatusListener listener) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001898 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001899 try {
1900 mGpsStatusProvider.removeGpsStatusListener(listener);
1901 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001902 Slog.e(TAG, "mGpsStatusProvider.removeGpsStatusListener failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905 }
1906
Nick Pellye0fd6932012-07-11 10:26:13 -07001907 @Override
destradaaea8a8a62014-06-23 18:19:03 -07001908 public boolean addGpsMeasurementsListener(
1909 IGpsMeasurementsListener listener,
1910 String packageName) {
1911 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1912 checkResolutionLevelIsSufficientForProviderUse(
1913 allowedResolutionLevel,
1914 LocationManager.GPS_PROVIDER);
1915
David Christieb870dbf2015-06-22 12:42:53 -07001916 int pid = Binder.getCallingPid();
destradaaea8a8a62014-06-23 18:19:03 -07001917 int uid = Binder.getCallingUid();
1918 long identity = Binder.clearCallingIdentity();
1919 boolean hasLocationAccess;
1920 try {
David Christieb870dbf2015-06-22 12:42:53 -07001921 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaaea8a8a62014-06-23 18:19:03 -07001922 } finally {
1923 Binder.restoreCallingIdentity(identity);
1924 }
1925
Wei Liu5241a4c2015-05-11 14:00:36 -07001926 if (!hasLocationAccess || mGpsMeasurementsProvider == null) {
destradaaea8a8a62014-06-23 18:19:03 -07001927 return false;
1928 }
destradaaea8a8a62014-06-23 18:19:03 -07001929 return mGpsMeasurementsProvider.addListener(listener);
1930 }
1931
1932 @Override
destradaa6568d702014-10-27 12:47:41 -07001933 public void removeGpsMeasurementsListener(IGpsMeasurementsListener listener) {
Wei Liu5241a4c2015-05-11 14:00:36 -07001934 if (mGpsMeasurementsProvider != null) {
1935 mGpsMeasurementsProvider.removeListener(listener);
1936 }
destradaaea8a8a62014-06-23 18:19:03 -07001937 }
1938
1939 @Override
destradaa4b3e3932014-07-21 18:01:47 -07001940 public boolean addGpsNavigationMessageListener(
1941 IGpsNavigationMessageListener listener,
1942 String packageName) {
1943 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1944 checkResolutionLevelIsSufficientForProviderUse(
1945 allowedResolutionLevel,
1946 LocationManager.GPS_PROVIDER);
1947
David Christieb870dbf2015-06-22 12:42:53 -07001948 int pid = Binder.getCallingPid();
destradaa4b3e3932014-07-21 18:01:47 -07001949 int uid = Binder.getCallingUid();
1950 long identity = Binder.clearCallingIdentity();
1951 boolean hasLocationAccess;
1952 try {
David Christieb870dbf2015-06-22 12:42:53 -07001953 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
destradaa4b3e3932014-07-21 18:01:47 -07001954 } finally {
1955 Binder.restoreCallingIdentity(identity);
1956 }
1957
Wei Liu5241a4c2015-05-11 14:00:36 -07001958 if (!hasLocationAccess || mGpsNavigationMessageProvider == null) {
destradaa4b3e3932014-07-21 18:01:47 -07001959 return false;
1960 }
1961 return mGpsNavigationMessageProvider.addListener(listener);
1962 }
1963
1964 @Override
destradaa6568d702014-10-27 12:47:41 -07001965 public void removeGpsNavigationMessageListener(IGpsNavigationMessageListener listener) {
Wei Liu5241a4c2015-05-11 14:00:36 -07001966 if (mGpsNavigationMessageProvider != null) {
1967 mGpsNavigationMessageProvider.removeListener(listener);
1968 }
destradaa4b3e3932014-07-21 18:01:47 -07001969 }
1970
1971 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001973 if (provider == null) {
1974 // throw NullPointerException to remain compatible with previous implementation
1975 throw new NullPointerException();
1976 }
Victoria Lease37425c32012-10-16 16:08:48 -07001977 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
1978 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04001981 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 != PackageManager.PERMISSION_GRANTED)) {
1983 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
1984 }
1985
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001986 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001987 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001988 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07001989
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001990 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
1992 }
1993
Nick Pellye0fd6932012-07-11 10:26:13 -07001994 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001995 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07001996 if (Binder.getCallingUid() != Process.myUid()) {
1997 throw new SecurityException(
1998 "calling sendNiResponse from outside of the system is not allowed");
1999 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04002000 try {
2001 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002002 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002003 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04002004 return false;
2005 }
2006 }
2007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002009 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11002010 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 * accessed by the caller
2012 */
Nick Pellye0fd6932012-07-11 10:26:13 -07002013 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002014 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07002015 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08002016 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07002017 }
2018
Victoria Lease37425c32012-10-16 16:08:48 -07002019 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
2020 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002022 LocationProviderInterface p;
2023 synchronized (mLock) {
2024 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 }
2026
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002027 if (p == null) return null;
2028 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
2030
Jason Monkb71218a2015-06-17 14:44:39 -04002031 /**
2032 * @return null if the provider does not exist
2033 * @throws SecurityException if the provider is not allowed to be
2034 * accessed by the caller
2035 */
2036 @Override
2037 public String getNetworkProviderPackage() {
2038 LocationProviderInterface p;
2039 synchronized (mLock) {
2040 if (mProvidersByName.get(LocationManager.NETWORK_PROVIDER) == null) {
2041 return null;
2042 }
2043 p = mProvidersByName.get(LocationManager.NETWORK_PROVIDER);
2044 }
2045
2046 if (p instanceof LocationProviderProxy) {
2047 return ((LocationProviderProxy) p).getConnectedPackageName();
2048 }
2049 return null;
2050 }
2051
Nick Pellye0fd6932012-07-11 10:26:13 -07002052 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07002054 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
2055 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002056 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
2057
Victoria Lease09eeaec2013-02-05 11:34:13 -08002058 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07002059 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07002060 try {
2061 synchronized (mLock) {
2062 LocationProviderInterface p = mProvidersByName.get(provider);
2063 if (p == null) return false;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002064
Victoria Lease09eeaec2013-02-05 11:34:13 -08002065 return isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07002066 }
2067 } finally {
2068 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002069 }
2070 }
2071
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002072 /**
2073 * Returns "true" if the UID belongs to a bound location provider.
2074 *
2075 * @param uid the uid
2076 * @return true if uid belongs to a bound location provider
2077 */
2078 private boolean isUidALocationProvider(int uid) {
2079 if (uid == Process.SYSTEM_UID) {
2080 return true;
2081 }
2082 if (mGeocodeProvider != null) {
David Christie1f141c12014-05-14 15:11:15 -07002083 if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002084 }
2085 for (LocationProviderProxy proxy : mProxyProviders) {
David Christie1f141c12014-05-14 15:11:15 -07002086 if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002087 }
2088 return false;
2089 }
2090
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002091 private void checkCallerIsProvider() {
2092 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
2093 == PackageManager.PERMISSION_GRANTED) {
2094 return;
2095 }
2096
2097 // Previously we only used the INSTALL_LOCATION_PROVIDER
2098 // check. But that is system or signature
2099 // protection level which is not flexible enough for
2100 // providers installed oustide the system image. So
2101 // also allow providers with a UID matching the
2102 // currently bound package name
2103
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002104 if (isUidALocationProvider(Binder.getCallingUid())) {
2105 return;
2106 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002107
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002108 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
2109 "or UID of a currently bound location provider");
2110 }
2111
David Christie1f141c12014-05-14 15:11:15 -07002112 /**
2113 * Returns true if the given package belongs to the given uid.
2114 */
2115 private boolean doesUidHavePackage(int uid, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002116 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 return false;
2118 }
David Christie1f141c12014-05-14 15:11:15 -07002119 String[] packageNames = mPackageManager.getPackagesForUid(uid);
2120 if (packageNames == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002121 return false;
2122 }
David Christie1f141c12014-05-14 15:11:15 -07002123 for (String name : packageNames) {
2124 if (packageName.equals(name)) {
2125 return true;
2126 }
2127 }
2128 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
2130
Nick Pellye0fd6932012-07-11 10:26:13 -07002131 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05002132 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002133 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04002134
Nick Pelly2eeeec22012-07-18 13:13:37 -07002135 if (!location.isComplete()) {
2136 Log.w(TAG, "Dropping incomplete location: " + location);
2137 return;
2138 }
2139
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002140 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
2141 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05002142 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002143 mLocationHandler.sendMessageAtFrontOfQueue(m);
2144 }
2145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146
Laurent Tu75defb62012-11-01 16:21:52 -07002147 private static boolean shouldBroadcastSafe(
2148 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 // Always broadcast the first update
2150 if (lastLoc == null) {
2151 return true;
2152 }
2153
Nick Pellyf1be6862012-05-15 10:53:42 -07002154 // Check whether sufficient time has passed
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002155 long minTime = record.mRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07002156 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
2157 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002158 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 return false;
2160 }
2161
2162 // Check whether sufficient distance has been traveled
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002163 double minDistance = record.mRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 if (minDistance > 0.0) {
2165 if (loc.distanceTo(lastLoc) <= minDistance) {
2166 return false;
2167 }
2168 }
2169
Laurent Tu75defb62012-11-01 16:21:52 -07002170 // Check whether sufficient number of udpates is left
2171 if (record.mRequest.getNumUpdates() <= 0) {
2172 return false;
2173 }
2174
2175 // Check whether the expiry date has passed
2176 if (record.mRequest.getExpireAt() < now) {
2177 return false;
2178 }
2179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 return true;
2181 }
2182
Mike Lockwooda4903f22010-02-17 06:42:23 -05002183 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07002184 if (D) Log.d(TAG, "incoming location: " + location);
2185
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002186 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05002187 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188
Laurent Tu60ec50a2012-10-04 17:00:10 -07002189 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002190 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002191 if (p == null) return;
2192
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002193 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07002194 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2195 Location lastNoGPSLocation = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002196 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002197 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002198 lastLocation = new Location(provider);
2199 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07002200 } else {
2201 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2202 if (noGPSLocation == null && lastNoGPSLocation != null) {
2203 // New location has no no-GPS location: adopt last no-GPS location. This is set
2204 // directly into location because we do not want to notify COARSE clients.
2205 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
2206 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07002207 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002208 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209
David Christie1b9b7b12013-04-15 15:31:11 -07002210 // Update last known coarse interval location if enough time has passed.
2211 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
2212 if (lastLocationCoarseInterval == null) {
2213 lastLocationCoarseInterval = new Location(location);
2214 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
2215 }
2216 long timeDiffNanos = location.getElapsedRealtimeNanos()
2217 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
2218 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
2219 lastLocationCoarseInterval.set(location);
2220 }
2221 // Don't ever return a coarse location that is more recent than the allowed update
2222 // interval (i.e. don't allow an app to keep registering and unregistering for
2223 // location updates to overcome the minimum interval).
2224 noGPSLocation =
2225 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2226
Laurent Tu60ec50a2012-10-04 17:00:10 -07002227 // Skip if there are no UpdateRecords for this provider.
2228 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
2229 if (records == null || records.size() == 0) return;
2230
Victoria Lease09016ab2012-09-16 12:33:15 -07002231 // Fetch coarse location
2232 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07002233 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002234 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
2235 }
2236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 // Fetch latest status update time
2238 long newStatusUpdateTime = p.getStatusUpdateTime();
2239
David Christie2ff96af2014-01-30 16:09:37 -08002240 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 Bundle extras = new Bundle();
2242 int status = p.getStatus(extras);
2243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002245 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07002246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002248 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07002250 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07002251
Victoria Lease269518e2012-10-29 08:25:39 -07002252 int receiverUserId = UserHandle.getUserId(receiver.mUid);
Amith Yamasanib27528d2014-06-05 15:02:10 -07002253 if (!isCurrentProfile(receiverUserId) && !isUidALocationProvider(receiver.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002254 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07002255 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07002256 " (current user: " + mCurrentUserId + ", app: " +
2257 receiver.mPackageName + ")");
2258 }
2259 continue;
2260 }
2261
Nick Pelly4035f5a2012-08-17 14:43:49 -07002262 if (mBlacklist.isBlacklisted(receiver.mPackageName)) {
2263 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
2264 receiver.mPackageName);
2265 continue;
2266 }
2267
David Christieb870dbf2015-06-22 12:42:53 -07002268 if (!reportLocationAccessNoThrow(receiver.mPid, receiver.mUid, receiver.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002269 receiver.mAllowedResolutionLevel)) {
2270 if (D) Log.d(TAG, "skipping loc update for no op app: " +
2271 receiver.mPackageName);
2272 continue;
2273 }
2274
Victoria Lease09016ab2012-09-16 12:33:15 -07002275 Location notifyLocation = null;
Victoria Lease37425c32012-10-16 16:08:48 -07002276 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2277 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002278 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002279 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002280 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002281 if (notifyLocation != null) {
2282 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002283 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002284 if (lastLoc == null) {
2285 lastLoc = new Location(notifyLocation);
2286 r.mLastFixBroadcast = lastLoc;
2287 } else {
2288 lastLoc.set(notifyLocation);
2289 }
2290 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2291 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2292 receiverDead = true;
2293 }
Laurent Tu75defb62012-11-01 16:21:52 -07002294 r.mRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
2296 }
2297
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002298 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002300 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002302 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002304 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002305 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002306 }
2307 }
2308
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002309 // track expired records
Laurent Tu75defb62012-11-01 16:21:52 -07002310 if (r.mRequest.getNumUpdates() <= 0 || r.mRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002311 if (deadUpdateRecords == null) {
2312 deadUpdateRecords = new ArrayList<UpdateRecord>();
2313 }
2314 deadUpdateRecords.add(r);
2315 }
2316 // track dead receivers
2317 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002318 if (deadReceivers == null) {
2319 deadReceivers = new ArrayList<Receiver>();
2320 }
2321 if (!deadReceivers.contains(receiver)) {
2322 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 }
2324 }
2325 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002326
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002327 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002329 for (Receiver receiver : deadReceivers) {
2330 removeUpdatesLocked(receiver);
2331 }
2332 }
2333 if (deadUpdateRecords != null) {
2334 for (UpdateRecord r : deadUpdateRecords) {
2335 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002337 applyRequirementsLocked(provider);
2338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 }
2340
2341 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002342 public LocationWorkerHandler(Looper looper) {
2343 super(looper, null, true);
2344 }
2345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 @Override
2347 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002348 switch (msg.what) {
2349 case MSG_LOCATION_CHANGED:
2350 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2351 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 }
2353 }
2354 }
2355
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002356 private boolean isMockProvider(String provider) {
2357 synchronized (mLock) {
2358 return mMockProviders.containsKey(provider);
2359 }
2360 }
2361
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002362 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002363 // create a working copy of the incoming Location so that the service can modify it without
2364 // disturbing the caller's copy
2365 Location myLocation = new Location(location);
2366 String provider = myLocation.getProvider();
2367
2368 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2369 // bit if location did not come from a mock provider because passive/fused providers can
2370 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2371 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2372 myLocation.setIsFromMockProvider(true);
2373 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002374
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002375 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002376 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2377 if (!passive) {
2378 // notify passive provider of the new location
2379 mPassiveProvider.updateLocation(myLocation);
2380 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002381 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
Mike Lockwoode97ae402010-09-29 15:23:46 -04002386 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2387 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002388 public void onPackageDisappeared(String packageName, int reason) {
2389 // remove all receivers associated with this package name
2390 synchronized (mLock) {
2391 ArrayList<Receiver> deadReceivers = null;
2392
2393 for (Receiver receiver : mReceivers.values()) {
2394 if (receiver.mPackageName.equals(packageName)) {
2395 if (deadReceivers == null) {
2396 deadReceivers = new ArrayList<Receiver>();
2397 }
2398 deadReceivers.add(receiver);
2399 }
2400 }
2401
2402 // perform removal outside of mReceivers loop
2403 if (deadReceivers != null) {
2404 for (Receiver receiver : deadReceivers) {
2405 removeUpdatesLocked(receiver);
2406 }
2407 }
2408 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002409 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002410 };
2411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 // Geocoder
2413
Nick Pellye0fd6932012-07-11 10:26:13 -07002414 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002415 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002416 return mGeocodeProvider != null;
2417 }
2418
Nick Pellye0fd6932012-07-11 10:26:13 -07002419 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002421 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002422 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002423 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2424 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002426 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428
Mike Lockwooda55c3212009-04-15 11:10:11 -04002429
Nick Pellye0fd6932012-07-11 10:26:13 -07002430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002432 double lowerLeftLatitude, double lowerLeftLongitude,
2433 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002434 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002435
2436 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002437 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2438 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2439 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002441 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
2443
2444 // Mock Providers
2445
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002446 private boolean canCallerAccessMockLocation(String opPackageName) {
2447 return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
2448 opPackageName) == AppOpsManager.MODE_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450
Nick Pellye0fd6932012-07-11 10:26:13 -07002451 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002452 public void addTestProvider(String name, ProviderProperties properties, String opPackageName) {
2453 if (!canCallerAccessMockLocation(opPackageName)) {
2454 return;
2455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456
Mike Lockwooda4903f22010-02-17 06:42:23 -05002457 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2458 throw new IllegalArgumentException("Cannot mock the passive location provider");
2459 }
2460
Mike Lockwood86328a92009-10-23 08:38:25 -04002461 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002462 synchronized (mLock) {
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002463 // remove the real provider if we are replacing GPS or network provider
2464 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002465 || LocationManager.NETWORK_PROVIDER.equals(name)
2466 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002467 LocationProviderInterface p = mProvidersByName.get(name);
2468 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002469 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002470 }
2471 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002472 addTestProviderLocked(name, properties);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 updateProvidersLocked();
2474 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002475 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002478 private void addTestProviderLocked(String name, ProviderProperties properties) {
2479 if (mProvidersByName.get(name) != null) {
2480 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2481 }
2482 MockProvider provider = new MockProvider(name, this, properties);
2483 addProviderLocked(provider);
2484 mMockProviders.put(name, provider);
2485 mLastLocation.put(name, null);
2486 mLastLocationCoarseInterval.put(name, null);
2487 }
2488
Nick Pellye0fd6932012-07-11 10:26:13 -07002489 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002490 public void removeTestProvider(String provider, String opPackageName) {
2491 if (!canCallerAccessMockLocation(opPackageName)) {
2492 return;
2493 }
2494
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002495 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002496
2497 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002498 // we don't leave anything dangling.
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002499 clearTestProviderEnabled(provider, opPackageName);
2500 clearTestProviderLocation(provider, opPackageName);
2501 clearTestProviderStatus(provider, opPackageName);
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002502
You Kima6d0b6f2012-10-28 03:58:44 +09002503 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002504 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2506 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002507 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002508 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002509
2510 // reinstate real provider if available
2511 LocationProviderInterface realProvider = mRealProviders.get(provider);
2512 if (realProvider != null) {
2513 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002514 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002515 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002516 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002518 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 }
2520 }
2521
Nick Pellye0fd6932012-07-11 10:26:13 -07002522 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002523 public void setTestProviderLocation(String provider, Location loc, String opPackageName) {
2524 if (!canCallerAccessMockLocation(opPackageName)) {
2525 return;
2526 }
2527
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002528 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002529 MockProvider mockProvider = mMockProviders.get(provider);
2530 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2532 }
Mike Lockwood95427cd2009-05-07 13:27:54 -04002533 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2534 long identity = Binder.clearCallingIdentity();
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002535 mockProvider.setLocation(loc);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002536 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
2538 }
2539
Nick Pellye0fd6932012-07-11 10:26:13 -07002540 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002541 public void clearTestProviderLocation(String provider, String opPackageName) {
2542 if (!canCallerAccessMockLocation(opPackageName)) {
2543 return;
2544 }
2545
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002546 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002547 MockProvider mockProvider = mMockProviders.get(provider);
2548 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2550 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002551 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
2553 }
2554
Nick Pellye0fd6932012-07-11 10:26:13 -07002555 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002556 public void setTestProviderEnabled(String provider, boolean enabled, String opPackageName) {
2557 if (!canCallerAccessMockLocation(opPackageName)) {
2558 return;
2559 }
2560
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002561 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002562 MockProvider mockProvider = mMockProviders.get(provider);
2563 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2565 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002566 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002568 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 mEnabledProviders.add(provider);
2570 mDisabledProviders.remove(provider);
2571 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002572 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 mEnabledProviders.remove(provider);
2574 mDisabledProviders.add(provider);
2575 }
2576 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002577 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579 }
2580
Nick Pellye0fd6932012-07-11 10:26:13 -07002581 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002582 public void clearTestProviderEnabled(String provider, String opPackageName) {
2583 if (!canCallerAccessMockLocation(opPackageName)) {
2584 return;
2585 }
2586
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002587 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002588 MockProvider mockProvider = mMockProviders.get(provider);
2589 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2591 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002592 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 mEnabledProviders.remove(provider);
2594 mDisabledProviders.remove(provider);
2595 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002596 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
2598 }
2599
Nick Pellye0fd6932012-07-11 10:26:13 -07002600 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002601 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime,
2602 String opPackageName) {
2603 if (!canCallerAccessMockLocation(opPackageName)) {
2604 return;
2605 }
2606
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002607 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002608 MockProvider mockProvider = mMockProviders.get(provider);
2609 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2611 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002612 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
2614 }
2615
Nick Pellye0fd6932012-07-11 10:26:13 -07002616 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002617 public void clearTestProviderStatus(String provider, String opPackageName) {
2618 if (!canCallerAccessMockLocation(opPackageName)) {
2619 return;
2620 }
2621
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002622 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002623 MockProvider mockProvider = mMockProviders.get(provider);
2624 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2626 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002627 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 }
2629 }
2630
2631 private void log(String log) {
2632 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002633 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 }
2635 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002636
2637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2639 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2640 != PackageManager.PERMISSION_GRANTED) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04002641 pw.println("Permission Denial: can't dump LocationManagerService from from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 + Binder.getCallingPid()
2643 + ", uid=" + Binder.getCallingUid());
2644 return;
2645 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002646
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002647 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002650 for (Receiver receiver : mReceivers.values()) {
2651 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 }
David Christie2ff96af2014-01-30 16:09:37 -08002653 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002654 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
2655 pw.println(" " + entry.getKey() + ":");
2656 for (UpdateRecord record : entry.getValue()) {
2657 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
2659 }
David Christie2ff96af2014-01-30 16:09:37 -08002660 pw.println(" Historical Records by Provider:");
2661 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
2662 : mRequestStatistics.statistics.entrySet()) {
2663 PackageProviderKey key = entry.getKey();
2664 PackageStatistics stats = entry.getValue();
2665 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
2666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002668 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
2669 String provider = entry.getKey();
2670 Location location = entry.getValue();
2671 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002673
David Christie1b9b7b12013-04-15 15:31:11 -07002674 pw.println(" Last Known Locations Coarse Intervals:");
2675 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
2676 String provider = entry.getKey();
2677 Location location = entry.getValue();
2678 pw.println(" " + provider + ": " + location);
2679 }
2680
Nick Pellye0fd6932012-07-11 10:26:13 -07002681 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 if (mEnabledProviders.size() > 0) {
2684 pw.println(" Enabled Providers:");
2685 for (String i : mEnabledProviders) {
2686 pw.println(" " + i);
2687 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 }
2690 if (mDisabledProviders.size() > 0) {
2691 pw.println(" Disabled Providers:");
2692 for (String i : mDisabledProviders) {
2693 pw.println(" " + i);
2694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07002696 pw.append(" ");
2697 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 if (mMockProviders.size() > 0) {
2699 pw.println(" Mock Providers:");
2700 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002701 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
2703 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002704
Nick Pelly74fa7ea2012-08-13 19:36:38 -07002705 pw.append(" fudger: ");
2706 mLocationFudger.dump(fd, pw, args);
2707
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002708 if (args.length > 0 && "short".equals(args[0])) {
2709 return;
2710 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002711 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002712 pw.print(provider.getName() + " Internal State");
2713 if (provider instanceof LocationProviderProxy) {
2714 LocationProviderProxy proxy = (LocationProviderProxy) provider;
2715 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002716 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002717 pw.println(":");
2718 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06002719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
2721 }
2722}