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