The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 20 | import static android.location.LocationProvider.AVAILABLE; |
Soonil Nagarkar | 94749f7 | 2018-11-08 11:46:43 -0800 | [diff] [blame] | 21 | import static android.provider.Settings.Global.LOCATION_DISABLE_STATUS_CALLBACKS; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 22 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 23 | import static com.android.internal.util.Preconditions.checkState; |
| 24 | |
Wei Wang | 980b7c2 | 2018-12-06 17:53:00 -0800 | [diff] [blame^] | 25 | import android.Manifest; |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
Wyatt Riley | 49097c0 | 2018-03-15 09:14:43 -0700 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 28 | import android.app.ActivityManager; |
| 29 | import android.app.AppOpsManager; |
| 30 | import android.app.PendingIntent; |
| 31 | import android.content.BroadcastReceiver; |
| 32 | import android.content.ContentResolver; |
| 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| 36 | import android.content.pm.ApplicationInfo; |
| 37 | import android.content.pm.PackageInfo; |
| 38 | import android.content.pm.PackageManager; |
| 39 | import android.content.pm.PackageManager.NameNotFoundException; |
| 40 | import android.content.pm.PackageManagerInternal; |
| 41 | import android.content.pm.ResolveInfo; |
| 42 | import android.content.pm.Signature; |
| 43 | import android.content.res.Resources; |
| 44 | import android.database.ContentObserver; |
| 45 | import android.hardware.location.ActivityRecognitionHardware; |
| 46 | import android.location.Address; |
| 47 | import android.location.Criteria; |
| 48 | import android.location.GeocoderParams; |
| 49 | import android.location.Geofence; |
| 50 | import android.location.IBatchedLocationCallback; |
| 51 | import android.location.IGnssMeasurementsListener; |
| 52 | import android.location.IGnssNavigationMessageListener; |
| 53 | import android.location.IGnssStatusListener; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 54 | import android.location.IGpsGeofenceHardware; |
| 55 | import android.location.ILocationListener; |
| 56 | import android.location.ILocationManager; |
| 57 | import android.location.INetInitiatedListener; |
| 58 | import android.location.Location; |
| 59 | import android.location.LocationManager; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 60 | import android.location.LocationRequest; |
| 61 | import android.os.Binder; |
| 62 | import android.os.Bundle; |
| 63 | import android.os.Handler; |
| 64 | import android.os.IBinder; |
| 65 | import android.os.Looper; |
| 66 | import android.os.Message; |
| 67 | import android.os.PowerManager; |
| 68 | import android.os.Process; |
| 69 | import android.os.RemoteException; |
| 70 | import android.os.SystemClock; |
| 71 | import android.os.UserHandle; |
| 72 | import android.os.UserManager; |
| 73 | import android.os.WorkSource; |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 74 | import android.os.WorkSource.WorkChain; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 75 | import android.provider.Settings; |
| 76 | import android.text.TextUtils; |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 77 | import android.util.ArrayMap; |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 78 | import android.util.ArraySet; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 79 | import android.util.EventLog; |
| 80 | import android.util.Log; |
| 81 | import android.util.Slog; |
Yu-Han Yang | a4d250e | 2018-10-02 21:29:20 -0700 | [diff] [blame] | 82 | |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 83 | import com.android.internal.content.PackageMonitor; |
| 84 | import com.android.internal.location.ProviderProperties; |
| 85 | import com.android.internal.location.ProviderRequest; |
| 86 | import com.android.internal.os.BackgroundThread; |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 87 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 88 | import com.android.internal.util.DumpUtils; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 89 | import com.android.server.location.AbstractLocationProvider; |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 90 | import com.android.server.location.ActivityRecognitionProxy; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 91 | import com.android.server.location.GeocoderProxy; |
| 92 | import com.android.server.location.GeofenceManager; |
| 93 | import com.android.server.location.GeofenceProxy; |
Yu-Han Yang | 3557cc7 | 2018-03-21 12:48:36 -0700 | [diff] [blame] | 94 | import com.android.server.location.GnssBatchingProvider; |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 95 | import com.android.server.location.GnssLocationProvider; |
| 96 | import com.android.server.location.GnssMeasurementsProvider; |
| 97 | import com.android.server.location.GnssNavigationMessageProvider; |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 98 | import com.android.server.location.GnssStatusListenerHelper; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 99 | import com.android.server.location.LocationBlacklist; |
| 100 | import com.android.server.location.LocationFudger; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 101 | import com.android.server.location.LocationProviderProxy; |
| 102 | import com.android.server.location.LocationRequestStatistics; |
| 103 | import com.android.server.location.LocationRequestStatistics.PackageProviderKey; |
| 104 | import com.android.server.location.LocationRequestStatistics.PackageStatistics; |
| 105 | import com.android.server.location.MockProvider; |
| 106 | import com.android.server.location.PassiveProvider; |
Yu-Han Yang | a4d250e | 2018-10-02 21:29:20 -0700 | [diff] [blame] | 107 | |
Mike Lockwood | 43e33f2 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 108 | import java.io.FileDescriptor; |
| 109 | import java.io.PrintWriter; |
| 110 | import java.util.ArrayList; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 111 | import java.util.Arrays; |
Mike Lockwood | 43e33f2 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 112 | import java.util.HashMap; |
| 113 | import java.util.HashSet; |
| 114 | import java.util.List; |
| 115 | import java.util.Map; |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 116 | import java.util.Map.Entry; |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 117 | import java.util.NoSuchElementException; |
Mike Lockwood | 43e33f2 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 118 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * The service class that manages LocationProviders and issues location |
| 122 | * updates and alerts. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | */ |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 124 | public class LocationManagerService extends ILocationManager.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | private static final String TAG = "LocationManagerService"; |
JP Abgrall | f79811e7 | 2013-02-01 18:45:05 -0800 | [diff] [blame] | 126 | public static final boolean D = Log.isLoggable(TAG, Log.DEBUG); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 127 | |
Olivier Gaillard | 7a22266 | 2017-11-20 16:07:24 +0000 | [diff] [blame] | 128 | private static final String WAKELOCK_KEY = "*location*"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 130 | // Location resolution level: no location data whatsoever |
| 131 | private static final int RESOLUTION_LEVEL_NONE = 0; |
| 132 | // Location resolution level: coarse location data only |
| 133 | private static final int RESOLUTION_LEVEL_COARSE = 1; |
| 134 | // Location resolution level: fine location data |
| 135 | private static final int RESOLUTION_LEVEL_FINE = 2; |
| 136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | private static final String ACCESS_LOCATION_EXTRA_COMMANDS = |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 138 | android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS; |
Mike Lockwood | 275555c | 2009-05-01 11:30:34 -0400 | [diff] [blame] | 139 | private static final String INSTALL_LOCATION_PROVIDER = |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 140 | android.Manifest.permission.INSTALL_LOCATION_PROVIDER; |
| 141 | |
| 142 | private static final String NETWORK_LOCATION_SERVICE_ACTION = |
Stan Chesnutt | 39062dd | 2013-07-22 14:33:30 -0700 | [diff] [blame] | 143 | "com.android.location.service.v3.NetworkLocationProvider"; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 144 | private static final String FUSED_LOCATION_SERVICE_ACTION = |
| 145 | "com.android.location.service.FusedLocationProvider"; |
| 146 | |
| 147 | private static final int MSG_LOCATION_CHANGED = 1; |
| 148 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 149 | private static final long NANOS_PER_MILLI = 1000000L; |
| 150 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 151 | // The maximum interval a location request can have and still be considered "high power". |
| 152 | private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000; |
| 153 | |
Soonil Nagarkar | ebda028 | 2017-04-10 14:55:37 -0700 | [diff] [blame] | 154 | private static final int FOREGROUND_IMPORTANCE_CUTOFF |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 155 | = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; |
Soonil Nagarkar | ebda028 | 2017-04-10 14:55:37 -0700 | [diff] [blame] | 156 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 157 | // default background throttling interval if not overriden in settings |
Soonil Nagarkar | de6780a | 2017-02-07 10:39:41 -0800 | [diff] [blame] | 158 | private static final long DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS = 30 * 60 * 1000; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 159 | |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 160 | // Default value for maximum age of last location returned to applications with foreground-only |
| 161 | // location permissions. |
| 162 | private static final long DEFAULT_LAST_LOCATION_MAX_AGE_MS = 20 * 60 * 1000; |
| 163 | |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 164 | // Location Providers may sometimes deliver location updates |
| 165 | // slightly faster that requested - provide grace period so |
| 166 | // we don't unnecessarily filter events that are otherwise on |
| 167 | // time |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 168 | private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100; |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 169 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 170 | private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest(); |
| 171 | |
| 172 | private final Context mContext; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 173 | private final AppOpsManager mAppOps; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 174 | |
| 175 | // used internally for synchronization |
| 176 | private final Object mLock = new Object(); |
| 177 | |
Wyatt Riley | a8037ff | 2016-08-04 16:10:06 -0700 | [diff] [blame] | 178 | // --- fields below are final after systemRunning() --- |
Nick Pelly | 74fa7ea | 2012-08-13 19:36:38 -0700 | [diff] [blame] | 179 | private LocationFudger mLocationFudger; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 180 | private GeofenceManager mGeofenceManager; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 181 | private PackageManager mPackageManager; |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 182 | private PowerManager mPowerManager; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 183 | private ActivityManager mActivityManager; |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 184 | private UserManager mUserManager; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 185 | private GeocoderProxy mGeocodeProvider; |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 186 | private GnssStatusListenerHelper mGnssStatusProvider; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 187 | private INetInitiatedListener mNetInitiatedListener; |
| 188 | private LocationWorkerHandler mLocationHandler; |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 189 | private PassiveProvider mPassiveProvider; // track passive provider for special cases |
| 190 | private LocationBlacklist mBlacklist; |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 191 | private GnssMeasurementsProvider mGnssMeasurementsProvider; |
| 192 | private GnssNavigationMessageProvider mGnssNavigationMessageProvider; |
Wei Wang | 980b7c2 | 2018-12-06 17:53:00 -0800 | [diff] [blame^] | 193 | private String mLocationControllerExtraPackage; |
| 194 | private boolean mLocationControllerExtraPackageEnabled; |
Wei Liu | 5241a4c | 2015-05-11 14:00:36 -0700 | [diff] [blame] | 195 | private IGpsGeofenceHardware mGpsGeofenceProxy; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 196 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 197 | // --- fields below are protected by mLock --- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 199 | // Mock (test) providers |
| 200 | private final HashMap<String, MockProvider> mMockProviders = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 201 | new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 203 | // all receivers |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 204 | private final HashMap<Object, Receiver> mReceivers = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 206 | // currently installed providers (with mocks replacing real providers) |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 207 | private final ArrayList<LocationProvider> mProviders = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 208 | new ArrayList<>(); |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 209 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 210 | // real providers, saved here when mocked out |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 211 | private final HashMap<String, LocationProvider> mRealProviders = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 212 | new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 214 | // mapping from provider name to provider |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 215 | private final HashMap<String, LocationProvider> mProvidersByName = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 216 | new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 218 | // mapping from provider name to all its UpdateRecords |
| 219 | private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 220 | new HashMap<>(); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 221 | |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 222 | private final LocationRequestStatistics mRequestStatistics = new LocationRequestStatistics(); |
| 223 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 224 | // mapping from provider name to last known location |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 225 | private final HashMap<String, Location> mLastLocation = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 227 | // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS. |
| 228 | // locations stored here are not fudged for coarse permissions. |
| 229 | private final HashMap<String, Location> mLastLocationCoarseInterval = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 230 | new HashMap<>(); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 231 | |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 232 | // all providers that operate over proxy, for authorizing incoming location and whitelisting |
| 233 | // throttling |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 234 | private final ArrayList<LocationProviderProxy> mProxyProviders = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 235 | new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 237 | private final ArraySet<String> mBackgroundThrottlePackageWhitelist = new ArraySet<>(); |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 238 | |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 239 | private final ArrayMap<IBinder, Identity> mGnssMeasurementsListeners = new ArrayMap<>(); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 240 | |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 241 | private final ArrayMap<IBinder, Identity> |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 242 | mGnssNavigationMessageListeners = new ArrayMap<>(); |
| 243 | |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 244 | // current active user on the device - other users are denied location data |
Xiaohui Chen | a449062 | 2015-09-22 15:29:31 -0700 | [diff] [blame] | 245 | private int mCurrentUserId = UserHandle.USER_SYSTEM; |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 246 | private int[] mCurrentUserProfiles = new int[]{UserHandle.USER_SYSTEM}; |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 247 | |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 248 | // Maximum age of last location returned to clients with foreground-only location permissions. |
| 249 | private long mLastLocationMaxAgeMs; |
| 250 | |
Lifu Tang | 9363b94 | 2016-02-16 18:07:00 -0800 | [diff] [blame] | 251 | private GnssLocationProvider.GnssSystemInfoProvider mGnssSystemInfoProvider; |
Lifu Tang | 82f893d | 2016-01-21 18:15:33 -0800 | [diff] [blame] | 252 | |
Siddharth Ray | bb608c8 | 2017-03-16 11:33:34 -0700 | [diff] [blame] | 253 | private GnssLocationProvider.GnssMetricsProvider mGnssMetricsProvider; |
Wyatt Riley | aa420d5 | 2017-07-03 15:14:42 -0700 | [diff] [blame] | 254 | |
Yu-Han Yang | 3557cc7 | 2018-03-21 12:48:36 -0700 | [diff] [blame] | 255 | private GnssBatchingProvider mGnssBatchingProvider; |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 256 | private IBatchedLocationCallback mGnssBatchingCallback; |
| 257 | private LinkedCallback mGnssBatchingDeathCallback; |
| 258 | private boolean mGnssBatchingInProgress = false; |
| 259 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 260 | public LocationManagerService(Context context) { |
| 261 | super(); |
| 262 | mContext = context; |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 263 | mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 264 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 265 | // Let the package manager query which are the default location |
| 266 | // providers as they get certain permissions granted by default. |
| 267 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 268 | PackageManagerInternal.class); |
| 269 | packageManagerInternal.setLocationPackagesProvider( |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 270 | userId -> mContext.getResources().getStringArray( |
| 271 | com.android.internal.R.array.config_locationProviderPackageNames)); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 272 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 273 | if (D) Log.d(TAG, "Constructed"); |
| 274 | |
Wyatt Riley | a8037ff | 2016-08-04 16:10:06 -0700 | [diff] [blame] | 275 | // most startup is deferred until systemRunning() |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 278 | public void systemRunning() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 279 | synchronized (mLock) { |
Wyatt Riley | a8037ff | 2016-08-04 16:10:06 -0700 | [diff] [blame] | 280 | if (D) Log.d(TAG, "systemRunning()"); |
Brian Muramatsu | bb95cb9 | 2012-08-29 10:43:21 -0700 | [diff] [blame] | 281 | |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 282 | // fetch package manager |
| 283 | mPackageManager = mContext.getPackageManager(); |
| 284 | |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 285 | // fetch power manager |
| 286 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 287 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 288 | // fetch activity manager |
| 289 | mActivityManager |
| 290 | = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 291 | |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 292 | // prepare worker thread |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 293 | mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper()); |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 294 | |
| 295 | // prepare mLocationHandler's dependents |
| 296 | mLocationFudger = new LocationFudger(mContext, mLocationHandler); |
| 297 | mBlacklist = new LocationBlacklist(mContext, mLocationHandler); |
| 298 | mBlacklist.init(); |
| 299 | mGeofenceManager = new GeofenceManager(mContext, mBlacklist); |
| 300 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 301 | // Monitor for app ops mode changes. |
Dianne Hackborn | 9bb0ee9 | 2013-09-22 12:31:38 -0700 | [diff] [blame] | 302 | AppOpsManager.OnOpChangedListener callback |
| 303 | = new AppOpsManager.OnOpChangedInternalListener() { |
| 304 | public void onOpChanged(int op, String packageName) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 305 | synchronized (mLock) { |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 306 | for (Receiver receiver : mReceivers.values()) { |
| 307 | receiver.updateMonitoring(true); |
| 308 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 309 | applyAllProviderRequirementsLocked(); |
| 310 | } |
| 311 | } |
| 312 | }; |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 313 | mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, |
| 314 | AppOpsManager.WATCH_FOREGROUND_CHANGES, callback); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 315 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 316 | PackageManager.OnPermissionsChangedListener permissionListener = uid -> { |
| 317 | synchronized (mLock) { |
| 318 | applyAllProviderRequirementsLocked(); |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 319 | } |
| 320 | }; |
| 321 | mPackageManager.addOnPermissionsChangeListener(permissionListener); |
| 322 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 323 | // listen for background/foreground changes |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 324 | ActivityManager.OnUidImportanceListener uidImportanceListener = |
| 325 | (uid, importance) -> mLocationHandler.post( |
| 326 | () -> onUidImportanceChanged(uid, importance)); |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 327 | mActivityManager.addOnUidImportanceListener(uidImportanceListener, |
Soonil Nagarkar | ebda028 | 2017-04-10 14:55:37 -0700 | [diff] [blame] | 328 | FOREGROUND_IMPORTANCE_CUTOFF); |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 329 | |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 330 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 331 | updateUserProfiles(mCurrentUserId); |
| 332 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 333 | updateBackgroundThrottlingWhitelistLocked(); |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 334 | updateLastLocationMaxAgeLocked(); |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 335 | |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 336 | // prepare providers |
| 337 | loadProvidersLocked(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 338 | updateProvidersSettingsLocked(); |
| 339 | for (LocationProvider provider : mProviders) { |
| 340 | applyRequirementsLocked(provider.getName()); |
| 341 | } |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 342 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 343 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 344 | // listen for settings changes |
Brian Muramatsu | bb95cb9 | 2012-08-29 10:43:21 -0700 | [diff] [blame] | 345 | mContext.getContentResolver().registerContentObserver( |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 346 | Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true, |
Brian Muramatsu | bb95cb9 | 2012-08-29 10:43:21 -0700 | [diff] [blame] | 347 | new ContentObserver(mLocationHandler) { |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 348 | @Override |
| 349 | public void onChange(boolean selfChange) { |
| 350 | synchronized (mLock) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 351 | updateProvidersSettingsLocked(); |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | }, UserHandle.USER_ALL); |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 355 | mContext.getContentResolver().registerContentObserver( |
| 356 | Settings.Global.getUriFor(Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS), |
| 357 | true, |
| 358 | new ContentObserver(mLocationHandler) { |
| 359 | @Override |
| 360 | public void onChange(boolean selfChange) { |
| 361 | synchronized (mLock) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 362 | for (LocationProvider provider : mProviders) { |
| 363 | applyRequirementsLocked(provider.getName()); |
| 364 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | }, UserHandle.USER_ALL); |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 368 | mContext.getContentResolver().registerContentObserver( |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 369 | Settings.Global.getUriFor(Settings.Global.LOCATION_LAST_LOCATION_MAX_AGE_MILLIS), |
| 370 | true, |
| 371 | new ContentObserver(mLocationHandler) { |
| 372 | @Override |
| 373 | public void onChange(boolean selfChange) { |
| 374 | synchronized (mLock) { |
| 375 | updateLastLocationMaxAgeLocked(); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | ); |
| 380 | mContext.getContentResolver().registerContentObserver( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 381 | Settings.Global.getUriFor( |
| 382 | Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST), |
| 383 | true, |
| 384 | new ContentObserver(mLocationHandler) { |
| 385 | @Override |
| 386 | public void onChange(boolean selfChange) { |
| 387 | synchronized (mLock) { |
| 388 | updateBackgroundThrottlingWhitelistLocked(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 389 | for (LocationProvider provider : mProviders) { |
| 390 | applyRequirementsLocked(provider.getName()); |
| 391 | } |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 392 | } |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 393 | } |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 394 | }, UserHandle.USER_ALL); |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 395 | |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 396 | mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true); |
Brian Muramatsu | bb95cb9 | 2012-08-29 10:43:21 -0700 | [diff] [blame] | 397 | |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 398 | // listen for user change |
| 399 | IntentFilter intentFilter = new IntentFilter(); |
| 400 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 401 | intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 402 | intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 403 | |
| 404 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 405 | @Override |
| 406 | public void onReceive(Context context, Intent intent) { |
| 407 | String action = intent.getAction(); |
| 408 | if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 409 | switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 410 | } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action) |
| 411 | || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) { |
| 412 | updateUserProfiles(mCurrentUserId); |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 415 | }, UserHandle.ALL, intentFilter, null, mLocationHandler); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 418 | private void onUidImportanceChanged(int uid, int importance) { |
| 419 | boolean foreground = isImportanceForeground(importance); |
| 420 | HashSet<String> affectedProviders = new HashSet<>(mRecordsByProvider.size()); |
| 421 | synchronized (mLock) { |
| 422 | for (Entry<String, ArrayList<UpdateRecord>> entry |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 423 | : mRecordsByProvider.entrySet()) { |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 424 | String provider = entry.getKey(); |
| 425 | for (UpdateRecord record : entry.getValue()) { |
| 426 | if (record.mReceiver.mIdentity.mUid == uid |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 427 | && record.mIsForegroundUid != foreground) { |
| 428 | if (D) { |
| 429 | Log.d(TAG, "request from uid " + uid + " is now " |
| 430 | + (foreground ? "foreground" : "background)")); |
| 431 | } |
Wyatt Riley | f7075e0 | 2018-04-12 17:54:26 -0700 | [diff] [blame] | 432 | record.updateForeground(foreground); |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 433 | |
| 434 | if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) { |
| 435 | affectedProviders.add(provider); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | for (String provider : affectedProviders) { |
| 441 | applyRequirementsLocked(provider); |
| 442 | } |
| 443 | |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 444 | for (Entry<IBinder, Identity> entry : mGnssMeasurementsListeners.entrySet()) { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 445 | Identity callerIdentity = entry.getValue(); |
| 446 | if (callerIdentity.mUid == uid) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 447 | if (D) { |
| 448 | Log.d(TAG, "gnss measurements listener from uid " + uid |
| 449 | + " is now " + (foreground ? "foreground" : "background)")); |
| 450 | } |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 451 | if (foreground || isThrottlingExemptLocked(entry.getValue())) { |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 452 | mGnssMeasurementsProvider.addListener( |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 453 | IGnssMeasurementsListener.Stub.asInterface(entry.getKey()), |
| 454 | callerIdentity.mUid, callerIdentity.mPackageName); |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 455 | } else { |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 456 | mGnssMeasurementsProvider.removeListener( |
| 457 | IGnssMeasurementsListener.Stub.asInterface(entry.getKey())); |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 462 | for (Entry<IBinder, Identity> entry : mGnssNavigationMessageListeners.entrySet()) { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 463 | Identity callerIdentity = entry.getValue(); |
| 464 | if (callerIdentity.mUid == uid) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 465 | if (D) { |
| 466 | Log.d(TAG, "gnss navigation message listener from uid " |
| 467 | + uid + " is now " |
| 468 | + (foreground ? "foreground" : "background)")); |
| 469 | } |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 470 | if (foreground || isThrottlingExemptLocked(entry.getValue())) { |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 471 | mGnssNavigationMessageProvider.addListener( |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 472 | IGnssNavigationMessageListener.Stub.asInterface(entry.getKey()), |
| 473 | callerIdentity.mUid, callerIdentity.mPackageName); |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 474 | } else { |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 475 | mGnssNavigationMessageProvider.removeListener( |
| 476 | IGnssNavigationMessageListener.Stub.asInterface(entry.getKey())); |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 480 | |
| 481 | // TODO(b/120449926): The GNSS status listeners should be handled similar to the above. |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 485 | private static boolean isImportanceForeground(int importance) { |
Soonil Nagarkar | ebda028 | 2017-04-10 14:55:37 -0700 | [diff] [blame] | 486 | return importance <= FOREGROUND_IMPORTANCE_CUTOFF; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Makes a list of userids that are related to the current user. This is |
| 491 | * relevant when using managed profiles. Otherwise the list only contains |
| 492 | * the current user. |
| 493 | * |
| 494 | * @param currentUserId the current user, who might have an alter-ego. |
| 495 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 496 | private void updateUserProfiles(int currentUserId) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 497 | int[] profileIds = mUserManager.getProfileIdsWithDisabled(currentUserId); |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 498 | synchronized (mLock) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 499 | mCurrentUserProfiles = profileIds; |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Checks if the specified userId matches any of the current foreground |
| 505 | * users stored in mCurrentUserProfiles. |
| 506 | */ |
| 507 | private boolean isCurrentProfile(int userId) { |
| 508 | synchronized (mLock) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 509 | return ArrayUtils.contains(mCurrentUserProfiles, userId); |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 513 | private void ensureFallbackFusedProviderPresentLocked(String[] pkgs) { |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 514 | PackageManager pm = mContext.getPackageManager(); |
| 515 | String systemPackageName = mContext.getPackageName(); |
| 516 | ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs); |
| 517 | |
| 518 | List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser( |
| 519 | new Intent(FUSED_LOCATION_SERVICE_ACTION), |
| 520 | PackageManager.GET_META_DATA, mCurrentUserId); |
| 521 | for (ResolveInfo rInfo : rInfos) { |
| 522 | String packageName = rInfo.serviceInfo.packageName; |
| 523 | |
| 524 | // Check that the signature is in the list of supported sigs. If it's not in |
| 525 | // this list the standard provider binding logic won't bind to it. |
| 526 | try { |
| 527 | PackageInfo pInfo; |
| 528 | pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 529 | if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) { |
| 530 | Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION + |
| 531 | ", but has wrong signature, ignoring"); |
| 532 | continue; |
| 533 | } |
| 534 | } catch (NameNotFoundException e) { |
| 535 | Log.e(TAG, "missing package: " + packageName); |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | // Get the version info |
| 540 | if (rInfo.serviceInfo.metaData == null) { |
| 541 | Log.w(TAG, "Found fused provider without metadata: " + packageName); |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | int version = rInfo.serviceInfo.metaData.getInt( |
| 546 | ServiceWatcher.EXTRA_SERVICE_VERSION, -1); |
| 547 | if (version == 0) { |
| 548 | // This should be the fallback fused location provider. |
| 549 | |
| 550 | // Make sure it's in the system partition. |
| 551 | if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 552 | if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName); |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | // Check that the fallback is signed the same as the OS |
| 557 | // as a proxy for coreApp="true" |
| 558 | if (pm.checkSignatures(systemPackageName, packageName) |
| 559 | != PackageManager.SIGNATURE_MATCH) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 560 | if (D) { |
| 561 | Log.d(TAG, "Fallback candidate not signed the same as system: " |
| 562 | + packageName); |
| 563 | } |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 564 | continue; |
| 565 | } |
| 566 | |
| 567 | // Found a valid fallback. |
| 568 | if (D) Log.d(TAG, "Found fallback provider: " + packageName); |
| 569 | return; |
| 570 | } else { |
| 571 | if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | throw new IllegalStateException("Unable to find a fused location provider that is in the " |
| 576 | + "system partition with version 0 and signed with the platform certificate. " |
| 577 | + "Such a package is needed to provide a default fused location provider in the " |
| 578 | + "event that no other fused location provider has been installed or is currently " |
| 579 | + "available. For example, coreOnly boot mode when decrypting the data " |
| 580 | + "partition. The fallback must also be marked coreApp=\"true\" in the manifest"); |
| 581 | } |
| 582 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 583 | private void loadProvidersLocked() { |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 584 | // create a passive location provider, which is always enabled |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 585 | LocationProvider passiveProviderManager = new LocationProvider( |
| 586 | LocationManager.PASSIVE_PROVIDER); |
| 587 | PassiveProvider passiveProvider = new PassiveProvider(passiveProviderManager); |
| 588 | |
| 589 | addProviderLocked(passiveProviderManager); |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 590 | mPassiveProvider = passiveProvider; |
| 591 | |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 592 | if (GnssLocationProvider.isSupported()) { |
Wei Liu | 5241a4c | 2015-05-11 14:00:36 -0700 | [diff] [blame] | 593 | // Create a gps location provider |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 594 | LocationProvider gnssProviderManager = new LocationProvider( |
| 595 | LocationManager.GPS_PROVIDER); |
| 596 | GnssLocationProvider gnssProvider = new GnssLocationProvider(mContext, |
| 597 | gnssProviderManager, |
Wei Liu | 5241a4c | 2015-05-11 14:00:36 -0700 | [diff] [blame] | 598 | mLocationHandler.getLooper()); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 599 | |
Lifu Tang | 9363b94 | 2016-02-16 18:07:00 -0800 | [diff] [blame] | 600 | mGnssSystemInfoProvider = gnssProvider.getGnssSystemInfoProvider(); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 601 | mGnssBatchingProvider = gnssProvider.getGnssBatchingProvider(); |
Siddharth Ray | bb608c8 | 2017-03-16 11:33:34 -0700 | [diff] [blame] | 602 | mGnssMetricsProvider = gnssProvider.getGnssMetricsProvider(); |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 603 | mGnssStatusProvider = gnssProvider.getGnssStatusProvider(); |
| 604 | mNetInitiatedListener = gnssProvider.getNetInitiatedListener(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 605 | addProviderLocked(gnssProviderManager); |
| 606 | mRealProviders.put(LocationManager.GPS_PROVIDER, gnssProviderManager); |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 607 | mGnssMeasurementsProvider = gnssProvider.getGnssMeasurementsProvider(); |
| 608 | mGnssNavigationMessageProvider = gnssProvider.getGnssNavigationMessageProvider(); |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 609 | mGpsGeofenceProxy = gnssProvider.getGpsGeofenceProxy(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 612 | /* |
| 613 | Load package name(s) containing location provider support. |
| 614 | These packages can contain services implementing location providers: |
| 615 | Geocoder Provider, Network Location Provider, and |
| 616 | Fused Location Provider. They will each be searched for |
| 617 | service components implementing these providers. |
| 618 | The location framework also has support for installation |
| 619 | of new location providers at run-time. The new package does not |
| 620 | have to be explicitly listed here, however it must have a signature |
| 621 | that matches the signature of at least one package on this list. |
| 622 | */ |
| 623 | Resources resources = mContext.getResources(); |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 624 | String[] pkgs = resources.getStringArray( |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 625 | com.android.internal.R.array.config_locationProviderPackageNames); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 626 | if (D) { |
| 627 | Log.d(TAG, "certificates for location providers pulled from: " + |
| 628 | Arrays.toString(pkgs)); |
| 629 | } |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 630 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 631 | ensureFallbackFusedProviderPresentLocked(pkgs); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 632 | |
| 633 | // bind to network provider |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 634 | |
| 635 | LocationProvider networkProviderManager = new LocationProvider( |
| 636 | LocationManager.NETWORK_PROVIDER); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 637 | LocationProviderProxy networkProvider = LocationProviderProxy.createAndBind( |
| 638 | mContext, |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 639 | networkProviderManager, |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 640 | NETWORK_LOCATION_SERVICE_ACTION, |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 641 | com.android.internal.R.bool.config_enableNetworkLocationOverlay, |
| 642 | com.android.internal.R.string.config_networkLocationProviderPackageName, |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 643 | com.android.internal.R.array.config_locationProviderPackageNames); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 644 | if (networkProvider != null) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 645 | mRealProviders.put(LocationManager.NETWORK_PROVIDER, networkProviderManager); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 646 | mProxyProviders.add(networkProvider); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 647 | addProviderLocked(networkProviderManager); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 648 | } else { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 649 | Slog.w(TAG, "no network location provider found"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // bind to fused provider |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 653 | LocationProvider fusedProviderManager = new LocationProvider( |
| 654 | LocationManager.FUSED_PROVIDER); |
| 655 | LocationProviderProxy fusedProvider = LocationProviderProxy.createAndBind( |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 656 | mContext, |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 657 | fusedProviderManager, |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 658 | FUSED_LOCATION_SERVICE_ACTION, |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 659 | com.android.internal.R.bool.config_enableFusedLocationOverlay, |
| 660 | com.android.internal.R.string.config_fusedLocationProviderPackageName, |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 661 | com.android.internal.R.array.config_locationProviderPackageNames); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 662 | if (fusedProvider != null) { |
| 663 | addProviderLocked(fusedProviderManager); |
| 664 | mProxyProviders.add(fusedProvider); |
| 665 | mRealProviders.put(LocationManager.FUSED_PROVIDER, fusedProviderManager); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 666 | } else { |
| 667 | Slog.e(TAG, "no fused location provider found", |
| 668 | new IllegalStateException("Location service needs a fused location provider")); |
| 669 | } |
| 670 | |
| 671 | // bind to geocoder provider |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 672 | mGeocodeProvider = GeocoderProxy.createAndBind(mContext, |
| 673 | com.android.internal.R.bool.config_enableGeocoderOverlay, |
| 674 | com.android.internal.R.string.config_geocoderProviderPackageName, |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 675 | com.android.internal.R.array.config_locationProviderPackageNames); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 676 | if (mGeocodeProvider == null) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 677 | Slog.e(TAG, "no geocoder provider found"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 678 | } |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 679 | |
destradaa | f9a274c | 2014-07-25 15:11:56 -0700 | [diff] [blame] | 680 | // bind to geofence provider |
| 681 | GeofenceProxy provider = GeofenceProxy.createAndBind( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 682 | mContext, com.android.internal.R.bool.config_enableGeofenceOverlay, |
destradaa | f9a274c | 2014-07-25 15:11:56 -0700 | [diff] [blame] | 683 | com.android.internal.R.string.config_geofenceProviderPackageName, |
| 684 | com.android.internal.R.array.config_locationProviderPackageNames, |
Wei Liu | 5241a4c | 2015-05-11 14:00:36 -0700 | [diff] [blame] | 685 | mGpsGeofenceProxy, |
Jiyong Park | 4cc3a1c | 2018-03-08 16:43:07 +0900 | [diff] [blame] | 686 | null); |
destradaa | f9a274c | 2014-07-25 15:11:56 -0700 | [diff] [blame] | 687 | if (provider == null) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 688 | Slog.d(TAG, "Unable to bind FLP Geofence proxy."); |
destradaa | 0682809a | 2013-08-12 18:50:30 -0700 | [diff] [blame] | 689 | } |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 690 | |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 691 | // bind to hardware activity recognition |
| 692 | boolean activityRecognitionHardwareIsSupported = ActivityRecognitionHardware.isSupported(); |
| 693 | ActivityRecognitionHardware activityRecognitionHardware = null; |
| 694 | if (activityRecognitionHardwareIsSupported) { |
| 695 | activityRecognitionHardware = ActivityRecognitionHardware.getInstance(mContext); |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 696 | } else { |
destradaa | 6b4893a | 2016-05-03 15:33:43 -0700 | [diff] [blame] | 697 | Slog.d(TAG, "Hardware Activity-Recognition not supported."); |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 698 | } |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 699 | ActivityRecognitionProxy proxy = ActivityRecognitionProxy.createAndBind( |
| 700 | mContext, |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 701 | activityRecognitionHardwareIsSupported, |
| 702 | activityRecognitionHardware, |
| 703 | com.android.internal.R.bool.config_enableActivityRecognitionHardwareOverlay, |
| 704 | com.android.internal.R.string.config_activityRecognitionHardwarePackageName, |
| 705 | com.android.internal.R.array.config_locationProviderPackageNames); |
| 706 | if (proxy == null) { |
destradaa | 6b4893a | 2016-05-03 15:33:43 -0700 | [diff] [blame] | 707 | Slog.d(TAG, "Unable to bind ActivityRecognitionProxy."); |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 708 | } |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 709 | |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 710 | String[] testProviderStrings = resources.getStringArray( |
| 711 | com.android.internal.R.array.config_testLocationProviders); |
| 712 | for (String testProviderString : testProviderStrings) { |
| 713 | String fragments[] = testProviderString.split(","); |
| 714 | String name = fragments[0].trim(); |
| 715 | if (mProvidersByName.get(name) != null) { |
| 716 | throw new IllegalArgumentException("Provider \"" + name + "\" already exists"); |
| 717 | } |
| 718 | ProviderProperties properties = new ProviderProperties( |
| 719 | Boolean.parseBoolean(fragments[1]) /* requiresNetwork */, |
| 720 | Boolean.parseBoolean(fragments[2]) /* requiresSatellite */, |
| 721 | Boolean.parseBoolean(fragments[3]) /* requiresCell */, |
| 722 | Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */, |
| 723 | Boolean.parseBoolean(fragments[5]) /* supportsAltitude */, |
| 724 | Boolean.parseBoolean(fragments[6]) /* supportsSpeed */, |
| 725 | Boolean.parseBoolean(fragments[7]) /* supportsBearing */, |
| 726 | Integer.parseInt(fragments[8]) /* powerRequirement */, |
| 727 | Integer.parseInt(fragments[9]) /* accuracy */); |
| 728 | addTestProviderLocked(name, properties); |
| 729 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 730 | } |
Mike Lockwood | 9637d47 | 2009-04-02 21:41:57 -0700 | [diff] [blame] | 731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | /** |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 733 | * Called when the device's active user changes. |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 734 | * |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 735 | * @param userId the new active user's UserId |
| 736 | */ |
| 737 | private void switchUser(int userId) { |
Jianzheng Zhou | d5c6946 | 2013-10-10 14:02:09 +0800 | [diff] [blame] | 738 | if (mCurrentUserId == userId) { |
| 739 | return; |
| 740 | } |
Victoria Lease | 83762d2 | 2012-10-03 13:51:17 -0700 | [diff] [blame] | 741 | mBlacklist.switchUser(userId); |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 742 | mLocationHandler.removeMessages(MSG_LOCATION_CHANGED); |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 743 | synchronized (mLock) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 744 | mLastLocation.clear(); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 745 | mLastLocationCoarseInterval.clear(); |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 746 | updateUserProfiles(userId); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 747 | updateProvidersSettingsLocked(); |
| 748 | mCurrentUserId = userId; |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 752 | private static final class Identity { |
| 753 | final int mUid; |
| 754 | final int mPid; |
| 755 | final String mPackageName; |
| 756 | |
| 757 | Identity(int uid, int pid, String packageName) { |
| 758 | mUid = uid; |
| 759 | mPid = pid; |
| 760 | mPackageName = packageName; |
| 761 | } |
| 762 | } |
| 763 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 764 | private class LocationProvider implements AbstractLocationProvider.LocationProviderManager { |
| 765 | |
| 766 | private final String mName; |
| 767 | private AbstractLocationProvider mProvider; |
| 768 | |
| 769 | // whether the provider is enabled in location settings |
| 770 | private boolean mSettingsEnabled; |
| 771 | |
| 772 | // whether the provider considers itself enabled |
| 773 | private volatile boolean mEnabled; |
| 774 | |
| 775 | @Nullable |
| 776 | private volatile ProviderProperties mProperties; |
| 777 | |
| 778 | private LocationProvider(String name) { |
| 779 | mName = name; |
| 780 | // TODO: initialize settings enabled? |
| 781 | } |
| 782 | |
| 783 | @Override |
| 784 | public void onAttachProvider(AbstractLocationProvider provider, boolean initiallyEnabled) { |
| 785 | checkState(mProvider == null); |
| 786 | |
| 787 | // the provider is not yet fully constructed at this point, so we may not do anything |
| 788 | // except save a reference for later use here. do not call any provider methods. |
| 789 | mProvider = provider; |
| 790 | mEnabled = initiallyEnabled; |
| 791 | mProperties = null; |
| 792 | } |
| 793 | |
| 794 | public String getName() { |
| 795 | return mName; |
| 796 | } |
| 797 | |
| 798 | public boolean isEnabled() { |
| 799 | return mSettingsEnabled && mEnabled; |
| 800 | } |
| 801 | |
| 802 | @Nullable |
| 803 | public ProviderProperties getProperties() { |
| 804 | return mProperties; |
| 805 | } |
| 806 | |
| 807 | public void setRequest(ProviderRequest request, WorkSource workSource) { |
| 808 | mProvider.setRequest(request, workSource); |
| 809 | } |
| 810 | |
| 811 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 812 | pw.println(mName + " provider:"); |
| 813 | pw.println(" setting=" + mSettingsEnabled); |
| 814 | pw.println(" enabled=" + mEnabled); |
| 815 | pw.println(" properties=" + mProperties); |
| 816 | mProvider.dump(fd, pw, args); |
| 817 | } |
| 818 | |
| 819 | public long getStatusUpdateTime() { |
| 820 | return mProvider.getStatusUpdateTime(); |
| 821 | } |
| 822 | |
| 823 | public int getStatus(Bundle extras) { |
| 824 | return mProvider.getStatus(extras); |
| 825 | } |
| 826 | |
| 827 | public void sendExtraCommand(String command, Bundle extras) { |
| 828 | mProvider.sendExtraCommand(command, extras); |
| 829 | } |
| 830 | |
| 831 | // called from any thread |
| 832 | @Override |
| 833 | public void onReportLocation(Location location) { |
| 834 | runOnHandler(() -> LocationManagerService.this.reportLocation(location, |
| 835 | mProvider == mPassiveProvider)); |
| 836 | } |
| 837 | |
| 838 | // called from any thread |
| 839 | @Override |
| 840 | public void onReportLocation(List<Location> locations) { |
| 841 | runOnHandler(() -> LocationManagerService.this.reportLocationBatch(locations)); |
| 842 | } |
| 843 | |
| 844 | // called from any thread |
| 845 | @Override |
| 846 | public void onSetEnabled(boolean enabled) { |
| 847 | runOnHandler(() -> { |
| 848 | if (enabled == mEnabled) { |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | mEnabled = enabled; |
| 853 | |
| 854 | if (!mSettingsEnabled) { |
| 855 | // this provider was disabled in settings anyways, so a change to it's own |
| 856 | // enabled status won't have any affect. |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | // traditionally clients can listen for changes to the LOCATION_PROVIDERS_ALLOWED |
| 861 | // setting to detect when providers are enabled or disabled (even though they aren't |
| 862 | // supposed to). to continue to support this we must force a change to this setting. |
| 863 | // we use the fused provider because this is forced to be always enabled in settings |
| 864 | // anyways, and so won't have any visible effect beyond triggering content observers |
| 865 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 866 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
| 867 | "+" + LocationManager.FUSED_PROVIDER, mCurrentUserId); |
| 868 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 869 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
| 870 | "-" + LocationManager.FUSED_PROVIDER, mCurrentUserId); |
| 871 | |
| 872 | synchronized (mLock) { |
| 873 | if (!enabled) { |
| 874 | // If any provider has been disabled, clear all last locations for all |
| 875 | // providers. This is to be on the safe side in case a provider has location |
| 876 | // derived from this disabled provider. |
| 877 | mLastLocation.clear(); |
| 878 | mLastLocationCoarseInterval.clear(); |
| 879 | } |
| 880 | |
| 881 | updateProviderListenersLocked(mName); |
| 882 | } |
| 883 | |
| 884 | mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION), |
| 885 | UserHandle.ALL); |
| 886 | }); |
| 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public void onSetProperties(ProviderProperties properties) { |
| 891 | runOnHandler(() -> mProperties = properties); |
| 892 | } |
| 893 | |
| 894 | private void setSettingsEnabled(boolean enabled) { |
| 895 | synchronized (mLock) { |
| 896 | if (mSettingsEnabled == enabled) { |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | mSettingsEnabled = enabled; |
| 901 | if (!mSettingsEnabled) { |
| 902 | // if any provider has been disabled, clear all last locations for all |
| 903 | // providers. this is to be on the safe side in case a provider has location |
| 904 | // derived from this disabled provider. |
| 905 | mLastLocation.clear(); |
| 906 | mLastLocationCoarseInterval.clear(); |
| 907 | updateProviderListenersLocked(mName); |
| 908 | } else if (mEnabled) { |
| 909 | updateProviderListenersLocked(mName); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | private void runOnHandler(Runnable runnable) { |
| 915 | if (Looper.myLooper() == mLocationHandler.getLooper()) { |
| 916 | runnable.run(); |
| 917 | } else { |
| 918 | mLocationHandler.post(runnable); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 923 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | * A wrapper class holding either an ILocationListener or a PendingIntent to receive |
| 925 | * location updates. |
| 926 | */ |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 927 | private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished { |
Yu-Han Yang | 2418982 | 2018-07-11 15:24:11 -0700 | [diff] [blame] | 928 | private static final long WAKELOCK_TIMEOUT_MILLIS = 60 * 1000; |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 929 | final Identity mIdentity; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 930 | private final int mAllowedResolutionLevel; // resolution level allowed to receiver |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 931 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 932 | private final ILocationListener mListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | final PendingIntent mPendingIntent; |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 934 | final WorkSource mWorkSource; // WorkSource for battery blame, or null to assign to caller. |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 935 | private final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver. |
| 936 | private final Object mKey; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 937 | |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 938 | final HashMap<String, UpdateRecord> mUpdateRecords = new HashMap<>(); |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 939 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 940 | // True if app ops has started monitoring this receiver for locations. |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 941 | private boolean mOpMonitoring; |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 942 | // True if app ops has started monitoring this receiver for high power (gps) locations. |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 943 | private boolean mOpHighPowerMonitoring; |
| 944 | private int mPendingBroadcasts; |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 945 | PowerManager.WakeLock mWakeLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 947 | private Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid, |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 948 | String packageName, WorkSource workSource, boolean hideFromAppOps) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | mListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | mPendingIntent = intent; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 951 | if (listener != null) { |
| 952 | mKey = listener.asBinder(); |
| 953 | } else { |
| 954 | mKey = intent; |
| 955 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 956 | mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 957 | mIdentity = new Identity(uid, pid, packageName); |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 958 | if (workSource != null && workSource.isEmpty()) { |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 959 | workSource = null; |
| 960 | } |
| 961 | mWorkSource = workSource; |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 962 | mHideFromAppOps = hideFromAppOps; |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 963 | |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 964 | updateMonitoring(true); |
| 965 | |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 966 | // construct/configure wakelock |
| 967 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 968 | if (workSource == null) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 969 | workSource = new WorkSource(mIdentity.mUid, mIdentity.mPackageName); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 970 | } |
| 971 | mWakeLock.setWorkSource(workSource); |
Yu-Han Yang | 2418982 | 2018-07-11 15:24:11 -0700 | [diff] [blame] | 972 | |
| 973 | // For a non-reference counted wakelock, each acquire will reset the timeout, and we |
| 974 | // only need to release it once. |
| 975 | mWakeLock.setReferenceCounted(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | @Override |
| 979 | public boolean equals(Object otherObj) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 980 | return (otherObj instanceof Receiver) && mKey.equals(((Receiver) otherObj).mKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | @Override |
| 984 | public int hashCode() { |
| 985 | return mKey.hashCode(); |
| 986 | } |
Mike Lockwood | 3681f26 | 2009-05-12 10:52:03 -0400 | [diff] [blame] | 987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | @Override |
| 989 | public String toString() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 990 | StringBuilder s = new StringBuilder(); |
| 991 | s.append("Reciever["); |
| 992 | s.append(Integer.toHexString(System.identityHashCode(this))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | if (mListener != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 994 | s.append(" listener"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | } else { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 996 | s.append(" intent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 998 | for (String p : mUpdateRecords.keySet()) { |
| 999 | s.append(" ").append(mUpdateRecords.get(p).toString()); |
| 1000 | } |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 1001 | s.append(" monitoring location: ").append(mOpMonitoring); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1002 | s.append("]"); |
| 1003 | return s.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1006 | /** |
| 1007 | * Update AppOp monitoring for this receiver. |
| 1008 | * |
| 1009 | * @param allow If true receiver is currently active, if false it's been removed. |
| 1010 | */ |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 1011 | public void updateMonitoring(boolean allow) { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 1012 | if (mHideFromAppOps) { |
| 1013 | return; |
| 1014 | } |
| 1015 | |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1016 | boolean requestingLocation = false; |
| 1017 | boolean requestingHighPowerLocation = false; |
| 1018 | if (allow) { |
| 1019 | // See if receiver has any enabled update records. Also note if any update records |
| 1020 | // are high power (has a high power provider with an interval under a threshold). |
| 1021 | for (UpdateRecord updateRecord : mUpdateRecords.values()) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1022 | if (isAllowedByUserSettingsLockedForUser(updateRecord.mProvider, |
| 1023 | mCurrentUserId)) { |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1024 | requestingLocation = true; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1025 | LocationManagerService.LocationProvider locationProvider |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 1026 | = mProvidersByName.get(updateRecord.mProvider); |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1027 | ProviderProperties properties = locationProvider != null |
| 1028 | ? locationProvider.getProperties() : null; |
| 1029 | if (properties != null |
| 1030 | && properties.mPowerRequirement == Criteria.POWER_HIGH |
| 1031 | && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) { |
| 1032 | requestingHighPowerLocation = true; |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1039 | // First update monitoring of any location request (including high power). |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1040 | mOpMonitoring = updateMonitoring( |
| 1041 | requestingLocation, |
| 1042 | mOpMonitoring, |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1043 | AppOpsManager.OP_MONITOR_LOCATION); |
| 1044 | |
| 1045 | // Now update monitoring of high power requests only. |
David Christie | c750c1f | 2013-08-08 12:56:57 -0700 | [diff] [blame] | 1046 | boolean wasHighPowerMonitoring = mOpHighPowerMonitoring; |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1047 | mOpHighPowerMonitoring = updateMonitoring( |
| 1048 | requestingHighPowerLocation, |
| 1049 | mOpHighPowerMonitoring, |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1050 | AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION); |
David Christie | c750c1f | 2013-08-08 12:56:57 -0700 | [diff] [blame] | 1051 | if (mOpHighPowerMonitoring != wasHighPowerMonitoring) { |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1052 | // Send an intent to notify that a high power request has been added/removed. |
David Christie | c750c1f | 2013-08-08 12:56:57 -0700 | [diff] [blame] | 1053 | Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION); |
| 1054 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1055 | } |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * Update AppOps monitoring for a single location request and op type. |
| 1060 | * |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1061 | * @param allowMonitoring True if monitoring is allowed for this request/op. |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1062 | * @param currentlyMonitoring True if AppOps is currently monitoring this request/op. |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1063 | * @param op AppOps code for the op to update. |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1064 | * @return True if monitoring is on for this request/op after updating. |
| 1065 | */ |
| 1066 | private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring, |
| 1067 | int op) { |
| 1068 | if (!currentlyMonitoring) { |
| 1069 | if (allowMonitoring) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1070 | return mAppOps.startOpNoThrow(op, mIdentity.mUid, mIdentity.mPackageName) |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1071 | == AppOpsManager.MODE_ALLOWED; |
| 1072 | } |
| 1073 | } else { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1074 | if (!allowMonitoring |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 1075 | || mAppOps.noteOpNoThrow(op, mIdentity.mUid, mIdentity.mPackageName) |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1076 | != AppOpsManager.MODE_ALLOWED) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1077 | mAppOps.finishOp(op, mIdentity.mUid, mIdentity.mPackageName); |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1078 | return false; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | return currentlyMonitoring; |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | public boolean isListener() { |
| 1086 | return mListener != null; |
| 1087 | } |
| 1088 | |
| 1089 | public boolean isPendingIntent() { |
| 1090 | return mPendingIntent != null; |
| 1091 | } |
| 1092 | |
| 1093 | public ILocationListener getListener() { |
| 1094 | if (mListener != null) { |
| 1095 | return mListener; |
| 1096 | } |
| 1097 | throw new IllegalStateException("Request for non-existent listener"); |
| 1098 | } |
| 1099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | public boolean callStatusChangedLocked(String provider, int status, Bundle extras) { |
| 1101 | if (mListener != null) { |
| 1102 | try { |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1103 | synchronized (this) { |
| 1104 | // synchronize to ensure incrementPendingBroadcastsLocked() |
| 1105 | // is called before decrementPendingBroadcasts() |
| 1106 | mListener.onStatusChanged(provider, status, extras); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1107 | // call this after broadcasting so we do not increment |
| 1108 | // if we throw an exeption. |
| 1109 | incrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1110 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | } catch (RemoteException e) { |
| 1112 | return false; |
| 1113 | } |
| 1114 | } else { |
| 1115 | Intent statusChanged = new Intent(); |
Victoria Lease | 61ecb02 | 2012-11-13 15:12:51 -0800 | [diff] [blame] | 1116 | statusChanged.putExtras(new Bundle(extras)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | statusChanged.putExtra(LocationManager.KEY_STATUS_CHANGED, status); |
| 1118 | try { |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1119 | synchronized (this) { |
| 1120 | // synchronize to ensure incrementPendingBroadcastsLocked() |
| 1121 | // is called before decrementPendingBroadcasts() |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1122 | mPendingIntent.send(mContext, 0, statusChanged, this, mLocationHandler, |
Lifu Tang | 519f0d0 | 2018-04-12 16:39:39 -0700 | [diff] [blame] | 1123 | getResolutionPermission(mAllowedResolutionLevel), |
| 1124 | PendingIntentUtils.createDontSendToRestrictedAppsBundle(null)); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1125 | // call this after broadcasting so we do not increment |
| 1126 | // if we throw an exeption. |
| 1127 | incrementPendingBroadcastsLocked(); |
| 1128 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } catch (PendingIntent.CanceledException e) { |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
| 1136 | public boolean callLocationChangedLocked(Location location) { |
| 1137 | if (mListener != null) { |
| 1138 | try { |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1139 | synchronized (this) { |
| 1140 | // synchronize to ensure incrementPendingBroadcastsLocked() |
| 1141 | // is called before decrementPendingBroadcasts() |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 1142 | mListener.onLocationChanged(new Location(location)); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1143 | // call this after broadcasting so we do not increment |
| 1144 | // if we throw an exeption. |
| 1145 | incrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1146 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | } catch (RemoteException e) { |
| 1148 | return false; |
| 1149 | } |
| 1150 | } else { |
| 1151 | Intent locationChanged = new Intent(); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1152 | locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, |
| 1153 | new Location(location)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | try { |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1155 | synchronized (this) { |
| 1156 | // synchronize to ensure incrementPendingBroadcastsLocked() |
| 1157 | // is called before decrementPendingBroadcasts() |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1158 | mPendingIntent.send(mContext, 0, locationChanged, this, mLocationHandler, |
Lifu Tang | 519f0d0 | 2018-04-12 16:39:39 -0700 | [diff] [blame] | 1159 | getResolutionPermission(mAllowedResolutionLevel), |
| 1160 | PendingIntentUtils.createDontSendToRestrictedAppsBundle(null)); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1161 | // call this after broadcasting so we do not increment |
| 1162 | // if we throw an exeption. |
| 1163 | incrementPendingBroadcastsLocked(); |
| 1164 | } |
| 1165 | } catch (PendingIntent.CanceledException e) { |
| 1166 | return false; |
| 1167 | } |
| 1168 | } |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1172 | private boolean callProviderEnabledLocked(String provider, boolean enabled) { |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1173 | // First update AppOp monitoring. |
| 1174 | // An app may get/lose location access as providers are enabled/disabled. |
| 1175 | updateMonitoring(true); |
| 1176 | |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1177 | if (mListener != null) { |
| 1178 | try { |
| 1179 | synchronized (this) { |
| 1180 | // synchronize to ensure incrementPendingBroadcastsLocked() |
| 1181 | // is called before decrementPendingBroadcasts() |
| 1182 | if (enabled) { |
| 1183 | mListener.onProviderEnabled(provider); |
| 1184 | } else { |
| 1185 | mListener.onProviderDisabled(provider); |
| 1186 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1187 | // call this after broadcasting so we do not increment |
| 1188 | // if we throw an exeption. |
| 1189 | incrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } catch (RemoteException e) { |
| 1192 | return false; |
| 1193 | } |
| 1194 | } else { |
| 1195 | Intent providerIntent = new Intent(); |
| 1196 | providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled); |
| 1197 | try { |
| 1198 | synchronized (this) { |
| 1199 | // synchronize to ensure incrementPendingBroadcastsLocked() |
| 1200 | // is called before decrementPendingBroadcasts() |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1201 | mPendingIntent.send(mContext, 0, providerIntent, this, mLocationHandler, |
Lifu Tang | 519f0d0 | 2018-04-12 16:39:39 -0700 | [diff] [blame] | 1202 | getResolutionPermission(mAllowedResolutionLevel), |
| 1203 | PendingIntentUtils.createDontSendToRestrictedAppsBundle(null)); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1204 | // call this after broadcasting so we do not increment |
| 1205 | // if we throw an exeption. |
| 1206 | incrementPendingBroadcastsLocked(); |
| 1207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | } catch (PendingIntent.CanceledException e) { |
| 1209 | return false; |
| 1210 | } |
| 1211 | } |
| 1212 | return true; |
| 1213 | } |
| 1214 | |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 1215 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | public void binderDied() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1217 | if (D) Log.d(TAG, "Location listener died"); |
| 1218 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1219 | synchronized (mLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | removeUpdatesLocked(this); |
| 1221 | } |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1222 | synchronized (this) { |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 1223 | clearPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1227 | @Override |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1228 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, |
| 1229 | int resultCode, String resultData, Bundle resultExtras) { |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 1230 | synchronized (this) { |
| 1231 | decrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 1235 | // this must be called while synchronized by caller in a synchronized block |
| 1236 | // containing the sending of the broadcaset |
| 1237 | private void incrementPendingBroadcastsLocked() { |
Yu-Han Yang | 2418982 | 2018-07-11 15:24:11 -0700 | [diff] [blame] | 1238 | mPendingBroadcasts++; |
| 1239 | mWakeLock.acquire(WAKELOCK_TIMEOUT_MILLIS); |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | private void decrementPendingBroadcastsLocked() { |
| 1243 | if (--mPendingBroadcasts == 0) { |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 1244 | if (mWakeLock.isHeld()) { |
| 1245 | mWakeLock.release(); |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | public void clearPendingBroadcastsLocked() { |
| 1251 | if (mPendingBroadcasts > 0) { |
| 1252 | mPendingBroadcasts = 0; |
| 1253 | if (mWakeLock.isHeld()) { |
| 1254 | mWakeLock.release(); |
| 1255 | } |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1260 | @Override |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1261 | public void locationCallbackFinished(ILocationListener listener) { |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 1262 | //Do not use getReceiverLocked here as that will add the ILocationListener to |
Joshua Bartel | 080b61b | 2009-10-05 12:44:46 -0400 | [diff] [blame] | 1263 | //the receiver list if it is not found. If it is not found then the |
| 1264 | //LocationListener was removed when it had a pending broadcast and should |
| 1265 | //not be added back. |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 1266 | synchronized (mLock) { |
| 1267 | IBinder binder = listener.asBinder(); |
| 1268 | Receiver receiver = mReceivers.get(binder); |
| 1269 | if (receiver != null) { |
| 1270 | synchronized (receiver) { |
| 1271 | // so wakelock calls will succeed |
| 1272 | long identity = Binder.clearCallingIdentity(); |
| 1273 | receiver.decrementPendingBroadcastsLocked(); |
| 1274 | Binder.restoreCallingIdentity(identity); |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 1275 | } |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 1276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Lifu Tang | 82f893d | 2016-01-21 18:15:33 -0800 | [diff] [blame] | 1280 | /** |
Wyatt Riley | d87cf91 | 2017-12-05 09:31:52 -0800 | [diff] [blame] | 1281 | * Returns the year of the GNSS hardware. |
Lifu Tang | 82f893d | 2016-01-21 18:15:33 -0800 | [diff] [blame] | 1282 | */ |
| 1283 | @Override |
Lifu Tang | 9363b94 | 2016-02-16 18:07:00 -0800 | [diff] [blame] | 1284 | public int getGnssYearOfHardware() { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1285 | if (mGnssSystemInfoProvider != null) { |
Lifu Tang | 9363b94 | 2016-02-16 18:07:00 -0800 | [diff] [blame] | 1286 | return mGnssSystemInfoProvider.getGnssYearOfHardware(); |
Lifu Tang | 82f893d | 2016-01-21 18:15:33 -0800 | [diff] [blame] | 1287 | } else { |
| 1288 | return 0; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Wyatt Riley | d87cf91 | 2017-12-05 09:31:52 -0800 | [diff] [blame] | 1292 | |
| 1293 | /** |
| 1294 | * Returns the model name of the GNSS hardware. |
| 1295 | */ |
| 1296 | @Override |
Wyatt Riley | 49097c0 | 2018-03-15 09:14:43 -0700 | [diff] [blame] | 1297 | @Nullable |
Wyatt Riley | d87cf91 | 2017-12-05 09:31:52 -0800 | [diff] [blame] | 1298 | public String getGnssHardwareModelName() { |
| 1299 | if (mGnssSystemInfoProvider != null) { |
| 1300 | return mGnssSystemInfoProvider.getGnssHardwareModelName(); |
| 1301 | } else { |
Wyatt Riley | 49097c0 | 2018-03-15 09:14:43 -0700 | [diff] [blame] | 1302 | return null; |
Wyatt Riley | d87cf91 | 2017-12-05 09:31:52 -0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1306 | /** |
| 1307 | * Runs some checks for GNSS (FINE) level permissions, used by several methods which directly |
| 1308 | * (try to) access GNSS information at this layer. |
| 1309 | */ |
| 1310 | private boolean hasGnssPermissions(String packageName) { |
| 1311 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
| 1312 | checkResolutionLevelIsSufficientForProviderUse( |
| 1313 | allowedResolutionLevel, |
| 1314 | LocationManager.GPS_PROVIDER); |
| 1315 | |
| 1316 | int pid = Binder.getCallingPid(); |
| 1317 | int uid = Binder.getCallingUid(); |
| 1318 | long identity = Binder.clearCallingIdentity(); |
| 1319 | boolean hasLocationAccess; |
| 1320 | try { |
| 1321 | hasLocationAccess = checkLocationAccess(pid, uid, packageName, allowedResolutionLevel); |
| 1322 | } finally { |
| 1323 | Binder.restoreCallingIdentity(identity); |
| 1324 | } |
| 1325 | |
| 1326 | return hasLocationAccess; |
| 1327 | } |
| 1328 | |
| 1329 | /** |
| 1330 | * Returns the GNSS batching size, if available. |
| 1331 | */ |
| 1332 | @Override |
| 1333 | public int getGnssBatchSize(String packageName) { |
| 1334 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 1335 | "Location Hardware permission not granted to access hardware batching"); |
| 1336 | |
| 1337 | if (hasGnssPermissions(packageName) && mGnssBatchingProvider != null) { |
Yu-Han Yang | 3557cc7 | 2018-03-21 12:48:36 -0700 | [diff] [blame] | 1338 | return mGnssBatchingProvider.getBatchSize(); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1339 | } else { |
| 1340 | return 0; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * Adds a callback for GNSS Batching events, if permissions allow, which are transported |
| 1346 | * to potentially multiple listeners by the BatchedLocationCallbackTransport above this. |
| 1347 | */ |
| 1348 | @Override |
| 1349 | public boolean addGnssBatchingCallback(IBatchedLocationCallback callback, String packageName) { |
| 1350 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 1351 | "Location Hardware permission not granted to access hardware batching"); |
| 1352 | |
| 1353 | if (!hasGnssPermissions(packageName) || mGnssBatchingProvider == null) { |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
| 1357 | mGnssBatchingCallback = callback; |
| 1358 | mGnssBatchingDeathCallback = new LinkedCallback(callback); |
| 1359 | try { |
| 1360 | callback.asBinder().linkToDeath(mGnssBatchingDeathCallback, 0 /* flags */); |
| 1361 | } catch (RemoteException e) { |
| 1362 | // if the remote process registering the listener is already dead, just swallow the |
| 1363 | // exception and return |
| 1364 | Log.e(TAG, "Remote listener already died.", e); |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
| 1371 | private class LinkedCallback implements IBinder.DeathRecipient { |
| 1372 | private final IBatchedLocationCallback mCallback; |
| 1373 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1374 | private LinkedCallback(@NonNull IBatchedLocationCallback callback) { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1375 | mCallback = callback; |
| 1376 | } |
| 1377 | |
| 1378 | @NonNull |
| 1379 | public IBatchedLocationCallback getUnderlyingListener() { |
| 1380 | return mCallback; |
| 1381 | } |
| 1382 | |
| 1383 | @Override |
| 1384 | public void binderDied() { |
| 1385 | Log.d(TAG, "Remote Batching Callback died: " + mCallback); |
| 1386 | stopGnssBatch(); |
| 1387 | removeGnssBatchingCallback(); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | /** |
| 1392 | * Removes callback for GNSS batching |
| 1393 | */ |
| 1394 | @Override |
| 1395 | public void removeGnssBatchingCallback() { |
| 1396 | try { |
| 1397 | mGnssBatchingCallback.asBinder().unlinkToDeath(mGnssBatchingDeathCallback, |
| 1398 | 0 /* flags */); |
| 1399 | } catch (NoSuchElementException e) { |
| 1400 | // if the death callback isn't connected (it should be...), log error, swallow the |
| 1401 | // exception and return |
| 1402 | Log.e(TAG, "Couldn't unlink death callback.", e); |
| 1403 | } |
| 1404 | mGnssBatchingCallback = null; |
| 1405 | mGnssBatchingDeathCallback = null; |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | /** |
| 1410 | * Starts GNSS batching, if available. |
| 1411 | */ |
| 1412 | @Override |
| 1413 | public boolean startGnssBatch(long periodNanos, boolean wakeOnFifoFull, String packageName) { |
| 1414 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 1415 | "Location Hardware permission not granted to access hardware batching"); |
| 1416 | |
| 1417 | if (!hasGnssPermissions(packageName) || mGnssBatchingProvider == null) { |
| 1418 | return false; |
| 1419 | } |
| 1420 | |
| 1421 | if (mGnssBatchingInProgress) { |
| 1422 | // Current design does not expect multiple starts to be called repeatedly |
| 1423 | Log.e(TAG, "startGnssBatch unexpectedly called w/o stopping prior batch"); |
| 1424 | // Try to clean up anyway, and continue |
| 1425 | stopGnssBatch(); |
| 1426 | } |
| 1427 | |
| 1428 | mGnssBatchingInProgress = true; |
| 1429 | return mGnssBatchingProvider.start(periodNanos, wakeOnFifoFull); |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * Flushes a GNSS batch in progress |
| 1434 | */ |
| 1435 | @Override |
| 1436 | public void flushGnssBatch(String packageName) { |
| 1437 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 1438 | "Location Hardware permission not granted to access hardware batching"); |
| 1439 | |
| 1440 | if (!hasGnssPermissions(packageName)) { |
| 1441 | Log.e(TAG, "flushGnssBatch called without GNSS permissions"); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | if (!mGnssBatchingInProgress) { |
| 1446 | Log.w(TAG, "flushGnssBatch called with no batch in progress"); |
| 1447 | } |
| 1448 | |
| 1449 | if (mGnssBatchingProvider != null) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1450 | mGnssBatchingProvider.flush(); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * Stops GNSS batching |
| 1456 | */ |
| 1457 | @Override |
| 1458 | public boolean stopGnssBatch() { |
| 1459 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 1460 | "Location Hardware permission not granted to access hardware batching"); |
| 1461 | |
| 1462 | if (mGnssBatchingProvider != null) { |
| 1463 | mGnssBatchingInProgress = false; |
| 1464 | return mGnssBatchingProvider.stop(); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1465 | } else { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1466 | return false; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | public void reportLocationBatch(List<Location> locations) { |
| 1472 | checkCallerIsProvider(); |
| 1473 | |
| 1474 | // Currently used only for GNSS locations - update permissions check if changed |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1475 | if (isAllowedByUserSettingsLockedForUser(LocationManager.GPS_PROVIDER, mCurrentUserId)) { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1476 | if (mGnssBatchingCallback == null) { |
| 1477 | Slog.e(TAG, "reportLocationBatch() called without active Callback"); |
| 1478 | return; |
| 1479 | } |
| 1480 | try { |
| 1481 | mGnssBatchingCallback.onLocationBatch(locations); |
| 1482 | } catch (RemoteException e) { |
| 1483 | Slog.e(TAG, "mGnssBatchingCallback.onLocationBatch failed", e); |
| 1484 | } |
| 1485 | } else { |
| 1486 | Slog.w(TAG, "reportLocationBatch() called without user permission, locations blocked"); |
| 1487 | } |
| 1488 | } |
| 1489 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1490 | private void addProviderLocked(LocationProvider provider) { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1491 | mProviders.add(provider); |
| 1492 | mProvidersByName.put(provider.getName(), provider); |
| 1493 | } |
| 1494 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1495 | private void removeProviderLocked(LocationProvider provider) { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1496 | mProviders.remove(provider); |
| 1497 | mProvidersByName.remove(provider.getName()); |
| 1498 | } |
| 1499 | |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 1500 | /** |
Victoria Lease | 09eeaec | 2013-02-05 11:34:13 -0800 | [diff] [blame] | 1501 | * Returns "true" if access to the specified location provider is allowed by the specified |
| 1502 | * user's settings. Access to all location providers is forbidden to non-location-provider |
| 1503 | * processes belonging to background users. |
| 1504 | * |
| 1505 | * @param provider the name of the location provider |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1506 | * @param userId the user id to query |
Victoria Lease | 09eeaec | 2013-02-05 11:34:13 -0800 | [diff] [blame] | 1507 | */ |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1508 | private boolean isAllowedByUserSettingsLockedForUser(String provider, int userId) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1509 | if (LocationManager.PASSIVE_PROVIDER.equals(provider)) { |
| 1510 | return isLocationEnabledForUser(userId); |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1511 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1512 | if (LocationManager.FUSED_PROVIDER.equals(provider)) { |
| 1513 | return isLocationEnabledForUser(userId); |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1514 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1515 | synchronized (mLock) { |
| 1516 | if (mMockProviders.containsKey(provider)) { |
| 1517 | return isLocationEnabledForUser(userId); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | long identity = Binder.clearCallingIdentity(); |
| 1522 | try { |
| 1523 | // Use system settings |
| 1524 | ContentResolver cr = mContext.getContentResolver(); |
| 1525 | String allowedProviders = Settings.Secure.getStringForUser( |
| 1526 | cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId); |
| 1527 | return TextUtils.delimitedStringContains(allowedProviders, ',', provider); |
| 1528 | } finally { |
| 1529 | Binder.restoreCallingIdentity(identity); |
| 1530 | } |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | /** |
| 1535 | * Returns "true" if access to the specified location provider is allowed by the specified |
| 1536 | * user's settings. Access to all location providers is forbidden to non-location-provider |
| 1537 | * processes belonging to background users. |
| 1538 | * |
| 1539 | * @param provider the name of the location provider |
| 1540 | * @param uid the requestor's UID |
| 1541 | * @param userId the user id to query |
| 1542 | */ |
| 1543 | private boolean isAllowedByUserSettingsLocked(String provider, int uid, int userId) { |
Amith Yamasani | b27528d | 2014-06-05 15:02:10 -0700 | [diff] [blame] | 1544 | if (!isCurrentProfile(UserHandle.getUserId(uid)) && !isUidALocationProvider(uid)) { |
Victoria Lease | 09eeaec | 2013-02-05 11:34:13 -0800 | [diff] [blame] | 1545 | return false; |
| 1546 | } |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1547 | return isAllowedByUserSettingsLockedForUser(provider, userId); |
Victoria Lease | 09eeaec | 2013-02-05 11:34:13 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /** |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1551 | * Returns the permission string associated with the specified resolution level. |
| 1552 | * |
| 1553 | * @param resolutionLevel the resolution level |
| 1554 | * @return the permission string |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1555 | */ |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1556 | private String getResolutionPermission(int resolutionLevel) { |
| 1557 | switch (resolutionLevel) { |
| 1558 | case RESOLUTION_LEVEL_FINE: |
| 1559 | return android.Manifest.permission.ACCESS_FINE_LOCATION; |
| 1560 | case RESOLUTION_LEVEL_COARSE: |
| 1561 | return android.Manifest.permission.ACCESS_COARSE_LOCATION; |
| 1562 | default: |
| 1563 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1564 | } |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1565 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1566 | |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1567 | /** |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1568 | * Returns the resolution level allowed to the given PID/UID pair. |
| 1569 | * |
| 1570 | * @param pid the PID |
| 1571 | * @param uid the UID |
| 1572 | * @return resolution level allowed to the pid/uid pair |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1573 | */ |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1574 | private int getAllowedResolutionLevel(int pid, int uid) { |
| 1575 | if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 1576 | pid, uid) == PERMISSION_GRANTED) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1577 | return RESOLUTION_LEVEL_FINE; |
| 1578 | } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION, |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 1579 | pid, uid) == PERMISSION_GRANTED) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1580 | return RESOLUTION_LEVEL_COARSE; |
| 1581 | } else { |
| 1582 | return RESOLUTION_LEVEL_NONE; |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1583 | } |
Victoria Lease | 4fab68b | 2012-09-13 13:20:59 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /** |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1587 | * Returns the resolution level allowed to the caller |
| 1588 | * |
| 1589 | * @return resolution level allowed to caller |
Victoria Lease | 4fab68b | 2012-09-13 13:20:59 -0700 | [diff] [blame] | 1590 | */ |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1591 | private int getCallerAllowedResolutionLevel() { |
| 1592 | return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid()); |
| 1593 | } |
| 1594 | |
| 1595 | /** |
| 1596 | * Throw SecurityException if specified resolution level is insufficient to use geofences. |
| 1597 | * |
| 1598 | * @param allowedResolutionLevel resolution level allowed to caller |
| 1599 | */ |
| 1600 | private void checkResolutionLevelIsSufficientForGeofenceUse(int allowedResolutionLevel) { |
| 1601 | if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
Victoria Lease | 4fab68b | 2012-09-13 13:20:59 -0700 | [diff] [blame] | 1602 | throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission"); |
| 1603 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1606 | /** |
| 1607 | * Return the minimum resolution level required to use the specified location provider. |
| 1608 | * |
| 1609 | * @param provider the name of the location provider |
| 1610 | * @return minimum resolution level required for provider |
| 1611 | */ |
| 1612 | private int getMinimumResolutionLevelForProviderUse(String provider) { |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1613 | if (LocationManager.GPS_PROVIDER.equals(provider) || |
| 1614 | LocationManager.PASSIVE_PROVIDER.equals(provider)) { |
| 1615 | // gps and passive providers require FINE permission |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1616 | return RESOLUTION_LEVEL_FINE; |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1617 | } else if (LocationManager.NETWORK_PROVIDER.equals(provider) || |
| 1618 | LocationManager.FUSED_PROVIDER.equals(provider)) { |
| 1619 | // network and fused providers are ok with COARSE or FINE |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1620 | return RESOLUTION_LEVEL_COARSE; |
Laurent Tu | 941221c | 2012-10-04 14:21:52 -0700 | [diff] [blame] | 1621 | } else { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1622 | for (LocationProvider lp : mProviders) { |
| 1623 | if (!lp.getName().equals(provider)) { |
| 1624 | continue; |
| 1625 | } |
| 1626 | |
Laurent Tu | 941221c | 2012-10-04 14:21:52 -0700 | [diff] [blame] | 1627 | ProviderProperties properties = lp.getProperties(); |
| 1628 | if (properties != null) { |
| 1629 | if (properties.mRequiresSatellite) { |
| 1630 | // provider requiring satellites require FINE permission |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1631 | return RESOLUTION_LEVEL_FINE; |
Laurent Tu | 941221c | 2012-10-04 14:21:52 -0700 | [diff] [blame] | 1632 | } else if (properties.mRequiresNetwork || properties.mRequiresCell) { |
| 1633 | // provider requiring network and or cell require COARSE or FINE |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1634 | return RESOLUTION_LEVEL_COARSE; |
Laurent Tu | 941221c | 2012-10-04 14:21:52 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | } |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1638 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1639 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1640 | return RESOLUTION_LEVEL_FINE; // if in doubt, require FINE |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1643 | /** |
| 1644 | * Throw SecurityException if specified resolution level is insufficient to use the named |
| 1645 | * location provider. |
| 1646 | * |
| 1647 | * @param allowedResolutionLevel resolution level allowed to caller |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1648 | * @param providerName the name of the location provider |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1649 | */ |
| 1650 | private void checkResolutionLevelIsSufficientForProviderUse(int allowedResolutionLevel, |
| 1651 | String providerName) { |
| 1652 | int requiredResolutionLevel = getMinimumResolutionLevelForProviderUse(providerName); |
| 1653 | if (allowedResolutionLevel < requiredResolutionLevel) { |
| 1654 | switch (requiredResolutionLevel) { |
| 1655 | case RESOLUTION_LEVEL_FINE: |
| 1656 | throw new SecurityException("\"" + providerName + "\" location provider " + |
| 1657 | "requires ACCESS_FINE_LOCATION permission."); |
| 1658 | case RESOLUTION_LEVEL_COARSE: |
| 1659 | throw new SecurityException("\"" + providerName + "\" location provider " + |
| 1660 | "requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission."); |
| 1661 | default: |
| 1662 | throw new SecurityException("Insufficient permission for \"" + providerName + |
| 1663 | "\" location provider."); |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1664 | } |
| 1665 | } |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1668 | /** |
| 1669 | * Throw SecurityException if WorkSource use is not allowed (i.e. can't blame other packages |
| 1670 | * for battery). |
| 1671 | */ |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 1672 | private void checkDeviceStatsAllowed() { |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1673 | mContext.enforceCallingOrSelfPermission( |
| 1674 | android.Manifest.permission.UPDATE_DEVICE_STATS, null); |
| 1675 | } |
| 1676 | |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 1677 | private void checkUpdateAppOpsAllowed() { |
| 1678 | mContext.enforceCallingOrSelfPermission( |
| 1679 | android.Manifest.permission.UPDATE_APP_OPS_STATS, null); |
| 1680 | } |
| 1681 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1682 | public static int resolutionLevelToOp(int allowedResolutionLevel) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1683 | if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) { |
| 1684 | if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1685 | return AppOpsManager.OP_COARSE_LOCATION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1686 | } else { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1687 | return AppOpsManager.OP_FINE_LOCATION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1688 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1689 | } |
| 1690 | return -1; |
| 1691 | } |
| 1692 | |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 1693 | private static String resolutionLevelToOpStr(int allowedResolutionLevel) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1694 | switch (allowedResolutionLevel) { |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 1695 | case RESOLUTION_LEVEL_COARSE: |
| 1696 | return AppOpsManager.OPSTR_COARSE_LOCATION; |
| 1697 | case RESOLUTION_LEVEL_FINE: |
| 1698 | return AppOpsManager.OPSTR_FINE_LOCATION; |
| 1699 | case RESOLUTION_LEVEL_NONE: |
| 1700 | // The client is not allowed to get any location, so both FINE and COARSE ops will |
| 1701 | // be denied. Pick the most restrictive one to be safe. |
| 1702 | return AppOpsManager.OPSTR_FINE_LOCATION; |
| 1703 | default: |
| 1704 | // Use the most restrictive ops if not sure. |
| 1705 | return AppOpsManager.OPSTR_FINE_LOCATION; |
| 1706 | } |
| 1707 | } |
| 1708 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1709 | private boolean reportLocationAccessNoThrow( |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 1710 | int pid, int uid, String packageName, int allowedResolutionLevel) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1711 | int op = resolutionLevelToOp(allowedResolutionLevel); |
| 1712 | if (op >= 0) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1713 | if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) { |
| 1714 | return false; |
| 1715 | } |
| 1716 | } |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 1717 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1718 | return getAllowedResolutionLevel(pid, uid) >= allowedResolutionLevel; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1719 | } |
| 1720 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1721 | private boolean checkLocationAccess(int pid, int uid, String packageName, |
| 1722 | int allowedResolutionLevel) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1723 | int op = resolutionLevelToOp(allowedResolutionLevel); |
| 1724 | if (op >= 0) { |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 1725 | if (mAppOps.noteOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1726 | return false; |
| 1727 | } |
| 1728 | } |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 1729 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1730 | return getAllowedResolutionLevel(pid, uid) >= allowedResolutionLevel; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1733 | /** |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 1734 | * Returns all providers by name, including passive and the ones that are not permitted to |
| 1735 | * be accessed by the calling activity or are currently disabled, but excluding fused. |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1736 | */ |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1737 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | public List<String> getAllProviders() { |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 1739 | ArrayList<String> out; |
| 1740 | synchronized (mLock) { |
| 1741 | out = new ArrayList<>(mProviders.size()); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1742 | for (LocationProvider provider : mProviders) { |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 1743 | String name = provider.getName(); |
| 1744 | if (LocationManager.FUSED_PROVIDER.equals(name)) { |
| 1745 | continue; |
| 1746 | } |
| 1747 | out.add(name); |
| 1748 | } |
| 1749 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1750 | if (D) Log.d(TAG, "getAllProviders()=" + out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | return out; |
| 1752 | } |
| 1753 | |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1754 | /** |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1755 | * Return all providers by name, that match criteria and are optionally |
| 1756 | * enabled. |
| 1757 | * Can return passive provider, but never returns fused provider. |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1758 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1759 | @Override |
| 1760 | public List<String> getProviders(Criteria criteria, boolean enabledOnly) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1761 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1762 | ArrayList<String> out; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1763 | int uid = Binder.getCallingUid(); |
Victoria Lease | 269518e | 2012-10-29 08:25:39 -0700 | [diff] [blame] | 1764 | long identity = Binder.clearCallingIdentity(); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1765 | try { |
| 1766 | synchronized (mLock) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1767 | out = new ArrayList<>(mProviders.size()); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1768 | for (LocationProvider provider : mProviders) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1769 | String name = provider.getName(); |
| 1770 | if (LocationManager.FUSED_PROVIDER.equals(name)) { |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1771 | continue; |
| 1772 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1773 | if (allowedResolutionLevel >= getMinimumResolutionLevelForProviderUse(name)) { |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1774 | if (enabledOnly |
| 1775 | && !isAllowedByUserSettingsLocked(name, uid, mCurrentUserId)) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1776 | continue; |
| 1777 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1778 | if (criteria != null |
| 1779 | && !android.location.LocationProvider.propertiesMeetCriteria( |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1780 | name, provider.getProperties(), criteria)) { |
| 1781 | continue; |
| 1782 | } |
| 1783 | out.add(name); |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1784 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1785 | } |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1786 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1787 | } finally { |
| 1788 | Binder.restoreCallingIdentity(identity); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1791 | if (D) Log.d(TAG, "getProviders()=" + out); |
| 1792 | return out; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /** |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1796 | * Return the name of the best provider given a Criteria object. |
| 1797 | * This method has been deprecated from the public API, |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1798 | * and the whole LocationProvider (including #meetsCriteria) |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1799 | * has been deprecated as well. So this method now uses |
| 1800 | * some simplified logic. |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1801 | */ |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1802 | @Override |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1803 | public String getBestProvider(Criteria criteria, boolean enabledOnly) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1804 | String result; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1805 | |
| 1806 | List<String> providers = getProviders(criteria, enabledOnly); |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1807 | if (!providers.isEmpty()) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1808 | result = pickBest(providers); |
| 1809 | if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result); |
| 1810 | return result; |
| 1811 | } |
| 1812 | providers = getProviders(null, enabledOnly); |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1813 | if (!providers.isEmpty()) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1814 | result = pickBest(providers); |
| 1815 | if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result); |
| 1816 | return result; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1819 | if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + null); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1820 | return null; |
| 1821 | } |
| 1822 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1823 | private String pickBest(List<String> providers) { |
Victoria Lease | 1925e29 | 2012-09-24 17:00:18 -0700 | [diff] [blame] | 1824 | if (providers.contains(LocationManager.GPS_PROVIDER)) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1825 | return LocationManager.GPS_PROVIDER; |
Victoria Lease | 1925e29 | 2012-09-24 17:00:18 -0700 | [diff] [blame] | 1826 | } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { |
| 1827 | return LocationManager.NETWORK_PROVIDER; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1828 | } else { |
| 1829 | return providers.get(0); |
| 1830 | } |
| 1831 | } |
| 1832 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1833 | @Override |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1834 | public boolean providerMeetsCriteria(String provider, Criteria criteria) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1835 | LocationProvider p = mProvidersByName.get(provider); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1836 | if (p == null) { |
| 1837 | throw new IllegalArgumentException("provider=" + provider); |
| 1838 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1839 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1840 | boolean result = android.location.LocationProvider.propertiesMeetCriteria( |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1841 | p.getName(), p.getProperties(), criteria); |
| 1842 | if (D) Log.d(TAG, "providerMeetsCriteria(" + provider + ", " + criteria + ")=" + result); |
| 1843 | return result; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1846 | private void updateProvidersSettingsLocked() { |
| 1847 | for (LocationProvider p : mProviders) { |
| 1848 | p.setSettingsEnabled(isAllowedByUserSettingsLockedForUser(p.getName(), mCurrentUserId)); |
Brad Fitzpatrick | 0c5a040 | 2010-08-27 14:01:23 -0700 | [diff] [blame] | 1849 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1850 | |
| 1851 | mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION), |
| 1852 | UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1855 | private void updateProviderListenersLocked(String provider) { |
| 1856 | LocationProvider p = mProvidersByName.get(provider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1857 | if (p == null) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1859 | boolean enabled = p.isEnabled(); |
| 1860 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1861 | ArrayList<Receiver> deadReceivers = null; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider); |
| 1864 | if (records != null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1865 | for (UpdateRecord record : records) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1866 | if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1867 | // Sends a notification message to the receiver |
| 1868 | if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) { |
| 1869 | if (deadReceivers == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1870 | deadReceivers = new ArrayList<>(); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1871 | } |
| 1872 | deadReceivers.add(record.mReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | if (deadReceivers != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1879 | for (int i = deadReceivers.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | removeUpdatesLocked(deadReceivers.get(i)); |
| 1881 | } |
| 1882 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1883 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1884 | applyRequirementsLocked(provider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1885 | } |
| 1886 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1887 | private void applyRequirementsLocked(String provider) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1888 | LocationProvider p = mProvidersByName.get(provider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1889 | if (p == null) return; |
| 1890 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1892 | WorkSource worksource = new WorkSource(); |
| 1893 | ProviderRequest providerRequest = new ProviderRequest(); |
| 1894 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1895 | ContentResolver resolver = mContext.getContentResolver(); |
| 1896 | long backgroundThrottleInterval = Settings.Global.getLong( |
| 1897 | resolver, |
| 1898 | Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS, |
| 1899 | DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS); |
| 1900 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1901 | if (p.isEnabled() && records != null && !records.isEmpty()) { |
| 1902 | // initialize the low power mode to true and set to false if any of the records requires |
| 1903 | providerRequest.lowPowerMode = true; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1904 | for (UpdateRecord record : records) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1905 | if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) { |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 1906 | if (checkLocationAccess( |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1907 | record.mReceiver.mIdentity.mPid, |
| 1908 | record.mReceiver.mIdentity.mUid, |
| 1909 | record.mReceiver.mIdentity.mPackageName, |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1910 | record.mReceiver.mAllowedResolutionLevel)) { |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 1911 | LocationRequest locationRequest = record.mRealRequest; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1912 | long interval = locationRequest.getInterval(); |
| 1913 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1914 | if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1915 | if (!record.mIsForegroundUid) { |
| 1916 | interval = Math.max(interval, backgroundThrottleInterval); |
| 1917 | } |
| 1918 | if (interval != locationRequest.getInterval()) { |
| 1919 | locationRequest = new LocationRequest(locationRequest); |
| 1920 | locationRequest.setInterval(interval); |
| 1921 | } |
| 1922 | } |
| 1923 | |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 1924 | record.mRequest = locationRequest; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1925 | providerRequest.locationRequests.add(locationRequest); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1926 | if (!locationRequest.isLowPowerMode()) { |
| 1927 | providerRequest.lowPowerMode = false; |
| 1928 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1929 | if (interval < providerRequest.interval) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1930 | providerRequest.reportLocation = true; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1931 | providerRequest.interval = interval; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1932 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1933 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1936 | |
| 1937 | if (providerRequest.reportLocation) { |
| 1938 | // calculate who to blame for power |
| 1939 | // This is somewhat arbitrary. We pick a threshold interval |
| 1940 | // that is slightly higher that the minimum interval, and |
| 1941 | // spread the blame across all applications with a request |
| 1942 | // under that threshold. |
| 1943 | long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2; |
| 1944 | for (UpdateRecord record : records) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1945 | if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1946 | LocationRequest locationRequest = record.mRequest; |
Svet Ganov | e998c73 | 2016-06-10 00:12:38 -0700 | [diff] [blame] | 1947 | |
| 1948 | // Don't assign battery blame for update records whose |
| 1949 | // client has no permission to receive location data. |
| 1950 | if (!providerRequest.locationRequests.contains(locationRequest)) { |
| 1951 | continue; |
| 1952 | } |
| 1953 | |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1954 | if (locationRequest.getInterval() <= thresholdInterval) { |
David Christie | e55c968 | 2013-08-22 10:10:34 -0700 | [diff] [blame] | 1955 | if (record.mReceiver.mWorkSource != null |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1956 | && isValidWorkSource(record.mReceiver.mWorkSource)) { |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1957 | worksource.add(record.mReceiver.mWorkSource); |
| 1958 | } else { |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1959 | // Assign blame to caller if there's no WorkSource associated with |
| 1960 | // the request or if it's invalid. |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1961 | worksource.add( |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1962 | record.mReceiver.mIdentity.mUid, |
| 1963 | record.mReceiver.mIdentity.mPackageName); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1964 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1965 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1966 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1967 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1968 | } |
| 1969 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1970 | |
| 1971 | if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest); |
| 1972 | p.setRequest(providerRequest, worksource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1975 | /** |
| 1976 | * Whether a given {@code WorkSource} associated with a Location request is valid. |
| 1977 | */ |
| 1978 | private static boolean isValidWorkSource(WorkSource workSource) { |
| 1979 | if (workSource.size() > 0) { |
| 1980 | // If the WorkSource has one or more non-chained UIDs, make sure they're accompanied |
| 1981 | // by tags. |
| 1982 | return workSource.getName(0) != null; |
| 1983 | } else { |
| 1984 | // For now, make sure callers have supplied an attribution tag for use with |
| 1985 | // AppOpsManager. This might be relaxed in the future. |
| 1986 | final ArrayList<WorkChain> workChains = workSource.getWorkChains(); |
| 1987 | return workChains != null && !workChains.isEmpty() && |
| 1988 | workChains.get(0).getAttributionTag() != null; |
| 1989 | } |
| 1990 | } |
| 1991 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1992 | @Override |
| 1993 | public String[] getBackgroundThrottlingWhitelist() { |
| 1994 | synchronized (mLock) { |
| 1995 | return mBackgroundThrottlePackageWhitelist.toArray( |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1996 | new String[0]); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | private void updateBackgroundThrottlingWhitelistLocked() { |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 2001 | String setting = Settings.Global.getString( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2002 | mContext.getContentResolver(), |
| 2003 | Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST); |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 2004 | if (setting == null) { |
| 2005 | setting = ""; |
| 2006 | } |
| 2007 | |
| 2008 | mBackgroundThrottlePackageWhitelist.clear(); |
| 2009 | mBackgroundThrottlePackageWhitelist.addAll( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2010 | SystemConfig.getInstance().getAllowUnthrottledLocation()); |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 2011 | mBackgroundThrottlePackageWhitelist.addAll( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2012 | Arrays.asList(setting.split(","))); |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 2013 | } |
| 2014 | |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 2015 | private void updateLastLocationMaxAgeLocked() { |
| 2016 | mLastLocationMaxAgeMs = |
| 2017 | Settings.Global.getLong( |
| 2018 | mContext.getContentResolver(), |
| 2019 | Settings.Global.LOCATION_LAST_LOCATION_MAX_AGE_MILLIS, |
| 2020 | DEFAULT_LAST_LOCATION_MAX_AGE_MS); |
| 2021 | } |
| 2022 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2023 | private boolean isThrottlingExemptLocked(Identity identity) { |
| 2024 | if (identity.mUid == Process.SYSTEM_UID) { |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 2025 | return true; |
| 2026 | } |
| 2027 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2028 | if (mBackgroundThrottlePackageWhitelist.contains(identity.mPackageName)) { |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 2029 | return true; |
| 2030 | } |
| 2031 | |
| 2032 | for (LocationProviderProxy provider : mProxyProviders) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2033 | if (identity.mPackageName.equals(provider.getConnectedPackageName())) { |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 2034 | return true; |
| 2035 | } |
| 2036 | } |
| 2037 | |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 2038 | return false; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2041 | private class UpdateRecord { |
| 2042 | final String mProvider; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2043 | private final LocationRequest mRealRequest; // original request from client |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2044 | LocationRequest mRequest; // possibly throttled version of the request |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2045 | private final Receiver mReceiver; |
| 2046 | private boolean mIsForegroundUid; |
| 2047 | private Location mLastFixBroadcast; |
| 2048 | private long mLastStatusBroadcast; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2049 | |
| 2050 | /** |
| 2051 | * Note: must be constructed with lock held. |
| 2052 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2053 | private UpdateRecord(String provider, LocationRequest request, Receiver receiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2054 | mProvider = provider; |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2055 | mRealRequest = request; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2056 | mRequest = request; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2057 | mReceiver = receiver; |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2058 | mIsForegroundUid = isImportanceForeground( |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2059 | mActivityManager.getPackageImportance(mReceiver.mIdentity.mPackageName)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | |
| 2061 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider); |
| 2062 | if (records == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2063 | records = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2064 | mRecordsByProvider.put(provider, records); |
| 2065 | } |
| 2066 | if (!records.contains(this)) { |
| 2067 | records.add(this); |
| 2068 | } |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2069 | |
| 2070 | // Update statistics for historical location requests by package/provider |
| 2071 | mRequestStatistics.startRequesting( |
Wyatt Riley | f7075e0 | 2018-04-12 17:54:26 -0700 | [diff] [blame] | 2072 | mReceiver.mIdentity.mPackageName, provider, request.getInterval(), |
| 2073 | mIsForegroundUid); |
| 2074 | } |
| 2075 | |
| 2076 | /** |
| 2077 | * Method to be called when record changes foreground/background |
| 2078 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2079 | private void updateForeground(boolean isForeground) { |
Wyatt Riley | f7075e0 | 2018-04-12 17:54:26 -0700 | [diff] [blame] | 2080 | mIsForegroundUid = isForeground; |
| 2081 | mRequestStatistics.updateForeground( |
| 2082 | mReceiver.mIdentity.mPackageName, mProvider, isForeground); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | /** |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2086 | * Method to be called when a record will no longer be used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2087 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2088 | private void disposeLocked(boolean removeReceiver) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2089 | mRequestStatistics.stopRequesting(mReceiver.mIdentity.mPackageName, mProvider); |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2090 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2091 | // remove from mRecordsByProvider |
| 2092 | ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider); |
| 2093 | if (globalRecords != null) { |
| 2094 | globalRecords.remove(this); |
| 2095 | } |
| 2096 | |
| 2097 | if (!removeReceiver) return; // the caller will handle the rest |
| 2098 | |
| 2099 | // remove from Receiver#mUpdateRecords |
| 2100 | HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2101 | receiverRecords.remove(this.mProvider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2102 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2103 | // and also remove the Receiver if it has no more update records |
| 2104 | if (receiverRecords.size() == 0) { |
| 2105 | removeUpdatesLocked(mReceiver); |
Mike Lockwood | 3a76fd6 | 2009-09-01 07:26:56 -0400 | [diff] [blame] | 2106 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | @Override |
| 2110 | public String toString() { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2111 | return "UpdateRecord[" + mProvider + " " + mReceiver.mIdentity.mPackageName |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2112 | + "(" + mReceiver.mIdentity.mUid + (mIsForegroundUid ? " foreground" |
| 2113 | : " background") |
Wyatt Riley | 19adc02 | 2018-05-22 13:30:51 -0700 | [diff] [blame] | 2114 | + ")" + " " + mRealRequest + " " |
| 2115 | + mReceiver.mWorkSource + "]"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2117 | } |
| 2118 | |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 2119 | private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid, |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2120 | String packageName, WorkSource workSource, boolean hideFromAppOps) { |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2121 | IBinder binder = listener.asBinder(); |
| 2122 | Receiver receiver = mReceivers.get(binder); |
| 2123 | if (receiver == null) { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2124 | receiver = new Receiver(listener, null, pid, uid, packageName, workSource, |
| 2125 | hideFromAppOps); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2126 | try { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2127 | receiver.getListener().asBinder().linkToDeath(receiver, 0); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2128 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2129 | Slog.e(TAG, "linkToDeath failed:", e); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2130 | return null; |
| 2131 | } |
Wen Jing | cb3ab22 | 2014-03-27 13:42:59 +0800 | [diff] [blame] | 2132 | mReceivers.put(binder, receiver); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2133 | } |
| 2134 | return receiver; |
| 2135 | } |
| 2136 | |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 2137 | private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName, |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2138 | WorkSource workSource, boolean hideFromAppOps) { |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2139 | Receiver receiver = mReceivers.get(intent); |
| 2140 | if (receiver == null) { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2141 | receiver = new Receiver(null, intent, pid, uid, packageName, workSource, |
| 2142 | hideFromAppOps); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2143 | mReceivers.put(intent, receiver); |
| 2144 | } |
| 2145 | return receiver; |
| 2146 | } |
| 2147 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2148 | /** |
| 2149 | * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution |
| 2150 | * and consistency requirements. |
| 2151 | * |
| 2152 | * @param request the LocationRequest from which to create a sanitized version |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2153 | * @return a version of request that meets the given resolution and consistency requirements |
| 2154 | * @hide |
| 2155 | */ |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2156 | private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel, |
| 2157 | boolean callerHasLocationHardwarePermission) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2158 | LocationRequest sanitizedRequest = new LocationRequest(request); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2159 | if (!callerHasLocationHardwarePermission) { |
| 2160 | // allow setting low power mode only for callers with location hardware permission |
| 2161 | sanitizedRequest.setLowPowerMode(false); |
| 2162 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2163 | if (resolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 2164 | switch (sanitizedRequest.getQuality()) { |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2165 | case LocationRequest.ACCURACY_FINE: |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2166 | sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2167 | break; |
| 2168 | case LocationRequest.POWER_HIGH: |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2169 | sanitizedRequest.setQuality(LocationRequest.POWER_LOW); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2170 | break; |
| 2171 | } |
| 2172 | // throttle |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2173 | if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) { |
| 2174 | sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2175 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2176 | if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) { |
| 2177 | sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2178 | } |
Nick Pelly | 74fa7ea | 2012-08-13 19:36:38 -0700 | [diff] [blame] | 2179 | } |
Nick Pelly | 4e31c4f | 2012-08-13 19:35:39 -0700 | [diff] [blame] | 2180 | // make getFastestInterval() the minimum of interval and fastest interval |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2181 | if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) { |
Nick Pelly | 4e31c4f | 2012-08-13 19:35:39 -0700 | [diff] [blame] | 2182 | request.setFastestInterval(request.getInterval()); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2183 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2184 | return sanitizedRequest; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2185 | } |
| 2186 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2187 | private void checkPackageName(String packageName) { |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2188 | if (packageName == null) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2189 | throw new SecurityException("invalid package name: " + null); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2190 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2191 | int uid = Binder.getCallingUid(); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2192 | String[] packages = mPackageManager.getPackagesForUid(uid); |
| 2193 | if (packages == null) { |
| 2194 | throw new SecurityException("invalid UID " + uid); |
| 2195 | } |
| 2196 | for (String pkg : packages) { |
| 2197 | if (packageName.equals(pkg)) return; |
| 2198 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2199 | throw new SecurityException("invalid package name: " + packageName); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2200 | } |
| 2201 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2202 | private void checkPendingIntent(PendingIntent intent) { |
| 2203 | if (intent == null) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2204 | throw new IllegalArgumentException("invalid pending intent: " + null); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2205 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 2208 | private Receiver checkListenerOrIntentLocked(ILocationListener listener, PendingIntent intent, |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2209 | int pid, int uid, String packageName, WorkSource workSource, boolean hideFromAppOps) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2210 | if (intent == null && listener == null) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2211 | throw new IllegalArgumentException("need either listener or intent"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2212 | } else if (intent != null && listener != null) { |
| 2213 | throw new IllegalArgumentException("cannot register both listener and intent"); |
| 2214 | } else if (intent != null) { |
| 2215 | checkPendingIntent(intent); |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2216 | return getReceiverLocked(intent, pid, uid, packageName, workSource, hideFromAppOps); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2217 | } else { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2218 | return getReceiverLocked(listener, pid, uid, packageName, workSource, hideFromAppOps); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2219 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2222 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2223 | public void requestLocationUpdates(LocationRequest request, ILocationListener listener, |
| 2224 | PendingIntent intent, String packageName) { |
| 2225 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
| 2226 | checkPackageName(packageName); |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2227 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
| 2228 | checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel, |
| 2229 | request.getProvider()); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 2230 | WorkSource workSource = request.getWorkSource(); |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 2231 | if (workSource != null && !workSource.isEmpty()) { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2232 | checkDeviceStatsAllowed(); |
| 2233 | } |
| 2234 | boolean hideFromAppOps = request.getHideFromAppOps(); |
| 2235 | if (hideFromAppOps) { |
| 2236 | checkUpdateAppOpsAllowed(); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 2237 | } |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2238 | boolean callerHasLocationHardwarePermission = |
| 2239 | mContext.checkCallingPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 2240 | == PERMISSION_GRANTED; |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2241 | LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel, |
| 2242 | callerHasLocationHardwarePermission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2244 | final int pid = Binder.getCallingPid(); |
| 2245 | final int uid = Binder.getCallingUid(); |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2246 | // providers may use public location API's, need to clear identity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2247 | long identity = Binder.clearCallingIdentity(); |
| 2248 | try { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2249 | // We don't check for MODE_IGNORED here; we will do that when we go to deliver |
| 2250 | // a location. |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 2251 | checkLocationAccess(pid, uid, packageName, allowedResolutionLevel); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2252 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2253 | synchronized (mLock) { |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 2254 | Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid, |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2255 | packageName, workSource, hideFromAppOps); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2256 | requestLocationUpdatesLocked(sanitizedRequest, recevier, uid, packageName); |
Mike Lockwood | 2d4d1bf | 2010-10-18 17:06:26 -0400 | [diff] [blame] | 2257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2258 | } finally { |
| 2259 | Binder.restoreCallingIdentity(identity); |
| 2260 | } |
| 2261 | } |
| 2262 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2263 | private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver, |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2264 | int uid, String packageName) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2265 | // Figure out the provider. Either its explicitly request (legacy use cases), or |
| 2266 | // use the fused provider |
| 2267 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
| 2268 | String name = request.getProvider(); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2269 | if (name == null) { |
| 2270 | throw new IllegalArgumentException("provider name must not be null"); |
| 2271 | } |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 2272 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2273 | LocationProvider provider = mProvidersByName.get(name); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2274 | if (provider == null) { |
Victoria Lease | b30f383 | 2013-10-13 12:15:40 -0700 | [diff] [blame] | 2275 | throw new IllegalArgumentException("provider doesn't exist: " + name); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2278 | UpdateRecord record = new UpdateRecord(name, request, receiver); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2279 | if (D) { |
| 2280 | Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver)) |
| 2281 | + " " + name + " " + request + " from " + packageName + "(" + uid + " " |
| 2282 | + (record.mIsForegroundUid ? "foreground" : "background") |
| 2283 | + (isThrottlingExemptLocked(receiver.mIdentity) |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2284 | ? " [whitelisted]" : "") + ")"); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2285 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2286 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2287 | UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record); |
| 2288 | if (oldRecord != null) { |
| 2289 | oldRecord.disposeLocked(false); |
| 2290 | } |
| 2291 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2292 | if (provider.isEnabled()) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2293 | applyRequirementsLocked(name); |
| 2294 | } else { |
| 2295 | // Notify the listener that updates are currently disabled |
| 2296 | receiver.callProviderEnabledLocked(name, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2297 | } |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 2298 | // Update the monitoring here just in case multiple location requests were added to the |
| 2299 | // same receiver (this request may be high power and the initial might not have been). |
| 2300 | receiver.updateMonitoring(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2303 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2304 | public void removeUpdates(ILocationListener listener, PendingIntent intent, |
| 2305 | String packageName) { |
| 2306 | checkPackageName(packageName); |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2307 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2308 | final int pid = Binder.getCallingPid(); |
| 2309 | final int uid = Binder.getCallingUid(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2310 | |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 2311 | synchronized (mLock) { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 2312 | Receiver receiver = checkListenerOrIntentLocked(listener, intent, pid, uid, |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2313 | packageName, null, false); |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 2314 | |
| 2315 | // providers may use public location API's, need to clear identity |
| 2316 | long identity = Binder.clearCallingIdentity(); |
| 2317 | try { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2318 | removeUpdatesLocked(receiver); |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 2319 | } finally { |
| 2320 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | private void removeUpdatesLocked(Receiver receiver) { |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 2326 | if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver))); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2327 | |
| 2328 | if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) { |
| 2329 | receiver.getListener().asBinder().unlinkToDeath(receiver, 0); |
| 2330 | synchronized (receiver) { |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 2331 | receiver.clearPendingBroadcastsLocked(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2332 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2333 | } |
| 2334 | |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 2335 | receiver.updateMonitoring(false); |
| 2336 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2337 | // Record which providers were associated with this listener |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2338 | HashSet<String> providers = new HashSet<>(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2339 | HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords; |
| 2340 | if (oldRecords != null) { |
| 2341 | // Call dispose() on the obsolete update records. |
| 2342 | for (UpdateRecord record : oldRecords.values()) { |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2343 | // Update statistics for historical location requests by package/provider |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2344 | record.disposeLocked(false); |
| 2345 | } |
| 2346 | // Accumulate providers |
| 2347 | providers.addAll(oldRecords.keySet()); |
| 2348 | } |
| 2349 | |
| 2350 | // update provider |
| 2351 | for (String provider : providers) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2352 | applyRequirementsLocked(provider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2356 | private void applyAllProviderRequirementsLocked() { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2357 | for (LocationProvider p : mProviders) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2358 | applyRequirementsLocked(p.getName()); |
| 2359 | } |
| 2360 | } |
| 2361 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2362 | @Override |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 2363 | public Location getLastLocation(LocationRequest request, String packageName) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2364 | if (D) Log.d(TAG, "getLastLocation: " + request); |
| 2365 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2366 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 2367 | checkPackageName(packageName); |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2368 | checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel, |
| 2369 | request.getProvider()); |
| 2370 | // no need to sanitize this request, as only the provider name is used |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 2371 | |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 2372 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2373 | final int uid = Binder.getCallingUid(); |
| 2374 | final long identity = Binder.clearCallingIdentity(); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2375 | try { |
| 2376 | if (mBlacklist.isBlacklisted(packageName)) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2377 | if (D) { |
| 2378 | Log.d(TAG, "not returning last loc for blacklisted app: " + |
| 2379 | packageName); |
| 2380 | } |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2381 | return null; |
| 2382 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2383 | |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 2384 | if (!reportLocationAccessNoThrow(pid, uid, packageName, allowedResolutionLevel)) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2385 | if (D) { |
| 2386 | Log.d(TAG, "not returning last loc for no op app: " + |
| 2387 | packageName); |
| 2388 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2389 | return null; |
| 2390 | } |
| 2391 | |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2392 | synchronized (mLock) { |
| 2393 | // Figure out the provider. Either its explicitly request (deprecated API's), |
| 2394 | // or use the fused provider |
| 2395 | String name = request.getProvider(); |
| 2396 | if (name == null) name = LocationManager.FUSED_PROVIDER; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2397 | LocationProvider provider = mProvidersByName.get(name); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2398 | if (provider == null) return null; |
| 2399 | |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2400 | if (!isAllowedByUserSettingsLocked(name, uid, mCurrentUserId)) return null; |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2401 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2402 | Location location; |
| 2403 | if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 2404 | // Make sure that an app with coarse permissions can't get frequent location |
| 2405 | // updates by calling LocationManager.getLastKnownLocation repeatedly. |
| 2406 | location = mLastLocationCoarseInterval.get(name); |
| 2407 | } else { |
| 2408 | location = mLastLocation.get(name); |
| 2409 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2410 | if (location == null) { |
| 2411 | return null; |
| 2412 | } |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 2413 | |
| 2414 | // Don't return stale location to apps with foreground-only location permission. |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 2415 | String op = resolutionLevelToOpStr(allowedResolutionLevel); |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 2416 | long locationAgeMs = SystemClock.elapsedRealtime() - |
| 2417 | location.getElapsedRealtimeNanos() / NANOS_PER_MILLI; |
| 2418 | if ((locationAgeMs > mLastLocationMaxAgeMs) |
| 2419 | && (mAppOps.unsafeCheckOp(op, uid, packageName) |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2420 | == AppOpsManager.MODE_FOREGROUND)) { |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 2421 | return null; |
| 2422 | } |
| 2423 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2424 | if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2425 | Location noGPSLocation = location.getExtraLocation( |
| 2426 | Location.EXTRA_NO_GPS_LOCATION); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2427 | if (noGPSLocation != null) { |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 2428 | return new Location(mLocationFudger.getOrCreate(noGPSLocation)); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2429 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2430 | } else { |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 2431 | return new Location(location); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2432 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2433 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2434 | return null; |
| 2435 | } finally { |
| 2436 | Binder.restoreCallingIdentity(identity); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2440 | /** |
| 2441 | * Provides an interface to inject and set the last location if location is not available |
| 2442 | * currently. |
| 2443 | * |
| 2444 | * This helps in cases where the product (Cars for example) has saved the last known location |
| 2445 | * before powering off. This interface lets the client inject the saved location while the GPS |
| 2446 | * chipset is getting its first fix, there by improving user experience. |
| 2447 | * |
| 2448 | * @param location - Location object to inject |
| 2449 | * @return true if update was successful, false if not |
| 2450 | */ |
| 2451 | @Override |
| 2452 | public boolean injectLocation(Location location) { |
| 2453 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 2454 | "Location Hardware permission not granted to inject location"); |
| 2455 | mContext.enforceCallingPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, |
| 2456 | "Access Fine Location permission not granted to inject Location"); |
| 2457 | |
| 2458 | if (location == null) { |
| 2459 | if (D) { |
| 2460 | Log.d(TAG, "injectLocation(): called with null location"); |
| 2461 | } |
| 2462 | return false; |
| 2463 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2464 | LocationProvider p = null; |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2465 | String provider = location.getProvider(); |
| 2466 | if (provider != null) { |
| 2467 | p = mProvidersByName.get(provider); |
| 2468 | } |
| 2469 | if (p == null) { |
| 2470 | if (D) { |
| 2471 | Log.d(TAG, "injectLocation(): unknown provider"); |
| 2472 | } |
| 2473 | return false; |
| 2474 | } |
| 2475 | synchronized (mLock) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2476 | if (!isAllowedByUserSettingsLockedForUser(provider, mCurrentUserId)) { |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2477 | if (D) { |
| 2478 | Log.d(TAG, "Location disabled in Settings for current user:" + mCurrentUserId); |
| 2479 | } |
| 2480 | return false; |
| 2481 | } else { |
| 2482 | // NOTE: If last location is already available, location is not injected. If |
| 2483 | // provider's normal source (like a GPS chipset) have already provided an output, |
| 2484 | // there is no need to inject this location. |
| 2485 | if (mLastLocation.get(provider) == null) { |
| 2486 | updateLastLocationLocked(location, provider); |
| 2487 | } else { |
| 2488 | if (D) { |
| 2489 | Log.d(TAG, "injectLocation(): Location exists. Not updating"); |
| 2490 | } |
| 2491 | return false; |
| 2492 | } |
| 2493 | } |
| 2494 | } |
| 2495 | return true; |
| 2496 | } |
| 2497 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2498 | @Override |
| 2499 | public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent, |
| 2500 | String packageName) { |
| 2501 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2502 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
| 2503 | checkResolutionLevelIsSufficientForGeofenceUse(allowedResolutionLevel); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2504 | checkPendingIntent(intent); |
| 2505 | checkPackageName(packageName); |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2506 | checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel, |
| 2507 | request.getProvider()); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2508 | // Require that caller can manage given document |
| 2509 | boolean callerHasLocationHardwarePermission = |
| 2510 | mContext.checkCallingPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 2511 | == PERMISSION_GRANTED; |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2512 | LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel, |
| 2513 | callerHasLocationHardwarePermission); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2514 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2515 | if (D) Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2516 | |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2517 | // geo-fence manager uses the public location API, need to clear identity |
| 2518 | int uid = Binder.getCallingUid(); |
Xiaohui Chen | a449062 | 2015-09-22 15:29:31 -0700 | [diff] [blame] | 2519 | // TODO: http://b/23822629 |
| 2520 | if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) { |
Victoria Lease | 56e675b | 2012-11-05 19:25:06 -0800 | [diff] [blame] | 2521 | // temporary measure until geofences work for secondary users |
| 2522 | Log.w(TAG, "proximity alerts are currently available only to the primary user"); |
| 2523 | return; |
| 2524 | } |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2525 | long identity = Binder.clearCallingIdentity(); |
| 2526 | try { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2527 | mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel, |
| 2528 | uid, packageName); |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2529 | } finally { |
| 2530 | Binder.restoreCallingIdentity(identity); |
| 2531 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | @Override |
| 2535 | public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2536 | checkPendingIntent(intent); |
| 2537 | checkPackageName(packageName); |
| 2538 | |
| 2539 | if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent); |
| 2540 | |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2541 | // geo-fence manager uses the public location API, need to clear identity |
| 2542 | long identity = Binder.clearCallingIdentity(); |
| 2543 | try { |
| 2544 | mGeofenceManager.removeFence(geofence, intent); |
| 2545 | } finally { |
| 2546 | Binder.restoreCallingIdentity(identity); |
| 2547 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2550 | @Override |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 2551 | public boolean registerGnssStatusCallback(IGnssStatusListener callback, String packageName) { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 2552 | if (!hasGnssPermissions(packageName) || mGnssStatusProvider == null) { |
Takayuki Hoshi | b254ab6a | 2014-10-23 16:46:02 +0900 | [diff] [blame] | 2553 | return false; |
| 2554 | } |
| 2555 | |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 2556 | // TODO(b/120449926): The GNSS status listeners should be handled similar to the GNSS |
| 2557 | // measurements listeners. |
| 2558 | return mGnssStatusProvider.addListener(callback, Binder.getCallingUid(), packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | } |
| 2560 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2561 | @Override |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 2562 | public void unregisterGnssStatusCallback(IGnssStatusListener callback) { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 2563 | mGnssStatusProvider.removeListener(callback); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2564 | } |
| 2565 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2566 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 2567 | public boolean addGnssMeasurementsListener( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2568 | IGnssMeasurementsListener listener, String packageName) { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 2569 | if (!hasGnssPermissions(packageName) || mGnssMeasurementsProvider == null) { |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 2570 | return false; |
| 2571 | } |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2572 | |
| 2573 | synchronized (mLock) { |
| 2574 | Identity callerIdentity |
| 2575 | = new Identity(Binder.getCallingUid(), Binder.getCallingPid(), packageName); |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 2576 | // TODO(b/120481270): Register for client death notification and update map. |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 2577 | mGnssMeasurementsListeners.put(listener.asBinder(), callerIdentity); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2578 | long identity = Binder.clearCallingIdentity(); |
| 2579 | try { |
| 2580 | if (isThrottlingExemptLocked(callerIdentity) |
| 2581 | || isImportanceForeground( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2582 | mActivityManager.getPackageImportance(packageName))) { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 2583 | return mGnssMeasurementsProvider.addListener(listener, |
| 2584 | callerIdentity.mUid, callerIdentity.mPackageName); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2585 | } |
| 2586 | } finally { |
| 2587 | Binder.restoreCallingIdentity(identity); |
| 2588 | } |
| 2589 | |
| 2590 | return true; |
| 2591 | } |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 2595 | public void removeGnssMeasurementsListener(IGnssMeasurementsListener listener) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2596 | if (mGnssMeasurementsProvider == null) { |
| 2597 | return; |
| 2598 | } |
| 2599 | |
| 2600 | synchronized (mLock) { |
| 2601 | mGnssMeasurementsListeners.remove(listener.asBinder()); |
| 2602 | mGnssMeasurementsProvider.removeListener(listener); |
Wei Liu | 5241a4c | 2015-05-11 14:00:36 -0700 | [diff] [blame] | 2603 | } |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 2607 | public boolean addGnssNavigationMessageListener( |
| 2608 | IGnssNavigationMessageListener listener, |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 2609 | String packageName) { |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 2610 | if (!hasGnssPermissions(packageName) || mGnssNavigationMessageProvider == null) { |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 2611 | return false; |
| 2612 | } |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2613 | |
| 2614 | synchronized (mLock) { |
| 2615 | Identity callerIdentity |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2616 | = new Identity(Binder.getCallingUid(), Binder.getCallingPid(), packageName); |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 2617 | // TODO(b/120481270): Register for client death notification and update map. |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 2618 | mGnssNavigationMessageListeners.put(listener.asBinder(), callerIdentity); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2619 | long identity = Binder.clearCallingIdentity(); |
| 2620 | try { |
| 2621 | if (isThrottlingExemptLocked(callerIdentity) |
| 2622 | || isImportanceForeground( |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2623 | mActivityManager.getPackageImportance(packageName))) { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 2624 | return mGnssNavigationMessageProvider.addListener(listener, |
| 2625 | callerIdentity.mUid, callerIdentity.mPackageName); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2626 | } |
| 2627 | } finally { |
| 2628 | Binder.restoreCallingIdentity(identity); |
| 2629 | } |
| 2630 | |
| 2631 | return true; |
| 2632 | } |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 2636 | public void removeGnssNavigationMessageListener(IGnssNavigationMessageListener listener) { |
| 2637 | if (mGnssNavigationMessageProvider != null) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2638 | synchronized (mLock) { |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 2639 | mGnssNavigationMessageListeners.remove(listener.asBinder()); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 2640 | mGnssNavigationMessageProvider.removeListener(listener); |
| 2641 | } |
Wei Liu | 5241a4c | 2015-05-11 14:00:36 -0700 | [diff] [blame] | 2642 | } |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2646 | public boolean sendExtraCommand(String provider, String command, Bundle extras) { |
Mike Lockwood | c6cc836 | 2009-08-17 13:16:08 -0400 | [diff] [blame] | 2647 | if (provider == null) { |
| 2648 | // throw NullPointerException to remain compatible with previous implementation |
| 2649 | throw new NullPointerException(); |
| 2650 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2651 | checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(), |
| 2652 | provider); |
Mike Lockwood | c6cc836 | 2009-08-17 13:16:08 -0400 | [diff] [blame] | 2653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | // and check for ACCESS_LOCATION_EXTRA_COMMANDS |
Mike Lockwood | b7e9922 | 2009-07-07 13:18:21 -0400 | [diff] [blame] | 2655 | if ((mContext.checkCallingOrSelfPermission(ACCESS_LOCATION_EXTRA_COMMANDS) |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 2656 | != PERMISSION_GRANTED)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2657 | throw new SecurityException("Requires ACCESS_LOCATION_EXTRA_COMMANDS permission"); |
| 2658 | } |
| 2659 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 2660 | synchronized (mLock) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2661 | LocationProvider p = mProvidersByName.get(provider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2662 | if (p == null) return false; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2663 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2664 | p.sendExtraCommand(command, extras); |
| 2665 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2669 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2670 | public boolean sendNiResponse(int notifId, int userResponse) { |
Mike Lockwood | 18ad9f6 | 2009-08-27 14:01:23 -0700 | [diff] [blame] | 2671 | if (Binder.getCallingUid() != Process.myUid()) { |
| 2672 | throw new SecurityException( |
| 2673 | "calling sendNiResponse from outside of the system is not allowed"); |
| 2674 | } |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 2675 | try { |
| 2676 | return mNetInitiatedListener.sendNiResponse(notifId, userResponse); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2677 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2678 | Slog.e(TAG, "RemoteException in LocationManagerService.sendNiResponse"); |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 2679 | return false; |
| 2680 | } |
| 2681 | } |
| 2682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2683 | /** |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 2684 | * @return null if the provider does not exist |
Alexey Tarasov | f2db9fb | 2009-09-01 02:37:07 +1100 | [diff] [blame] | 2685 | * @throws SecurityException if the provider is not allowed to be |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2686 | * accessed by the caller |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | */ |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2688 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2689 | public ProviderProperties getProviderProperties(String provider) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2690 | checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(), |
| 2691 | provider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2693 | LocationProvider p; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2694 | synchronized (mLock) { |
| 2695 | p = mProvidersByName.get(provider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2696 | } |
| 2697 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2698 | if (p == null) return null; |
| 2699 | return p.getProperties(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2700 | } |
| 2701 | |
Jason Monk | b71218a | 2015-06-17 14:44:39 -0400 | [diff] [blame] | 2702 | /** |
| 2703 | * @return null if the provider does not exist |
| 2704 | * @throws SecurityException if the provider is not allowed to be |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2705 | * accessed by the caller |
Jason Monk | b71218a | 2015-06-17 14:44:39 -0400 | [diff] [blame] | 2706 | */ |
| 2707 | @Override |
| 2708 | public String getNetworkProviderPackage() { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2709 | LocationProvider p; |
Jason Monk | b71218a | 2015-06-17 14:44:39 -0400 | [diff] [blame] | 2710 | synchronized (mLock) { |
Jason Monk | b71218a | 2015-06-17 14:44:39 -0400 | [diff] [blame] | 2711 | p = mProvidersByName.get(LocationManager.NETWORK_PROVIDER); |
| 2712 | } |
| 2713 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2714 | if (p == null) { |
| 2715 | return null; |
| 2716 | } |
| 2717 | if (p.mProvider instanceof LocationProviderProxy) { |
| 2718 | return ((LocationProviderProxy) p.mProvider).getConnectedPackageName(); |
Jason Monk | b71218a | 2015-06-17 14:44:39 -0400 | [diff] [blame] | 2719 | } |
| 2720 | return null; |
| 2721 | } |
| 2722 | |
Wei Wang | 980b7c2 | 2018-12-06 17:53:00 -0800 | [diff] [blame^] | 2723 | @Override |
| 2724 | public void setLocationControllerExtraPackage(String packageName) { |
| 2725 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 2726 | Manifest.permission.LOCATION_HARDWARE + " permission required"); |
| 2727 | synchronized (mLock) { |
| 2728 | mLocationControllerExtraPackage = packageName; |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | @Override |
| 2733 | public String getLocationControllerExtraPackage() { |
| 2734 | synchronized (mLock) { |
| 2735 | return mLocationControllerExtraPackage; |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | @Override |
| 2740 | public void setLocationControllerExtraPackageEnabled(boolean enabled) { |
| 2741 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 2742 | Manifest.permission.LOCATION_HARDWARE + " permission required"); |
| 2743 | synchronized (mLock) { |
| 2744 | mLocationControllerExtraPackageEnabled = enabled; |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | @Override |
| 2749 | public boolean isLocationControllerExtraPackageEnabled() { |
| 2750 | synchronized (mLock) { |
| 2751 | return mLocationControllerExtraPackageEnabled |
| 2752 | && (mLocationControllerExtraPackage != null); |
| 2753 | } |
| 2754 | } |
| 2755 | |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 2756 | /** |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2757 | * Returns the current location enabled/disabled status for a user |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2758 | * |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2759 | * @param userId the id of the user |
| 2760 | * @return true if location is enabled |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2761 | */ |
| 2762 | @Override |
| 2763 | public boolean isLocationEnabledForUser(int userId) { |
| 2764 | // Check INTERACT_ACROSS_USERS permission if userId is not current user id. |
| 2765 | checkInteractAcrossUsersPermission(userId); |
| 2766 | |
| 2767 | long identity = Binder.clearCallingIdentity(); |
| 2768 | try { |
| 2769 | synchronized (mLock) { |
| 2770 | final String allowedProviders = Settings.Secure.getStringForUser( |
| 2771 | mContext.getContentResolver(), |
| 2772 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
| 2773 | userId); |
| 2774 | if (allowedProviders == null) { |
| 2775 | return false; |
| 2776 | } |
| 2777 | final List<String> providerList = Arrays.asList(allowedProviders.split(",")); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2778 | for (String provider : mRealProviders.keySet()) { |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2779 | if (provider.equals(LocationManager.PASSIVE_PROVIDER) |
| 2780 | || provider.equals(LocationManager.FUSED_PROVIDER)) { |
| 2781 | continue; |
| 2782 | } |
| 2783 | if (providerList.contains(provider)) { |
| 2784 | return true; |
| 2785 | } |
| 2786 | } |
| 2787 | return false; |
| 2788 | } |
| 2789 | } finally { |
| 2790 | Binder.restoreCallingIdentity(identity); |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | /** |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2795 | * Enable or disable location for a user |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2796 | * |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2797 | * @param enabled true to enable location, false to disable location |
| 2798 | * @param userId the id of the user |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2799 | */ |
| 2800 | @Override |
| 2801 | public void setLocationEnabledForUser(boolean enabled, int userId) { |
| 2802 | mContext.enforceCallingPermission( |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2803 | android.Manifest.permission.WRITE_SECURE_SETTINGS, |
| 2804 | "Requires WRITE_SECURE_SETTINGS permission"); |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2805 | |
| 2806 | // Check INTERACT_ACROSS_USERS permission if userId is not current user id. |
| 2807 | checkInteractAcrossUsersPermission(userId); |
| 2808 | |
| 2809 | long identity = Binder.clearCallingIdentity(); |
| 2810 | try { |
| 2811 | synchronized (mLock) { |
| 2812 | final Set<String> allRealProviders = mRealProviders.keySet(); |
| 2813 | // Update all providers on device plus gps and network provider when disabling |
| 2814 | // location |
| 2815 | Set<String> allProvidersSet = new ArraySet<>(allRealProviders.size() + 2); |
| 2816 | allProvidersSet.addAll(allRealProviders); |
| 2817 | // When disabling location, disable gps and network provider that could have been |
| 2818 | // enabled by location mode api. |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2819 | if (!enabled) { |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2820 | allProvidersSet.add(LocationManager.GPS_PROVIDER); |
| 2821 | allProvidersSet.add(LocationManager.NETWORK_PROVIDER); |
| 2822 | } |
| 2823 | if (allProvidersSet.isEmpty()) { |
| 2824 | return; |
| 2825 | } |
| 2826 | // to ensure thread safety, we write the provider name with a '+' or '-' |
| 2827 | // and let the SettingsProvider handle it rather than reading and modifying |
| 2828 | // the list of enabled providers. |
| 2829 | final String prefix = enabled ? "+" : "-"; |
| 2830 | StringBuilder locationProvidersAllowed = new StringBuilder(); |
| 2831 | for (String provider : allProvidersSet) { |
| 2832 | if (provider.equals(LocationManager.PASSIVE_PROVIDER) |
| 2833 | || provider.equals(LocationManager.FUSED_PROVIDER)) { |
| 2834 | continue; |
| 2835 | } |
| 2836 | locationProvidersAllowed.append(prefix); |
| 2837 | locationProvidersAllowed.append(provider); |
| 2838 | locationProvidersAllowed.append(","); |
| 2839 | } |
| 2840 | // Remove the trailing comma |
| 2841 | locationProvidersAllowed.setLength(locationProvidersAllowed.length() - 1); |
| 2842 | Settings.Secure.putStringForUser( |
| 2843 | mContext.getContentResolver(), |
| 2844 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
| 2845 | locationProvidersAllowed.toString(), |
| 2846 | userId); |
| 2847 | } |
| 2848 | } finally { |
| 2849 | Binder.restoreCallingIdentity(identity); |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | /** |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2854 | * Returns the current enabled/disabled status of a location provider and user |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2855 | * |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2856 | * @param providerName name of the provider |
| 2857 | * @param userId the id of the user |
| 2858 | * @return true if the provider exists and is enabled |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2859 | */ |
| 2860 | @Override |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2861 | public boolean isProviderEnabledForUser(String providerName, int userId) { |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2862 | // Check INTERACT_ACROSS_USERS permission if userId is not current user id. |
| 2863 | checkInteractAcrossUsersPermission(userId); |
| 2864 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2865 | if (!isLocationEnabledForUser(userId)) { |
| 2866 | return false; |
| 2867 | } |
| 2868 | |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2869 | // Fused provider is accessed indirectly via criteria rather than the provider-based APIs, |
| 2870 | // so we discourage its use |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2871 | if (LocationManager.FUSED_PROVIDER.equals(providerName)) return false; |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2872 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2873 | long identity = Binder.clearCallingIdentity(); |
| 2874 | try { |
| 2875 | LocationProvider provider; |
| 2876 | synchronized (mLock) { |
| 2877 | provider = mProvidersByName.get(providerName); |
| 2878 | } |
| 2879 | return provider != null && provider.isEnabled(); |
| 2880 | } finally { |
| 2881 | Binder.restoreCallingIdentity(identity); |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | /** |
| 2886 | * Enable or disable a single location provider. |
| 2887 | * |
| 2888 | * @param provider name of the provider |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2889 | * @param enabled true to enable the provider. False to disable the provider |
| 2890 | * @param userId the id of the user to set |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2891 | * @return true if the value was set, false on errors |
| 2892 | */ |
| 2893 | @Override |
| 2894 | public boolean setProviderEnabledForUser(String provider, boolean enabled, int userId) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2895 | return false; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | /** |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2899 | * Method for checking INTERACT_ACROSS_USERS permission if specified user id is not the same as |
| 2900 | * current user id |
| 2901 | * |
| 2902 | * @param userId the user id to get or set value |
| 2903 | */ |
| 2904 | private void checkInteractAcrossUsersPermission(int userId) { |
| 2905 | int uid = Binder.getCallingUid(); |
| 2906 | if (UserHandle.getUserId(uid) != userId) { |
| 2907 | if (ActivityManager.checkComponentPermission( |
| 2908 | android.Manifest.permission.INTERACT_ACROSS_USERS, uid, -1, true) |
| 2909 | != PERMISSION_GRANTED) { |
| 2910 | throw new SecurityException("Requires INTERACT_ACROSS_USERS permission"); |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /** |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 2916 | * Returns "true" if the UID belongs to a bound location provider. |
| 2917 | * |
| 2918 | * @param uid the uid |
| 2919 | * @return true if uid belongs to a bound location provider |
| 2920 | */ |
| 2921 | private boolean isUidALocationProvider(int uid) { |
| 2922 | if (uid == Process.SYSTEM_UID) { |
| 2923 | return true; |
| 2924 | } |
| 2925 | if (mGeocodeProvider != null) { |
David Christie | 1f141c1 | 2014-05-14 15:11:15 -0700 | [diff] [blame] | 2926 | if (doesUidHavePackage(uid, mGeocodeProvider.getConnectedPackageName())) return true; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 2927 | } |
| 2928 | for (LocationProviderProxy proxy : mProxyProviders) { |
David Christie | 1f141c1 | 2014-05-14 15:11:15 -0700 | [diff] [blame] | 2929 | if (doesUidHavePackage(uid, proxy.getConnectedPackageName())) return true; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 2930 | } |
| 2931 | return false; |
| 2932 | } |
| 2933 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2934 | private void checkCallerIsProvider() { |
| 2935 | if (mContext.checkCallingOrSelfPermission(INSTALL_LOCATION_PROVIDER) |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 2936 | == PERMISSION_GRANTED) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | // Previously we only used the INSTALL_LOCATION_PROVIDER |
| 2941 | // check. But that is system or signature |
| 2942 | // protection level which is not flexible enough for |
| 2943 | // providers installed oustide the system image. So |
| 2944 | // also allow providers with a UID matching the |
| 2945 | // currently bound package name |
| 2946 | |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 2947 | if (isUidALocationProvider(Binder.getCallingUid())) { |
| 2948 | return; |
| 2949 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2950 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2951 | throw new SecurityException("need INSTALL_LOCATION_PROVIDER permission, " + |
| 2952 | "or UID of a currently bound location provider"); |
| 2953 | } |
| 2954 | |
David Christie | 1f141c1 | 2014-05-14 15:11:15 -0700 | [diff] [blame] | 2955 | /** |
| 2956 | * Returns true if the given package belongs to the given uid. |
| 2957 | */ |
| 2958 | private boolean doesUidHavePackage(int uid, String packageName) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2959 | if (packageName == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2960 | return false; |
| 2961 | } |
David Christie | 1f141c1 | 2014-05-14 15:11:15 -0700 | [diff] [blame] | 2962 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 2963 | if (packageNames == null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2964 | return false; |
| 2965 | } |
David Christie | 1f141c1 | 2014-05-14 15:11:15 -0700 | [diff] [blame] | 2966 | for (String name : packageNames) { |
| 2967 | if (packageName.equals(name)) { |
| 2968 | return true; |
| 2969 | } |
| 2970 | } |
| 2971 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2972 | } |
| 2973 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2974 | @Override |
Mike Lockwood | a4903f2 | 2010-02-17 06:42:23 -0500 | [diff] [blame] | 2975 | public void reportLocation(Location location, boolean passive) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2976 | checkCallerIsProvider(); |
Mike Lockwood | 275555c | 2009-05-01 11:30:34 -0400 | [diff] [blame] | 2977 | |
Nick Pelly | 2eeeec2 | 2012-07-18 13:13:37 -0700 | [diff] [blame] | 2978 | if (!location.isComplete()) { |
| 2979 | Log.w(TAG, "Dropping incomplete location: " + location); |
| 2980 | return; |
| 2981 | } |
| 2982 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2983 | mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location); |
| 2984 | Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location); |
Mike Lockwood | a4903f2 | 2010-02-17 06:42:23 -0500 | [diff] [blame] | 2985 | m.arg1 = (passive ? 1 : 0); |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 2986 | mLocationHandler.sendMessageAtFrontOfQueue(m); |
| 2987 | } |
| 2988 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2989 | |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 2990 | private static boolean shouldBroadcastSafe( |
| 2991 | Location loc, Location lastLoc, UpdateRecord record, long now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2992 | // Always broadcast the first update |
| 2993 | if (lastLoc == null) { |
| 2994 | return true; |
| 2995 | } |
| 2996 | |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 2997 | // Check whether sufficient time has passed |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2998 | long minTime = record.mRealRequest.getFastestInterval(); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2999 | long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos()) |
| 3000 | / NANOS_PER_MILLI; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3001 | if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3002 | return false; |
| 3003 | } |
| 3004 | |
| 3005 | // Check whether sufficient distance has been traveled |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 3006 | double minDistance = record.mRealRequest.getSmallestDisplacement(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3007 | if (minDistance > 0.0) { |
| 3008 | if (loc.distanceTo(lastLoc) <= minDistance) { |
| 3009 | return false; |
| 3010 | } |
| 3011 | } |
| 3012 | |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 3013 | // Check whether sufficient number of udpates is left |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 3014 | if (record.mRealRequest.getNumUpdates() <= 0) { |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 3015 | return false; |
| 3016 | } |
| 3017 | |
| 3018 | // Check whether the expiry date has passed |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 3019 | return record.mRealRequest.getExpireAt() >= now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
Mike Lockwood | a4903f2 | 2010-02-17 06:42:23 -0500 | [diff] [blame] | 3022 | private void handleLocationChangedLocked(Location location, boolean passive) { |
Nick Pelly | 4e31c4f | 2012-08-13 19:35:39 -0700 | [diff] [blame] | 3023 | if (D) Log.d(TAG, "incoming location: " + location); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3024 | long now = SystemClock.elapsedRealtime(); |
Mike Lockwood | a4903f2 | 2010-02-17 06:42:23 -0500 | [diff] [blame] | 3025 | String provider = (passive ? LocationManager.PASSIVE_PROVIDER : location.getProvider()); |
Laurent Tu | 60ec50a | 2012-10-04 17:00:10 -0700 | [diff] [blame] | 3026 | // Skip if the provider is unknown. |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3027 | LocationProvider p = mProvidersByName.get(provider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3028 | if (p == null) return; |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 3029 | updateLastLocationLocked(location, provider); |
| 3030 | // mLastLocation should have been updated from the updateLastLocationLocked call above. |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3031 | Location lastLocation = mLastLocation.get(provider); |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 3032 | if (lastLocation == null) { |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 3033 | Log.e(TAG, "handleLocationChangedLocked() updateLastLocation failed"); |
| 3034 | return; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 3035 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 3037 | // Update last known coarse interval location if enough time has passed. |
| 3038 | Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get(provider); |
| 3039 | if (lastLocationCoarseInterval == null) { |
| 3040 | lastLocationCoarseInterval = new Location(location); |
| 3041 | mLastLocationCoarseInterval.put(provider, lastLocationCoarseInterval); |
| 3042 | } |
| 3043 | long timeDiffNanos = location.getElapsedRealtimeNanos() |
| 3044 | - lastLocationCoarseInterval.getElapsedRealtimeNanos(); |
| 3045 | if (timeDiffNanos > LocationFudger.FASTEST_INTERVAL_MS * NANOS_PER_MILLI) { |
| 3046 | lastLocationCoarseInterval.set(location); |
| 3047 | } |
| 3048 | // Don't ever return a coarse location that is more recent than the allowed update |
| 3049 | // interval (i.e. don't allow an app to keep registering and unregistering for |
| 3050 | // location updates to overcome the minimum interval). |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 3051 | Location noGPSLocation = |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 3052 | lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION); |
| 3053 | |
Laurent Tu | 60ec50a | 2012-10-04 17:00:10 -0700 | [diff] [blame] | 3054 | // Skip if there are no UpdateRecords for this provider. |
| 3055 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider); |
| 3056 | if (records == null || records.size() == 0) return; |
| 3057 | |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 3058 | // Fetch coarse location |
| 3059 | Location coarseLocation = null; |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 3060 | if (noGPSLocation != null) { |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 3061 | coarseLocation = mLocationFudger.getOrCreate(noGPSLocation); |
| 3062 | } |
| 3063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3064 | // Fetch latest status update time |
| 3065 | long newStatusUpdateTime = p.getStatusUpdateTime(); |
| 3066 | |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 3067 | // Get latest status |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3068 | Bundle extras = new Bundle(); |
| 3069 | int status = p.getStatus(extras); |
| 3070 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3071 | ArrayList<Receiver> deadReceivers = null; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3072 | ArrayList<UpdateRecord> deadUpdateRecords = null; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3073 | |
Soonil Nagarkar | 94749f7 | 2018-11-08 11:46:43 -0800 | [diff] [blame] | 3074 | // Broadcast location to all listeners |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3075 | for (UpdateRecord r : records) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3076 | Receiver receiver = r.mReceiver; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 3077 | boolean receiverDead = false; |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 3078 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 3079 | int receiverUserId = UserHandle.getUserId(receiver.mIdentity.mUid); |
| 3080 | if (!isCurrentProfile(receiverUserId) |
| 3081 | && !isUidALocationProvider(receiver.mIdentity.mUid)) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 3082 | if (D) { |
Victoria Lease | 269518e | 2012-10-29 08:25:39 -0700 | [diff] [blame] | 3083 | Log.d(TAG, "skipping loc update for background user " + receiverUserId + |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 3084 | " (current user: " + mCurrentUserId + ", app: " + |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 3085 | receiver.mIdentity.mPackageName + ")"); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 3086 | } |
| 3087 | continue; |
| 3088 | } |
| 3089 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 3090 | if (mBlacklist.isBlacklisted(receiver.mIdentity.mPackageName)) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 3091 | if (D) { |
| 3092 | Log.d(TAG, "skipping loc update for blacklisted app: " + |
| 3093 | receiver.mIdentity.mPackageName); |
| 3094 | } |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 3095 | continue; |
| 3096 | } |
| 3097 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 3098 | if (!reportLocationAccessNoThrow( |
| 3099 | receiver.mIdentity.mPid, |
| 3100 | receiver.mIdentity.mUid, |
| 3101 | receiver.mIdentity.mPackageName, |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3102 | receiver.mAllowedResolutionLevel)) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 3103 | if (D) { |
| 3104 | Log.d(TAG, "skipping loc update for no op app: " + |
| 3105 | receiver.mIdentity.mPackageName); |
| 3106 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3107 | continue; |
| 3108 | } |
| 3109 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3110 | Location notifyLocation; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 3111 | if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 3112 | notifyLocation = coarseLocation; // use coarse location |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3113 | } else { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 3114 | notifyLocation = lastLocation; // use fine location |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3115 | } |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 3116 | if (notifyLocation != null) { |
| 3117 | Location lastLoc = r.mLastFixBroadcast; |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 3118 | if ((lastLoc == null) || shouldBroadcastSafe(notifyLocation, lastLoc, r, now)) { |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 3119 | if (lastLoc == null) { |
| 3120 | lastLoc = new Location(notifyLocation); |
| 3121 | r.mLastFixBroadcast = lastLoc; |
| 3122 | } else { |
| 3123 | lastLoc.set(notifyLocation); |
| 3124 | } |
| 3125 | if (!receiver.callLocationChangedLocked(notifyLocation)) { |
| 3126 | Slog.w(TAG, "RemoteException calling onLocationChanged on " + receiver); |
| 3127 | receiverDead = true; |
| 3128 | } |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 3129 | r.mRealRequest.decrementNumUpdates(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
Soonil Nagarkar | 94749f7 | 2018-11-08 11:46:43 -0800 | [diff] [blame] | 3133 | // TODO: location provider status callbacks have been disabled and deprecated, and are |
| 3134 | // guarded behind this setting now. should be removed completely post-Q |
| 3135 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 3136 | LOCATION_DISABLE_STATUS_CALLBACKS, 1) == 0) { |
| 3137 | long prevStatusUpdateTime = r.mLastStatusBroadcast; |
| 3138 | if ((newStatusUpdateTime > prevStatusUpdateTime) |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3139 | && (prevStatusUpdateTime != 0 || status != AVAILABLE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3140 | |
Soonil Nagarkar | 94749f7 | 2018-11-08 11:46:43 -0800 | [diff] [blame] | 3141 | r.mLastStatusBroadcast = newStatusUpdateTime; |
| 3142 | if (!receiver.callStatusChangedLocked(provider, status, extras)) { |
| 3143 | receiverDead = true; |
| 3144 | Slog.w(TAG, "RemoteException calling onStatusChanged on " + receiver); |
| 3145 | } |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3149 | // track expired records |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 3150 | if (r.mRealRequest.getNumUpdates() <= 0 || r.mRealRequest.getExpireAt() < now) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3151 | if (deadUpdateRecords == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3152 | deadUpdateRecords = new ArrayList<>(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3153 | } |
| 3154 | deadUpdateRecords.add(r); |
| 3155 | } |
| 3156 | // track dead receivers |
| 3157 | if (receiverDead) { |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 3158 | if (deadReceivers == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3159 | deadReceivers = new ArrayList<>(); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 3160 | } |
| 3161 | if (!deadReceivers.contains(receiver)) { |
| 3162 | deadReceivers.add(receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3166 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3167 | // remove dead records and receivers outside the loop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3168 | if (deadReceivers != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3169 | for (Receiver receiver : deadReceivers) { |
| 3170 | removeUpdatesLocked(receiver); |
| 3171 | } |
| 3172 | } |
| 3173 | if (deadUpdateRecords != null) { |
| 3174 | for (UpdateRecord r : deadUpdateRecords) { |
| 3175 | r.disposeLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3176 | } |
Victoria Lease | 8b38b29 | 2012-12-04 15:04:43 -0800 | [diff] [blame] | 3177 | applyRequirementsLocked(provider); |
| 3178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 3181 | /** |
| 3182 | * Updates last location with the given location |
| 3183 | * |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3184 | * @param location new location to update |
| 3185 | * @param provider Location provider to update for |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 3186 | */ |
| 3187 | private void updateLastLocationLocked(Location location, String provider) { |
| 3188 | Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION); |
| 3189 | Location lastNoGPSLocation; |
| 3190 | Location lastLocation = mLastLocation.get(provider); |
| 3191 | if (lastLocation == null) { |
| 3192 | lastLocation = new Location(provider); |
| 3193 | mLastLocation.put(provider, lastLocation); |
| 3194 | } else { |
| 3195 | lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION); |
| 3196 | if (noGPSLocation == null && lastNoGPSLocation != null) { |
| 3197 | // New location has no no-GPS location: adopt last no-GPS location. This is set |
| 3198 | // directly into location because we do not want to notify COARSE clients. |
| 3199 | location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation); |
| 3200 | } |
| 3201 | } |
| 3202 | lastLocation.set(location); |
| 3203 | } |
| 3204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3205 | private class LocationWorkerHandler extends Handler { |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 3206 | public LocationWorkerHandler(Looper looper) { |
| 3207 | super(looper, null, true); |
| 3208 | } |
| 3209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3210 | @Override |
| 3211 | public void handleMessage(Message msg) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3212 | switch (msg.what) { |
| 3213 | case MSG_LOCATION_CHANGED: |
| 3214 | handleLocationChanged((Location) msg.obj, msg.arg1 == 1); |
| 3215 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | |
Victoria Lease | 54ca7ae | 2013-01-08 09:39:50 -0800 | [diff] [blame] | 3220 | private boolean isMockProvider(String provider) { |
| 3221 | synchronized (mLock) { |
| 3222 | return mMockProviders.containsKey(provider); |
| 3223 | } |
| 3224 | } |
| 3225 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3226 | private void handleLocationChanged(Location location, boolean passive) { |
Victoria Lease | 54ca7ae | 2013-01-08 09:39:50 -0800 | [diff] [blame] | 3227 | // create a working copy of the incoming Location so that the service can modify it without |
| 3228 | // disturbing the caller's copy |
| 3229 | Location myLocation = new Location(location); |
| 3230 | String provider = myLocation.getProvider(); |
| 3231 | |
| 3232 | // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this |
| 3233 | // bit if location did not come from a mock provider because passive/fused providers can |
| 3234 | // forward locations from mock providers, and should not grant them legitimacy in doing so. |
| 3235 | if (!myLocation.isFromMockProvider() && isMockProvider(provider)) { |
| 3236 | myLocation.setIsFromMockProvider(true); |
| 3237 | } |
Jeff Sharkey | 5e61331 | 2012-01-30 11:16:20 -0800 | [diff] [blame] | 3238 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3239 | synchronized (mLock) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3240 | if (!passive) { |
| 3241 | // notify passive provider of the new location |
| 3242 | mPassiveProvider.updateLocation(myLocation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3243 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3244 | handleLocationChangedLocked(myLocation, passive); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3245 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3246 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3247 | |
Mike Lockwood | e97ae40 | 2010-09-29 15:23:46 -0400 | [diff] [blame] | 3248 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 3249 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3250 | public void onPackageDisappeared(String packageName, int reason) { |
| 3251 | // remove all receivers associated with this package name |
| 3252 | synchronized (mLock) { |
| 3253 | ArrayList<Receiver> deadReceivers = null; |
| 3254 | |
| 3255 | for (Receiver receiver : mReceivers.values()) { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 3256 | if (receiver.mIdentity.mPackageName.equals(packageName)) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3257 | if (deadReceivers == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3258 | deadReceivers = new ArrayList<>(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3259 | } |
| 3260 | deadReceivers.add(receiver); |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | // perform removal outside of mReceivers loop |
| 3265 | if (deadReceivers != null) { |
| 3266 | for (Receiver receiver : deadReceivers) { |
| 3267 | removeUpdatesLocked(receiver); |
| 3268 | } |
| 3269 | } |
| 3270 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3271 | } |
Mike Lockwood | e97ae40 | 2010-09-29 15:23:46 -0400 | [diff] [blame] | 3272 | }; |
| 3273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3274 | // Geocoder |
| 3275 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3276 | @Override |
Mike Lockwood | e15735a | 2010-09-20 17:48:47 -0400 | [diff] [blame] | 3277 | public boolean geocoderIsPresent() { |
Mark Vandevoorde | 01ac80b | 2010-05-21 15:43:26 -0700 | [diff] [blame] | 3278 | return mGeocodeProvider != null; |
| 3279 | } |
| 3280 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3281 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3282 | public String getFromLocation(double latitude, double longitude, int maxResults, |
Mike Lockwood | 3490140 | 2010-01-04 12:14:21 -0500 | [diff] [blame] | 3283 | GeocoderParams params, List<Address> addrs) { |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 3284 | if (mGeocodeProvider != null) { |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 3285 | return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, |
| 3286 | params, addrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3287 | } |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 3288 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3289 | } |
| 3290 | |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 3291 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3292 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3293 | public String getFromLocationName(String locationName, |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 3294 | double lowerLeftLatitude, double lowerLeftLongitude, |
| 3295 | double upperRightLatitude, double upperRightLongitude, int maxResults, |
Mike Lockwood | 3490140 | 2010-01-04 12:14:21 -0500 | [diff] [blame] | 3296 | GeocoderParams params, List<Address> addrs) { |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 3297 | |
| 3298 | if (mGeocodeProvider != null) { |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 3299 | return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude, |
| 3300 | lowerLeftLongitude, upperRightLatitude, upperRightLongitude, |
| 3301 | maxResults, params, addrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | } |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 3303 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | // Mock Providers |
| 3307 | |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3308 | private boolean canCallerAccessMockLocation(String opPackageName) { |
| 3309 | return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(), |
| 3310 | opPackageName) == AppOpsManager.MODE_ALLOWED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3311 | } |
| 3312 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3313 | @Override |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3314 | public void addTestProvider(String name, ProviderProperties properties, String opPackageName) { |
| 3315 | if (!canCallerAccessMockLocation(opPackageName)) { |
| 3316 | return; |
| 3317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | |
Mike Lockwood | a4903f2 | 2010-02-17 06:42:23 -0500 | [diff] [blame] | 3319 | if (LocationManager.PASSIVE_PROVIDER.equals(name)) { |
| 3320 | throw new IllegalArgumentException("Cannot mock the passive location provider"); |
| 3321 | } |
| 3322 | |
Mike Lockwood | 86328a9 | 2009-10-23 08:38:25 -0400 | [diff] [blame] | 3323 | long identity = Binder.clearCallingIdentity(); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 3324 | synchronized (mLock) { |
Mike Lockwood | 7566c1d | 2009-08-25 10:05:18 -0700 | [diff] [blame] | 3325 | // remove the real provider if we are replacing GPS or network provider |
| 3326 | if (LocationManager.GPS_PROVIDER.equals(name) |
Nick Pelly | 1332b53 | 2012-08-21 16:25:47 -0700 | [diff] [blame] | 3327 | || LocationManager.NETWORK_PROVIDER.equals(name) |
| 3328 | || LocationManager.FUSED_PROVIDER.equals(name)) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3329 | LocationProvider p = mProvidersByName.get(name); |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 3330 | if (p != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3331 | removeProviderLocked(p); |
Mike Lockwood | 7566c1d | 2009-08-25 10:05:18 -0700 | [diff] [blame] | 3332 | } |
| 3333 | } |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 3334 | addTestProviderLocked(name, properties); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3335 | } |
Mike Lockwood | 86328a9 | 2009-10-23 08:38:25 -0400 | [diff] [blame] | 3336 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3337 | } |
| 3338 | |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 3339 | private void addTestProviderLocked(String name, ProviderProperties properties) { |
| 3340 | if (mProvidersByName.get(name) != null) { |
| 3341 | throw new IllegalArgumentException("Provider \"" + name + "\" already exists"); |
| 3342 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3343 | |
| 3344 | LocationProvider provider = new LocationProvider(name); |
| 3345 | MockProvider mockProvider = new MockProvider(provider, properties); |
| 3346 | |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 3347 | addProviderLocked(provider); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3348 | mMockProviders.put(name, mockProvider); |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 3349 | mLastLocation.put(name, null); |
| 3350 | mLastLocationCoarseInterval.put(name, null); |
| 3351 | } |
| 3352 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3353 | @Override |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3354 | public void removeTestProvider(String provider, String opPackageName) { |
| 3355 | if (!canCallerAccessMockLocation(opPackageName)) { |
| 3356 | return; |
| 3357 | } |
| 3358 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 3359 | synchronized (mLock) { |
You Kim | a6d0b6f | 2012-10-28 03:58:44 +0900 | [diff] [blame] | 3360 | MockProvider mockProvider = mMockProviders.remove(provider); |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 3361 | if (mockProvider == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3362 | throw new IllegalArgumentException("Provider \"" + provider + "\" unknown"); |
| 3363 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3364 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3365 | long identity = Binder.clearCallingIdentity(); |
| 3366 | try { |
| 3367 | removeProviderLocked(mProvidersByName.get(provider)); |
| 3368 | |
| 3369 | // reinstate real provider if available |
| 3370 | LocationProvider realProvider = mRealProviders.get(provider); |
| 3371 | if (realProvider != null) { |
| 3372 | addProviderLocked(realProvider); |
| 3373 | } |
| 3374 | mLastLocation.put(provider, null); |
| 3375 | mLastLocationCoarseInterval.put(provider, null); |
| 3376 | } finally { |
| 3377 | Binder.restoreCallingIdentity(identity); |
Mike Lockwood | 7566c1d | 2009-08-25 10:05:18 -0700 | [diff] [blame] | 3378 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3379 | } |
| 3380 | } |
| 3381 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3382 | @Override |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3383 | public void setTestProviderLocation(String provider, Location loc, String opPackageName) { |
| 3384 | if (!canCallerAccessMockLocation(opPackageName)) { |
| 3385 | return; |
| 3386 | } |
| 3387 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 3388 | synchronized (mLock) { |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 3389 | MockProvider mockProvider = mMockProviders.get(provider); |
| 3390 | if (mockProvider == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3391 | throw new IllegalArgumentException("Provider \"" + provider + "\" unknown"); |
| 3392 | } |
Tom O'Neill | a206a0f | 2016-12-15 10:26:28 -0800 | [diff] [blame] | 3393 | |
| 3394 | // Ensure that the location is marked as being mock. There's some logic to do this in |
| 3395 | // handleLocationChanged(), but it fails if loc has the wrong provider (bug 33091107). |
| 3396 | Location mock = new Location(loc); |
| 3397 | mock.setIsFromMockProvider(true); |
| 3398 | |
| 3399 | if (!TextUtils.isEmpty(loc.getProvider()) && !provider.equals(loc.getProvider())) { |
| 3400 | // The location has an explicit provider that is different from the mock provider |
| 3401 | // name. The caller may be trying to fool us via bug 33091107. |
| 3402 | EventLog.writeEvent(0x534e4554, "33091107", Binder.getCallingUid(), |
| 3403 | provider + "!=" + loc.getProvider()); |
| 3404 | } |
| 3405 | |
Mike Lockwood | 95427cd | 2009-05-07 13:27:54 -0400 | [diff] [blame] | 3406 | // clear calling identity so INSTALL_LOCATION_PROVIDER permission is not required |
| 3407 | long identity = Binder.clearCallingIdentity(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3408 | try { |
| 3409 | mockProvider.setLocation(mock); |
| 3410 | } finally { |
| 3411 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3416 | @Override |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3417 | public void setTestProviderEnabled(String provider, boolean enabled, String opPackageName) { |
| 3418 | if (!canCallerAccessMockLocation(opPackageName)) { |
| 3419 | return; |
| 3420 | } |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3421 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 3422 | synchronized (mLock) { |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 3423 | MockProvider mockProvider = mMockProviders.get(provider); |
| 3424 | if (mockProvider == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3425 | throw new IllegalArgumentException("Provider \"" + provider + "\" unknown"); |
| 3426 | } |
Mike Lockwood | 86328a9 | 2009-10-23 08:38:25 -0400 | [diff] [blame] | 3427 | long identity = Binder.clearCallingIdentity(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3428 | try { |
| 3429 | mockProvider.setEnabled(enabled); |
| 3430 | } finally { |
| 3431 | Binder.restoreCallingIdentity(identity); |
| 3432 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3436 | @Override |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 3437 | public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime, |
| 3438 | String opPackageName) { |
| 3439 | if (!canCallerAccessMockLocation(opPackageName)) { |
| 3440 | return; |
| 3441 | } |
| 3442 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 3443 | synchronized (mLock) { |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 3444 | MockProvider mockProvider = mMockProviders.get(provider); |
| 3445 | if (mockProvider == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3446 | throw new IllegalArgumentException("Provider \"" + provider + "\" unknown"); |
| 3447 | } |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 3448 | mockProvider.setStatus(status, extras, updateTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3452 | private void log(String log) { |
| 3453 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3454 | Slog.d(TAG, log); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3455 | } |
| 3456 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3457 | |
| 3458 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3459 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3460 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3461 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 3462 | synchronized (mLock) { |
Siddharth Ray | bb608c8 | 2017-03-16 11:33:34 -0700 | [diff] [blame] | 3463 | if (args.length > 0 && args[0].equals("--gnssmetrics")) { |
| 3464 | if (mGnssMetricsProvider != null) { |
| 3465 | pw.append(mGnssMetricsProvider.getGnssMetricsAsProtoString()); |
| 3466 | } |
| 3467 | return; |
| 3468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3469 | pw.println("Current Location Manager state:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3470 | pw.println(" Location Listeners:"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3471 | for (Receiver receiver : mReceivers.values()) { |
| 3472 | pw.println(" " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3473 | } |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 3474 | pw.println(" Active Records by Provider:"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3475 | for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) { |
| 3476 | pw.println(" " + entry.getKey() + ":"); |
| 3477 | for (UpdateRecord record : entry.getValue()) { |
| 3478 | pw.println(" " + record); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | } |
| 3480 | } |
Wyatt Riley | 11cc749 | 2018-01-17 08:48:27 -0800 | [diff] [blame] | 3481 | pw.println(" Active GnssMeasurement Listeners:"); |
| 3482 | for (Identity identity : mGnssMeasurementsListeners.values()) { |
| 3483 | pw.println(" " + identity.mPid + " " + identity.mUid + " " |
| 3484 | + identity.mPackageName + ": " + isThrottlingExemptLocked(identity)); |
| 3485 | } |
| 3486 | pw.println(" Active GnssNavigationMessage Listeners:"); |
| 3487 | for (Identity identity : mGnssNavigationMessageListeners.values()) { |
| 3488 | pw.println(" " + identity.mPid + " " + identity.mUid + " " |
| 3489 | + identity.mPackageName + ": " + isThrottlingExemptLocked(identity)); |
| 3490 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3491 | pw.println(" Overlay Provider Packages:"); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3492 | for (LocationProvider provider : mProviders) { |
| 3493 | if (provider.mProvider instanceof LocationProviderProxy) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3494 | pw.println(" " + provider.getName() + ": " |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3495 | + ((LocationProviderProxy) provider.mProvider) |
| 3496 | .getConnectedPackageName()); |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 3497 | } |
| 3498 | } |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 3499 | pw.println(" Historical Records by Provider:"); |
| 3500 | for (Map.Entry<PackageProviderKey, PackageStatistics> entry |
| 3501 | : mRequestStatistics.statistics.entrySet()) { |
| 3502 | PackageProviderKey key = entry.getKey(); |
| 3503 | PackageStatistics stats = entry.getValue(); |
| 3504 | pw.println(" " + key.packageName + ": " + key.providerName + ": " + stats); |
| 3505 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3506 | pw.println(" Last Known Locations:"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3507 | for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) { |
| 3508 | String provider = entry.getKey(); |
| 3509 | Location location = entry.getValue(); |
| 3510 | pw.println(" " + provider + ": " + location); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3511 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3512 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 3513 | pw.println(" Last Known Locations Coarse Intervals:"); |
| 3514 | for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) { |
| 3515 | String provider = entry.getKey(); |
| 3516 | Location location = entry.getValue(); |
| 3517 | pw.println(" " + provider + ": " + location); |
| 3518 | } |
| 3519 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 3520 | mGeofenceManager.dump(pw); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3521 | |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 3522 | pw.append(" "); |
| 3523 | mBlacklist.dump(pw); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3524 | if (mMockProviders.size() > 0) { |
| 3525 | pw.println(" Mock Providers:"); |
| 3526 | for (Map.Entry<String, MockProvider> i : mMockProviders.entrySet()) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3527 | i.getValue().dump(fd, pw, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3528 | } |
| 3529 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3530 | |
Wei Wang | 980b7c2 | 2018-12-06 17:53:00 -0800 | [diff] [blame^] | 3531 | if (mLocationControllerExtraPackage != null) { |
| 3532 | pw.println(" Location controller extra package: " + mLocationControllerExtraPackage |
| 3533 | + " enabled: " + mLocationControllerExtraPackageEnabled); |
| 3534 | } |
| 3535 | |
Soonil Nagarkar | 2b565df | 2017-02-14 13:33:23 -0800 | [diff] [blame] | 3536 | if (!mBackgroundThrottlePackageWhitelist.isEmpty()) { |
| 3537 | pw.println(" Throttling Whitelisted Packages:"); |
| 3538 | for (String packageName : mBackgroundThrottlePackageWhitelist) { |
| 3539 | pw.println(" " + packageName); |
| 3540 | } |
| 3541 | } |
| 3542 | |
Nick Pelly | 74fa7ea | 2012-08-13 19:36:38 -0700 | [diff] [blame] | 3543 | pw.append(" fudger: "); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 3544 | mLocationFudger.dump(fd, pw, args); |
Nick Pelly | 74fa7ea | 2012-08-13 19:36:38 -0700 | [diff] [blame] | 3545 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3546 | if (args.length > 0 && "short".equals(args[0])) { |
| 3547 | return; |
| 3548 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 3549 | for (LocationProvider provider : mProviders) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 3550 | provider.dump(fd, pw, args); |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 3551 | } |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 3552 | if (mGnssBatchingInProgress) { |
| 3553 | pw.println(" GNSS batching in progress"); |
| 3554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3555 | } |
| 3556 | } |
| 3557 | } |