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