blob: a4e673d975e449c8368185c3101ee1ecb14e1f0c [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
Soonil Nagarkar7decfb62017-01-18 12:18:49 -080019import android.app.ActivityManager;
Wyatt Rileycf879db2017-01-12 13:57:38 -080020import android.annotation.NonNull;
Svet Ganovadc1cf42015-06-15 16:36:24 -070021import android.content.pm.PackageManagerInternal;
Soonil Nagarkar681d7112017-02-23 17:14:16 -080022import android.util.ArrayMap;
Soonil Nagarkar2b565df2017-02-14 13:33:23 -080023import android.util.ArraySet;
destradaaea8a8a62014-06-23 18:19:03 -070024import com.android.internal.content.PackageMonitor;
25import com.android.internal.location.ProviderProperties;
26import com.android.internal.location.ProviderRequest;
27import com.android.internal.os.BackgroundThread;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070028import com.android.internal.util.ArrayUtils;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060029import com.android.internal.util.DumpUtils;
destradaaa4fa3b52014-07-09 10:46:39 -070030import com.android.server.location.ActivityRecognitionProxy;
destradaaea8a8a62014-06-23 18:19:03 -070031import com.android.server.location.FlpHardwareProvider;
32import com.android.server.location.FusedProxy;
33import com.android.server.location.GeocoderProxy;
34import com.android.server.location.GeofenceManager;
35import com.android.server.location.GeofenceProxy;
Lifu Tang818aa2c2016-02-01 01:52:00 -080036import com.android.server.location.GnssLocationProvider;
37import com.android.server.location.GnssMeasurementsProvider;
38import com.android.server.location.GnssNavigationMessageProvider;
destradaaea8a8a62014-06-23 18:19:03 -070039import com.android.server.location.LocationBlacklist;
40import com.android.server.location.LocationFudger;
41import com.android.server.location.LocationProviderInterface;
42import com.android.server.location.LocationProviderProxy;
43import com.android.server.location.LocationRequestStatistics;
44import com.android.server.location.LocationRequestStatistics.PackageProviderKey;
45import com.android.server.location.LocationRequestStatistics.PackageStatistics;
46import com.android.server.location.MockProvider;
47import com.android.server.location.PassiveProvider;
48
Dianne Hackborna06de0f2012-12-11 16:34:47 -080049import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.app.PendingIntent;
Victoria Lease38389b62012-09-30 11:44:22 -070051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ContentResolver;
53import android.content.Context;
54import android.content.Intent;
Victoria Lease38389b62012-09-30 11:44:22 -070055import android.content.IntentFilter;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070056import android.content.pm.ApplicationInfo;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050057import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.pm.PackageManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070059import android.content.pm.PackageManager.NameNotFoundException;
Jeff Hamiltonfbadb692012-10-05 14:21:58 -050060import android.content.pm.ResolveInfo;
61import android.content.pm.Signature;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050062import android.content.res.Resources;
Brian Muramatsubb95cb92012-08-29 10:43:21 -070063import android.database.ContentObserver;
destradaaa4fa3b52014-07-09 10:46:39 -070064import android.hardware.location.ActivityRecognitionHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.location.Address;
Mike Lockwood03ca2162010-04-01 08:10:09 -070066import android.location.Criteria;
Mike Lockwood34901402010-01-04 12:14:21 -050067import android.location.GeocoderParams;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070068import android.location.Geofence;
Wyatt Rileycf879db2017-01-12 13:57:38 -080069import android.location.IBatchedLocationCallback;
Lifu Tang818aa2c2016-02-01 01:52:00 -080070import android.location.IGnssMeasurementsListener;
Lifu Tang30f95a72016-01-07 23:20:38 -080071import android.location.IGnssStatusListener;
72import android.location.IGnssStatusProvider;
Wei Liu5241a4c2015-05-11 14:00:36 -070073import android.location.IGpsGeofenceHardware;
Lifu Tang818aa2c2016-02-01 01:52:00 -080074import android.location.IGnssNavigationMessageListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.location.ILocationListener;
76import android.location.ILocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040077import android.location.INetInitiatedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.location.Location;
79import android.location.LocationManager;
80import android.location.LocationProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070081import android.location.LocationRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Binder;
83import android.os.Bundle;
84import android.os.Handler;
85import android.os.IBinder;
Mike Lockwood3d12b512009-04-21 23:25:35 -070086import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.Message;
88import android.os.PowerManager;
Mike Lockwoode932f7f2009-04-06 10:51:26 -070089import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.RemoteException;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070091import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070092import android.os.UserHandle;
Amith Yamasanib27528d2014-06-05 15:02:10 -070093import android.os.UserManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070094import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.provider.Settings;
Tom O'Neilla206a0f2016-12-15 10:26:28 -080096import android.text.TextUtils;
97import android.util.EventLog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080099import android.util.Slog;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700100
Mike Lockwood43e33f22010-03-26 10:41:48 -0400101import java.io.FileDescriptor;
102import java.io.PrintWriter;
103import java.util.ArrayList;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700104import java.util.Arrays;
Mike Lockwood43e33f22010-03-26 10:41:48 -0400105import java.util.HashMap;
106import java.util.HashSet;
107import java.util.List;
108import java.util.Map;
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800109import java.util.Map.Entry;
Wyatt Rileycf879db2017-01-12 13:57:38 -0800110import java.util.NoSuchElementException;
Mike Lockwood43e33f22010-03-26 10:41:48 -0400111import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113/**
114 * The service class that manages LocationProviders and issues location
115 * updates and alerts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
Victoria Lease5cd731a2012-12-19 15:04:21 -0800117public class LocationManagerService extends ILocationManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private static final String TAG = "LocationManagerService";
JP Abgrallf79811e72013-02-01 18:45:05 -0800119 public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700120
121 private static final String WAKELOCK_KEY = TAG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Victoria Lease37425c32012-10-16 16:08:48 -0700123 // Location resolution level: no location data whatsoever
124 private static final int RESOLUTION_LEVEL_NONE = 0;
125 // Location resolution level: coarse location data only
126 private static final int RESOLUTION_LEVEL_COARSE = 1;
127 // Location resolution level: fine location data
128 private static final int RESOLUTION_LEVEL_FINE = 2;
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 private static final String ACCESS_MOCK_LOCATION =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700131 android.Manifest.permission.ACCESS_MOCK_LOCATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 private static final String ACCESS_LOCATION_EXTRA_COMMANDS =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700133 android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS;
Mike Lockwood275555c2009-05-01 11:30:34 -0400134 private static final String INSTALL_LOCATION_PROVIDER =
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700135 android.Manifest.permission.INSTALL_LOCATION_PROVIDER;
136
137 private static final String NETWORK_LOCATION_SERVICE_ACTION =
Stan Chesnutt39062dd2013-07-22 14:33:30 -0700138 "com.android.location.service.v3.NetworkLocationProvider";
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700139 private static final String FUSED_LOCATION_SERVICE_ACTION =
140 "com.android.location.service.FusedLocationProvider";
141
142 private static final int MSG_LOCATION_CHANGED = 1;
143
David Christie1b9b7b12013-04-15 15:31:11 -0700144 private static final long NANOS_PER_MILLI = 1000000L;
145
David Christie0b837452013-07-29 16:02:13 -0700146 // The maximum interval a location request can have and still be considered "high power".
147 private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000;
148
Soonil Nagarkarebda0282017-04-10 14:55:37 -0700149 private static final int FOREGROUND_IMPORTANCE_CUTOFF
150 = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;
151
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800152 // default background throttling interval if not overriden in settings
Soonil Nagarkarde6780a2017-02-07 10:39:41 -0800153 private static final long DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS = 30 * 60 * 1000;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800154
Nick Pellyf1be6862012-05-15 10:53:42 -0700155 // Location Providers may sometimes deliver location updates
156 // slightly faster that requested - provide grace period so
157 // we don't unnecessarily filter events that are otherwise on
158 // time
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700159 private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100;
Nick Pellyf1be6862012-05-15 10:53:42 -0700160
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700161 private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest();
162
163 private final Context mContext;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800164 private final AppOpsManager mAppOps;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700165
166 // used internally for synchronization
167 private final Object mLock = new Object();
168
Wyatt Rileya8037ff2016-08-04 16:10:06 -0700169 // --- fields below are final after systemRunning() ---
Nick Pelly74fa7ea2012-08-13 19:36:38 -0700170 private LocationFudger mLocationFudger;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700171 private GeofenceManager mGeofenceManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700172 private PackageManager mPackageManager;
Victoria Lease0aa28602013-05-29 15:28:26 -0700173 private PowerManager mPowerManager;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800174 private ActivityManager mActivityManager;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700175 private UserManager mUserManager;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700176 private GeocoderProxy mGeocodeProvider;
Lifu Tang30f95a72016-01-07 23:20:38 -0800177 private IGnssStatusProvider mGnssStatusProvider;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700178 private INetInitiatedListener mNetInitiatedListener;
179 private LocationWorkerHandler mLocationHandler;
Nick Pelly4035f5a2012-08-17 14:43:49 -0700180 private PassiveProvider mPassiveProvider; // track passive provider for special cases
181 private LocationBlacklist mBlacklist;
Lifu Tang818aa2c2016-02-01 01:52:00 -0800182 private GnssMeasurementsProvider mGnssMeasurementsProvider;
183 private GnssNavigationMessageProvider mGnssNavigationMessageProvider;
Wei Liu5241a4c2015-05-11 14:00:36 -0700184 private IGpsGeofenceHardware mGpsGeofenceProxy;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700185
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700186 // --- fields below are protected by mLock ---
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 // Set of providers that are explicitly enabled
Wyatt Rileya8037ff2016-08-04 16:10:06 -0700188 // Only used by passive, fused & test. Network & GPS are controlled separately, and not listed.
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800189 private final Set<String> mEnabledProviders = new HashSet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
191 // Set of providers that are explicitly disabled
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800192 private final Set<String> mDisabledProviders = new HashSet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700194 // Mock (test) providers
195 private final HashMap<String, MockProvider> mMockProviders =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800196 new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700198 // all receivers
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800199 private final HashMap<Object, Receiver> mReceivers = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700201 // currently installed providers (with mocks replacing real providers)
Mike Lockwoodd03ff942010-02-09 08:46:14 -0500202 private final ArrayList<LocationProviderInterface> mProviders =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800203 new ArrayList<>();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -0400204
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700205 // real providers, saved here when mocked out
206 private final HashMap<String, LocationProviderInterface> mRealProviders =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800207 new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700209 // mapping from provider name to provider
210 private final HashMap<String, LocationProviderInterface> mProvidersByName =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800211 new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700213 // mapping from provider name to all its UpdateRecords
214 private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800215 new HashMap<>();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700216
David Christie2ff96af2014-01-30 16:09:37 -0800217 private final LocationRequestStatistics mRequestStatistics = new LocationRequestStatistics();
218
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700219 // mapping from provider name to last known location
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800220 private final HashMap<String, Location> mLastLocation = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
David Christie1b9b7b12013-04-15 15:31:11 -0700222 // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS.
223 // locations stored here are not fudged for coarse permissions.
224 private final HashMap<String, Location> mLastLocationCoarseInterval =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800225 new HashMap<>();
David Christie1b9b7b12013-04-15 15:31:11 -0700226
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -0800227 // all providers that operate over proxy, for authorizing incoming location and whitelisting
228 // throttling
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700229 private final ArrayList<LocationProviderProxy> mProxyProviders =
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800230 new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Soonil Nagarkar2b565df2017-02-14 13:33:23 -0800232 private final ArraySet<String> mBackgroundThrottlePackageWhitelist = new ArraySet<>();
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -0800233
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800234 private final ArrayMap<IGnssMeasurementsListener, Identity> mGnssMeasurementsListeners =
235 new ArrayMap<>();
236
237 private final ArrayMap<IGnssNavigationMessageListener, Identity>
238 mGnssNavigationMessageListeners = new ArrayMap<>();
239
Victoria Lease38389b62012-09-30 11:44:22 -0700240 // current active user on the device - other users are denied location data
Xiaohui Chena4490622015-09-22 15:29:31 -0700241 private int mCurrentUserId = UserHandle.USER_SYSTEM;
242 private int[] mCurrentUserProfiles = new int[] { UserHandle.USER_SYSTEM };
Victoria Lease38389b62012-09-30 11:44:22 -0700243
Lifu Tang9363b942016-02-16 18:07:00 -0800244 private GnssLocationProvider.GnssSystemInfoProvider mGnssSystemInfoProvider;
Lifu Tang82f893d2016-01-21 18:15:33 -0800245
Siddharth Raybb608c82017-03-16 11:33:34 -0700246 private GnssLocationProvider.GnssMetricsProvider mGnssMetricsProvider;
Wyatt Rileyaa420d52017-07-03 15:14:42 -0700247
248 private GnssLocationProvider.GnssBatchingProvider mGnssBatchingProvider;
Wyatt Rileycf879db2017-01-12 13:57:38 -0800249 private IBatchedLocationCallback mGnssBatchingCallback;
250 private LinkedCallback mGnssBatchingDeathCallback;
251 private boolean mGnssBatchingInProgress = false;
252
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700253 public LocationManagerService(Context context) {
254 super();
255 mContext = context;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800256 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800257
Svet Ganovadc1cf42015-06-15 16:36:24 -0700258 // Let the package manager query which are the default location
259 // providers as they get certain permissions granted by default.
260 PackageManagerInternal packageManagerInternal = LocalServices.getService(
261 PackageManagerInternal.class);
262 packageManagerInternal.setLocationPackagesProvider(
263 new PackageManagerInternal.PackagesProvider() {
264 @Override
265 public String[] getPackages(int userId) {
266 return mContext.getResources().getStringArray(
267 com.android.internal.R.array.config_locationProviderPackageNames);
268 }
269 });
270
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700271 if (D) Log.d(TAG, "Constructed");
272
Wyatt Rileya8037ff2016-08-04 16:10:06 -0700273 // most startup is deferred until systemRunning()
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700274 }
275
Svetoslav Ganova0027152013-06-25 14:59:53 -0700276 public void systemRunning() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700277 synchronized (mLock) {
Wyatt Rileya8037ff2016-08-04 16:10:06 -0700278 if (D) Log.d(TAG, "systemRunning()");
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700279
Victoria Lease5cd731a2012-12-19 15:04:21 -0800280 // fetch package manager
281 mPackageManager = mContext.getPackageManager();
282
Victoria Lease0aa28602013-05-29 15:28:26 -0700283 // fetch power manager
284 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800285
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800286 // fetch activity manager
287 mActivityManager
288 = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
289
Victoria Lease5cd731a2012-12-19 15:04:21 -0800290 // prepare worker thread
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700291 mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
Victoria Lease5cd731a2012-12-19 15:04:21 -0800292
293 // prepare mLocationHandler's dependents
294 mLocationFudger = new LocationFudger(mContext, mLocationHandler);
295 mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
296 mBlacklist.init();
297 mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
298
Dianne Hackbornc2293022013-02-06 23:14:49 -0800299 // Monitor for app ops mode changes.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700300 AppOpsManager.OnOpChangedListener callback
301 = new AppOpsManager.OnOpChangedInternalListener() {
302 public void onOpChanged(int op, String packageName) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800303 synchronized (mLock) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700304 for (Receiver receiver : mReceivers.values()) {
305 receiver.updateMonitoring(true);
306 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800307 applyAllProviderRequirementsLocked();
308 }
309 }
310 };
311 mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
312
David Christieb870dbf2015-06-22 12:42:53 -0700313 PackageManager.OnPermissionsChangedListener permissionListener
314 = new PackageManager.OnPermissionsChangedListener() {
315 @Override
316 public void onPermissionsChanged(final int uid) {
317 synchronized (mLock) {
318 applyAllProviderRequirementsLocked();
319 }
320 }
321 };
322 mPackageManager.addOnPermissionsChangeListener(permissionListener);
323
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800324 // listen for background/foreground changes
325 ActivityManager.OnUidImportanceListener uidImportanceListener
326 = new ActivityManager.OnUidImportanceListener() {
327 @Override
328 public void onUidImportance(int uid, int importance) {
329 boolean foreground = isImportanceForeground(importance);
330 HashSet<String> affectedProviders = new HashSet<>(mRecordsByProvider.size());
331 synchronized (mLock) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800332 for (Entry<String, ArrayList<UpdateRecord>> entry
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800333 : mRecordsByProvider.entrySet()) {
334 String provider = entry.getKey();
335 for (UpdateRecord record : entry.getValue()) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800336 if (record.mReceiver.mIdentity.mUid == uid
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800337 && record.mIsForegroundUid != foreground) {
338 if (D) Log.d(TAG, "request from uid " + uid + " is now "
339 + (foreground ? "foreground" : "background)"));
340 record.mIsForegroundUid = foreground;
341
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800342 if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800343 affectedProviders.add(provider);
344 }
345 }
346 }
347 }
348 for (String provider : affectedProviders) {
349 applyRequirementsLocked(provider);
350 }
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800351
352 for (Entry<IGnssMeasurementsListener, Identity> entry
353 : mGnssMeasurementsListeners.entrySet()) {
354 if (entry.getValue().mUid == uid) {
355 if (D) Log.d(TAG, "gnss measurements listener from uid " + uid
356 + " is now " + (foreground ? "foreground" : "background)"));
357 if (foreground || isThrottlingExemptLocked(entry.getValue())) {
358 mGnssMeasurementsProvider.addListener(entry.getKey());
359 } else {
360 mGnssMeasurementsProvider.removeListener(entry.getKey());
361 }
362 }
363 }
364
365 for (Entry<IGnssNavigationMessageListener, Identity> entry
366 : mGnssNavigationMessageListeners.entrySet()) {
367 if (entry.getValue().mUid == uid) {
368 if (D) Log.d(TAG, "gnss navigation message listener from uid "
369 + uid + " is now "
370 + (foreground ? "foreground" : "background)"));
371 if (foreground || isThrottlingExemptLocked(entry.getValue())) {
372 mGnssNavigationMessageProvider.addListener(entry.getKey());
373 } else {
374 mGnssNavigationMessageProvider.removeListener(entry.getKey());
375 }
376 }
377 }
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800378 }
379
380 }
381 };
382 mActivityManager.addOnUidImportanceListener(uidImportanceListener,
Soonil Nagarkarebda0282017-04-10 14:55:37 -0700383 FOREGROUND_IMPORTANCE_CUTOFF);
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800384
Amith Yamasanib27528d2014-06-05 15:02:10 -0700385 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
386 updateUserProfiles(mCurrentUserId);
387
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800388 updateBackgroundThrottlingWhitelistLocked();
Soonil Nagarkar2b565df2017-02-14 13:33:23 -0800389
Victoria Lease5cd731a2012-12-19 15:04:21 -0800390 // prepare providers
391 loadProvidersLocked();
392 updateProvidersLocked();
393 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700394
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700395 // listen for settings changes
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700396 mContext.getContentResolver().registerContentObserver(
Laurent Tu75defb62012-11-01 16:21:52 -0700397 Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700398 new ContentObserver(mLocationHandler) {
Victoria Lease5cd731a2012-12-19 15:04:21 -0800399 @Override
400 public void onChange(boolean selfChange) {
401 synchronized (mLock) {
402 updateProvidersLocked();
403 }
404 }
405 }, UserHandle.USER_ALL);
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800406 mContext.getContentResolver().registerContentObserver(
407 Settings.Global.getUriFor(Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS),
408 true,
409 new ContentObserver(mLocationHandler) {
410 @Override
411 public void onChange(boolean selfChange) {
412 synchronized (mLock) {
413 updateProvidersLocked();
414 }
415 }
416 }, UserHandle.USER_ALL);
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -0800417 mContext.getContentResolver().registerContentObserver(
418 Settings.Global.getUriFor(
419 Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST),
420 true,
421 new ContentObserver(mLocationHandler) {
422 @Override
423 public void onChange(boolean selfChange) {
424 synchronized (mLock) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800425 updateBackgroundThrottlingWhitelistLocked();
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -0800426 updateProvidersLocked();
427 }
428 }
429 }, UserHandle.USER_ALL);
Victoria Lease5cd731a2012-12-19 15:04:21 -0800430 mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
Brian Muramatsubb95cb92012-08-29 10:43:21 -0700431
Victoria Lease38389b62012-09-30 11:44:22 -0700432 // listen for user change
433 IntentFilter intentFilter = new IntentFilter();
434 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700435 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
436 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
destradaab9026982015-08-27 17:34:54 -0700437 intentFilter.addAction(Intent.ACTION_SHUTDOWN);
Victoria Lease38389b62012-09-30 11:44:22 -0700438
439 mContext.registerReceiverAsUser(new BroadcastReceiver() {
440 @Override
441 public void onReceive(Context context, Intent intent) {
442 String action = intent.getAction();
443 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
444 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
Amith Yamasanib27528d2014-06-05 15:02:10 -0700445 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
446 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
447 updateUserProfiles(mCurrentUserId);
destradaab9026982015-08-27 17:34:54 -0700448 } else if (Intent.ACTION_SHUTDOWN.equals(action)) {
Wyatt Rileya8037ff2016-08-04 16:10:06 -0700449 // shutdown only if UserId indicates whole system, not just one user
450 if(D) Log.d(TAG, "Shutdown received with UserId: " + getSendingUserId());
451 if (getSendingUserId() == UserHandle.USER_ALL) {
452 shutdownComponents();
453 }
Victoria Lease38389b62012-09-30 11:44:22 -0700454 }
455 }
Victoria Lease5cd731a2012-12-19 15:04:21 -0800456 }, UserHandle.ALL, intentFilter, null, mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700457 }
458
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800459 private static boolean isImportanceForeground(int importance) {
Soonil Nagarkarebda0282017-04-10 14:55:37 -0700460 return importance <= FOREGROUND_IMPORTANCE_CUTOFF;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800461 }
462
Amith Yamasanib27528d2014-06-05 15:02:10 -0700463 /**
destradaab9026982015-08-27 17:34:54 -0700464 * Provides a way for components held by the {@link LocationManagerService} to clean-up
465 * gracefully on system's shutdown.
466 *
467 * NOTES:
468 * 1) Only provides a chance to clean-up on an opt-in basis. This guarantees back-compat
469 * support for components that do not wish to handle such event.
470 */
471 private void shutdownComponents() {
472 if(D) Log.d(TAG, "Shutting down components...");
473
474 LocationProviderInterface gpsProvider = mProvidersByName.get(LocationManager.GPS_PROVIDER);
475 if (gpsProvider != null && gpsProvider.isEnabled()) {
476 gpsProvider.disable();
477 }
478
destradaa2e385072015-10-14 16:45:58 -0700479 // it is needed to check if FLP HW provider is supported before accessing the instance, this
480 // avoids an exception to be thrown by the singleton factory method
481 if (FlpHardwareProvider.isSupported()) {
482 FlpHardwareProvider flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
destradaab9026982015-08-27 17:34:54 -0700483 flpHardwareProvider.cleanup();
484 }
485 }
486
487 /**
Amith Yamasanib27528d2014-06-05 15:02:10 -0700488 * Makes a list of userids that are related to the current user. This is
489 * relevant when using managed profiles. Otherwise the list only contains
490 * the current user.
491 *
492 * @param currentUserId the current user, who might have an alter-ego.
493 */
494 void updateUserProfiles(int currentUserId) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700495 int[] profileIds = mUserManager.getProfileIdsWithDisabled(currentUserId);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700496 synchronized (mLock) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700497 mCurrentUserProfiles = profileIds;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700498 }
499 }
500
501 /**
502 * Checks if the specified userId matches any of the current foreground
503 * users stored in mCurrentUserProfiles.
504 */
505 private boolean isCurrentProfile(int userId) {
506 synchronized (mLock) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700507 return ArrayUtils.contains(mCurrentUserProfiles, userId);
Amith Yamasanib27528d2014-06-05 15:02:10 -0700508 }
509 }
510
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500511 private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
512 PackageManager pm = mContext.getPackageManager();
513 String systemPackageName = mContext.getPackageName();
514 ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);
515
516 List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
517 new Intent(FUSED_LOCATION_SERVICE_ACTION),
518 PackageManager.GET_META_DATA, mCurrentUserId);
519 for (ResolveInfo rInfo : rInfos) {
520 String packageName = rInfo.serviceInfo.packageName;
521
522 // Check that the signature is in the list of supported sigs. If it's not in
523 // this list the standard provider binding logic won't bind to it.
524 try {
525 PackageInfo pInfo;
526 pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
527 if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
528 Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
529 ", but has wrong signature, ignoring");
530 continue;
531 }
532 } catch (NameNotFoundException e) {
533 Log.e(TAG, "missing package: " + packageName);
534 continue;
535 }
536
537 // Get the version info
538 if (rInfo.serviceInfo.metaData == null) {
539 Log.w(TAG, "Found fused provider without metadata: " + packageName);
540 continue;
541 }
542
543 int version = rInfo.serviceInfo.metaData.getInt(
544 ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
545 if (version == 0) {
546 // This should be the fallback fused location provider.
547
548 // Make sure it's in the system partition.
549 if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
550 if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
551 continue;
552 }
553
554 // Check that the fallback is signed the same as the OS
555 // as a proxy for coreApp="true"
556 if (pm.checkSignatures(systemPackageName, packageName)
557 != PackageManager.SIGNATURE_MATCH) {
558 if (D) Log.d(TAG, "Fallback candidate not signed the same as system: "
559 + packageName);
560 continue;
561 }
562
563 // Found a valid fallback.
564 if (D) Log.d(TAG, "Found fallback provider: " + packageName);
565 return;
566 } else {
567 if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
568 }
569 }
570
571 throw new IllegalStateException("Unable to find a fused location provider that is in the "
572 + "system partition with version 0 and signed with the platform certificate. "
573 + "Such a package is needed to provide a default fused location provider in the "
574 + "event that no other fused location provider has been installed or is currently "
575 + "available. For example, coreOnly boot mode when decrypting the data "
576 + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
577 }
578
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700579 private void loadProvidersLocked() {
Victoria Lease5c24fd02012-10-01 11:00:50 -0700580 // create a passive location provider, which is always enabled
581 PassiveProvider passiveProvider = new PassiveProvider(this);
582 addProviderLocked(passiveProvider);
583 mEnabledProviders.add(passiveProvider.getName());
584 mPassiveProvider = passiveProvider;
585
Lifu Tang30f95a72016-01-07 23:20:38 -0800586 if (GnssLocationProvider.isSupported()) {
Wei Liu5241a4c2015-05-11 14:00:36 -0700587 // Create a gps location provider
Lifu Tang30f95a72016-01-07 23:20:38 -0800588 GnssLocationProvider gnssProvider = new GnssLocationProvider(mContext, this,
Wei Liu5241a4c2015-05-11 14:00:36 -0700589 mLocationHandler.getLooper());
Lifu Tang9363b942016-02-16 18:07:00 -0800590 mGnssSystemInfoProvider = gnssProvider.getGnssSystemInfoProvider();
Wyatt Rileycf879db2017-01-12 13:57:38 -0800591 mGnssBatchingProvider = gnssProvider.getGnssBatchingProvider();
Siddharth Raybb608c82017-03-16 11:33:34 -0700592 mGnssMetricsProvider = gnssProvider.getGnssMetricsProvider();
Lifu Tang30f95a72016-01-07 23:20:38 -0800593 mGnssStatusProvider = gnssProvider.getGnssStatusProvider();
594 mNetInitiatedListener = gnssProvider.getNetInitiatedListener();
595 addProviderLocked(gnssProvider);
596 mRealProviders.put(LocationManager.GPS_PROVIDER, gnssProvider);
Lifu Tang818aa2c2016-02-01 01:52:00 -0800597 mGnssMeasurementsProvider = gnssProvider.getGnssMeasurementsProvider();
598 mGnssNavigationMessageProvider = gnssProvider.getGnssNavigationMessageProvider();
Lifu Tang30f95a72016-01-07 23:20:38 -0800599 mGpsGeofenceProxy = gnssProvider.getGpsGeofenceProxy();
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700600 }
601
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700602 /*
603 Load package name(s) containing location provider support.
604 These packages can contain services implementing location providers:
605 Geocoder Provider, Network Location Provider, and
606 Fused Location Provider. They will each be searched for
607 service components implementing these providers.
608 The location framework also has support for installation
609 of new location providers at run-time. The new package does not
610 have to be explicitly listed here, however it must have a signature
611 that matches the signature of at least one package on this list.
612 */
613 Resources resources = mContext.getResources();
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800614 ArrayList<String> providerPackageNames = new ArrayList<>();
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500615 String[] pkgs = resources.getStringArray(
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700616 com.android.internal.R.array.config_locationProviderPackageNames);
Jeff Hamiltonfbadb692012-10-05 14:21:58 -0500617 if (D) Log.d(TAG, "certificates for location providers pulled from: " +
618 Arrays.toString(pkgs));
619 if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs));
620
621 ensureFallbackFusedProviderPresentLocked(providerPackageNames);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700622
623 // bind to network provider
624 LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind(
625 mContext,
626 LocationManager.NETWORK_PROVIDER,
627 NETWORK_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700628 com.android.internal.R.bool.config_enableNetworkLocationOverlay,
629 com.android.internal.R.string.config_networkLocationProviderPackageName,
630 com.android.internal.R.array.config_locationProviderPackageNames,
631 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700632 if (networkProvider != null) {
633 mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProvider);
634 mProxyProviders.add(networkProvider);
635 addProviderLocked(networkProvider);
636 } else {
637 Slog.w(TAG, "no network location provider found");
638 }
639
640 // bind to fused provider
641 LocationProviderProxy fusedLocationProvider = LocationProviderProxy.createAndBind(
642 mContext,
643 LocationManager.FUSED_PROVIDER,
644 FUSED_LOCATION_SERVICE_ACTION,
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700645 com.android.internal.R.bool.config_enableFusedLocationOverlay,
646 com.android.internal.R.string.config_fusedLocationProviderPackageName,
647 com.android.internal.R.array.config_locationProviderPackageNames,
648 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700649 if (fusedLocationProvider != null) {
650 addProviderLocked(fusedLocationProvider);
651 mProxyProviders.add(fusedLocationProvider);
652 mEnabledProviders.add(fusedLocationProvider.getName());
Kenny Rootc3575182012-10-09 12:44:40 -0700653 mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedLocationProvider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700654 } else {
655 Slog.e(TAG, "no fused location provider found",
656 new IllegalStateException("Location service needs a fused location provider"));
657 }
658
659 // bind to geocoder provider
Zhentao Sunc5fc9982013-04-17 17:47:53 -0700660 mGeocodeProvider = GeocoderProxy.createAndBind(mContext,
661 com.android.internal.R.bool.config_enableGeocoderOverlay,
662 com.android.internal.R.string.config_geocoderProviderPackageName,
663 com.android.internal.R.array.config_locationProviderPackageNames,
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800664 mLocationHandler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700665 if (mGeocodeProvider == null) {
666 Slog.e(TAG, "no geocoder provider found");
667 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700668
destradaaa4fa3b52014-07-09 10:46:39 -0700669 // bind to fused hardware provider if supported
destradaabeea4422014-07-30 18:17:21 -0700670 // in devices without support, requesting an instance of FlpHardwareProvider will raise an
671 // exception, so make sure we only do that when supported
672 FlpHardwareProvider flpHardwareProvider;
destradaa5ce66d82014-05-28 18:24:08 -0700673 if (FlpHardwareProvider.isSupported()) {
destradaabeea4422014-07-30 18:17:21 -0700674 flpHardwareProvider = FlpHardwareProvider.getInstance(mContext);
destradaaf9a274c2014-07-25 15:11:56 -0700675 FusedProxy fusedProxy = FusedProxy.createAndBind(
676 mContext,
677 mLocationHandler,
678 flpHardwareProvider.getLocationHardware(),
679 com.android.internal.R.bool.config_enableHardwareFlpOverlay,
680 com.android.internal.R.string.config_hardwareFlpPackageName,
681 com.android.internal.R.array.config_locationProviderPackageNames);
682 if (fusedProxy == null) {
destradaa6b4893a2016-05-03 15:33:43 -0700683 Slog.d(TAG, "Unable to bind FusedProxy.");
destradaaf9a274c2014-07-25 15:11:56 -0700684 }
destradaacfbdcd22014-04-30 11:29:11 -0700685 } else {
destradaabeea4422014-07-30 18:17:21 -0700686 flpHardwareProvider = null;
destradaa6b4893a2016-05-03 15:33:43 -0700687 Slog.d(TAG, "FLP HAL not supported");
destradaaf9a274c2014-07-25 15:11:56 -0700688 }
689
690 // bind to geofence provider
691 GeofenceProxy provider = GeofenceProxy.createAndBind(
692 mContext,com.android.internal.R.bool.config_enableGeofenceOverlay,
693 com.android.internal.R.string.config_geofenceProviderPackageName,
694 com.android.internal.R.array.config_locationProviderPackageNames,
695 mLocationHandler,
Wei Liu5241a4c2015-05-11 14:00:36 -0700696 mGpsGeofenceProxy,
destradaabeea4422014-07-30 18:17:21 -0700697 flpHardwareProvider != null ? flpHardwareProvider.getGeofenceHardware() : null);
destradaaf9a274c2014-07-25 15:11:56 -0700698 if (provider == null) {
destradaa6b4893a2016-05-03 15:33:43 -0700699 Slog.d(TAG, "Unable to bind FLP Geofence proxy.");
destradaa0682809a2013-08-12 18:50:30 -0700700 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900701
destradaa6e2fe752015-06-23 17:25:53 -0700702 // bind to hardware activity recognition
703 boolean activityRecognitionHardwareIsSupported = ActivityRecognitionHardware.isSupported();
704 ActivityRecognitionHardware activityRecognitionHardware = null;
705 if (activityRecognitionHardwareIsSupported) {
706 activityRecognitionHardware = ActivityRecognitionHardware.getInstance(mContext);
destradaaa4fa3b52014-07-09 10:46:39 -0700707 } else {
destradaa6b4893a2016-05-03 15:33:43 -0700708 Slog.d(TAG, "Hardware Activity-Recognition not supported.");
destradaaa4fa3b52014-07-09 10:46:39 -0700709 }
destradaa6e2fe752015-06-23 17:25:53 -0700710 ActivityRecognitionProxy proxy = ActivityRecognitionProxy.createAndBind(
711 mContext,
712 mLocationHandler,
713 activityRecognitionHardwareIsSupported,
714 activityRecognitionHardware,
715 com.android.internal.R.bool.config_enableActivityRecognitionHardwareOverlay,
716 com.android.internal.R.string.config_activityRecognitionHardwarePackageName,
717 com.android.internal.R.array.config_locationProviderPackageNames);
718 if (proxy == null) {
destradaa6b4893a2016-05-03 15:33:43 -0700719 Slog.d(TAG, "Unable to bind ActivityRecognitionProxy.");
destradaa6e2fe752015-06-23 17:25:53 -0700720 }
destradaaa4fa3b52014-07-09 10:46:39 -0700721
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +0900722 String[] testProviderStrings = resources.getStringArray(
723 com.android.internal.R.array.config_testLocationProviders);
724 for (String testProviderString : testProviderStrings) {
725 String fragments[] = testProviderString.split(",");
726 String name = fragments[0].trim();
727 if (mProvidersByName.get(name) != null) {
728 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
729 }
730 ProviderProperties properties = new ProviderProperties(
731 Boolean.parseBoolean(fragments[1]) /* requiresNetwork */,
732 Boolean.parseBoolean(fragments[2]) /* requiresSatellite */,
733 Boolean.parseBoolean(fragments[3]) /* requiresCell */,
734 Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */,
735 Boolean.parseBoolean(fragments[5]) /* supportsAltitude */,
736 Boolean.parseBoolean(fragments[6]) /* supportsSpeed */,
737 Boolean.parseBoolean(fragments[7]) /* supportsBearing */,
738 Integer.parseInt(fragments[8]) /* powerRequirement */,
739 Integer.parseInt(fragments[9]) /* accuracy */);
740 addTestProviderLocked(name, properties);
741 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700742 }
Mike Lockwood9637d472009-04-02 21:41:57 -0700743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 /**
Victoria Lease38389b62012-09-30 11:44:22 -0700745 * Called when the device's active user changes.
746 * @param userId the new active user's UserId
747 */
748 private void switchUser(int userId) {
Jianzheng Zhoud5c69462013-10-10 14:02:09 +0800749 if (mCurrentUserId == userId) {
750 return;
751 }
Victoria Lease83762d22012-10-03 13:51:17 -0700752 mBlacklist.switchUser(userId);
Victoria Lease03cdd3d2013-02-01 15:15:54 -0800753 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED);
Victoria Lease38389b62012-09-30 11:44:22 -0700754 synchronized (mLock) {
Victoria Leaseb711d572012-10-02 13:14:11 -0700755 mLastLocation.clear();
David Christie1b9b7b12013-04-15 15:31:11 -0700756 mLastLocationCoarseInterval.clear();
Victoria Leaseb711d572012-10-02 13:14:11 -0700757 for (LocationProviderInterface p : mProviders) {
Amith Yamasanib27528d2014-06-05 15:02:10 -0700758 updateProviderListenersLocked(p.getName(), false);
Victoria Leaseb711d572012-10-02 13:14:11 -0700759 }
Victoria Lease38389b62012-09-30 11:44:22 -0700760 mCurrentUserId = userId;
Amith Yamasanib27528d2014-06-05 15:02:10 -0700761 updateUserProfiles(userId);
Victoria Leaseb711d572012-10-02 13:14:11 -0700762 updateProvidersLocked();
Victoria Lease38389b62012-09-30 11:44:22 -0700763 }
764 }
765
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800766 private static final class Identity {
767 final int mUid;
768 final int mPid;
769 final String mPackageName;
770
771 Identity(int uid, int pid, String packageName) {
772 mUid = uid;
773 mPid = pid;
774 mPackageName = packageName;
775 }
776 }
777
Victoria Lease38389b62012-09-30 11:44:22 -0700778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 * A wrapper class holding either an ILocationListener or a PendingIntent to receive
780 * location updates.
781 */
Mike Lockwood48f17512009-04-23 09:12:08 -0700782 private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800783 final Identity mIdentity;
Victoria Lease37425c32012-10-16 16:08:48 -0700784 final int mAllowedResolutionLevel; // resolution level allowed to receiver
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 final ILocationListener mListener;
787 final PendingIntent mPendingIntent;
David Christie82edc9b2013-07-19 11:31:42 -0700788 final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller.
David Christie40e57822013-07-30 11:36:48 -0700789 final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 final Object mKey;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700791
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800792 final HashMap<String,UpdateRecord> mUpdateRecords = new HashMap<>();
Nick Pellyf1be6862012-05-15 10:53:42 -0700793
David Christie0b837452013-07-29 16:02:13 -0700794 // True if app ops has started monitoring this receiver for locations.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700795 boolean mOpMonitoring;
David Christie0b837452013-07-29 16:02:13 -0700796 // True if app ops has started monitoring this receiver for high power (gps) locations.
797 boolean mOpHighPowerMonitoring;
Mike Lockwood48f17512009-04-23 09:12:08 -0700798 int mPendingBroadcasts;
Victoria Lease0aa28602013-05-29 15:28:26 -0700799 PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700801 Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -0700802 String packageName, WorkSource workSource, boolean hideFromAppOps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 mListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 mPendingIntent = intent;
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700805 if (listener != null) {
806 mKey = listener.asBinder();
807 } else {
808 mKey = intent;
809 }
Victoria Lease37425c32012-10-16 16:08:48 -0700810 mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid);
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800811 mIdentity = new Identity(uid, pid, packageName);
David Christie82edc9b2013-07-19 11:31:42 -0700812 if (workSource != null && workSource.size() <= 0) {
813 workSource = null;
814 }
815 mWorkSource = workSource;
David Christie40e57822013-07-30 11:36:48 -0700816 mHideFromAppOps = hideFromAppOps;
Victoria Lease0aa28602013-05-29 15:28:26 -0700817
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700818 updateMonitoring(true);
819
Victoria Lease0aa28602013-05-29 15:28:26 -0700820 // construct/configure wakelock
821 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
David Christie82edc9b2013-07-19 11:31:42 -0700822 if (workSource == null) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800823 workSource = new WorkSource(mIdentity.mUid, mIdentity.mPackageName);
David Christie82edc9b2013-07-19 11:31:42 -0700824 }
825 mWakeLock.setWorkSource(workSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827
828 @Override
829 public boolean equals(Object otherObj) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -0800830 return (otherObj instanceof Receiver) && mKey.equals(((Receiver) otherObj).mKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832
833 @Override
834 public int hashCode() {
835 return mKey.hashCode();
836 }
Mike Lockwood3681f262009-05-12 10:52:03 -0400837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 @Override
839 public String toString() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700840 StringBuilder s = new StringBuilder();
841 s.append("Reciever[");
842 s.append(Integer.toHexString(System.identityHashCode(this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 if (mListener != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700844 s.append(" listener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 } else {
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700846 s.append(" intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700848 for (String p : mUpdateRecords.keySet()) {
849 s.append(" ").append(mUpdateRecords.get(p).toString());
850 }
851 s.append("]");
852 return s.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
854
David Christie15b31912013-08-13 15:54:32 -0700855 /**
856 * Update AppOp monitoring for this receiver.
857 *
858 * @param allow If true receiver is currently active, if false it's been removed.
859 */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700860 public void updateMonitoring(boolean allow) {
David Christie40e57822013-07-30 11:36:48 -0700861 if (mHideFromAppOps) {
862 return;
863 }
864
David Christie15b31912013-08-13 15:54:32 -0700865 boolean requestingLocation = false;
866 boolean requestingHighPowerLocation = false;
867 if (allow) {
868 // See if receiver has any enabled update records. Also note if any update records
869 // are high power (has a high power provider with an interval under a threshold).
870 for (UpdateRecord updateRecord : mUpdateRecords.values()) {
871 if (isAllowedByCurrentUserSettingsLocked(updateRecord.mProvider)) {
872 requestingLocation = true;
873 LocationProviderInterface locationProvider
David Christie2ff96af2014-01-30 16:09:37 -0800874 = mProvidersByName.get(updateRecord.mProvider);
David Christie15b31912013-08-13 15:54:32 -0700875 ProviderProperties properties = locationProvider != null
876 ? locationProvider.getProperties() : null;
877 if (properties != null
878 && properties.mPowerRequirement == Criteria.POWER_HIGH
879 && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) {
880 requestingHighPowerLocation = true;
881 break;
882 }
883 }
884 }
885 }
886
David Christie0b837452013-07-29 16:02:13 -0700887 // First update monitoring of any location request (including high power).
David Christie15b31912013-08-13 15:54:32 -0700888 mOpMonitoring = updateMonitoring(
889 requestingLocation,
890 mOpMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700891 AppOpsManager.OP_MONITOR_LOCATION);
892
893 // Now update monitoring of high power requests only.
David Christiec750c1f2013-08-08 12:56:57 -0700894 boolean wasHighPowerMonitoring = mOpHighPowerMonitoring;
David Christie15b31912013-08-13 15:54:32 -0700895 mOpHighPowerMonitoring = updateMonitoring(
896 requestingHighPowerLocation,
897 mOpHighPowerMonitoring,
David Christie0b837452013-07-29 16:02:13 -0700898 AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION);
David Christiec750c1f2013-08-08 12:56:57 -0700899 if (mOpHighPowerMonitoring != wasHighPowerMonitoring) {
David Christie15b31912013-08-13 15:54:32 -0700900 // Send an intent to notify that a high power request has been added/removed.
David Christiec750c1f2013-08-08 12:56:57 -0700901 Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
902 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
903 }
David Christie0b837452013-07-29 16:02:13 -0700904 }
905
906 /**
907 * Update AppOps monitoring for a single location request and op type.
908 *
909 * @param allowMonitoring True if monitoring is allowed for this request/op.
910 * @param currentlyMonitoring True if AppOps is currently monitoring this request/op.
911 * @param op AppOps code for the op to update.
912 * @return True if monitoring is on for this request/op after updating.
913 */
914 private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring,
915 int op) {
916 if (!currentlyMonitoring) {
917 if (allowMonitoring) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800918 return mAppOps.startOpNoThrow(op, mIdentity.mUid, mIdentity.mPackageName)
David Christie0b837452013-07-29 16:02:13 -0700919 == AppOpsManager.MODE_ALLOWED;
920 }
921 } else {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800922 if (!allowMonitoring
923 || mAppOps.checkOpNoThrow(op, mIdentity.mUid, mIdentity.mPackageName)
David Christie0b837452013-07-29 16:02:13 -0700924 != AppOpsManager.MODE_ALLOWED) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -0800925 mAppOps.finishOp(op, mIdentity.mUid, mIdentity.mPackageName);
David Christie0b837452013-07-29 16:02:13 -0700926 return false;
927 }
928 }
929
930 return currentlyMonitoring;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700931 }
932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 public boolean isListener() {
934 return mListener != null;
935 }
936
937 public boolean isPendingIntent() {
938 return mPendingIntent != null;
939 }
940
941 public ILocationListener getListener() {
942 if (mListener != null) {
943 return mListener;
944 }
945 throw new IllegalStateException("Request for non-existent listener");
946 }
947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 public boolean callStatusChangedLocked(String provider, int status, Bundle extras) {
949 if (mListener != null) {
950 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700951 synchronized (this) {
952 // synchronize to ensure incrementPendingBroadcastsLocked()
953 // is called before decrementPendingBroadcasts()
954 mListener.onStatusChanged(provider, status, extras);
Nick Pellye0fd6932012-07-11 10:26:13 -0700955 // call this after broadcasting so we do not increment
956 // if we throw an exeption.
957 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 } catch (RemoteException e) {
960 return false;
961 }
962 } else {
963 Intent statusChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800964 statusChanged.putExtras(new Bundle(extras));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status);
966 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700967 synchronized (this) {
968 // synchronize to ensure incrementPendingBroadcastsLocked()
969 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700970 mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -0700971 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -0700972 // call this after broadcasting so we do not increment
973 // if we throw an exeption.
974 incrementPendingBroadcastsLocked();
975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 } catch (PendingIntent.CanceledException e) {
977 return false;
978 }
979 }
980 return true;
981 }
982
983 public boolean callLocationChangedLocked(Location location) {
984 if (mListener != null) {
985 try {
Mike Lockwood48f17512009-04-23 09:12:08 -0700986 synchronized (this) {
987 // synchronize to ensure incrementPendingBroadcastsLocked()
988 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800989 mListener.onLocationChanged(new Location(location));
Nick Pellye0fd6932012-07-11 10:26:13 -0700990 // call this after broadcasting so we do not increment
991 // if we throw an exeption.
992 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -0700993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 } catch (RemoteException e) {
995 return false;
996 }
997 } else {
998 Intent locationChanged = new Intent();
Victoria Lease61ecb022012-11-13 15:12:51 -0800999 locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, new Location(location));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 try {
Mike Lockwood48f17512009-04-23 09:12:08 -07001001 synchronized (this) {
1002 // synchronize to ensure incrementPendingBroadcastsLocked()
1003 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001004 mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -07001005 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -07001006 // call this after broadcasting so we do not increment
1007 // if we throw an exeption.
1008 incrementPendingBroadcastsLocked();
1009 }
1010 } catch (PendingIntent.CanceledException e) {
1011 return false;
1012 }
1013 }
1014 return true;
1015 }
1016
1017 public boolean callProviderEnabledLocked(String provider, boolean enabled) {
David Christie15b31912013-08-13 15:54:32 -07001018 // First update AppOp monitoring.
1019 // An app may get/lose location access as providers are enabled/disabled.
1020 updateMonitoring(true);
1021
Mike Lockwood48f17512009-04-23 09:12:08 -07001022 if (mListener != null) {
1023 try {
1024 synchronized (this) {
1025 // synchronize to ensure incrementPendingBroadcastsLocked()
1026 // is called before decrementPendingBroadcasts()
1027 if (enabled) {
1028 mListener.onProviderEnabled(provider);
1029 } else {
1030 mListener.onProviderDisabled(provider);
1031 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001032 // call this after broadcasting so we do not increment
1033 // if we throw an exeption.
1034 incrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -07001035 }
1036 } catch (RemoteException e) {
1037 return false;
1038 }
1039 } else {
1040 Intent providerIntent = new Intent();
1041 providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled);
1042 try {
1043 synchronized (this) {
1044 // synchronize to ensure incrementPendingBroadcastsLocked()
1045 // is called before decrementPendingBroadcasts()
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001046 mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler,
Victoria Lease37425c32012-10-16 16:08:48 -07001047 getResolutionPermission(mAllowedResolutionLevel));
Mike Lockwood48f17512009-04-23 09:12:08 -07001048 // call this after broadcasting so we do not increment
1049 // if we throw an exeption.
1050 incrementPendingBroadcastsLocked();
1051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 } catch (PendingIntent.CanceledException e) {
1053 return false;
1054 }
1055 }
1056 return true;
1057 }
1058
Nick Pellyf1be6862012-05-15 10:53:42 -07001059 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 public void binderDied() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001061 if (D) Log.d(TAG, "Location listener died");
1062
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001063 synchronized (mLock) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 removeUpdatesLocked(this);
1065 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001066 synchronized (this) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001067 clearPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -07001068 }
1069 }
1070
Nick Pellye0fd6932012-07-11 10:26:13 -07001071 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -07001072 public void onSendFinished(PendingIntent pendingIntent, Intent intent,
1073 int resultCode, String resultData, Bundle resultExtras) {
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001074 synchronized (this) {
1075 decrementPendingBroadcastsLocked();
Mike Lockwood48f17512009-04-23 09:12:08 -07001076 }
1077 }
1078
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001079 // this must be called while synchronized by caller in a synchronized block
1080 // containing the sending of the broadcaset
1081 private void incrementPendingBroadcastsLocked() {
1082 if (mPendingBroadcasts++ == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001083 mWakeLock.acquire();
Mike Lockwood0528b9b2009-05-07 10:12:54 -04001084 }
1085 }
1086
1087 private void decrementPendingBroadcastsLocked() {
1088 if (--mPendingBroadcasts == 0) {
Victoria Lease0aa28602013-05-29 15:28:26 -07001089 if (mWakeLock.isHeld()) {
1090 mWakeLock.release();
1091 }
1092 }
1093 }
1094
1095 public void clearPendingBroadcastsLocked() {
1096 if (mPendingBroadcasts > 0) {
1097 mPendingBroadcasts = 0;
1098 if (mWakeLock.isHeld()) {
1099 mWakeLock.release();
1100 }
Mike Lockwood48f17512009-04-23 09:12:08 -07001101 }
1102 }
1103 }
1104
Nick Pellye0fd6932012-07-11 10:26:13 -07001105 @Override
Mike Lockwood48f17512009-04-23 09:12:08 -07001106 public void locationCallbackFinished(ILocationListener listener) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001107 //Do not use getReceiverLocked here as that will add the ILocationListener to
Joshua Bartel080b61b2009-10-05 12:44:46 -04001108 //the receiver list if it is not found. If it is not found then the
1109 //LocationListener was removed when it had a pending broadcast and should
1110 //not be added back.
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001111 synchronized (mLock) {
1112 IBinder binder = listener.asBinder();
1113 Receiver receiver = mReceivers.get(binder);
1114 if (receiver != null) {
1115 synchronized (receiver) {
1116 // so wakelock calls will succeed
1117 long identity = Binder.clearCallingIdentity();
1118 receiver.decrementPendingBroadcastsLocked();
1119 Binder.restoreCallingIdentity(identity);
David Christie2ff96af2014-01-30 16:09:37 -08001120 }
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123 }
1124
Lifu Tang82f893d2016-01-21 18:15:33 -08001125 /**
Lifu Tang9363b942016-02-16 18:07:00 -08001126 * Returns the system information of the GNSS hardware.
Lifu Tang82f893d2016-01-21 18:15:33 -08001127 */
1128 @Override
Lifu Tang9363b942016-02-16 18:07:00 -08001129 public int getGnssYearOfHardware() {
Wyatt Rileycf879db2017-01-12 13:57:38 -08001130 if (mGnssSystemInfoProvider != null) {
Lifu Tang9363b942016-02-16 18:07:00 -08001131 return mGnssSystemInfoProvider.getGnssYearOfHardware();
Lifu Tang82f893d2016-01-21 18:15:33 -08001132 } else {
1133 return 0;
1134 }
1135 }
1136
Wyatt Rileycf879db2017-01-12 13:57:38 -08001137 /**
1138 * Runs some checks for GNSS (FINE) level permissions, used by several methods which directly
1139 * (try to) access GNSS information at this layer.
1140 */
1141 private boolean hasGnssPermissions(String packageName) {
1142 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
1143 checkResolutionLevelIsSufficientForProviderUse(
1144 allowedResolutionLevel,
1145 LocationManager.GPS_PROVIDER);
1146
1147 int pid = Binder.getCallingPid();
1148 int uid = Binder.getCallingUid();
1149 long identity = Binder.clearCallingIdentity();
1150 boolean hasLocationAccess;
1151 try {
1152 hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
1153 } finally {
1154 Binder.restoreCallingIdentity(identity);
1155 }
1156
1157 return hasLocationAccess;
1158 }
1159
1160 /**
1161 * Returns the GNSS batching size, if available.
1162 */
1163 @Override
1164 public int getGnssBatchSize(String packageName) {
1165 mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE,
1166 "Location Hardware permission not granted to access hardware batching");
1167
1168 if (hasGnssPermissions(packageName) && mGnssBatchingProvider != null) {
1169 return mGnssBatchingProvider.getSize();
1170 } else {
1171 return 0;
1172 }
1173 }
1174
1175 /**
1176 * Adds a callback for GNSS Batching events, if permissions allow, which are transported
1177 * to potentially multiple listeners by the BatchedLocationCallbackTransport above this.
1178 */
1179 @Override
1180 public boolean addGnssBatchingCallback(IBatchedLocationCallback callback, String packageName) {
1181 mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE,
1182 "Location Hardware permission not granted to access hardware batching");
1183
1184 if (!hasGnssPermissions(packageName) || mGnssBatchingProvider == null) {
1185 return false;
1186 }
1187
1188 mGnssBatchingCallback = callback;
1189 mGnssBatchingDeathCallback = new LinkedCallback(callback);
1190 try {
1191 callback.asBinder().linkToDeath(mGnssBatchingDeathCallback, 0 /* flags */);
1192 } catch (RemoteException e) {
1193 // if the remote process registering the listener is already dead, just swallow the
1194 // exception and return
1195 Log.e(TAG, "Remote listener already died.", e);
1196 return false;
1197 }
1198
1199 return true;
1200 }
1201
1202 private class LinkedCallback implements IBinder.DeathRecipient {
1203 private final IBatchedLocationCallback mCallback;
1204
1205 public LinkedCallback(@NonNull IBatchedLocationCallback callback) {
1206 mCallback = callback;
1207 }
1208
1209 @NonNull
1210 public IBatchedLocationCallback getUnderlyingListener() {
1211 return mCallback;
1212 }
1213
1214 @Override
1215 public void binderDied() {
1216 Log.d(TAG, "Remote Batching Callback died: " + mCallback);
1217 stopGnssBatch();
1218 removeGnssBatchingCallback();
1219 }
1220 }
1221
1222 /**
1223 * Removes callback for GNSS batching
1224 */
1225 @Override
1226 public void removeGnssBatchingCallback() {
1227 try {
1228 mGnssBatchingCallback.asBinder().unlinkToDeath(mGnssBatchingDeathCallback,
1229 0 /* flags */);
1230 } catch (NoSuchElementException e) {
1231 // if the death callback isn't connected (it should be...), log error, swallow the
1232 // exception and return
1233 Log.e(TAG, "Couldn't unlink death callback.", e);
1234 }
1235 mGnssBatchingCallback = null;
1236 mGnssBatchingDeathCallback = null;
1237 }
1238
1239
1240 /**
1241 * Starts GNSS batching, if available.
1242 */
1243 @Override
1244 public boolean startGnssBatch(long periodNanos, boolean wakeOnFifoFull, String packageName) {
1245 mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE,
1246 "Location Hardware permission not granted to access hardware batching");
1247
1248 if (!hasGnssPermissions(packageName) || mGnssBatchingProvider == null) {
1249 return false;
1250 }
1251
1252 if (mGnssBatchingInProgress) {
1253 // Current design does not expect multiple starts to be called repeatedly
1254 Log.e(TAG, "startGnssBatch unexpectedly called w/o stopping prior batch");
1255 // Try to clean up anyway, and continue
1256 stopGnssBatch();
1257 }
1258
1259 mGnssBatchingInProgress = true;
1260 return mGnssBatchingProvider.start(periodNanos, wakeOnFifoFull);
1261 }
1262
1263 /**
1264 * Flushes a GNSS batch in progress
1265 */
1266 @Override
1267 public void flushGnssBatch(String packageName) {
1268 mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE,
1269 "Location Hardware permission not granted to access hardware batching");
1270
1271 if (!hasGnssPermissions(packageName)) {
1272 Log.e(TAG, "flushGnssBatch called without GNSS permissions");
1273 return;
1274 }
1275
1276 if (!mGnssBatchingInProgress) {
1277 Log.w(TAG, "flushGnssBatch called with no batch in progress");
1278 }
1279
1280 if (mGnssBatchingProvider != null) {
1281 mGnssBatchingProvider.flush();
1282 }
1283 }
1284
1285 /**
1286 * Stops GNSS batching
1287 */
1288 @Override
1289 public boolean stopGnssBatch() {
1290 mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE,
1291 "Location Hardware permission not granted to access hardware batching");
1292
1293 if (mGnssBatchingProvider != null) {
1294 mGnssBatchingInProgress = false;
1295 return mGnssBatchingProvider.stop();
1296 } else {
1297 return false;
1298 }
1299 }
1300
1301 @Override
1302 public void reportLocationBatch(List<Location> locations) {
1303 checkCallerIsProvider();
1304
1305 // Currently used only for GNSS locations - update permissions check if changed
1306 if (isAllowedByCurrentUserSettingsLocked(LocationManager.GPS_PROVIDER)) {
1307 if (mGnssBatchingCallback == null) {
1308 Slog.e(TAG, "reportLocationBatch() called without active Callback");
1309 return;
1310 }
1311 try {
1312 mGnssBatchingCallback.onLocationBatch(locations);
1313 } catch (RemoteException e) {
1314 Slog.e(TAG, "mGnssBatchingCallback.onLocationBatch failed", e);
1315 }
1316 } else {
1317 Slog.w(TAG, "reportLocationBatch() called without user permission, locations blocked");
1318 }
1319 }
1320
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001321 private void addProviderLocked(LocationProviderInterface provider) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001322 mProviders.add(provider);
1323 mProvidersByName.put(provider.getName(), provider);
1324 }
1325
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001326 private void removeProviderLocked(LocationProviderInterface provider) {
1327 provider.disable();
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001328 mProviders.remove(provider);
1329 mProvidersByName.remove(provider.getName());
1330 }
1331
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001332 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001333 * Returns "true" if access to the specified location provider is allowed by the current
1334 * user's settings. Access to all location providers is forbidden to non-location-provider
1335 * processes belonging to background users.
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001336 *
1337 * @param provider the name of the location provider
Victoria Lease03cdd3d2013-02-01 15:15:54 -08001338 */
Victoria Lease09eeaec2013-02-05 11:34:13 -08001339 private boolean isAllowedByCurrentUserSettingsLocked(String provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 if (mEnabledProviders.contains(provider)) {
1341 return true;
1342 }
1343 if (mDisabledProviders.contains(provider)) {
1344 return false;
1345 }
1346 // Use system settings
1347 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348
Victoria Leaseb711d572012-10-02 13:14:11 -07001349 return Settings.Secure.isLocationProviderEnabledForUser(resolver, provider, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001352 /**
Victoria Lease09eeaec2013-02-05 11:34:13 -08001353 * Returns "true" if access to the specified location provider is allowed by the specified
1354 * user's settings. Access to all location providers is forbidden to non-location-provider
1355 * processes belonging to background users.
1356 *
1357 * @param provider the name of the location provider
1358 * @param uid the requestor's UID
Victoria Lease09eeaec2013-02-05 11:34:13 -08001359 */
1360 private boolean isAllowedByUserSettingsLocked(String provider, int uid) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001361 if (!isCurrentProfile(UserHandle.getUserId(uid)) && !isUidALocationProvider(uid)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001362 return false;
1363 }
1364 return isAllowedByCurrentUserSettingsLocked(provider);
1365 }
1366
1367 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001368 * Returns the permission string associated with the specified resolution level.
1369 *
1370 * @param resolutionLevel the resolution level
1371 * @return the permission string
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001372 */
Victoria Lease37425c32012-10-16 16:08:48 -07001373 private String getResolutionPermission(int resolutionLevel) {
1374 switch (resolutionLevel) {
1375 case RESOLUTION_LEVEL_FINE:
1376 return android.Manifest.permission.ACCESS_FINE_LOCATION;
1377 case RESOLUTION_LEVEL_COARSE:
1378 return android.Manifest.permission.ACCESS_COARSE_LOCATION;
1379 default:
1380 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
Victoria Leaseda479c52012-10-15 15:24:16 -07001382 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001383
Victoria Leaseda479c52012-10-15 15:24:16 -07001384 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001385 * Returns the resolution level allowed to the given PID/UID pair.
1386 *
1387 * @param pid the PID
1388 * @param uid the UID
1389 * @return resolution level allowed to the pid/uid pair
Victoria Leaseda479c52012-10-15 15:24:16 -07001390 */
Victoria Lease37425c32012-10-16 16:08:48 -07001391 private int getAllowedResolutionLevel(int pid, int uid) {
1392 if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
1393 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1394 return RESOLUTION_LEVEL_FINE;
1395 } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION,
1396 pid, uid) == PackageManager.PERMISSION_GRANTED) {
1397 return RESOLUTION_LEVEL_COARSE;
1398 } else {
1399 return RESOLUTION_LEVEL_NONE;
Victoria Leaseda479c52012-10-15 15:24:16 -07001400 }
Victoria Lease4fab68b2012-09-13 13:20:59 -07001401 }
1402
1403 /**
Victoria Lease37425c32012-10-16 16:08:48 -07001404 * Returns the resolution level allowed to the caller
1405 *
1406 * @return resolution level allowed to caller
Victoria Lease4fab68b2012-09-13 13:20:59 -07001407 */
Victoria Lease37425c32012-10-16 16:08:48 -07001408 private int getCallerAllowedResolutionLevel() {
1409 return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid());
1410 }
1411
1412 /**
1413 * Throw SecurityException if specified resolution level is insufficient to use geofences.
1414 *
1415 * @param allowedResolutionLevel resolution level allowed to caller
1416 */
1417 private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) {
1418 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Lease4fab68b2012-09-13 13:20:59 -07001419 throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
1420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422
Victoria Lease37425c32012-10-16 16:08:48 -07001423 /**
1424 * Return the minimum resolution level required to use the specified location provider.
1425 *
1426 * @param provider the name of the location provider
1427 * @return minimum resolution level required for provider
1428 */
1429 private int getMinimumResolutionLevelForProviderUse(String provider) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001430 if (LocationManager.GPS_PROVIDER.equals(provider) ||
1431 LocationManager.PASSIVE_PROVIDER.equals(provider)) {
1432 // gps and passive providers require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001433 return RESOLUTION_LEVEL_FINE;
Victoria Lease8dbb6342012-09-21 16:55:53 -07001434 } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
1435 LocationManager.FUSED_PROVIDER.equals(provider)) {
1436 // network and fused providers are ok with COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001437 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001438 } else {
1439 // mock providers
1440 LocationProviderInterface lp = mMockProviders.get(provider);
1441 if (lp != null) {
1442 ProviderProperties properties = lp.getProperties();
1443 if (properties != null) {
1444 if (properties.mRequiresSatellite) {
1445 // provider requiring satellites require FINE permission
Victoria Lease37425c32012-10-16 16:08:48 -07001446 return RESOLUTION_LEVEL_FINE;
Laurent Tu941221c2012-10-04 14:21:52 -07001447 } else if (properties.mRequiresNetwork || properties.mRequiresCell) {
1448 // provider requiring network and or cell require COARSE or FINE
Victoria Lease37425c32012-10-16 16:08:48 -07001449 return RESOLUTION_LEVEL_COARSE;
Laurent Tu941221c2012-10-04 14:21:52 -07001450 }
1451 }
1452 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001453 }
Victoria Lease37425c32012-10-16 16:08:48 -07001454 return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE
Victoria Leaseda479c52012-10-15 15:24:16 -07001455 }
1456
Victoria Lease37425c32012-10-16 16:08:48 -07001457 /**
1458 * Throw SecurityException if specified resolution level is insufficient to use the named
1459 * location provider.
1460 *
1461 * @param allowedResolutionLevel resolution level allowed to caller
1462 * @param providerName the name of the location provider
1463 */
1464 private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel,
1465 String providerName) {
1466 int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName);
1467 if (allowedResolutionLevel < requiredResolutionLevel) {
1468 switch (requiredResolutionLevel) {
1469 case RESOLUTION_LEVEL_FINE:
1470 throw new SecurityException("\"" + providerName + "\" location provider " +
1471 "requires ACCESS_FINE_LOCATION permission.");
1472 case RESOLUTION_LEVEL_COARSE:
1473 throw new SecurityException("\"" + providerName + "\" location provider " +
1474 "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.");
1475 default:
1476 throw new SecurityException("Insufficient permission for \"" + providerName +
1477 "\" location provider.");
Victoria Leaseda479c52012-10-15 15:24:16 -07001478 }
1479 }
Victoria Lease8dbb6342012-09-21 16:55:53 -07001480 }
1481
David Christie82edc9b2013-07-19 11:31:42 -07001482 /**
1483 * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages
1484 * for battery).
1485 */
David Christie40e57822013-07-30 11:36:48 -07001486 private void checkDeviceStatsAllowed() {
David Christie82edc9b2013-07-19 11:31:42 -07001487 mContext.enforceCallingOrSelfPermission(
1488 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
1489 }
1490
David Christie40e57822013-07-30 11:36:48 -07001491 private void checkUpdateAppOpsAllowed() {
1492 mContext.enforceCallingOrSelfPermission(
1493 android.Manifest.permission.UPDATE_APP_OPS_STATS, null);
1494 }
1495
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001496 public static int resolutionLevelToOp(int allowedResolutionLevel) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001497 if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
1498 if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001499 return AppOpsManager.OP_COARSE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001500 } else {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001501 return AppOpsManager.OP_FINE_LOCATION;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001502 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001503 }
1504 return -1;
1505 }
1506
David Christieb870dbf2015-06-22 12:42:53 -07001507 boolean reportLocationAccessNoThrow(
1508 int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001509 int op = resolutionLevelToOp(allowedResolutionLevel);
1510 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001511 if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1512 return false;
1513 }
1514 }
David Christieb870dbf2015-06-22 12:42:53 -07001515
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001516 return getAllowedResolutionLevel(pid, uid) >= allowedResolutionLevel;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001517 }
1518
David Christieb870dbf2015-06-22 12:42:53 -07001519 boolean checkLocationAccess(int pid, int uid, String packageName, int allowedResolutionLevel) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001520 int op = resolutionLevelToOp(allowedResolutionLevel);
1521 if (op >= 0) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001522 if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
1523 return false;
1524 }
1525 }
David Christieb870dbf2015-06-22 12:42:53 -07001526
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001527 return getAllowedResolutionLevel(pid, uid) >= allowedResolutionLevel;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001528 }
1529
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001530 /**
1531 * Returns all providers by name, including passive, but excluding
Laurent Tu0d21e212012-10-02 15:33:48 -07001532 * fused, also including ones that are not permitted to
1533 * be accessed by the calling activity or are currently disabled.
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001534 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public List<String> getAllProviders() {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001537 ArrayList<String> out;
1538 synchronized (mLock) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001539 out = new ArrayList<>(mProviders.size());
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001540 for (LocationProviderInterface provider : mProviders) {
1541 String name = provider.getName();
1542 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07001543 continue;
1544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 out.add(name);
1546 }
1547 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001548
1549 if (D) Log.d(TAG, "getAllProviders()=" + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 return out;
1551 }
1552
Mike Lockwood03ca2162010-04-01 08:10:09 -07001553 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001554 * Return all providers by name, that match criteria and are optionally
1555 * enabled.
1556 * Can return passive provider, but never returns fused provider.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001557 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001558 @Override
1559 public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
Victoria Lease37425c32012-10-16 16:08:48 -07001560 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001561 ArrayList<String> out;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001562 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07001563 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07001564 try {
1565 synchronized (mLock) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001566 out = new ArrayList<>(mProviders.size());
Victoria Leaseb711d572012-10-02 13:14:11 -07001567 for (LocationProviderInterface provider : mProviders) {
1568 String name = provider.getName();
1569 if (LocationManager.FUSED_PROVIDER.equals(name)) {
Victoria Lease8dbb6342012-09-21 16:55:53 -07001570 continue;
1571 }
Victoria Lease37425c32012-10-16 16:08:48 -07001572 if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08001573 if (enabledOnly && !isAllowedByUserSettingsLocked(name, uid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001574 continue;
1575 }
1576 if (criteria != null && !LocationProvider.propertiesMeetCriteria(
1577 name, provider.getProperties(), criteria)) {
1578 continue;
1579 }
1580 out.add(name);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001581 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001582 }
Mike Lockwood03ca2162010-04-01 08:10:09 -07001583 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001584 } finally {
1585 Binder.restoreCallingIdentity(identity);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001586 }
1587
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001588 if (D) Log.d(TAG, "getProviders()=" + out);
1589 return out;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001590 }
1591
1592 /**
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001593 * Return the name of the best provider given a Criteria object.
1594 * This method has been deprecated from the public API,
Victoria Lease8dbb6342012-09-21 16:55:53 -07001595 * and the whole LocationProvider (including #meetsCriteria)
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001596 * has been deprecated as well. So this method now uses
1597 * some simplified logic.
Mike Lockwood03ca2162010-04-01 08:10:09 -07001598 */
Nick Pellye0fd6932012-07-11 10:26:13 -07001599 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001600 public String getBestProvider(Criteria criteria, boolean enabledOnly) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001601 String result = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001602
1603 List<String> providers = getProviders(criteria, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001604 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001605 result = pickBest(providers);
1606 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1607 return result;
1608 }
1609 providers = getProviders(null, enabledOnly);
Victoria Lease8dbb6342012-09-21 16:55:53 -07001610 if (!providers.isEmpty()) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001611 result = pickBest(providers);
1612 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
1613 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001614 }
1615
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001616 if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
Mike Lockwood03ca2162010-04-01 08:10:09 -07001617 return null;
1618 }
1619
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001620 private String pickBest(List<String> providers) {
Victoria Lease1925e292012-09-24 17:00:18 -07001621 if (providers.contains(LocationManager.GPS_PROVIDER)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001622 return LocationManager.GPS_PROVIDER;
Victoria Lease1925e292012-09-24 17:00:18 -07001623 } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
1624 return LocationManager.NETWORK_PROVIDER;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001625 } else {
1626 return providers.get(0);
1627 }
1628 }
1629
Nick Pellye0fd6932012-07-11 10:26:13 -07001630 @Override
Mike Lockwood03ca2162010-04-01 08:10:09 -07001631 public boolean providerMeetsCriteria(String provider, Criteria criteria) {
1632 LocationProviderInterface p = mProvidersByName.get(provider);
1633 if (p == null) {
1634 throw new IllegalArgumentException("provider=" + provider);
1635 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001636
1637 boolean result = LocationProvider.propertiesMeetCriteria(
1638 p.getName(), p.getProperties(), criteria);
1639 if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result);
1640 return result;
Mike Lockwood03ca2162010-04-01 08:10:09 -07001641 }
1642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 private void updateProvidersLocked() {
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001644 boolean changesMade = false;
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04001645 for (int i = mProviders.size() - 1; i >= 0; i--) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001646 LocationProviderInterface p = mProviders.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 boolean isEnabled = p.isEnabled();
1648 String name = p.getName();
Victoria Lease09eeaec2013-02-05 11:34:13 -08001649 boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 if (isEnabled && !shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001651 updateProviderListenersLocked(name, false);
David Christieb084fef2013-12-18 14:33:57 -08001652 // If any provider has been disabled, clear all last locations for all providers.
1653 // This is to be on the safe side in case a provider has location derived from
1654 // this disabled provider.
1655 mLastLocation.clear();
1656 mLastLocationCoarseInterval.clear();
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001657 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } else if (!isEnabled && shouldBeEnabled) {
Amith Yamasanib27528d2014-06-05 15:02:10 -07001659 updateProviderListenersLocked(name, true);
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001660 changesMade = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
Brad Fitzpatrick0c5a0402010-08-27 14:01:23 -07001662 }
1663 if (changesMade) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001664 mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
1665 UserHandle.ALL);
Tom O'Neill40a86c22013-09-03 18:05:13 -07001666 mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
1667 UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
1669 }
1670
Amith Yamasanib27528d2014-06-05 15:02:10 -07001671 private void updateProviderListenersLocked(String provider, boolean enabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 int listeners = 0;
1673
Mike Lockwoodd03ff942010-02-09 08:46:14 -05001674 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001675 if (p == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676
1677 ArrayList<Receiver> deadReceivers = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07001678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1680 if (records != null) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001681 for (UpdateRecord record : records) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001682 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001683 // Sends a notification message to the receiver
1684 if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
1685 if (deadReceivers == null) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001686 deadReceivers = new ArrayList<>();
Victoria Leaseb711d572012-10-02 13:14:11 -07001687 }
1688 deadReceivers.add(record.mReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001690 listeners++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693 }
1694
1695 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001696 for (int i = deadReceivers.size() - 1; i >= 0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 removeUpdatesLocked(deadReceivers.get(i));
1698 }
1699 }
Nick Pellye0fd6932012-07-11 10:26:13 -07001700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 if (enabled) {
1702 p.enable();
1703 if (listeners > 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001704 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 p.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 }
1710
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001711 private void applyRequirementsLocked(String provider) {
1712 LocationProviderInterface p = mProvidersByName.get(provider);
1713 if (p == null) return;
1714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001716 WorkSource worksource = new WorkSource();
1717 ProviderRequest providerRequest = new ProviderRequest();
1718
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001719 ContentResolver resolver = mContext.getContentResolver();
1720 long backgroundThrottleInterval = Settings.Global.getLong(
1721 resolver,
1722 Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS,
1723 DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS);
1724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 if (records != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001726 for (UpdateRecord record : records) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001727 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) {
David Christieb870dbf2015-06-22 12:42:53 -07001728 if (checkLocationAccess(
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001729 record.mReceiver.mIdentity.mPid,
1730 record.mReceiver.mIdentity.mUid,
1731 record.mReceiver.mIdentity.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001732 record.mReceiver.mAllowedResolutionLevel)) {
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07001733 LocationRequest locationRequest = record.mRealRequest;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001734 long interval = locationRequest.getInterval();
1735
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001736 if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001737 if (!record.mIsForegroundUid) {
1738 interval = Math.max(interval, backgroundThrottleInterval);
1739 }
1740 if (interval != locationRequest.getInterval()) {
1741 locationRequest = new LocationRequest(locationRequest);
1742 locationRequest.setInterval(interval);
1743 }
1744 }
1745
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07001746 record.mRequest = locationRequest;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001747 providerRequest.locationRequests.add(locationRequest);
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001748 if (interval < providerRequest.interval) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001749 providerRequest.reportLocation = true;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001750 providerRequest.interval = interval;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001751 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001752 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001753 }
1754 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001755
1756 if (providerRequest.reportLocation) {
1757 // calculate who to blame for power
1758 // This is somewhat arbitrary. We pick a threshold interval
1759 // that is slightly higher that the minimum interval, and
1760 // spread the blame across all applications with a request
1761 // under that threshold.
1762 long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
1763 for (UpdateRecord record : records) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001764 if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) {
Victoria Leaseb711d572012-10-02 13:14:11 -07001765 LocationRequest locationRequest = record.mRequest;
Svet Ganove998c732016-06-10 00:12:38 -07001766
1767 // Don't assign battery blame for update records whose
1768 // client has no permission to receive location data.
1769 if (!providerRequest.locationRequests.contains(locationRequest)) {
1770 continue;
1771 }
1772
Victoria Leaseb711d572012-10-02 13:14:11 -07001773 if (locationRequest.getInterval() <= thresholdInterval) {
David Christiee55c9682013-08-22 10:10:34 -07001774 if (record.mReceiver.mWorkSource != null
1775 && record.mReceiver.mWorkSource.size() > 0
1776 && record.mReceiver.mWorkSource.getName(0) != null) {
David Christie82edc9b2013-07-19 11:31:42 -07001777 // Assign blame to another work source.
David Christiee55c9682013-08-22 10:10:34 -07001778 // Can only assign blame if the WorkSource contains names.
David Christie82edc9b2013-07-19 11:31:42 -07001779 worksource.add(record.mReceiver.mWorkSource);
1780 } else {
1781 // Assign blame to caller.
1782 worksource.add(
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001783 record.mReceiver.mIdentity.mUid,
1784 record.mReceiver.mIdentity.mPackageName);
David Christie82edc9b2013-07-19 11:31:42 -07001785 }
Victoria Leaseb711d572012-10-02 13:14:11 -07001786 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001787 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001791
1792 if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
1793 p.setRequest(providerRequest, worksource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 }
1795
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001796 @Override
1797 public String[] getBackgroundThrottlingWhitelist() {
1798 synchronized (mLock) {
1799 return mBackgroundThrottlePackageWhitelist.toArray(
1800 new String[mBackgroundThrottlePackageWhitelist.size()]);
1801 }
1802 }
1803
1804 private void updateBackgroundThrottlingWhitelistLocked() {
Soonil Nagarkar2b565df2017-02-14 13:33:23 -08001805 String setting = Settings.Global.getString(
1806 mContext.getContentResolver(),
1807 Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST);
1808 if (setting == null) {
1809 setting = "";
1810 }
1811
1812 mBackgroundThrottlePackageWhitelist.clear();
1813 mBackgroundThrottlePackageWhitelist.addAll(
1814 SystemConfig.getInstance().getAllowUnthrottledLocation());
1815 mBackgroundThrottlePackageWhitelist.addAll(
1816 Arrays.asList(setting.split(",")));
1817 }
1818
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001819 private boolean isThrottlingExemptLocked(Identity identity) {
1820 if (identity.mUid == Process.SYSTEM_UID) {
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -08001821 return true;
1822 }
1823
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001824 if (mBackgroundThrottlePackageWhitelist.contains(identity.mPackageName)) {
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -08001825 return true;
1826 }
1827
1828 for (LocationProviderProxy provider : mProxyProviders) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001829 if (identity.mPackageName.equals(provider.getConnectedPackageName())) {
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -08001830 return true;
1831 }
1832 }
1833
Soonil Nagarkar2f1f7e82017-01-24 12:52:10 -08001834 return false;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001835 }
1836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 private class UpdateRecord {
1838 final String mProvider;
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07001839 final LocationRequest mRealRequest; // original request from client
1840 LocationRequest mRequest; // possibly throttled version of the request
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 final Receiver mReceiver;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001842 boolean mIsForegroundUid;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001843 Location mLastFixBroadcast;
1844 long mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845
1846 /**
1847 * Note: must be constructed with lock held.
1848 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001849 UpdateRecord(String provider, LocationRequest request, Receiver receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 mProvider = provider;
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07001851 mRealRequest = request;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001852 mRequest = request;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 mReceiver = receiver;
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001854 mIsForegroundUid = isImportanceForeground(
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001855 mActivityManager.getPackageImportance(mReceiver.mIdentity.mPackageName));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856
1857 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
1858 if (records == null) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001859 records = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 mRecordsByProvider.put(provider, records);
1861 }
1862 if (!records.contains(this)) {
1863 records.add(this);
1864 }
David Christie2ff96af2014-01-30 16:09:37 -08001865
1866 // Update statistics for historical location requests by package/provider
1867 mRequestStatistics.startRequesting(
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001868 mReceiver.mIdentity.mPackageName, provider, request.getInterval());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870
1871 /**
David Christie2ff96af2014-01-30 16:09:37 -08001872 * Method to be called when a record will no longer be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001874 void disposeLocked(boolean removeReceiver) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001875 mRequestStatistics.stopRequesting(mReceiver.mIdentity.mPackageName, mProvider);
David Christie2ff96af2014-01-30 16:09:37 -08001876
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001877 // remove from mRecordsByProvider
1878 ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider);
1879 if (globalRecords != null) {
1880 globalRecords.remove(this);
1881 }
1882
1883 if (!removeReceiver) return; // the caller will handle the rest
1884
1885 // remove from Receiver#mUpdateRecords
1886 HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords;
1887 if (receiverRecords != null) {
1888 receiverRecords.remove(this.mProvider);
1889
1890 // and also remove the Receiver if it has no more update records
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08001891 if (receiverRecords.size() == 0) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001892 removeUpdatesLocked(mReceiver);
1893 }
Mike Lockwood3a76fd62009-09-01 07:26:56 -04001894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
1896
1897 @Override
1898 public String toString() {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08001899 return "UpdateRecord[" + mProvider + " " + mReceiver.mIdentity.mPackageName
1900 + "(" + mReceiver.mIdentity.mUid + (mIsForegroundUid ? " foreground" : " background")
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07001901 + ")" + " " + mRealRequest + "]";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
1904
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001905 private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
David Christie40e57822013-07-30 11:36:48 -07001906 String packageName, WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001907 IBinder binder = listener.asBinder();
1908 Receiver receiver = mReceivers.get(binder);
1909 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001910 receiver = new Receiver(listener, null, pid, uid, packageName, workSource,
1911 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001912 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001913 receiver.getListener().asBinder().linkToDeath(receiver, 0);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001914 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001915 Slog.e(TAG, "linkToDeath failed:", e);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001916 return null;
1917 }
Wen Jingcb3ab222014-03-27 13:42:59 +08001918 mReceivers.put(binder, receiver);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001919 }
1920 return receiver;
1921 }
1922
David Christie82edc9b2013-07-19 11:31:42 -07001923 private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName,
David Christie40e57822013-07-30 11:36:48 -07001924 WorkSource workSource, boolean hideFromAppOps) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001925 Receiver receiver = mReceivers.get(intent);
1926 if (receiver == null) {
David Christie40e57822013-07-30 11:36:48 -07001927 receiver = new Receiver(null, intent, pid, uid, packageName, workSource,
1928 hideFromAppOps);
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001929 mReceivers.put(intent, receiver);
1930 }
1931 return receiver;
1932 }
1933
Victoria Lease37425c32012-10-16 16:08:48 -07001934 /**
1935 * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution
1936 * and consistency requirements.
1937 *
1938 * @param request the LocationRequest from which to create a sanitized version
Victoria Lease37425c32012-10-16 16:08:48 -07001939 * @return a version of request that meets the given resolution and consistency requirements
1940 * @hide
1941 */
1942 private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel) {
1943 LocationRequest sanitizedRequest = new LocationRequest(request);
1944 if (resolutionLevel < RESOLUTION_LEVEL_FINE) {
1945 switch (sanitizedRequest.getQuality()) {
Victoria Lease09016ab2012-09-16 12:33:15 -07001946 case LocationRequest.ACCURACY_FINE:
Victoria Lease37425c32012-10-16 16:08:48 -07001947 sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK);
Victoria Lease09016ab2012-09-16 12:33:15 -07001948 break;
1949 case LocationRequest.POWER_HIGH:
Victoria Lease37425c32012-10-16 16:08:48 -07001950 sanitizedRequest.setQuality(LocationRequest.POWER_LOW);
Victoria Lease09016ab2012-09-16 12:33:15 -07001951 break;
1952 }
1953 // throttle
Victoria Lease37425c32012-10-16 16:08:48 -07001954 if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1955 sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001956 }
Victoria Lease37425c32012-10-16 16:08:48 -07001957 if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) {
1958 sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS);
Victoria Lease09016ab2012-09-16 12:33:15 -07001959 }
Nick Pelly74fa7ea2012-08-13 19:36:38 -07001960 }
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001961 // make getFastestInterval() the minimum of interval and fastest interval
Victoria Lease37425c32012-10-16 16:08:48 -07001962 if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07001963 request.setFastestInterval(request.getInterval());
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001964 }
Victoria Lease37425c32012-10-16 16:08:48 -07001965 return sanitizedRequest;
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04001966 }
1967
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001968 private void checkPackageName(String packageName) {
Nick Pellye0fd6932012-07-11 10:26:13 -07001969 if (packageName == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001970 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001971 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001972 int uid = Binder.getCallingUid();
Nick Pellye0fd6932012-07-11 10:26:13 -07001973 String[] packages = mPackageManager.getPackagesForUid(uid);
1974 if (packages == null) {
1975 throw new SecurityException("invalid UID " + uid);
1976 }
1977 for (String pkg : packages) {
1978 if (packageName.equals(pkg)) return;
1979 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001980 throw new SecurityException("invalid package name: " + packageName);
Nick Pellye0fd6932012-07-11 10:26:13 -07001981 }
1982
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001983 private void checkPendingIntent(PendingIntent intent) {
1984 if (intent == null) {
1985 throw new IllegalArgumentException("invalid pending intent: " + intent);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001986 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001987 }
1988
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07001989 private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent,
David Christie40e57822013-07-30 11:36:48 -07001990 int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001991 if (intent == null && listener == null) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001992 throw new IllegalArgumentException("need either listener or intent");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001993 } else if (intent != null && listener != null) {
1994 throw new IllegalArgumentException("cannot register both listener and intent");
1995 } else if (intent != null) {
1996 checkPendingIntent(intent);
David Christie40e57822013-07-30 11:36:48 -07001997 return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07001998 } else {
David Christie40e57822013-07-30 11:36:48 -07001999 return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002000 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -07002001 }
2002
Nick Pellye0fd6932012-07-11 10:26:13 -07002003 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002004 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
2005 PendingIntent intent, String packageName) {
2006 if (request == null) request = DEFAULT_LOCATION_REQUEST;
2007 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07002008 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
2009 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
2010 request.getProvider());
David Christie82edc9b2013-07-19 11:31:42 -07002011 WorkSource workSource = request.getWorkSource();
2012 if (workSource != null && workSource.size() > 0) {
David Christie40e57822013-07-30 11:36:48 -07002013 checkDeviceStatsAllowed();
2014 }
2015 boolean hideFromAppOps = request.getHideFromAppOps();
2016 if (hideFromAppOps) {
2017 checkUpdateAppOpsAllowed();
David Christie82edc9b2013-07-19 11:31:42 -07002018 }
Victoria Lease37425c32012-10-16 16:08:48 -07002019 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002021 final int pid = Binder.getCallingPid();
2022 final int uid = Binder.getCallingUid();
Nick Pelly2b7a0d02012-08-17 15:09:44 -07002023 // providers may use public location API's, need to clear identity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 long identity = Binder.clearCallingIdentity();
2025 try {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002026 // We don't check for MODE_IGNORED here; we will do that when we go to deliver
2027 // a location.
David Christieb870dbf2015-06-22 12:42:53 -07002028 checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002029
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002030 synchronized (mLock) {
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07002031 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid,
David Christie40e57822013-07-30 11:36:48 -07002032 packageName, workSource, hideFromAppOps);
Victoria Lease37425c32012-10-16 16:08:48 -07002033 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
Mike Lockwood2d4d1bf2010-10-18 17:06:26 -04002034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 } finally {
2036 Binder.restoreCallingIdentity(identity);
2037 }
2038 }
2039
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002040 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
2041 int pid, int uid, String packageName) {
2042 // Figure out the provider. Either its explicitly request (legacy use cases), or
2043 // use the fused provider
2044 if (request == null) request = DEFAULT_LOCATION_REQUEST;
2045 String name = request.getProvider();
Victoria Lease09016ab2012-09-16 12:33:15 -07002046 if (name == null) {
2047 throw new IllegalArgumentException("provider name must not be null");
2048 }
Zhentao Sunc5fc9982013-04-17 17:47:53 -07002049
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002050 LocationProviderInterface provider = mProvidersByName.get(name);
2051 if (provider == null) {
Victoria Leaseb30f3832013-10-13 12:15:40 -07002052 throw new IllegalArgumentException("provider doesn't exist: " + name);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002053 }
2054
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002055 UpdateRecord record = new UpdateRecord(name, request, receiver);
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002056 if (D) Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
2057 + " " + name + " " + request + " from " + packageName + "(" + uid + " "
2058 + (record.mIsForegroundUid ? "foreground" : "background")
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002059 + (isThrottlingExemptLocked(receiver.mIdentity)
2060 ? " [whitelisted]" : "") + ")");
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002061
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002062 UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
2063 if (oldRecord != null) {
2064 oldRecord.disposeLocked(false);
2065 }
2066
Victoria Lease09eeaec2013-02-05 11:34:13 -08002067 boolean isProviderEnabled = isAllowedByUserSettingsLocked(name, uid);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002068 if (isProviderEnabled) {
2069 applyRequirementsLocked(name);
2070 } else {
2071 // Notify the listener that updates are currently disabled
2072 receiver.callProviderEnabledLocked(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
David Christie0b837452013-07-29 16:02:13 -07002074 // Update the monitoring here just in case multiple location requests were added to the
2075 // same receiver (this request may be high power and the initial might not have been).
2076 receiver.updateMonitoring(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078
Nick Pellye0fd6932012-07-11 10:26:13 -07002079 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002080 public void removeUpdates(ILocationListener listener, PendingIntent intent,
2081 String packageName) {
2082 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07002083
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002084 final int pid = Binder.getCallingPid();
2085 final int uid = Binder.getCallingUid();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002086
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07002087 synchronized (mLock) {
David Christie82edc9b2013-07-19 11:31:42 -07002088 WorkSource workSource = null;
David Christie40e57822013-07-30 11:36:48 -07002089 boolean hideFromAppOps = false;
2090 Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid,
2091 packageName, workSource, hideFromAppOps);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07002092
2093 // providers may use public location API's, need to clear identity
2094 long identity = Binder.clearCallingIdentity();
2095 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002096 removeUpdatesLocked(receiver);
Dianne Hackbornf5fdca92013-06-05 14:53:33 -07002097 } finally {
2098 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101 }
2102
2103 private void removeUpdatesLocked(Receiver receiver) {
Dianne Hackborn7ff30112012-11-08 11:12:09 -08002104 if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver)));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002105
2106 if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
2107 receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
2108 synchronized (receiver) {
Victoria Lease0aa28602013-05-29 15:28:26 -07002109 receiver.clearPendingBroadcastsLocked();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07002113 receiver.updateMonitoring(false);
2114
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002115 // Record which providers were associated with this listener
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002116 HashSet<String> providers = new HashSet<>();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002117 HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords;
2118 if (oldRecords != null) {
2119 // Call dispose() on the obsolete update records.
2120 for (UpdateRecord record : oldRecords.values()) {
David Christie2ff96af2014-01-30 16:09:37 -08002121 // Update statistics for historical location requests by package/provider
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002122 record.disposeLocked(false);
2123 }
2124 // Accumulate providers
2125 providers.addAll(oldRecords.keySet());
2126 }
2127
2128 // update provider
2129 for (String provider : providers) {
2130 // If provider is already disabled, don't need to do anything
Victoria Lease09eeaec2013-02-05 11:34:13 -08002131 if (!isAllowedByCurrentUserSettingsLocked(provider)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002132 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 }
2134
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002135 applyRequirementsLocked(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
2137 }
2138
Dianne Hackbornc2293022013-02-06 23:14:49 -08002139 private void applyAllProviderRequirementsLocked() {
2140 for (LocationProviderInterface p : mProviders) {
2141 // If provider is already disabled, don't need to do anything
Dianne Hackborn64d41d72013-02-07 00:33:31 -08002142 if (!isAllowedByCurrentUserSettingsLocked(p.getName())) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08002143 continue;
2144 }
2145
2146 applyRequirementsLocked(p.getName());
2147 }
2148 }
2149
Nick Pellye0fd6932012-07-11 10:26:13 -07002150 @Override
Nick Pelly4035f5a2012-08-17 14:43:49 -07002151 public Location getLastLocation(LocationRequest request, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002152 if (D) Log.d(TAG, "getLastLocation: " + request);
2153 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07002154 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
Nick Pelly4035f5a2012-08-17 14:43:49 -07002155 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07002156 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
2157 request.getProvider());
2158 // no need to sanitize this request, as only the provider name is used
Nick Pelly4035f5a2012-08-17 14:43:49 -07002159
David Christieb870dbf2015-06-22 12:42:53 -07002160 final int pid = Binder.getCallingPid();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002161 final int uid = Binder.getCallingUid();
2162 final long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07002163 try {
2164 if (mBlacklist.isBlacklisted(packageName)) {
2165 if (D) Log.d(TAG, "not returning last loc for blacklisted app: " +
2166 packageName);
Victoria Lease09016ab2012-09-16 12:33:15 -07002167 return null;
2168 }
Victoria Leaseb711d572012-10-02 13:14:11 -07002169
David Christieb870dbf2015-06-22 12:42:53 -07002170 if (!reportLocationAccessNoThrow(pid, uid, packageName, allowedResolutionLevel)) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002171 if (D) Log.d(TAG, "not returning last loc for no op app: " +
2172 packageName);
2173 return null;
2174 }
2175
Victoria Leaseb711d572012-10-02 13:14:11 -07002176 synchronized (mLock) {
2177 // Figure out the provider. Either its explicitly request (deprecated API's),
2178 // or use the fused provider
2179 String name = request.getProvider();
2180 if (name == null) name = LocationManager.FUSED_PROVIDER;
2181 LocationProviderInterface provider = mProvidersByName.get(name);
2182 if (provider == null) return null;
2183
Victoria Lease09eeaec2013-02-05 11:34:13 -08002184 if (!isAllowedByUserSettingsLocked(name, uid)) return null;
Victoria Leaseb711d572012-10-02 13:14:11 -07002185
David Christie1b9b7b12013-04-15 15:31:11 -07002186 Location location;
2187 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2188 // Make sure that an app with coarse permissions can't get frequent location
2189 // updates by calling LocationManager.getLastKnownLocation repeatedly.
2190 location = mLastLocationCoarseInterval.get(name);
2191 } else {
2192 location = mLastLocation.get(name);
2193 }
Victoria Leaseb711d572012-10-02 13:14:11 -07002194 if (location == null) {
2195 return null;
2196 }
Victoria Lease37425c32012-10-16 16:08:48 -07002197 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002198 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2199 if (noGPSLocation != null) {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08002200 return new Location(mLocationFudger.getOrCreate(noGPSLocation));
Victoria Leaseb711d572012-10-02 13:14:11 -07002201 }
Victoria Lease37425c32012-10-16 16:08:48 -07002202 } else {
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08002203 return new Location(location);
Victoria Lease09016ab2012-09-16 12:33:15 -07002204 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002205 }
Victoria Leaseb711d572012-10-02 13:14:11 -07002206 return null;
2207 } finally {
2208 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002209 }
2210 }
2211
2212 @Override
2213 public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
2214 String packageName) {
2215 if (request == null) request = DEFAULT_LOCATION_REQUEST;
Victoria Lease37425c32012-10-16 16:08:48 -07002216 int allowedResolutionLevel = getCallerAllowedResolutionLevel();
2217 checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002218 checkPendingIntent(intent);
2219 checkPackageName(packageName);
Victoria Lease37425c32012-10-16 16:08:48 -07002220 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
2221 request.getProvider());
2222 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002223
Victoria Lease37425c32012-10-16 16:08:48 -07002224 if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002225
Nick Pelly2b7a0d02012-08-17 15:09:44 -07002226 // geo-fence manager uses the public location API, need to clear identity
2227 int uid = Binder.getCallingUid();
Xiaohui Chena4490622015-09-22 15:29:31 -07002228 // TODO: http://b/23822629
2229 if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) {
Victoria Lease56e675b2012-11-05 19:25:06 -08002230 // temporary measure until geofences work for secondary users
2231 Log.w(TAG, "proximity alerts are currently available only to the primary user");
2232 return;
2233 }
Nick Pelly2b7a0d02012-08-17 15:09:44 -07002234 long identity = Binder.clearCallingIdentity();
2235 try {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002236 mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel,
2237 uid, packageName);
Nick Pelly2b7a0d02012-08-17 15:09:44 -07002238 } finally {
2239 Binder.restoreCallingIdentity(identity);
2240 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002241 }
2242
2243 @Override
2244 public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002245 checkPendingIntent(intent);
2246 checkPackageName(packageName);
2247
2248 if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent);
2249
Nick Pelly2b7a0d02012-08-17 15:09:44 -07002250 // geo-fence manager uses the public location API, need to clear identity
2251 long identity = Binder.clearCallingIdentity();
2252 try {
2253 mGeofenceManager.removeFence(geofence, intent);
2254 } finally {
2255 Binder.restoreCallingIdentity(identity);
2256 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002257 }
2258
2259
2260 @Override
Lifu Tang30f95a72016-01-07 23:20:38 -08002261 public boolean registerGnssStatusCallback(IGnssStatusListener callback, String packageName) {
Wyatt Rileycf879db2017-01-12 13:57:38 -08002262 if (!hasGnssPermissions(packageName) || mGnssStatusProvider == null) {
Takayuki Hoshib254ab6a2014-10-23 16:46:02 +09002263 return false;
2264 }
2265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 try {
Lifu Tang30f95a72016-01-07 23:20:38 -08002267 mGnssStatusProvider.registerGnssStatusCallback(callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 } catch (RemoteException e) {
Lifu Tang30f95a72016-01-07 23:20:38 -08002269 Slog.e(TAG, "mGpsStatusProvider.registerGnssStatusCallback failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 return false;
2271 }
2272 return true;
2273 }
2274
Nick Pellye0fd6932012-07-11 10:26:13 -07002275 @Override
Lifu Tang30f95a72016-01-07 23:20:38 -08002276 public void unregisterGnssStatusCallback(IGnssStatusListener callback) {
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002277 synchronized (mLock) {
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002278 try {
Lifu Tang30f95a72016-01-07 23:20:38 -08002279 mGnssStatusProvider.unregisterGnssStatusCallback(callback);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002280 } catch (Exception e) {
Lifu Tang30f95a72016-01-07 23:20:38 -08002281 Slog.e(TAG, "mGpsStatusProvider.unregisterGnssStatusCallback failed", e);
Mike Lockwood15e3d0f2009-05-01 07:53:28 -04002282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
2284 }
2285
Nick Pellye0fd6932012-07-11 10:26:13 -07002286 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08002287 public boolean addGnssMeasurementsListener(
2288 IGnssMeasurementsListener listener,
destradaaea8a8a62014-06-23 18:19:03 -07002289 String packageName) {
Wyatt Rileycf879db2017-01-12 13:57:38 -08002290 if (!hasGnssPermissions(packageName) || mGnssMeasurementsProvider == null) {
destradaaea8a8a62014-06-23 18:19:03 -07002291 return false;
2292 }
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002293
2294 synchronized (mLock) {
2295 Identity callerIdentity
2296 = new Identity(Binder.getCallingUid(), Binder.getCallingPid(), packageName);
2297 mGnssMeasurementsListeners.put(listener, callerIdentity);
2298 long identity = Binder.clearCallingIdentity();
2299 try {
2300 if (isThrottlingExemptLocked(callerIdentity)
2301 || isImportanceForeground(
2302 mActivityManager.getPackageImportance(packageName))) {
2303 return mGnssMeasurementsProvider.addListener(listener);
2304 }
2305 } finally {
2306 Binder.restoreCallingIdentity(identity);
2307 }
2308
2309 return true;
2310 }
destradaaea8a8a62014-06-23 18:19:03 -07002311 }
2312
2313 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08002314 public void removeGnssMeasurementsListener(IGnssMeasurementsListener listener) {
2315 if (mGnssMeasurementsProvider != null) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002316 synchronized (mLock) {
2317 mGnssMeasurementsListeners.remove(listener);
2318 mGnssMeasurementsProvider.removeListener(listener);
2319 }
Wei Liu5241a4c2015-05-11 14:00:36 -07002320 }
destradaaea8a8a62014-06-23 18:19:03 -07002321 }
2322
2323 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08002324 public boolean addGnssNavigationMessageListener(
2325 IGnssNavigationMessageListener listener,
destradaa4b3e3932014-07-21 18:01:47 -07002326 String packageName) {
Wyatt Rileycf879db2017-01-12 13:57:38 -08002327 if (!hasGnssPermissions(packageName) || mGnssNavigationMessageProvider == null) {
destradaa4b3e3932014-07-21 18:01:47 -07002328 return false;
2329 }
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002330
2331 synchronized (mLock) {
2332 Identity callerIdentity
2333 = new Identity(Binder.getCallingUid(), Binder.getCallingPid(), packageName);
2334 mGnssNavigationMessageListeners.put(listener, callerIdentity);
2335 long identity = Binder.clearCallingIdentity();
2336 try {
2337 if (isThrottlingExemptLocked(callerIdentity)
2338 || isImportanceForeground(
2339 mActivityManager.getPackageImportance(packageName))) {
2340 return mGnssNavigationMessageProvider.addListener(listener);
2341 }
2342 } finally {
2343 Binder.restoreCallingIdentity(identity);
2344 }
2345
2346 return true;
2347 }
destradaa4b3e3932014-07-21 18:01:47 -07002348 }
2349
2350 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -08002351 public void removeGnssNavigationMessageListener(IGnssNavigationMessageListener listener) {
2352 if (mGnssNavigationMessageProvider != null) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002353 synchronized (mLock) {
2354 mGnssNavigationMessageListeners.remove(listener);
2355 mGnssNavigationMessageProvider.removeListener(listener);
2356 }
Wei Liu5241a4c2015-05-11 14:00:36 -07002357 }
destradaa4b3e3932014-07-21 18:01:47 -07002358 }
2359
2360 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 public boolean sendExtraCommand(String provider, String command, Bundle extras) {
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04002362 if (provider == null) {
2363 // throw NullPointerException to remain compatible with previous implementation
2364 throw new NullPointerException();
2365 }
Victoria Lease37425c32012-10-16 16:08:48 -07002366 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
2367 provider);
Mike Lockwoodc6cc8362009-08-17 13:16:08 -04002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 // and check for ACCESS_LOCATION_EXTRA_COMMANDS
Mike Lockwoodb7e99222009-07-07 13:18:21 -04002370 if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 != PackageManager.PERMISSION_GRANTED)) {
2372 throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission");
2373 }
2374
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002375 synchronized (mLock) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002376 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002377 if (p == null) return false;
Nick Pellye0fd6932012-07-11 10:26:13 -07002378
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002379 return p.sendExtraCommand(command, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 }
2381 }
2382
Nick Pellye0fd6932012-07-11 10:26:13 -07002383 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002384 public boolean sendNiResponse(int notifId, int userResponse) {
Mike Lockwood18ad9f62009-08-27 14:01:23 -07002385 if (Binder.getCallingUid() != Process.myUid()) {
2386 throw new SecurityException(
2387 "calling sendNiResponse from outside of the system is not allowed");
2388 }
Danke Xie22d1f9f2009-08-18 18:28:45 -04002389 try {
2390 return mNetInitiatedListener.sendNiResponse(notifId, userResponse);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002391 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002392 Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse");
Danke Xie22d1f9f2009-08-18 18:28:45 -04002393 return false;
2394 }
2395 }
2396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 /**
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002398 * @return null if the provider does not exist
Alexey Tarasovf2db9fb2009-09-01 02:37:07 +11002399 * @throws SecurityException if the provider is not allowed to be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 * accessed by the caller
2401 */
Nick Pellye0fd6932012-07-11 10:26:13 -07002402 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002403 public ProviderProperties getProviderProperties(String provider) {
Laurent Tub7f9d252012-10-16 14:25:00 -07002404 if (mProvidersByName.get(provider) == null) {
David Christie2ff96af2014-01-30 16:09:37 -08002405 return null;
Laurent Tub7f9d252012-10-16 14:25:00 -07002406 }
2407
Victoria Lease37425c32012-10-16 16:08:48 -07002408 checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
2409 provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002411 LocationProviderInterface p;
2412 synchronized (mLock) {
2413 p = mProvidersByName.get(provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 }
2415
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002416 if (p == null) return null;
2417 return p.getProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
2419
Jason Monkb71218a2015-06-17 14:44:39 -04002420 /**
2421 * @return null if the provider does not exist
2422 * @throws SecurityException if the provider is not allowed to be
2423 * accessed by the caller
2424 */
2425 @Override
2426 public String getNetworkProviderPackage() {
2427 LocationProviderInterface p;
2428 synchronized (mLock) {
2429 if (mProvidersByName.get(LocationManager.NETWORK_PROVIDER) == null) {
2430 return null;
2431 }
2432 p = mProvidersByName.get(LocationManager.NETWORK_PROVIDER);
2433 }
2434
2435 if (p instanceof LocationProviderProxy) {
2436 return ((LocationProviderProxy) p).getConnectedPackageName();
2437 }
2438 return null;
2439 }
2440
Nick Pellye0fd6932012-07-11 10:26:13 -07002441 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 public boolean isProviderEnabled(String provider) {
Tom O'Neilld5759432013-09-11 11:03:03 -07002443 // Fused provider is accessed indirectly via criteria rather than the provider-based APIs,
2444 // so we discourage its use
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002445 if (LocationManager.FUSED_PROVIDER.equals(provider)) return false;
2446
Victoria Lease09eeaec2013-02-05 11:34:13 -08002447 int uid = Binder.getCallingUid();
Victoria Lease269518e2012-10-29 08:25:39 -07002448 long identity = Binder.clearCallingIdentity();
Victoria Leaseb711d572012-10-02 13:14:11 -07002449 try {
2450 synchronized (mLock) {
2451 LocationProviderInterface p = mProvidersByName.get(provider);
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002452 return p != null && isAllowedByUserSettingsLocked(provider, uid);
Victoria Leaseb711d572012-10-02 13:14:11 -07002453 }
2454 } finally {
2455 Binder.restoreCallingIdentity(identity);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002456 }
2457 }
2458
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002459 /**
2460 * Returns "true" if the UID belongs to a bound location provider.
2461 *
2462 * @param uid the uid
2463 * @return true if uid belongs to a bound location provider
2464 */
2465 private boolean isUidALocationProvider(int uid) {
2466 if (uid == Process.SYSTEM_UID) {
2467 return true;
2468 }
2469 if (mGeocodeProvider != null) {
David Christie1f141c12014-05-14 15:11:15 -07002470 if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002471 }
2472 for (LocationProviderProxy proxy : mProxyProviders) {
David Christie1f141c12014-05-14 15:11:15 -07002473 if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true;
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002474 }
2475 return false;
2476 }
2477
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002478 private void checkCallerIsProvider() {
2479 if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER)
2480 == PackageManager.PERMISSION_GRANTED) {
2481 return;
2482 }
2483
2484 // Previously we only used the INSTALL_LOCATION_PROVIDER
2485 // check. But that is system or signature
2486 // protection level which is not flexible enough for
2487 // providers installed oustide the system image. So
2488 // also allow providers with a UID matching the
2489 // currently bound package name
2490
Victoria Lease03cdd3d2013-02-01 15:15:54 -08002491 if (isUidALocationProvider(Binder.getCallingUid())) {
2492 return;
2493 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002494
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002495 throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " +
2496 "or UID of a currently bound location provider");
2497 }
2498
David Christie1f141c12014-05-14 15:11:15 -07002499 /**
2500 * Returns true if the given package belongs to the given uid.
2501 */
2502 private boolean doesUidHavePackage(int uid, String packageName) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002503 if (packageName == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 return false;
2505 }
David Christie1f141c12014-05-14 15:11:15 -07002506 String[] packageNames = mPackageManager.getPackagesForUid(uid);
2507 if (packageNames == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002508 return false;
2509 }
David Christie1f141c12014-05-14 15:11:15 -07002510 for (String name : packageNames) {
2511 if (packageName.equals(name)) {
2512 return true;
2513 }
2514 }
2515 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 }
2517
Nick Pellye0fd6932012-07-11 10:26:13 -07002518 @Override
Mike Lockwooda4903f22010-02-17 06:42:23 -05002519 public void reportLocation(Location location, boolean passive) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002520 checkCallerIsProvider();
Mike Lockwood275555c2009-05-01 11:30:34 -04002521
Nick Pelly2eeeec22012-07-18 13:13:37 -07002522 if (!location.isComplete()) {
2523 Log.w(TAG, "Dropping incomplete location: " + location);
2524 return;
2525 }
2526
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002527 mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
2528 Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
Mike Lockwooda4903f22010-02-17 06:42:23 -05002529 m.arg1 = (passive ? 1 : 0);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002530 mLocationHandler.sendMessageAtFrontOfQueue(m);
2531 }
2532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533
Laurent Tu75defb62012-11-01 16:21:52 -07002534 private static boolean shouldBroadcastSafe(
2535 Location loc, Location lastLoc, UpdateRecord record, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 // Always broadcast the first update
2537 if (lastLoc == null) {
2538 return true;
2539 }
2540
Nick Pellyf1be6862012-05-15 10:53:42 -07002541 // Check whether sufficient time has passed
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07002542 long minTime = record.mRealRequest.getFastestInterval();
David Christie1b9b7b12013-04-15 15:31:11 -07002543 long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
2544 / NANOS_PER_MILLI;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002545 if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 return false;
2547 }
2548
2549 // Check whether sufficient distance has been traveled
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07002550 double minDistance = record.mRealRequest.getSmallestDisplacement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 if (minDistance > 0.0) {
2552 if (loc.distanceTo(lastLoc) <= minDistance) {
2553 return false;
2554 }
2555 }
2556
Laurent Tu75defb62012-11-01 16:21:52 -07002557 // Check whether sufficient number of udpates is left
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07002558 if (record.mRealRequest.getNumUpdates() <= 0) {
Laurent Tu75defb62012-11-01 16:21:52 -07002559 return false;
2560 }
2561
2562 // Check whether the expiry date has passed
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07002563 return record.mRealRequest.getExpireAt() >= now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 }
2565
Mike Lockwooda4903f22010-02-17 06:42:23 -05002566 private void handleLocationChangedLocked(Location location, boolean passive) {
Nick Pelly4e31c4f2012-08-13 19:35:39 -07002567 if (D) Log.d(TAG, "incoming location: " + location);
2568
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002569 long now = SystemClock.elapsedRealtime();
Mike Lockwooda4903f22010-02-17 06:42:23 -05002570 String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571
Laurent Tu60ec50a2012-10-04 17:00:10 -07002572 // Skip if the provider is unknown.
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002573 LocationProviderInterface p = mProvidersByName.get(provider);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002574 if (p == null) return;
2575
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002576 // Update last known locations
Victoria Lease09016ab2012-09-16 12:33:15 -07002577 Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002578 Location lastNoGPSLocation;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002579 Location lastLocation = mLastLocation.get(provider);
Mike Lockwood4e50b782009-04-03 08:24:43 -07002580 if (lastLocation == null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002581 lastLocation = new Location(provider);
2582 mLastLocation.put(provider, lastLocation);
Victoria Lease09016ab2012-09-16 12:33:15 -07002583 } else {
2584 lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2585 if (noGPSLocation == null && lastNoGPSLocation != null) {
2586 // New location has no no-GPS location: adopt last no-GPS location. This is set
2587 // directly into location because we do not want to notify COARSE clients.
2588 location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation);
2589 }
Mike Lockwood4e50b782009-04-03 08:24:43 -07002590 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002591 lastLocation.set(location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592
David Christie1b9b7b12013-04-15 15:31:11 -07002593 // Update last known coarse interval location if enough time has passed.
2594 Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider);
2595 if (lastLocationCoarseInterval == null) {
2596 lastLocationCoarseInterval = new Location(location);
2597 mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval);
2598 }
2599 long timeDiffNanos = location.getElapsedRealtimeNanos()
2600 - lastLocationCoarseInterval.getElapsedRealtimeNanos();
2601 if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) {
2602 lastLocationCoarseInterval.set(location);
2603 }
2604 // Don't ever return a coarse location that is more recent than the allowed update
2605 // interval (i.e. don't allow an app to keep registering and unregistering for
2606 // location updates to overcome the minimum interval).
2607 noGPSLocation =
2608 lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION);
2609
Laurent Tu60ec50a2012-10-04 17:00:10 -07002610 // Skip if there are no UpdateRecords for this provider.
2611 ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
2612 if (records == null || records.size() == 0) return;
2613
Victoria Lease09016ab2012-09-16 12:33:15 -07002614 // Fetch coarse location
2615 Location coarseLocation = null;
David Christie1b9b7b12013-04-15 15:31:11 -07002616 if (noGPSLocation != null) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002617 coarseLocation = mLocationFudger.getOrCreate(noGPSLocation);
2618 }
2619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 // Fetch latest status update time
2621 long newStatusUpdateTime = p.getStatusUpdateTime();
2622
David Christie2ff96af2014-01-30 16:09:37 -08002623 // Get latest status
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 Bundle extras = new Bundle();
2625 int status = p.getStatus(extras);
2626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 ArrayList<Receiver> deadReceivers = null;
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002628 ArrayList<UpdateRecord> deadUpdateRecords = null;
Nick Pellye0fd6932012-07-11 10:26:13 -07002629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 // Broadcast location or status to all listeners
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002631 for (UpdateRecord r : records) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 Receiver receiver = r.mReceiver;
Mike Lockwood03ca2162010-04-01 08:10:09 -07002633 boolean receiverDead = false;
Nick Pelly4035f5a2012-08-17 14:43:49 -07002634
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002635 int receiverUserId = UserHandle.getUserId(receiver.mIdentity.mUid);
2636 if (!isCurrentProfile(receiverUserId)
2637 && !isUidALocationProvider(receiver.mIdentity.mUid)) {
Victoria Leaseb711d572012-10-02 13:14:11 -07002638 if (D) {
Victoria Lease269518e2012-10-29 08:25:39 -07002639 Log.d(TAG, "skipping loc update for background user " + receiverUserId +
Victoria Leaseb711d572012-10-02 13:14:11 -07002640 " (current user: " + mCurrentUserId + ", app: " +
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002641 receiver.mIdentity.mPackageName + ")");
Victoria Leaseb711d572012-10-02 13:14:11 -07002642 }
2643 continue;
2644 }
2645
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002646 if (mBlacklist.isBlacklisted(receiver.mIdentity.mPackageName)) {
Nick Pelly4035f5a2012-08-17 14:43:49 -07002647 if (D) Log.d(TAG, "skipping loc update for blacklisted app: " +
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002648 receiver.mIdentity.mPackageName);
Nick Pelly4035f5a2012-08-17 14:43:49 -07002649 continue;
2650 }
2651
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002652 if (!reportLocationAccessNoThrow(
2653 receiver.mIdentity.mPid,
2654 receiver.mIdentity.mUid,
2655 receiver.mIdentity.mPackageName,
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002656 receiver.mAllowedResolutionLevel)) {
2657 if (D) Log.d(TAG, "skipping loc update for no op app: " +
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002658 receiver.mIdentity.mPackageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002659 continue;
2660 }
2661
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002662 Location notifyLocation;
Victoria Lease37425c32012-10-16 16:08:48 -07002663 if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
2664 notifyLocation = coarseLocation; // use coarse location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002665 } else {
Victoria Lease37425c32012-10-16 16:08:48 -07002666 notifyLocation = lastLocation; // use fine location
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002667 }
Victoria Lease09016ab2012-09-16 12:33:15 -07002668 if (notifyLocation != null) {
2669 Location lastLoc = r.mLastFixBroadcast;
Laurent Tu75defb62012-11-01 16:21:52 -07002670 if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) {
Victoria Lease09016ab2012-09-16 12:33:15 -07002671 if (lastLoc == null) {
2672 lastLoc = new Location(notifyLocation);
2673 r.mLastFixBroadcast = lastLoc;
2674 } else {
2675 lastLoc.set(notifyLocation);
2676 }
2677 if (!receiver.callLocationChangedLocked(notifyLocation)) {
2678 Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver);
2679 receiverDead = true;
2680 }
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07002681 r.mRealRequest.decrementNumUpdates();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 }
2683 }
2684
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002685 long prevStatusUpdateTime = r.mLastStatusBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 if ((newStatusUpdateTime > prevStatusUpdateTime) &&
Victoria Lease09016ab2012-09-16 12:33:15 -07002687 (prevStatusUpdateTime != 0 || status != LocationProvider.AVAILABLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002689 r.mLastStatusBroadcast = newStatusUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 if (!receiver.callStatusChangedLocked(provider, status, extras)) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002691 receiverDead = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002692 Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver);
Mike Lockwood03ca2162010-04-01 08:10:09 -07002693 }
2694 }
2695
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002696 // track expired records
Soonil Nagarkard4def0c2017-05-23 15:54:55 -07002697 if (r.mRealRequest.getNumUpdates() <= 0 || r.mRealRequest.getExpireAt() < now) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002698 if (deadUpdateRecords == null) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002699 deadUpdateRecords = new ArrayList<>();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002700 }
2701 deadUpdateRecords.add(r);
2702 }
2703 // track dead receivers
2704 if (receiverDead) {
Mike Lockwood03ca2162010-04-01 08:10:09 -07002705 if (deadReceivers == null) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002706 deadReceivers = new ArrayList<>();
Mike Lockwood03ca2162010-04-01 08:10:09 -07002707 }
2708 if (!deadReceivers.contains(receiver)) {
2709 deadReceivers.add(receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 }
2711 }
2712 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002713
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002714 // remove dead records and receivers outside the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 if (deadReceivers != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002716 for (Receiver receiver : deadReceivers) {
2717 removeUpdatesLocked(receiver);
2718 }
2719 }
2720 if (deadUpdateRecords != null) {
2721 for (UpdateRecord r : deadUpdateRecords) {
2722 r.disposeLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
Victoria Lease8b38b292012-12-04 15:04:43 -08002724 applyRequirementsLocked(provider);
2725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
2727
2728 private class LocationWorkerHandler extends Handler {
Victoria Lease5cd731a2012-12-19 15:04:21 -08002729 public LocationWorkerHandler(Looper looper) {
2730 super(looper, null, true);
2731 }
2732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 @Override
2734 public void handleMessage(Message msg) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002735 switch (msg.what) {
2736 case MSG_LOCATION_CHANGED:
2737 handleLocationChanged((Location) msg.obj, msg.arg1 == 1);
2738 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740 }
2741 }
2742
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002743 private boolean isMockProvider(String provider) {
2744 synchronized (mLock) {
2745 return mMockProviders.containsKey(provider);
2746 }
2747 }
2748
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002749 private void handleLocationChanged(Location location, boolean passive) {
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002750 // create a working copy of the incoming Location so that the service can modify it without
2751 // disturbing the caller's copy
2752 Location myLocation = new Location(location);
2753 String provider = myLocation.getProvider();
2754
2755 // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
2756 // bit if location did not come from a mock provider because passive/fused providers can
2757 // forward locations from mock providers, and should not grant them legitimacy in doing so.
2758 if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
2759 myLocation.setIsFromMockProvider(true);
2760 }
Jeff Sharkey5e613312012-01-30 11:16:20 -08002761
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002762 synchronized (mLock) {
Victoria Lease09eeaec2013-02-05 11:34:13 -08002763 if (isAllowedByCurrentUserSettingsLocked(provider)) {
2764 if (!passive) {
2765 // notify passive provider of the new location
2766 mPassiveProvider.updateLocation(myLocation);
2767 }
Victoria Lease54ca7ae2013-01-08 09:39:50 -08002768 handleLocationChangedLocked(myLocation, passive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772
Mike Lockwoode97ae402010-09-29 15:23:46 -04002773 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
2774 @Override
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002775 public void onPackageDisappeared(String packageName, int reason) {
2776 // remove all receivers associated with this package name
2777 synchronized (mLock) {
2778 ArrayList<Receiver> deadReceivers = null;
2779
2780 for (Receiver receiver : mReceivers.values()) {
Soonil Nagarkar681d7112017-02-23 17:14:16 -08002781 if (receiver.mIdentity.mPackageName.equals(packageName)) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002782 if (deadReceivers == null) {
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08002783 deadReceivers = new ArrayList<>();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002784 }
2785 deadReceivers.add(receiver);
2786 }
2787 }
2788
2789 // perform removal outside of mReceivers loop
2790 if (deadReceivers != null) {
2791 for (Receiver receiver : deadReceivers) {
2792 removeUpdatesLocked(receiver);
2793 }
2794 }
2795 }
Nick Pellye0fd6932012-07-11 10:26:13 -07002796 }
Mike Lockwoode97ae402010-09-29 15:23:46 -04002797 };
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 // Geocoder
2800
Nick Pellye0fd6932012-07-11 10:26:13 -07002801 @Override
Mike Lockwoode15735a2010-09-20 17:48:47 -04002802 public boolean geocoderIsPresent() {
Mark Vandevoorde01ac80b2010-05-21 15:43:26 -07002803 return mGeocodeProvider != null;
2804 }
2805
Nick Pellye0fd6932012-07-11 10:26:13 -07002806 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 public String getFromLocation(double latitude, double longitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002808 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002809 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002810 return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults,
2811 params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002813 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 }
2815
Mike Lockwooda55c3212009-04-15 11:10:11 -04002816
Nick Pellye0fd6932012-07-11 10:26:13 -07002817 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 public String getFromLocationName(String locationName,
Mike Lockwooda55c3212009-04-15 11:10:11 -04002819 double lowerLeftLatitude, double lowerLeftLongitude,
2820 double upperRightLatitude, double upperRightLongitude, int maxResults,
Mike Lockwood34901402010-01-04 12:14:21 -05002821 GeocoderParams params, List<Address> addrs) {
Mike Lockwooda55c3212009-04-15 11:10:11 -04002822
2823 if (mGeocodeProvider != null) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -05002824 return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude,
2825 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
2826 maxResults, params, addrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
Mike Lockwooda55c3212009-04-15 11:10:11 -04002828 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 }
2830
2831 // Mock Providers
2832
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002833 private boolean canCallerAccessMockLocation(String opPackageName) {
2834 return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
2835 opPackageName) == AppOpsManager.MODE_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 }
2837
Nick Pellye0fd6932012-07-11 10:26:13 -07002838 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002839 public void addTestProvider(String name, ProviderProperties properties, String opPackageName) {
2840 if (!canCallerAccessMockLocation(opPackageName)) {
2841 return;
2842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Mike Lockwooda4903f22010-02-17 06:42:23 -05002844 if (LocationManager.PASSIVE_PROVIDER.equals(name)) {
2845 throw new IllegalArgumentException("Cannot mock the passive location provider");
2846 }
2847
Mike Lockwood86328a92009-10-23 08:38:25 -04002848 long identity = Binder.clearCallingIdentity();
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002849 synchronized (mLock) {
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002850 // remove the real provider if we are replacing GPS or network provider
2851 if (LocationManager.GPS_PROVIDER.equals(name)
Nick Pelly1332b532012-08-21 16:25:47 -07002852 || LocationManager.NETWORK_PROVIDER.equals(name)
2853 || LocationManager.FUSED_PROVIDER.equals(name)) {
Mike Lockwoodd03ff942010-02-09 08:46:14 -05002854 LocationProviderInterface p = mProvidersByName.get(name);
2855 if (p != null) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002856 removeProviderLocked(p);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002857 }
2858 }
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002859 addTestProviderLocked(name, properties);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 updateProvidersLocked();
2861 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002862 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 }
2864
Ji-Hwan Lee26bdb8f2014-04-21 20:48:19 +09002865 private void addTestProviderLocked(String name, ProviderProperties properties) {
2866 if (mProvidersByName.get(name) != null) {
2867 throw new IllegalArgumentException("Provider \"" + name + "\" already exists");
2868 }
2869 MockProvider provider = new MockProvider(name, this, properties);
2870 addProviderLocked(provider);
2871 mMockProviders.put(name, provider);
2872 mLastLocation.put(name, null);
2873 mLastLocationCoarseInterval.put(name, null);
2874 }
2875
Nick Pellye0fd6932012-07-11 10:26:13 -07002876 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002877 public void removeTestProvider(String provider, String opPackageName) {
2878 if (!canCallerAccessMockLocation(opPackageName)) {
2879 return;
2880 }
2881
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002882 synchronized (mLock) {
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002883
2884 // These methods can't be called after removing the test provider, so first make sure
Tom O'Neillfe6d3c52014-03-04 08:26:17 -08002885 // we don't leave anything dangling.
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002886 clearTestProviderEnabled(provider, opPackageName);
2887 clearTestProviderLocation(provider, opPackageName);
2888 clearTestProviderStatus(provider, opPackageName);
Tom O'Neill07ee5d12014-03-03 17:48:35 -08002889
You Kima6d0b6f2012-10-28 03:58:44 +09002890 MockProvider mockProvider = mMockProviders.remove(provider);
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002891 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2893 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002894 long identity = Binder.clearCallingIdentity();
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002895 removeProviderLocked(mProvidersByName.get(provider));
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002896
2897 // reinstate real provider if available
2898 LocationProviderInterface realProvider = mRealProviders.get(provider);
2899 if (realProvider != null) {
2900 addProviderLocked(realProvider);
Mike Lockwood7566c1d2009-08-25 10:05:18 -07002901 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07002902 mLastLocation.put(provider, null);
David Christie1b9b7b12013-04-15 15:31:11 -07002903 mLastLocationCoarseInterval.put(provider, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002905 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 }
2907 }
2908
Nick Pellye0fd6932012-07-11 10:26:13 -07002909 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002910 public void setTestProviderLocation(String provider, Location loc, String opPackageName) {
2911 if (!canCallerAccessMockLocation(opPackageName)) {
2912 return;
2913 }
2914
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002915 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002916 MockProvider mockProvider = mMockProviders.get(provider);
2917 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2919 }
Tom O'Neilla206a0f2016-12-15 10:26:28 -08002920
2921 // Ensure that the location is marked as being mock. There's some logic to do this in
2922 // handleLocationChanged(), but it fails if loc has the wrong provider (bug 33091107).
2923 Location mock = new Location(loc);
2924 mock.setIsFromMockProvider(true);
2925
2926 if (!TextUtils.isEmpty(loc.getProvider()) && !provider.equals(loc.getProvider())) {
2927 // The location has an explicit provider that is different from the mock provider
2928 // name. The caller may be trying to fool us via bug 33091107.
2929 EventLog.writeEvent(0x534e4554, "33091107", Binder.getCallingUid(),
2930 provider + "!=" + loc.getProvider());
2931 }
2932
Mike Lockwood95427cd2009-05-07 13:27:54 -04002933 // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required
2934 long identity = Binder.clearCallingIdentity();
Tom O'Neilla206a0f2016-12-15 10:26:28 -08002935 mockProvider.setLocation(mock);
Mike Lockwood95427cd2009-05-07 13:27:54 -04002936 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
2938 }
2939
Nick Pellye0fd6932012-07-11 10:26:13 -07002940 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002941 public void clearTestProviderLocation(String provider, String opPackageName) {
2942 if (!canCallerAccessMockLocation(opPackageName)) {
2943 return;
2944 }
2945
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002946 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002947 MockProvider mockProvider = mMockProviders.get(provider);
2948 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2950 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002951 mockProvider.clearLocation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 }
2953 }
2954
Nick Pellye0fd6932012-07-11 10:26:13 -07002955 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002956 public void setTestProviderEnabled(String provider, boolean enabled, String opPackageName) {
2957 if (!canCallerAccessMockLocation(opPackageName)) {
2958 return;
2959 }
2960
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002961 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002962 MockProvider mockProvider = mMockProviders.get(provider);
2963 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2965 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002966 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 if (enabled) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002968 mockProvider.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 mEnabledProviders.add(provider);
2970 mDisabledProviders.remove(provider);
2971 } else {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002972 mockProvider.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 mEnabledProviders.remove(provider);
2974 mDisabledProviders.add(provider);
2975 }
2976 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002977 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979 }
2980
Nick Pellye0fd6932012-07-11 10:26:13 -07002981 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07002982 public void clearTestProviderEnabled(String provider, String opPackageName) {
2983 if (!canCallerAccessMockLocation(opPackageName)) {
2984 return;
2985 }
2986
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04002987 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07002988 MockProvider mockProvider = mMockProviders.get(provider);
2989 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
2991 }
Mike Lockwood86328a92009-10-23 08:38:25 -04002992 long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 mEnabledProviders.remove(provider);
2994 mDisabledProviders.remove(provider);
2995 updateProvidersLocked();
Mike Lockwood86328a92009-10-23 08:38:25 -04002996 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 }
2998 }
2999
Nick Pellye0fd6932012-07-11 10:26:13 -07003000 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07003001 public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime,
3002 String opPackageName) {
3003 if (!canCallerAccessMockLocation(opPackageName)) {
3004 return;
3005 }
3006
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04003007 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07003008 MockProvider mockProvider = mMockProviders.get(provider);
3009 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
3011 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07003012 mockProvider.setStatus(status, extras, updateTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
3014 }
3015
Nick Pellye0fd6932012-07-11 10:26:13 -07003016 @Override
Svet Ganovf7e9cf42015-05-13 10:40:31 -07003017 public void clearTestProviderStatus(String provider, String opPackageName) {
3018 if (!canCallerAccessMockLocation(opPackageName)) {
3019 return;
3020 }
3021
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04003022 synchronized (mLock) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07003023 MockProvider mockProvider = mMockProviders.get(provider);
3024 if (mockProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 throw new IllegalArgumentException("Provider \"" + provider + "\" unknown");
3026 }
Mike Lockwood7ec434e2009-03-27 07:46:48 -07003027 mockProvider.clearStatus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
3029 }
3030
3031 private void log(String log) {
3032 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003033 Slog.d(TAG, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 }
3035 }
Nick Pellye0fd6932012-07-11 10:26:13 -07003036
3037 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06003039 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Nick Pellye0fd6932012-07-11 10:26:13 -07003040
Mike Lockwood2f82c4e2009-04-17 08:24:10 -04003041 synchronized (mLock) {
Siddharth Raybb608c82017-03-16 11:33:34 -07003042 if (args.length > 0 && args[0].equals("--gnssmetrics")) {
3043 if (mGnssMetricsProvider != null) {
3044 pw.append(mGnssMetricsProvider.getGnssMetricsAsProtoString());
3045 }
3046 return;
3047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 pw.println("Current Location Manager state:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 pw.println(" Location Listeners:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003050 for (Receiver receiver : mReceivers.values()) {
3051 pw.println(" " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 }
David Christie2ff96af2014-01-30 16:09:37 -08003053 pw.println(" Active Records by Provider:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003054 for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) {
3055 pw.println(" " + entry.getKey() + ":");
3056 for (UpdateRecord record : entry.getValue()) {
3057 pw.println(" " + record);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
Soonil Nagarkar7decfb62017-01-18 12:18:49 -08003060 pw.println(" Overlay Provider Packages:");
3061 for (LocationProviderInterface provider : mProviders) {
3062 if (provider instanceof LocationProviderProxy) {
3063 pw.println(" " + provider.getName() + ": "
3064 + ((LocationProviderProxy) provider).getConnectedPackageName());
3065 }
3066 }
David Christie2ff96af2014-01-30 16:09:37 -08003067 pw.println(" Historical Records by Provider:");
3068 for (Map.Entry<PackageProviderKey, PackageStatistics> entry
3069 : mRequestStatistics.statistics.entrySet()) {
3070 PackageProviderKey key = entry.getKey();
3071 PackageStatistics stats = entry.getValue();
3072 pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats);
3073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 pw.println(" Last Known Locations:");
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003075 for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) {
3076 String provider = entry.getKey();
3077 Location location = entry.getValue();
3078 pw.println(" " + provider + ": " + location);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003080
David Christie1b9b7b12013-04-15 15:31:11 -07003081 pw.println(" Last Known Locations Coarse Intervals:");
3082 for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) {
3083 String provider = entry.getKey();
3084 Location location = entry.getValue();
3085 pw.println(" " + provider + ": " + location);
3086 }
3087
Nick Pellye0fd6932012-07-11 10:26:13 -07003088 mGeofenceManager.dump(pw);
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 if (mEnabledProviders.size() > 0) {
3091 pw.println(" Enabled Providers:");
3092 for (String i : mEnabledProviders) {
3093 pw.println(" " + i);
3094 }
Nick Pellye0fd6932012-07-11 10:26:13 -07003095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 }
3097 if (mDisabledProviders.size() > 0) {
3098 pw.println(" Disabled Providers:");
3099 for (String i : mDisabledProviders) {
3100 pw.println(" " + i);
3101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 }
Nick Pelly4035f5a2012-08-17 14:43:49 -07003103 pw.append(" ");
3104 mBlacklist.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 if (mMockProviders.size() > 0) {
3106 pw.println(" Mock Providers:");
3107 for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) {
Mike Lockwood7ec434e2009-03-27 07:46:48 -07003108 i.getValue().dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
3110 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003111
Soonil Nagarkar2b565df2017-02-14 13:33:23 -08003112 if (!mBackgroundThrottlePackageWhitelist.isEmpty()) {
3113 pw.println(" Throttling Whitelisted Packages:");
3114 for (String packageName : mBackgroundThrottlePackageWhitelist) {
3115 pw.println(" " + packageName);
3116 }
3117 }
3118
Nick Pelly74fa7ea2012-08-13 19:36:38 -07003119 pw.append(" fudger: ");
3120 mLocationFudger.dump(fd, pw, args);
3121
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003122 if (args.length > 0 && "short".equals(args[0])) {
3123 return;
3124 }
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06003125 for (LocationProviderInterface provider: mProviders) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003126 pw.print(provider.getName() + " Internal State");
3127 if (provider instanceof LocationProviderProxy) {
3128 LocationProviderProxy proxy = (LocationProviderProxy) provider;
3129 pw.print(" (" + proxy.getConnectedPackageName() + ")");
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06003130 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -07003131 pw.println(":");
3132 provider.dump(fd, pw, args);
Fred Fettinger3c8fbdf2010-01-04 15:38:13 -06003133 }
Wyatt Rileycf879db2017-01-12 13:57:38 -08003134 if (mGnssBatchingInProgress) {
3135 pw.println(" GNSS batching in progress");
3136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 }
3138 }
3139}