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