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