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