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