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