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