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