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