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 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_COARSE_LOCATION; |
| 20 | import static android.Manifest.permission.ACCESS_FINE_LOCATION; |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 22 | import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 24 | import static android.location.LocationManager.FUSED_PROVIDER; |
| 25 | import static android.location.LocationManager.GPS_PROVIDER; |
| 26 | import static android.location.LocationManager.NETWORK_PROVIDER; |
| 27 | import static android.location.LocationManager.PASSIVE_PROVIDER; |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 28 | import static android.os.PowerManager.locationPowerSaveModeToString; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 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.AppOpsManager; |
| 34 | import android.app.PendingIntent; |
| 35 | import android.content.BroadcastReceiver; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.IntentFilter; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 40 | import android.location.Address; |
| 41 | import android.location.Criteria; |
| 42 | import android.location.GeocoderParams; |
| 43 | import android.location.Geofence; |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 44 | import android.location.GnssMeasurementCorrections; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 45 | import android.location.IBatchedLocationCallback; |
| 46 | import android.location.IGnssMeasurementsListener; |
| 47 | import android.location.IGnssNavigationMessageListener; |
| 48 | import android.location.IGnssStatusListener; |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 49 | import android.location.IGpsGeofenceHardware; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 50 | import android.location.ILocationListener; |
| 51 | import android.location.ILocationManager; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 52 | import android.location.Location; |
| 53 | import android.location.LocationManager; |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 54 | import android.location.LocationManagerInternal; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 55 | import android.location.LocationRequest; |
Chad Brubaker | f113333 | 2019-03-15 14:13:59 -0700 | [diff] [blame] | 56 | import android.location.LocationTime; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 57 | import android.os.Binder; |
| 58 | import android.os.Bundle; |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 59 | import android.os.CancellationSignal; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 60 | import android.os.Handler; |
| 61 | import android.os.IBinder; |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 62 | import android.os.ICancellationSignal; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 63 | import android.os.PowerManager; |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 64 | import android.os.PowerManager.ServiceType; |
| 65 | import android.os.PowerManagerInternal; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 66 | import android.os.Process; |
| 67 | import android.os.RemoteException; |
| 68 | import android.os.SystemClock; |
| 69 | import android.os.UserHandle; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 70 | import android.os.WorkSource; |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 71 | import android.os.WorkSource.WorkChain; |
Hongyi Zhang | 700137e | 2019-05-23 21:19:36 -0700 | [diff] [blame] | 72 | import android.stats.location.LocationStatsEnums; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 73 | import android.text.TextUtils; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 74 | import android.util.EventLog; |
| 75 | import android.util.Log; |
| 76 | import android.util.Slog; |
Soonil Nagarkar | 06e37f6 | 2019-12-04 16:05:08 -0800 | [diff] [blame] | 77 | import android.util.SparseArray; |
WyattRiley | ba6072f | 2019-04-18 07:37:52 -0700 | [diff] [blame] | 78 | import android.util.TimeUtils; |
Yu-Han Yang | a4d250e | 2018-10-02 21:29:20 -0700 | [diff] [blame] | 79 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 80 | import com.android.internal.annotations.GuardedBy; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 81 | import com.android.internal.content.PackageMonitor; |
| 82 | import com.android.internal.location.ProviderProperties; |
| 83 | import com.android.internal.location.ProviderRequest; |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 84 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 85 | import com.android.internal.util.DumpUtils; |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 86 | import com.android.internal.util.IndentingPrintWriter; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 87 | import com.android.internal.util.Preconditions; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 88 | import com.android.server.location.AbstractLocationProvider; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 89 | import com.android.server.location.AbstractLocationProvider.State; |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 90 | import com.android.server.location.AppForegroundHelper; |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 91 | import com.android.server.location.CallerIdentity; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 92 | import com.android.server.location.GeocoderProxy; |
| 93 | import com.android.server.location.GeofenceManager; |
| 94 | import com.android.server.location.GeofenceProxy; |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 95 | import com.android.server.location.HardwareActivityRecognitionProxy; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 96 | import com.android.server.location.LocationFudger; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 97 | import com.android.server.location.LocationProviderProxy; |
| 98 | import com.android.server.location.LocationRequestStatistics; |
| 99 | import com.android.server.location.LocationRequestStatistics.PackageProviderKey; |
| 100 | import com.android.server.location.LocationRequestStatistics.PackageStatistics; |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 101 | import com.android.server.location.LocationUsageLogger; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 102 | import com.android.server.location.MockProvider; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 103 | import com.android.server.location.MockableLocationProvider; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 104 | import com.android.server.location.PassiveProvider; |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 105 | import com.android.server.location.SettingsHelper; |
| 106 | import com.android.server.location.UserInfoHelper; |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 107 | import com.android.server.location.gnss.GnssManagerService; |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 108 | import com.android.server.pm.permission.PermissionManagerServiceInternal; |
Yu-Han Yang | a4d250e | 2018-10-02 21:29:20 -0700 | [diff] [blame] | 109 | |
Soonil Nagarkar | ae6ce77 | 2019-05-01 13:16:17 -0700 | [diff] [blame] | 110 | import java.io.ByteArrayOutputStream; |
Mike Lockwood | 43e33f2 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 111 | import java.io.FileDescriptor; |
Soonil Nagarkar | ae6ce77 | 2019-05-01 13:16:17 -0700 | [diff] [blame] | 112 | import java.io.PrintStream; |
Mike Lockwood | 43e33f2 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 113 | import java.io.PrintWriter; |
| 114 | import java.util.ArrayList; |
Soonil Nagarkar | 0d77ea6 | 2019-01-31 14:36:56 -0800 | [diff] [blame] | 115 | import java.util.Collections; |
Mike Lockwood | 43e33f2 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 116 | import java.util.HashMap; |
| 117 | import java.util.HashSet; |
| 118 | import java.util.List; |
| 119 | import java.util.Map; |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 120 | import java.util.Map.Entry; |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 121 | import java.util.Objects; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 122 | import java.util.Set; |
WyattRiley | d6b82a6 | 2019-12-30 18:12:50 -0800 | [diff] [blame] | 123 | import java.util.TreeMap; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 124 | import java.util.concurrent.CopyOnWriteArrayList; |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 125 | import java.util.concurrent.TimeUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * The service class that manages LocationProviders and issues location |
| 129 | * updates and alerts. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | */ |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 131 | public class LocationManagerService extends ILocationManager.Stub { |
Soonil Nagarkar | 141417a | 2019-10-28 11:12:48 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Controls lifecycle of LocationManagerService. |
| 135 | */ |
| 136 | public static class Lifecycle extends SystemService { |
| 137 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 138 | private final LocationManagerService mService; |
Soonil Nagarkar | 141417a | 2019-10-28 11:12:48 -0700 | [diff] [blame] | 139 | |
| 140 | public Lifecycle(Context context) { |
| 141 | super(context); |
| 142 | mService = new LocationManagerService(context); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void onStart() { |
| 147 | publishBinderService(Context.LOCATION_SERVICE, mService); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onBootPhase(int phase) { |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 152 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 153 | // the location service must be functioning after this boot phase |
| 154 | mService.onSystemReady(); |
| 155 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 156 | // some providers rely on third party code, so we wait to initialize |
| 157 | // providers until third party code is allowed to run |
| 158 | mService.onSystemThirdPartyAppsCanStart(); |
Soonil Nagarkar | 141417a | 2019-10-28 11:12:48 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 163 | public static final String TAG = "LocationManagerService"; |
JP Abgrall | f79811e7 | 2013-02-01 18:45:05 -0800 | [diff] [blame] | 164 | public static final boolean D = Log.isLoggable(TAG, Log.DEBUG); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 165 | |
Olivier Gaillard | 7a22266 | 2017-11-20 16:07:24 +0000 | [diff] [blame] | 166 | private static final String WAKELOCK_KEY = "*location*"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 168 | private static final int RESOLUTION_LEVEL_NONE = 0; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 169 | private static final int RESOLUTION_LEVEL_COARSE = 1; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 170 | private static final int RESOLUTION_LEVEL_FINE = 2; |
| 171 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 172 | private static final String NETWORK_LOCATION_SERVICE_ACTION = |
Stan Chesnutt | 39062dd | 2013-07-22 14:33:30 -0700 | [diff] [blame] | 173 | "com.android.location.service.v3.NetworkLocationProvider"; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 174 | private static final String FUSED_LOCATION_SERVICE_ACTION = |
| 175 | "com.android.location.service.FusedLocationProvider"; |
| 176 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 177 | // The maximum interval a location request can have and still be considered "high power". |
| 178 | private static final long HIGH_POWER_INTERVAL_MS = 5 * 60 * 1000; |
| 179 | |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 180 | // maximum age of a location before it is no longer considered "current" |
| 181 | private static final long MAX_CURRENT_LOCATION_AGE_MS = 10 * 1000; |
| 182 | |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 183 | // Location Providers may sometimes deliver location updates |
| 184 | // slightly faster that requested - provide grace period so |
| 185 | // we don't unnecessarily filter events that are otherwise on |
| 186 | // time |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 187 | private static final int MAX_PROVIDER_SCHEDULING_JITTER_MS = 100; |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 188 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 189 | private static final LocationRequest DEFAULT_LOCATION_REQUEST = new LocationRequest(); |
| 190 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 191 | private final Object mLock = new Object(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 192 | private final Context mContext; |
| 193 | private final Handler mHandler; |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 194 | private final LocalService mLocalService; |
| 195 | private final UserInfoHelper mUserInfoHelper; |
| 196 | private final SettingsHelper mSettingsHelper; |
| 197 | private final AppForegroundHelper mAppForegroundHelper; |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 198 | private final LocationUsageLogger mLocationUsageLogger; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 199 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 200 | @Nullable private GnssManagerService mGnssManagerService = null; |
| 201 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 202 | private final PassiveLocationProviderManager mPassiveManager; |
| 203 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 204 | private AppOpsManager mAppOps; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 205 | private PackageManager mPackageManager; |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 206 | private PowerManager mPowerManager; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 207 | |
| 208 | private GeofenceManager mGeofenceManager; |
| 209 | private LocationFudger mLocationFudger; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 210 | private GeocoderProxy mGeocodeProvider; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 211 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 212 | @GuardedBy("mLock") |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 213 | private String mExtraLocationControllerPackage; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 214 | @GuardedBy("mLock") |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 215 | private boolean mExtraLocationControllerPackageEnabled; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 216 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 217 | // @GuardedBy("mLock") |
| 218 | // hold lock for write or to prevent write, no lock for read |
| 219 | private final CopyOnWriteArrayList<LocationProviderManager> mProviderManagers = |
| 220 | new CopyOnWriteArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 222 | @GuardedBy("mLock") |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 223 | private final HashMap<Object, Receiver> mReceivers = new HashMap<>(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 224 | private final HashMap<String, ArrayList<UpdateRecord>> mRecordsByProvider = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 225 | new HashMap<>(); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 226 | |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 227 | private final LocationRequestStatistics mRequestStatistics = new LocationRequestStatistics(); |
| 228 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 229 | // mapping from provider name to last known location |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 230 | @GuardedBy("mLock") |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 231 | private final HashMap<String, Location> mLastLocation = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 233 | // same as mLastLocation, but is not updated faster than LocationFudger.FASTEST_INTERVAL_MS. |
| 234 | // locations stored here are not fudged for coarse permissions. |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 235 | @GuardedBy("mLock") |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 236 | private final HashMap<String, Location> mLastLocationCoarseInterval = |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 237 | new HashMap<>(); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 238 | |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 239 | @GuardedBy("mLock") |
| 240 | @PowerManager.LocationPowerSaveMode |
| 241 | private int mBatterySaverMode; |
| 242 | |
Soonil Nagarkar | 141417a | 2019-10-28 11:12:48 -0700 | [diff] [blame] | 243 | private LocationManagerService(Context context) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 244 | mContext = context; |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 245 | mHandler = FgThread.getHandler(); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 246 | mLocalService = new LocalService(); |
| 247 | |
| 248 | LocalServices.addService(LocationManagerInternal.class, mLocalService); |
| 249 | |
| 250 | mUserInfoHelper = new UserInfoHelper(mContext); |
| 251 | mSettingsHelper = new SettingsHelper(mContext, mHandler); |
| 252 | mAppForegroundHelper = new AppForegroundHelper(mContext); |
Hongyi Zhang | 700137e | 2019-05-23 21:19:36 -0700 | [diff] [blame] | 253 | mLocationUsageLogger = new LocationUsageLogger(); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 254 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 255 | // set up passive provider - we do this early because it has no dependencies on system |
| 256 | // services or external code that isn't ready yet, and because this allows the variable to |
| 257 | // be final. other more complex providers are initialized later, when system services are |
| 258 | // ready |
| 259 | mPassiveManager = new PassiveLocationProviderManager(); |
| 260 | mProviderManagers.add(mPassiveManager); |
| 261 | mPassiveManager.setRealProvider(new PassiveProvider(mContext)); |
| 262 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 263 | // Let the package manager query which are the default location |
| 264 | // providers as they get certain permissions granted by default. |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 265 | PermissionManagerServiceInternal permissionManagerInternal = LocalServices.getService( |
| 266 | PermissionManagerServiceInternal.class); |
| 267 | permissionManagerInternal.setLocationPackagesProvider( |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 268 | userId -> mContext.getResources().getStringArray( |
| 269 | com.android.internal.R.array.config_locationProviderPackageNames)); |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 270 | permissionManagerInternal.setLocationExtraPackagesProvider( |
Wei Wang | ffb94e6 | 2019-01-14 00:05:45 -0800 | [diff] [blame] | 271 | userId -> mContext.getResources().getStringArray( |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 272 | com.android.internal.R.array.config_locationExtraPackageNames)); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 273 | |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 274 | // most startup is deferred until systemReady() |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 277 | private void onSystemReady() { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 278 | mUserInfoHelper.onSystemReady(); |
| 279 | mSettingsHelper.onSystemReady(); |
| 280 | mAppForegroundHelper.onSystemReady(); |
| 281 | |
| 282 | if (GnssManagerService.isGnssSupported()) { |
| 283 | mGnssManagerService = new GnssManagerService(mContext, mSettingsHelper, |
| 284 | mAppForegroundHelper, mLocationUsageLogger); |
| 285 | mGnssManagerService.onSystemReady(); |
| 286 | } |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 287 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 288 | synchronized (mLock) { |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 289 | mPackageManager = mContext.getPackageManager(); |
| 290 | mAppOps = mContext.getSystemService(AppOpsManager.class); |
| 291 | mPowerManager = mContext.getSystemService(PowerManager.class); |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 292 | |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 293 | mLocationFudger = new LocationFudger(mContext, mHandler); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 294 | mGeofenceManager = new GeofenceManager(mContext, mSettingsHelper); |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 295 | |
| 296 | PowerManagerInternal localPowerManager = |
| 297 | LocalServices.getService(PowerManagerInternal.class); |
| 298 | |
| 299 | // add listeners |
| 300 | mAppOps.startWatchingMode( |
| 301 | AppOpsManager.OP_COARSE_LOCATION, |
| 302 | null, |
| 303 | AppOpsManager.WATCH_FOREGROUND_CHANGES, |
| 304 | new AppOpsManager.OnOpChangedInternalListener() { |
| 305 | public void onOpChanged(int op, String packageName) { |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 306 | // onOpChanged invoked on ui thread, move to our thread to reduce risk |
| 307 | // of blocking ui thread |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 308 | mHandler.post(() -> { |
| 309 | synchronized (mLock) { |
| 310 | onAppOpChangedLocked(); |
| 311 | } |
| 312 | }); |
| 313 | } |
| 314 | }); |
| 315 | mPackageManager.addOnPermissionsChangeListener( |
| 316 | uid -> { |
| 317 | // listener invoked on ui thread, move to our thread to reduce risk of |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 318 | // blocking ui thread |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 319 | mHandler.post(() -> { |
| 320 | synchronized (mLock) { |
| 321 | onPermissionsChangedLocked(); |
| 322 | } |
| 323 | }); |
| 324 | }); |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 325 | |
| 326 | localPowerManager.registerLowPowerModeObserver(ServiceType.LOCATION, |
| 327 | state -> { |
| 328 | // listener invoked on ui thread, move to our thread to reduce risk of |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 329 | // blocking ui thread |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 330 | mHandler.post(() -> { |
| 331 | synchronized (mLock) { |
| 332 | onBatterySaverModeChangedLocked(state.locationMode); |
| 333 | } |
| 334 | }); |
| 335 | }); |
| 336 | mBatterySaverMode = mPowerManager.getLocationPowerSaveMode(); |
| 337 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 338 | mSettingsHelper.addOnLocationEnabledChangedListener(this::onLocationModeChanged); |
| 339 | mSettingsHelper.addOnBackgroundThrottleIntervalChangedListener( |
| 340 | this::onBackgroundThrottleIntervalChanged); |
| 341 | mSettingsHelper.addOnBackgroundThrottlePackageWhitelistChangedListener( |
| 342 | this::onBackgroundThrottleWhitelistChanged); |
| 343 | mSettingsHelper.addOnIgnoreSettingsPackageWhitelistChangedListener( |
| 344 | this::onIgnoreSettingsWhitelistChanged); |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 345 | |
| 346 | new PackageMonitor() { |
| 347 | @Override |
| 348 | public void onPackageDisappeared(String packageName, int reason) { |
| 349 | synchronized (mLock) { |
| 350 | LocationManagerService.this.onPackageDisappearedLocked(packageName); |
| 351 | } |
| 352 | } |
| 353 | }.register(mContext, mHandler.getLooper(), true); |
| 354 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 355 | mUserInfoHelper.addListener(this::onUserChanged); |
| 356 | |
| 357 | mAppForegroundHelper.addListener(this::onAppForegroundChanged); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 358 | |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 359 | IntentFilter intentFilter = new IntentFilter(); |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 360 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 361 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 362 | |
| 363 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 364 | @Override |
| 365 | public void onReceive(Context context, Intent intent) { |
| 366 | final String action = intent.getAction(); |
| 367 | if (action == null) { |
| 368 | return; |
| 369 | } |
| 370 | synchronized (mLock) { |
| 371 | switch (action) { |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 372 | case Intent.ACTION_SCREEN_ON: |
| 373 | case Intent.ACTION_SCREEN_OFF: |
| 374 | onScreenStateChangedLocked(); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | }, UserHandle.ALL, intentFilter, null, mHandler); |
| 380 | |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 381 | // switching the user from null to current here performs the bulk of the initialization |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 382 | // work. the user being changed will cause a reload of all user specific settings, which |
| 383 | // causes initialization, and propagates changes until a steady state is reached |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 384 | onUserChanged(UserHandle.USER_NULL, mUserInfoHelper.getCurrentUserId()); |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 385 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 386 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 387 | |
Soonil Nagarkar | 3709606 | 2019-11-25 13:24:48 -0800 | [diff] [blame] | 388 | private void onSystemThirdPartyAppsCanStart() { |
| 389 | synchronized (mLock) { |
| 390 | // prepare providers |
| 391 | initializeProvidersLocked(); |
| 392 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 395 | @GuardedBy("mLock") |
| 396 | private void onAppOpChangedLocked() { |
| 397 | for (Receiver receiver : mReceivers.values()) { |
| 398 | receiver.updateMonitoring(true); |
| 399 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 400 | for (LocationProviderManager manager : mProviderManagers) { |
| 401 | applyRequirementsLocked(manager); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | @GuardedBy("mLock") |
| 406 | private void onPermissionsChangedLocked() { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 407 | for (LocationProviderManager manager : mProviderManagers) { |
| 408 | applyRequirementsLocked(manager); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | @GuardedBy("mLock") |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 413 | private void onBatterySaverModeChangedLocked(int newLocationMode) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 414 | if (mBatterySaverMode == newLocationMode) { |
| 415 | return; |
| 416 | } |
| 417 | |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 418 | if (D) { |
| 419 | Slog.d(TAG, |
| 420 | "Battery Saver location mode changed from " |
| 421 | + locationPowerSaveModeToString(mBatterySaverMode) + " to " |
| 422 | + locationPowerSaveModeToString(newLocationMode)); |
| 423 | } |
| 424 | |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 425 | mBatterySaverMode = newLocationMode; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 426 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 427 | for (LocationProviderManager manager : mProviderManagers) { |
| 428 | applyRequirementsLocked(manager); |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | @GuardedBy("mLock") |
| 433 | private void onScreenStateChangedLocked() { |
| 434 | if (mBatterySaverMode == PowerManager.LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 435 | for (LocationProviderManager manager : mProviderManagers) { |
| 436 | applyRequirementsLocked(manager); |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 441 | private void onLocationModeChanged(int userId) { |
| 442 | boolean enabled = mSettingsHelper.isLocationEnabled(userId); |
| 443 | |
Soonil Nagarkar | 06e37f6 | 2019-12-04 16:05:08 -0800 | [diff] [blame] | 444 | if (D) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 445 | Log.d(TAG, "[u" + userId + "] location enabled = " + enabled); |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 448 | synchronized (mLock) { |
| 449 | Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION) |
| 450 | .putExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled) |
| 451 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 452 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 453 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 454 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 455 | for (LocationProviderManager manager : mProviderManagers) { |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 456 | manager.onEnabledChangedLocked(userId); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 457 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 458 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @GuardedBy("mLock") |
| 462 | private void onPackageDisappearedLocked(String packageName) { |
| 463 | ArrayList<Receiver> deadReceivers = null; |
| 464 | |
| 465 | for (Receiver receiver : mReceivers.values()) { |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 466 | if (receiver.mCallerIdentity.mPackageName.equals(packageName)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 467 | if (deadReceivers == null) { |
| 468 | deadReceivers = new ArrayList<>(); |
| 469 | } |
| 470 | deadReceivers.add(receiver); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // perform removal outside of mReceivers loop |
| 475 | if (deadReceivers != null) { |
| 476 | for (Receiver receiver : deadReceivers) { |
| 477 | removeUpdatesLocked(receiver); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 482 | private void onAppForegroundChanged(int uid, boolean foreground) { |
| 483 | synchronized (mLock) { |
| 484 | HashSet<String> affectedProviders = new HashSet<>(mRecordsByProvider.size()); |
| 485 | for (Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) { |
| 486 | String provider = entry.getKey(); |
| 487 | for (UpdateRecord record : entry.getValue()) { |
| 488 | if (record.mReceiver.mCallerIdentity.mUid == uid |
| 489 | && record.mIsForegroundUid != foreground) { |
| 490 | record.updateForeground(foreground); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 491 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 492 | if (!isThrottlingExempt(record.mReceiver.mCallerIdentity)) { |
| 493 | affectedProviders.add(provider); |
| 494 | } |
Soonil Nagarkar | e056b0d | 2017-06-21 13:08:16 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 498 | for (String provider : affectedProviders) { |
| 499 | applyRequirementsLocked(provider); |
| 500 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 501 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 504 | private void onBackgroundThrottleIntervalChanged() { |
| 505 | synchronized (mLock) { |
| 506 | for (LocationProviderManager manager : mProviderManagers) { |
| 507 | applyRequirementsLocked(manager); |
| 508 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 509 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 512 | private void onBackgroundThrottleWhitelistChanged() { |
| 513 | synchronized (mLock) { |
| 514 | for (LocationProviderManager manager : mProviderManagers) { |
| 515 | applyRequirementsLocked(manager); |
| 516 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 517 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 520 | private void onIgnoreSettingsWhitelistChanged() { |
| 521 | synchronized (mLock) { |
| 522 | for (LocationProviderManager manager : mProviderManagers) { |
| 523 | applyRequirementsLocked(manager); |
| 524 | } |
Soonil Nagarkar | 397ad58 | 2019-01-23 22:47:57 -0800 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 528 | @GuardedBy("mLock") |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 529 | private void initializeProvidersLocked() { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 530 | if (mGnssManagerService != null) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 531 | LocationProviderManager gnssManager = new LocationProviderManager(GPS_PROVIDER); |
| 532 | mProviderManagers.add(gnssManager); |
| 533 | gnssManager.setRealProvider(mGnssManagerService.getGnssLocationProvider()); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 536 | LocationProviderProxy networkProvider = LocationProviderProxy.createAndRegister( |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 537 | mContext, |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 538 | NETWORK_LOCATION_SERVICE_ACTION, |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 539 | com.android.internal.R.bool.config_enableNetworkLocationOverlay, |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 540 | com.android.internal.R.string.config_networkLocationProviderPackageName); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 541 | if (networkProvider != null) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 542 | LocationProviderManager networkManager = new LocationProviderManager(NETWORK_PROVIDER); |
| 543 | mProviderManagers.add(networkManager); |
| 544 | networkManager.setRealProvider(networkProvider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 545 | } else { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 546 | Slog.w(TAG, "no network location provider found"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 549 | // ensure that a fused provider exists which will work in direct boot |
| 550 | Preconditions.checkState(!mContext.getPackageManager().queryIntentServicesAsUser( |
| 551 | new Intent(FUSED_LOCATION_SERVICE_ACTION), |
| 552 | MATCH_DIRECT_BOOT_AWARE | MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM).isEmpty(), |
| 553 | "Unable to find a direct boot aware fused location provider"); |
| 554 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 555 | // bind to fused provider |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 556 | LocationProviderProxy fusedProvider = LocationProviderProxy.createAndRegister( |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 557 | mContext, |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 558 | FUSED_LOCATION_SERVICE_ACTION, |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 559 | com.android.internal.R.bool.config_enableFusedLocationOverlay, |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 560 | com.android.internal.R.string.config_fusedLocationProviderPackageName); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 561 | if (fusedProvider != null) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 562 | LocationProviderManager fusedManager = new LocationProviderManager(FUSED_PROVIDER); |
| 563 | mProviderManagers.add(fusedManager); |
| 564 | fusedManager.setRealProvider(fusedProvider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 565 | } else { |
| 566 | Slog.e(TAG, "no fused location provider found", |
| 567 | new IllegalStateException("Location service needs a fused location provider")); |
| 568 | } |
| 569 | |
| 570 | // bind to geocoder provider |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 571 | mGeocodeProvider = GeocoderProxy.createAndRegister(mContext); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 572 | if (mGeocodeProvider == null) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 573 | Slog.e(TAG, "no geocoder provider found"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 574 | } |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 575 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 576 | // bind to geofence proxy |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 577 | if (mGnssManagerService != null) { |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 578 | IGpsGeofenceHardware gpsGeofenceHardware = mGnssManagerService.getGpsGeofenceProxy(); |
| 579 | if (gpsGeofenceHardware != null) { |
| 580 | GeofenceProxy provider = GeofenceProxy.createAndBind(mContext, gpsGeofenceHardware); |
| 581 | if (provider == null) { |
| 582 | Slog.d(TAG, "unable to bind to GeofenceProxy"); |
| 583 | } |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 584 | } |
destradaa | 0682809a | 2013-08-12 18:50:30 -0700 | [diff] [blame] | 585 | } |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 586 | |
Brian Duddie | cfa5b5b | 2019-01-22 18:01:40 +0000 | [diff] [blame] | 587 | // bind to hardware activity recognition |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 588 | HardwareActivityRecognitionProxy hardwareActivityRecognitionProxy = |
| 589 | HardwareActivityRecognitionProxy.createAndRegister(mContext); |
| 590 | if (hardwareActivityRecognitionProxy == null) { |
| 591 | Log.e(TAG, "unable to bind ActivityRecognitionProxy"); |
Brian Duddie | cfa5b5b | 2019-01-22 18:01:40 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 594 | String[] testProviderStrings = mContext.getResources().getStringArray( |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 595 | com.android.internal.R.array.config_testLocationProviders); |
| 596 | for (String testProviderString : testProviderStrings) { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 597 | String[] fragments = testProviderString.split(","); |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 598 | String name = fragments[0].trim(); |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 599 | ProviderProperties properties = new ProviderProperties( |
| 600 | Boolean.parseBoolean(fragments[1]) /* requiresNetwork */, |
| 601 | Boolean.parseBoolean(fragments[2]) /* requiresSatellite */, |
| 602 | Boolean.parseBoolean(fragments[3]) /* requiresCell */, |
| 603 | Boolean.parseBoolean(fragments[4]) /* hasMonetaryCost */, |
| 604 | Boolean.parseBoolean(fragments[5]) /* supportsAltitude */, |
| 605 | Boolean.parseBoolean(fragments[6]) /* supportsSpeed */, |
| 606 | Boolean.parseBoolean(fragments[7]) /* supportsBearing */, |
| 607 | Integer.parseInt(fragments[8]) /* powerRequirement */, |
| 608 | Integer.parseInt(fragments[9]) /* accuracy */); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 609 | addTestProvider(name, properties, mContext.getOpPackageName()); |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 610 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 611 | } |
Mike Lockwood | 9637d47 | 2009-04-02 21:41:57 -0700 | [diff] [blame] | 612 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 613 | private void onUserChanged(int oldUserId, int newUserId) { |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 614 | if (D) { |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 615 | Log.d(TAG, "foreground user is changing to " + newUserId); |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 618 | synchronized (mLock) { |
| 619 | for (LocationProviderManager manager : mProviderManagers) { |
| 620 | // update LOCATION_PROVIDERS_ALLOWED for best effort backwards compatibility |
| 621 | mSettingsHelper.setLocationProviderAllowed(manager.getName(), |
| 622 | manager.isEnabled(newUserId), newUserId); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 623 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 624 | manager.onEnabledChangedLocked(oldUserId); |
| 625 | manager.onEnabledChangedLocked(newUserId); |
| 626 | } |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 627 | } |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 630 | /** |
| 631 | * Location provider manager, manages a LocationProvider. |
| 632 | */ |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 633 | class LocationProviderManager implements MockableLocationProvider.Listener { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 634 | |
| 635 | private final String mName; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 636 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 637 | // acquiring mLock makes operations on mProvider atomic, but is otherwise unnecessary |
| 638 | protected final MockableLocationProvider mProvider; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 639 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 640 | // enabled state for parent user ids, no entry implies false. location state is only kept |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 641 | // for parent user ids, the location state for a profile user id is assumed to be the same |
| 642 | // as for the parent. if querying this structure, ensure that the user id being used is a |
| 643 | // parent id or the results may be incorrect. |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 644 | @GuardedBy("mLock") |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 645 | private final SparseArray<Boolean> mEnabled; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 646 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 647 | private LocationProviderManager(String name) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 648 | mName = name; |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 649 | mEnabled = new SparseArray<>(1); |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 650 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 651 | // initialize last since this lets our reference escape |
| 652 | mProvider = new MockableLocationProvider(mContext, mLock, this); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 653 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 654 | // we can assume all users start with disabled location state since the initial state |
| 655 | // of all providers is disabled. no need to initialize mEnabled further. |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 658 | public String getName() { |
| 659 | return mName; |
| 660 | } |
| 661 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 662 | public boolean hasProvider() { |
| 663 | return mProvider.getProvider() != null; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 666 | public void setRealProvider(AbstractLocationProvider provider) { |
| 667 | mProvider.setRealProvider(provider); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 670 | public void setMockProvider(@Nullable MockProvider provider) { |
| 671 | mProvider.setMockProvider(provider); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 674 | public Set<String> getPackages() { |
| 675 | return mProvider.getState().providerPackageNames; |
| 676 | } |
| 677 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 678 | @Nullable |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 679 | public ProviderProperties getProperties() { |
| 680 | return mProvider.getState().properties; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 683 | public void setMockProviderAllowed(boolean enabled) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 684 | synchronized (mLock) { |
| 685 | if (!mProvider.isMock()) { |
| 686 | throw new IllegalArgumentException(mName + " provider is not a test provider"); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 687 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 688 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 689 | mProvider.setMockProviderAllowed(enabled); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 690 | } |
Soonil Nagarkar | d334f7b | 2019-07-08 16:16:19 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 693 | public void setMockProviderLocation(Location location) { |
| 694 | synchronized (mLock) { |
| 695 | if (!mProvider.isMock()) { |
| 696 | throw new IllegalArgumentException(mName + " provider is not a test provider"); |
Soonil Nagarkar | d334f7b | 2019-07-08 16:16:19 -0700 | [diff] [blame] | 697 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 698 | |
| 699 | String locationProvider = location.getProvider(); |
| 700 | if (!TextUtils.isEmpty(locationProvider) && !mName.equals(locationProvider)) { |
| 701 | // The location has an explicit provider that is different from the mock |
| 702 | // provider name. The caller may be trying to fool us via b/33091107. |
| 703 | EventLog.writeEvent(0x534e4554, "33091107", Binder.getCallingUid(), |
| 704 | mName + "!=" + locationProvider); |
| 705 | } |
| 706 | |
| 707 | mProvider.setMockProviderLocation(location); |
| 708 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 711 | public List<LocationRequest> getMockProviderRequests() { |
| 712 | synchronized (mLock) { |
| 713 | if (!mProvider.isMock()) { |
| 714 | throw new IllegalArgumentException(mName + " provider is not a test provider"); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 715 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 716 | |
| 717 | return mProvider.getCurrentRequest().locationRequests; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 718 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | public void setRequest(ProviderRequest request) { |
| 722 | mProvider.setRequest(request); |
| 723 | } |
| 724 | |
| 725 | public void sendExtraCommand(int uid, int pid, String command, Bundle extras) { |
| 726 | mProvider.sendExtraCommand(uid, pid, command, extras); |
| 727 | } |
| 728 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 729 | @GuardedBy("mLock") |
Soonil Nagarkar | d334f7b | 2019-07-08 16:16:19 -0700 | [diff] [blame] | 730 | @Override |
| 731 | public void onReportLocation(Location location) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 732 | // don't validate mock locations |
| 733 | if (!location.isFromMockProvider()) { |
| 734 | if (location.getLatitude() == 0 && location.getLongitude() == 0) { |
| 735 | Slog.w(TAG, "blocking 0,0 location from " + mName + " provider"); |
| 736 | return; |
| 737 | } |
Soonil Nagarkar | 7fc24c6 | 2019-10-18 17:32:38 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 740 | handleLocationChangedLocked(location, this); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 743 | @GuardedBy("mLock") |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 744 | @Override |
| 745 | public void onReportLocation(List<Location> locations) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 746 | if (mGnssManagerService == null) { |
| 747 | return; |
| 748 | } |
Soonil Nagarkar | d334f7b | 2019-07-08 16:16:19 -0700 | [diff] [blame] | 749 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 750 | if (!GPS_PROVIDER.equals(mName) || !isEnabled()) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 751 | Slog.w(TAG, "reportLocationBatch() called without user permission"); |
| 752 | return; |
Soonil Nagarkar | d334f7b | 2019-07-08 16:16:19 -0700 | [diff] [blame] | 753 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 754 | |
| 755 | mGnssManagerService.onReportLocation(locations); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 758 | @GuardedBy("mLock") |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 759 | @Override |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 760 | public void onStateChanged(State oldState, State newState) { |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 761 | if (oldState.allowed != newState.allowed) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 762 | // it would be more correct to call this for all users, but we know this can |
| 763 | // only affect the current user since providers are disabled for non-current |
| 764 | // users |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 765 | onEnabledChangedLocked(mUserInfoHelper.getCurrentUserId()); |
Soonil Nagarkar | d334f7b | 2019-07-08 16:16:19 -0700 | [diff] [blame] | 766 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 769 | public void requestSetAllowed(boolean allowed) { |
| 770 | mProvider.requestSetAllowed(allowed); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 773 | public boolean isEnabled() { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 774 | return isEnabled(mUserInfoHelper.getCurrentUserId()); |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | public boolean isEnabled(int userId) { |
Soonil Nagarkar | 66c0bac | 2019-01-15 13:36:44 -0800 | [diff] [blame] | 778 | synchronized (mLock) { |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 779 | // normalize user id to always refer to parent since profile state is always the |
| 780 | // same as parent state |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 781 | userId = mUserInfoHelper.getParentUserId(userId); |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 782 | return mEnabled.get(userId, Boolean.FALSE); |
Soonil Nagarkar | 66c0bac | 2019-01-15 13:36:44 -0800 | [diff] [blame] | 783 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 786 | @GuardedBy("mLock") |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 787 | public void onEnabledChangedLocked(int userId) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 788 | if (userId == UserHandle.USER_NULL) { |
| 789 | // only used during initialization - we don't care about the null user |
| 790 | return; |
| 791 | } |
| 792 | |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 793 | // normalize user id to always refer to parent since profile state is always the same |
| 794 | // as parent state |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 795 | userId = mUserInfoHelper.getParentUserId(userId); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 796 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 797 | // if any property that contributes to "enabled" here changes state, it MUST result |
| 798 | // in a direct or indrect call to onEnabledChangedLocked. this allows the provider to |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 799 | // guarantee that it will always eventually reach the correct state. |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 800 | boolean enabled = (userId == mUserInfoHelper.getCurrentUserId()) |
| 801 | && mSettingsHelper.isLocationEnabled(userId) && mProvider.getState().allowed; |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 802 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 803 | if (enabled == isEnabled(userId)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 804 | return; |
| 805 | } |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 806 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 807 | mEnabled.put(userId, enabled); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 808 | |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 809 | if (D) { |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 810 | Log.d(TAG, "[u" + userId + "] " + mName + " provider enabled = " + enabled); |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Soonil Nagarkar | 57f4b7b | 2019-12-09 10:19:18 -0800 | [diff] [blame] | 813 | // fused and passive provider never get public updates for legacy reasons |
| 814 | if (!FUSED_PROVIDER.equals(mName) && !PASSIVE_PROVIDER.equals(mName)) { |
| 815 | // update LOCATION_PROVIDERS_ALLOWED for best effort backwards compatibility |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 816 | mSettingsHelper.setLocationProviderAllowed(mName, enabled, userId); |
Soonil Nagarkar | 57f4b7b | 2019-12-09 10:19:18 -0800 | [diff] [blame] | 817 | |
Soonil Nagarkar | dbd467e | 2020-01-14 15:10:45 -0800 | [diff] [blame] | 818 | Intent intent = new Intent(LocationManager.PROVIDERS_CHANGED_ACTION) |
| 819 | .putExtra(LocationManager.EXTRA_PROVIDER_NAME, mName) |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 820 | .putExtra(LocationManager.EXTRA_PROVIDER_ENABLED, enabled) |
Soonil Nagarkar | dbd467e | 2020-01-14 15:10:45 -0800 | [diff] [blame] | 821 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 822 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Soonil Nagarkar | 57f4b7b | 2019-12-09 10:19:18 -0800 | [diff] [blame] | 823 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 824 | } |
| 825 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 826 | if (!enabled) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 827 | // If any provider has been disabled, clear all last locations for all |
| 828 | // providers. This is to be on the safe side in case a provider has location |
| 829 | // derived from this disabled provider. |
| 830 | mLastLocation.clear(); |
| 831 | mLastLocationCoarseInterval.clear(); |
| 832 | } |
| 833 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 834 | updateProviderEnabledLocked(this); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 835 | } |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 836 | |
| 837 | public void dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args) { |
| 838 | synchronized (mLock) { |
| 839 | pw.print(mName + " provider"); |
| 840 | if (mProvider.isMock()) { |
| 841 | pw.print(" [mock]"); |
| 842 | } |
| 843 | pw.println(":"); |
| 844 | |
| 845 | pw.increaseIndent(); |
| 846 | |
Soonil Nagarkar | ed5a200 | 2020-01-28 10:53:53 -0800 | [diff] [blame] | 847 | pw.println("enabled=" + isEnabled()); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | mProvider.dump(fd, pw, args); |
| 851 | |
| 852 | pw.decreaseIndent(); |
| 853 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 856 | class PassiveLocationProviderManager extends LocationProviderManager { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 857 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 858 | private PassiveLocationProviderManager() { |
| 859 | super(PASSIVE_PROVIDER); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | @Override |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 863 | public void setRealProvider(AbstractLocationProvider provider) { |
| 864 | Preconditions.checkArgument(provider instanceof PassiveProvider); |
| 865 | super.setRealProvider(provider); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 868 | @Override |
| 869 | public void setMockProvider(@Nullable MockProvider provider) { |
| 870 | if (provider != null) { |
| 871 | throw new IllegalArgumentException("Cannot mock the passive provider"); |
| 872 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 875 | public void updateLocation(Location location) { |
| 876 | synchronized (mLock) { |
| 877 | PassiveProvider passiveProvider = (PassiveProvider) mProvider.getProvider(); |
| 878 | Preconditions.checkState(passiveProvider != null); |
| 879 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 880 | long identity = Binder.clearCallingIdentity(); |
| 881 | try { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 882 | passiveProvider.updateLocation(location); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 883 | } finally { |
| 884 | Binder.restoreCallingIdentity(identity); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 890 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | * A wrapper class holding either an ILocationListener or a PendingIntent to receive |
| 892 | * location updates. |
| 893 | */ |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 894 | private final class Receiver extends LocationManagerServiceUtils.LinkedListenerBase implements |
| 895 | PendingIntent.OnFinished { |
Yu-Han Yang | 2418982 | 2018-07-11 15:24:11 -0700 | [diff] [blame] | 896 | private static final long WAKELOCK_TIMEOUT_MILLIS = 60 * 1000; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 897 | private final int mAllowedResolutionLevel; // resolution level allowed to receiver |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 898 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 899 | private final ILocationListener mListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | final PendingIntent mPendingIntent; |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 901 | 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] | 902 | private final boolean mHideFromAppOps; // True if AppOps should not monitor this receiver. |
| 903 | private final Object mKey; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 904 | |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 905 | final HashMap<String, UpdateRecord> mUpdateRecords = new HashMap<>(); |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 906 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 907 | // True if app ops has started monitoring this receiver for locations. |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 908 | private boolean mOpMonitoring; |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 909 | // 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] | 910 | private boolean mOpHighPowerMonitoring; |
| 911 | private int mPendingBroadcasts; |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 912 | PowerManager.WakeLock mWakeLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 914 | private Receiver(ILocationListener listener, PendingIntent intent, int pid, int uid, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 915 | String packageName, @Nullable String featureId, WorkSource workSource, |
| 916 | boolean hideFromAppOps, @NonNull String listenerIdentifier) { |
| 917 | super(new CallerIdentity(uid, pid, packageName, featureId, listenerIdentifier), |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 918 | "LocationListener"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | mListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | mPendingIntent = intent; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 921 | if (listener != null) { |
| 922 | mKey = listener.asBinder(); |
| 923 | } else { |
| 924 | mKey = intent; |
| 925 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 926 | mAllowedResolutionLevel = getAllowedResolutionLevel(pid, uid); |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 927 | if (workSource != null && workSource.isEmpty()) { |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 928 | workSource = null; |
| 929 | } |
| 930 | mWorkSource = workSource; |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 931 | mHideFromAppOps = hideFromAppOps; |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 932 | |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 933 | updateMonitoring(true); |
| 934 | |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 935 | // construct/configure wakelock |
| 936 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 937 | if (workSource == null) { |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 938 | workSource = new WorkSource(mCallerIdentity.mUid, mCallerIdentity.mPackageName); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 939 | } |
| 940 | mWakeLock.setWorkSource(workSource); |
Yu-Han Yang | 2418982 | 2018-07-11 15:24:11 -0700 | [diff] [blame] | 941 | |
| 942 | // For a non-reference counted wakelock, each acquire will reset the timeout, and we |
| 943 | // only need to release it once. |
| 944 | mWakeLock.setReferenceCounted(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | @Override |
| 948 | public boolean equals(Object otherObj) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 949 | return (otherObj instanceof Receiver) && mKey.equals(((Receiver) otherObj).mKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | @Override |
| 953 | public int hashCode() { |
| 954 | return mKey.hashCode(); |
| 955 | } |
Mike Lockwood | 3681f26 | 2009-05-12 10:52:03 -0400 | [diff] [blame] | 956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | @Override |
| 958 | public String toString() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 959 | StringBuilder s = new StringBuilder(); |
| 960 | s.append("Reciever["); |
| 961 | s.append(Integer.toHexString(System.identityHashCode(this))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | if (mListener != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 963 | s.append(" listener"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | } else { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 965 | s.append(" intent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 967 | for (String p : mUpdateRecords.keySet()) { |
| 968 | s.append(" ").append(mUpdateRecords.get(p).toString()); |
| 969 | } |
Wei Wang | dd070f2 | 2018-06-21 11:29:40 -0700 | [diff] [blame] | 970 | s.append(" monitoring location: ").append(mOpMonitoring); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 971 | s.append("]"); |
| 972 | return s.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | } |
| 974 | |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 975 | /** |
| 976 | * Update AppOp monitoring for this receiver. |
| 977 | * |
| 978 | * @param allow If true receiver is currently active, if false it's been removed. |
| 979 | */ |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 980 | public void updateMonitoring(boolean allow) { |
David Christie | 40e5782 | 2013-07-30 11:36:48 -0700 | [diff] [blame] | 981 | if (mHideFromAppOps) { |
| 982 | return; |
| 983 | } |
| 984 | |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 985 | boolean requestingLocation = false; |
| 986 | boolean requestingHighPowerLocation = false; |
| 987 | if (allow) { |
| 988 | // See if receiver has any enabled update records. Also note if any update records |
| 989 | // are high power (has a high power provider with an interval under a threshold). |
| 990 | for (UpdateRecord updateRecord : mUpdateRecords.values()) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 991 | LocationProviderManager manager = getLocationProviderManager( |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 992 | updateRecord.mProvider); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 993 | if (manager == null) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 994 | continue; |
| 995 | } |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 996 | if (!manager.isEnabled() && !isSettingsExempt(updateRecord)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 997 | continue; |
| 998 | } |
| 999 | |
| 1000 | requestingLocation = true; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1001 | ProviderProperties properties = manager.getProperties(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1002 | if (properties != null |
| 1003 | && properties.mPowerRequirement == Criteria.POWER_HIGH |
| 1004 | && updateRecord.mRequest.getInterval() < HIGH_POWER_INTERVAL_MS) { |
| 1005 | requestingHighPowerLocation = true; |
| 1006 | break; |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1011 | // First update monitoring of any location request (including high power). |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1012 | mOpMonitoring = updateMonitoring( |
| 1013 | requestingLocation, |
| 1014 | mOpMonitoring, |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1015 | AppOpsManager.OP_MONITOR_LOCATION); |
| 1016 | |
| 1017 | // Now update monitoring of high power requests only. |
David Christie | c750c1f | 2013-08-08 12:56:57 -0700 | [diff] [blame] | 1018 | boolean wasHighPowerMonitoring = mOpHighPowerMonitoring; |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1019 | mOpHighPowerMonitoring = updateMonitoring( |
| 1020 | requestingHighPowerLocation, |
| 1021 | mOpHighPowerMonitoring, |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1022 | AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION); |
David Christie | c750c1f | 2013-08-08 12:56:57 -0700 | [diff] [blame] | 1023 | if (mOpHighPowerMonitoring != wasHighPowerMonitoring) { |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1024 | // 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] | 1025 | Intent intent = new Intent(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION); |
| 1026 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1027 | } |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Update AppOps monitoring for a single location request and op type. |
| 1032 | * |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1033 | * @param allowMonitoring True if monitoring is allowed for this request/op. |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1034 | * @param currentlyMonitoring True if AppOps is currently monitoring this request/op. |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1035 | * @param op AppOps code for the op to update. |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1036 | * @return True if monitoring is on for this request/op after updating. |
| 1037 | */ |
| 1038 | private boolean updateMonitoring(boolean allowMonitoring, boolean currentlyMonitoring, |
| 1039 | int op) { |
| 1040 | if (!currentlyMonitoring) { |
| 1041 | if (allowMonitoring) { |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1042 | return mAppOps.startOpNoThrow(op, mCallerIdentity.mUid, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1043 | mCallerIdentity.mPackageName, false, mCallerIdentity.mFeatureId, null) |
| 1044 | == AppOpsManager.MODE_ALLOWED; |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } else { |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1047 | if (!allowMonitoring |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 1048 | || mAppOps.checkOpNoThrow(op, mCallerIdentity.mUid, |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1049 | mCallerIdentity.mPackageName) != AppOpsManager.MODE_ALLOWED) { |
| 1050 | mAppOps.finishOp(op, mCallerIdentity.mUid, mCallerIdentity.mPackageName); |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1051 | return false; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | return currentlyMonitoring; |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | public boolean isListener() { |
| 1059 | return mListener != null; |
| 1060 | } |
| 1061 | |
| 1062 | public boolean isPendingIntent() { |
| 1063 | return mPendingIntent != null; |
| 1064 | } |
| 1065 | |
| 1066 | public ILocationListener getListener() { |
| 1067 | if (mListener != null) { |
| 1068 | return mListener; |
| 1069 | } |
| 1070 | throw new IllegalStateException("Request for non-existent listener"); |
| 1071 | } |
| 1072 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | public boolean callLocationChangedLocked(Location location) { |
| 1074 | if (mListener != null) { |
| 1075 | try { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1076 | mListener.onLocationChanged(new Location(location)); |
| 1077 | // call this after broadcasting so we do not increment |
| 1078 | // if we throw an exception. |
| 1079 | incrementPendingBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } catch (RemoteException e) { |
| 1081 | return false; |
| 1082 | } |
| 1083 | } else { |
| 1084 | Intent locationChanged = new Intent(); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1085 | locationChanged.putExtra(LocationManager.KEY_LOCATION_CHANGED, |
| 1086 | new Location(location)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | try { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1088 | mPendingIntent.send(mContext, 0, locationChanged, this, mHandler, |
| 1089 | getResolutionPermission(mAllowedResolutionLevel), |
| 1090 | PendingIntentUtils.createDontSendToRestrictedAppsBundle(null)); |
| 1091 | // call this after broadcasting so we do not increment |
| 1092 | // if we throw an exception. |
| 1093 | incrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1094 | } catch (PendingIntent.CanceledException e) { |
| 1095 | return false; |
| 1096 | } |
| 1097 | } |
| 1098 | return true; |
| 1099 | } |
| 1100 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1101 | private boolean callProviderEnabledLocked(String provider, boolean enabled) { |
David Christie | 15b3191 | 2013-08-13 15:54:32 -0700 | [diff] [blame] | 1102 | // First update AppOp monitoring. |
| 1103 | // An app may get/lose location access as providers are enabled/disabled. |
| 1104 | updateMonitoring(true); |
| 1105 | |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1106 | if (mListener != null) { |
| 1107 | try { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1108 | if (enabled) { |
| 1109 | mListener.onProviderEnabled(provider); |
| 1110 | } else { |
| 1111 | mListener.onProviderDisabled(provider); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1112 | } |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1113 | // call this after broadcasting so we do not increment |
| 1114 | // if we throw an exception. |
| 1115 | incrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1116 | } catch (RemoteException e) { |
| 1117 | return false; |
| 1118 | } |
| 1119 | } else { |
| 1120 | Intent providerIntent = new Intent(); |
| 1121 | providerIntent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled); |
| 1122 | try { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1123 | mPendingIntent.send(mContext, 0, providerIntent, this, mHandler, |
| 1124 | getResolutionPermission(mAllowedResolutionLevel), |
| 1125 | PendingIntentUtils.createDontSendToRestrictedAppsBundle(null)); |
| 1126 | // call this after broadcasting so we do not increment |
| 1127 | // if we throw an exception. |
| 1128 | incrementPendingBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } catch (PendingIntent.CanceledException e) { |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
Soonil Nagarkar | 39d614a | 2019-10-31 12:10:31 -0700 | [diff] [blame] | 1136 | public void callRemovedLocked() { |
| 1137 | if (mListener != null) { |
| 1138 | try { |
| 1139 | mListener.onRemoved(); |
| 1140 | } catch (RemoteException e) { |
| 1141 | // doesn't matter |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 1146 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | public void binderDied() { |
Anil Admal | 98d49b7 | 2019-02-06 15:26:33 -0800 | [diff] [blame] | 1148 | if (D) Log.d(TAG, "Remote " + mListenerName + " died."); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1149 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1150 | synchronized (mLock) { |
| 1151 | removeUpdatesLocked(this); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1152 | clearPendingBroadcastsLocked(); |
| 1153 | } |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1156 | @Override |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1157 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, |
| 1158 | int resultCode, String resultData, Bundle resultExtras) { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1159 | synchronized (mLock) { |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 1160 | decrementPendingBroadcastsLocked(); |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 1164 | // this must be called while synchronized by caller in a synchronized block |
| 1165 | // containing the sending of the broadcaset |
| 1166 | private void incrementPendingBroadcastsLocked() { |
Yu-Han Yang | 2418982 | 2018-07-11 15:24:11 -0700 | [diff] [blame] | 1167 | mPendingBroadcasts++; |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1168 | // so wakelock calls will succeed |
| 1169 | long identity = Binder.clearCallingIdentity(); |
| 1170 | try { |
| 1171 | mWakeLock.acquire(WAKELOCK_TIMEOUT_MILLIS); |
| 1172 | } finally { |
| 1173 | Binder.restoreCallingIdentity(identity); |
| 1174 | } |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | private void decrementPendingBroadcastsLocked() { |
| 1178 | if (--mPendingBroadcasts == 0) { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1179 | // so wakelock calls will succeed |
| 1180 | long identity = Binder.clearCallingIdentity(); |
| 1181 | try { |
| 1182 | if (mWakeLock.isHeld()) { |
| 1183 | mWakeLock.release(); |
| 1184 | } |
| 1185 | } finally { |
| 1186 | Binder.restoreCallingIdentity(identity); |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | public void clearPendingBroadcastsLocked() { |
| 1192 | if (mPendingBroadcasts > 0) { |
| 1193 | mPendingBroadcasts = 0; |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1194 | // so wakelock calls will succeed |
| 1195 | long identity = Binder.clearCallingIdentity(); |
| 1196 | try { |
| 1197 | if (mWakeLock.isHeld()) { |
| 1198 | mWakeLock.release(); |
| 1199 | } |
| 1200 | } finally { |
| 1201 | Binder.restoreCallingIdentity(identity); |
Victoria Lease | 0aa2860 | 2013-05-29 15:28:26 -0700 | [diff] [blame] | 1202 | } |
Mike Lockwood | 48f1751 | 2009-04-23 09:12:08 -0700 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1207 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1208 | public void locationCallbackFinished(ILocationListener listener) { |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 1209 | //Do not use getReceiverLocked here as that will add the ILocationListener to |
Joshua Bartel | 080b61b | 2009-10-05 12:44:46 -0400 | [diff] [blame] | 1210 | //the receiver list if it is not found. If it is not found then the |
| 1211 | //LocationListener was removed when it had a pending broadcast and should |
| 1212 | //not be added back. |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1213 | synchronized (mLock) { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1214 | Receiver receiver = mReceivers.get(listener.asBinder()); |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 1215 | if (receiver != null) { |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1216 | receiver.decrementPendingBroadcastsLocked(); |
Dianne Hackborn | f5fdca9 | 2013-06-05 14:53:33 -0700 | [diff] [blame] | 1217 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
Lifu Tang | 82f893d | 2016-01-21 18:15:33 -0800 | [diff] [blame] | 1221 | @Override |
Lifu Tang | 9363b94 | 2016-02-16 18:07:00 -0800 | [diff] [blame] | 1222 | public int getGnssYearOfHardware() { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1223 | return mGnssManagerService == null ? 0 : mGnssManagerService.getGnssYearOfHardware(); |
Lifu Tang | 82f893d | 2016-01-21 18:15:33 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Wyatt Riley | d87cf91 | 2017-12-05 09:31:52 -0800 | [diff] [blame] | 1226 | @Override |
Wyatt Riley | 49097c0 | 2018-03-15 09:14:43 -0700 | [diff] [blame] | 1227 | @Nullable |
Wyatt Riley | d87cf91 | 2017-12-05 09:31:52 -0800 | [diff] [blame] | 1228 | public String getGnssHardwareModelName() { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1229 | return mGnssManagerService == null ? "" : mGnssManagerService.getGnssHardwareModelName(); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1232 | @Override |
| 1233 | public int getGnssBatchSize(String packageName) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1234 | return mGnssManagerService == null ? 0 : mGnssManagerService.getGnssBatchSize(packageName); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1237 | @Override |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1238 | public boolean addGnssBatchingCallback(IBatchedLocationCallback callback, String packageName, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1239 | String featureId, String listenerIdentifier) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 1240 | Objects.requireNonNull(listenerIdentifier); |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1241 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1242 | return mGnssManagerService != null && mGnssManagerService.addGnssBatchingCallback( |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1243 | callback, packageName, featureId, listenerIdentifier); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1246 | @Override |
| 1247 | public void removeGnssBatchingCallback() { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1248 | if (mGnssManagerService != null) mGnssManagerService.removeGnssBatchingCallback(); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1251 | @Override |
| 1252 | public boolean startGnssBatch(long periodNanos, boolean wakeOnFifoFull, String packageName) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1253 | return mGnssManagerService != null && mGnssManagerService.startGnssBatch(periodNanos, |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1254 | wakeOnFifoFull, packageName); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1257 | @Override |
| 1258 | public void flushGnssBatch(String packageName) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1259 | if (mGnssManagerService != null) mGnssManagerService.flushGnssBatch(packageName); |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Wyatt Riley | cf879db | 2017-01-12 13:57:38 -0800 | [diff] [blame] | 1262 | @Override |
| 1263 | public boolean stopGnssBatch() { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1264 | return mGnssManagerService != null && mGnssManagerService.stopGnssBatch(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1267 | @Nullable |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1268 | private LocationProviderManager getLocationProviderManager(String providerName) { |
| 1269 | for (LocationProviderManager manager : mProviderManagers) { |
| 1270 | if (providerName.equals(manager.getName())) { |
| 1271 | return manager; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1272 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1275 | return null; |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1278 | private String getResolutionPermission(int resolutionLevel) { |
| 1279 | switch (resolutionLevel) { |
| 1280 | case RESOLUTION_LEVEL_FINE: |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1281 | return ACCESS_FINE_LOCATION; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1282 | case RESOLUTION_LEVEL_COARSE: |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1283 | return ACCESS_COARSE_LOCATION; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1284 | default: |
| 1285 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | } |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1287 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1288 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1289 | private int getAllowedResolutionLevel(int pid, int uid) { |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1290 | if (mContext.checkPermission(ACCESS_FINE_LOCATION, pid, uid) == PERMISSION_GRANTED) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1291 | return RESOLUTION_LEVEL_FINE; |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1292 | } else if (mContext.checkPermission(ACCESS_COARSE_LOCATION, pid, uid) |
| 1293 | == PERMISSION_GRANTED) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1294 | return RESOLUTION_LEVEL_COARSE; |
| 1295 | } else { |
| 1296 | return RESOLUTION_LEVEL_NONE; |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1297 | } |
Victoria Lease | 4fab68b | 2012-09-13 13:20:59 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1300 | private int getCallerAllowedResolutionLevel() { |
| 1301 | return getAllowedResolutionLevel(Binder.getCallingPid(), Binder.getCallingUid()); |
| 1302 | } |
| 1303 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1304 | private boolean checkCallingOrSelfLocationPermission() { |
| 1305 | return mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED |
| 1306 | || mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) |
| 1307 | == PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1310 | private void enforceCallingOrSelfLocationPermission() { |
| 1311 | if (checkCallingOrSelfLocationPermission()) { |
| 1312 | return; |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1313 | } |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1314 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1315 | throw new SecurityException("uid " + Binder.getCallingUid() + " does not have " |
| 1316 | + ACCESS_COARSE_LOCATION + " or " + ACCESS_FINE_LOCATION + "."); |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1319 | private void enforceCallingOrSelfPackageName(String packageName) { |
| 1320 | int uid = Binder.getCallingUid(); |
| 1321 | if (ArrayUtils.contains(mPackageManager.getPackagesForUid(uid), packageName)) { |
| 1322 | return; |
Victoria Lease | da479c5 | 2012-10-15 15:24:16 -0700 | [diff] [blame] | 1323 | } |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1324 | |
| 1325 | throw new SecurityException("invalid package \"" + packageName + "\" for uid " + uid); |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1328 | public static int resolutionLevelToOp(int allowedResolutionLevel) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1329 | if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) { |
| 1330 | if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1331 | return AppOpsManager.OP_COARSE_LOCATION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1332 | } else { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1333 | return AppOpsManager.OP_FINE_LOCATION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1334 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1335 | } |
| 1336 | return -1; |
| 1337 | } |
| 1338 | |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 1339 | private static String resolutionLevelToOpStr(int allowedResolutionLevel) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1340 | switch (allowedResolutionLevel) { |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 1341 | case RESOLUTION_LEVEL_COARSE: |
| 1342 | return AppOpsManager.OPSTR_COARSE_LOCATION; |
| 1343 | case RESOLUTION_LEVEL_FINE: |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1344 | // fall through |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 1345 | case RESOLUTION_LEVEL_NONE: |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1346 | // fall through |
Wei Wang | b86334f | 2018-07-03 16:33:24 -0700 | [diff] [blame] | 1347 | default: |
| 1348 | // Use the most restrictive ops if not sure. |
| 1349 | return AppOpsManager.OPSTR_FINE_LOCATION; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1353 | private boolean reportLocationAccessNoThrow(int pid, int uid, String packageName, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1354 | @Nullable String featureId, int allowedResolutionLevel, @Nullable String message) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1355 | int op = resolutionLevelToOp(allowedResolutionLevel); |
| 1356 | if (op >= 0) { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1357 | if (mAppOps.noteOpNoThrow(op, uid, packageName, featureId, message) |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1358 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1359 | return false; |
| 1360 | } |
| 1361 | } |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 1362 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1363 | return getAllowedResolutionLevel(pid, uid) >= allowedResolutionLevel; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1366 | private boolean checkLocationAccess(int pid, int uid, String packageName, |
| 1367 | int allowedResolutionLevel) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1368 | int op = resolutionLevelToOp(allowedResolutionLevel); |
| 1369 | if (op >= 0) { |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 1370 | if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1371 | return false; |
| 1372 | } |
| 1373 | } |
David Christie | b870dbf | 2015-06-22 12:42:53 -0700 | [diff] [blame] | 1374 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1375 | return getAllowedResolutionLevel(pid, uid) >= allowedResolutionLevel; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1378 | /** |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 1379 | * Returns all providers by name, including passive and the ones that are not permitted to |
| 1380 | * 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] | 1381 | */ |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1382 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | public List<String> getAllProviders() { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1384 | ArrayList<String> providers = new ArrayList<>(mProviderManagers.size()); |
| 1385 | for (LocationProviderManager manager : mProviderManagers) { |
| 1386 | if (FUSED_PROVIDER.equals(manager.getName())) { |
| 1387 | continue; |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 1388 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1389 | providers.add(manager.getName()); |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 1390 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1391 | return providers; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1394 | /** |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1395 | * Return all providers by name, that match criteria and are optionally |
| 1396 | * enabled. |
| 1397 | * Can return passive provider, but never returns fused provider. |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1398 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1399 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1400 | public List<String> getProviders(Criteria criteria, boolean enabledOnly) { |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1401 | if (!checkCallingOrSelfLocationPermission()) { |
| 1402 | return Collections.emptyList(); |
| 1403 | } |
| 1404 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1405 | synchronized (mLock) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1406 | ArrayList<String> providers = new ArrayList<>(mProviderManagers.size()); |
| 1407 | for (LocationProviderManager manager : mProviderManagers) { |
| 1408 | String name = manager.getName(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1409 | if (FUSED_PROVIDER.equals(name)) { |
| 1410 | continue; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1411 | } |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 1412 | if (enabledOnly && !manager.isEnabled()) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1413 | continue; |
| 1414 | } |
| 1415 | if (criteria != null |
| 1416 | && !android.location.LocationProvider.propertiesMeetCriteria( |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1417 | name, manager.getProperties(), criteria)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1418 | continue; |
| 1419 | } |
| 1420 | providers.add(name); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1421 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1422 | return providers; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1423 | } |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /** |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1427 | * Return the name of the best provider given a Criteria object. |
| 1428 | * This method has been deprecated from the public API, |
Victoria Lease | 8dbb634 | 2012-09-21 16:55:53 -0700 | [diff] [blame] | 1429 | * and the whole LocationProvider (including #meetsCriteria) |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1430 | * has been deprecated as well. So this method now uses |
| 1431 | * some simplified logic. |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1432 | */ |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1433 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1434 | public String getBestProvider(Criteria criteria, boolean enabledOnly) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1435 | List<String> providers = getProviders(criteria, enabledOnly); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1436 | if (providers.isEmpty()) { |
| 1437 | providers = getProviders(null, enabledOnly); |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1440 | if (!providers.isEmpty()) { |
| 1441 | if (providers.contains(GPS_PROVIDER)) { |
| 1442 | return GPS_PROVIDER; |
| 1443 | } else if (providers.contains(NETWORK_PROVIDER)) { |
| 1444 | return NETWORK_PROVIDER; |
| 1445 | } else { |
| 1446 | return providers.get(0); |
| 1447 | } |
| 1448 | } |
| 1449 | |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1450 | return null; |
| 1451 | } |
| 1452 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1453 | @GuardedBy("mLock") |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 1454 | private void updateProviderEnabledLocked(LocationProviderManager manager) { |
| 1455 | boolean enabled = manager.isEnabled(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1456 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | ArrayList<Receiver> deadReceivers = null; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1458 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1459 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(manager.getName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | if (records != null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1461 | for (UpdateRecord record : records) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1462 | if (!mUserInfoHelper.isCurrentUserOrProfile( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1463 | UserHandle.getUserId(record.mReceiver.mCallerIdentity.mUid))) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1464 | continue; |
| 1465 | } |
| 1466 | |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1467 | // requests that ignore location settings will never provide notifications |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1468 | if (isSettingsExempt(record)) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1469 | continue; |
| 1470 | } |
| 1471 | |
| 1472 | // Sends a notification message to the receiver |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 1473 | if (!record.mReceiver.callProviderEnabledLocked(manager.getName(), enabled)) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1474 | if (deadReceivers == null) { |
| 1475 | deadReceivers = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | } |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1477 | deadReceivers.add(record.mReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | if (deadReceivers != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1483 | for (int i = deadReceivers.size() - 1; i >= 0; i--) { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1484 | removeUpdatesLocked(deadReceivers.get(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | } |
| 1486 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1487 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1488 | applyRequirementsLocked(manager); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1491 | @GuardedBy("mLock") |
| 1492 | private void applyRequirementsLocked(String providerName) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1493 | LocationProviderManager manager = getLocationProviderManager(providerName); |
| 1494 | if (manager != null) { |
| 1495 | applyRequirementsLocked(manager); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1496 | } |
| 1497 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1498 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1499 | @GuardedBy("mLock") |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1500 | private void applyRequirementsLocked(LocationProviderManager manager) { |
| 1501 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(manager.getName()); |
| 1502 | ProviderRequest.Builder providerRequest = new ProviderRequest.Builder(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1503 | |
Soonil Nagarkar | 04cff6c | 2019-03-19 11:29:52 -0700 | [diff] [blame] | 1504 | // if provider is not active, it should not respond to requests |
| 1505 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1506 | if (mProviderManagers.contains(manager) && records != null && !records.isEmpty()) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1507 | long backgroundThrottleInterval = mSettingsHelper.getBackgroundThrottleIntervalMs(); |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1508 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1509 | ArrayList<LocationRequest> requests = new ArrayList<>(records.size()); |
| 1510 | |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1511 | final boolean isForegroundOnlyMode = |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 1512 | mBatterySaverMode == PowerManager.LOCATION_MODE_FOREGROUND_ONLY; |
| 1513 | final boolean shouldThrottleRequests = |
| 1514 | mBatterySaverMode |
| 1515 | == PowerManager.LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF |
| 1516 | && !mPowerManager.isInteractive(); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1517 | // initialize the low power mode to true and set to false if any of the records requires |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1518 | providerRequest.setLowPowerMode(true); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1519 | for (UpdateRecord record : records) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1520 | if (!mUserInfoHelper.isCurrentUserOrProfile( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1521 | UserHandle.getUserId(record.mReceiver.mCallerIdentity.mUid))) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1522 | continue; |
| 1523 | } |
| 1524 | if (!checkLocationAccess( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1525 | record.mReceiver.mCallerIdentity.mPid, |
| 1526 | record.mReceiver.mCallerIdentity.mUid, |
| 1527 | record.mReceiver.mCallerIdentity.mPackageName, |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1528 | record.mReceiver.mAllowedResolutionLevel)) { |
| 1529 | continue; |
| 1530 | } |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 1531 | final boolean isBatterySaverDisablingLocation = shouldThrottleRequests |
| 1532 | || (isForegroundOnlyMode && !record.mIsForegroundUid); |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 1533 | if (!manager.isEnabled() || isBatterySaverDisablingLocation) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1534 | if (isSettingsExempt(record)) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1535 | providerRequest.setLocationSettingsIgnored(true); |
| 1536 | providerRequest.setLowPowerMode(false); |
Soonil Nagarkar | 35c3b91 | 2019-01-31 10:31:24 -0800 | [diff] [blame] | 1537 | } else { |
| 1538 | continue; |
| 1539 | } |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1540 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1541 | |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1542 | LocationRequest locationRequest = record.mRealRequest; |
| 1543 | long interval = locationRequest.getInterval(); |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1544 | |
Soonil Nagarkar | 509580f | 2019-02-06 15:57:26 -0800 | [diff] [blame] | 1545 | |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1546 | // if we're forcing location, don't apply any throttling |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1547 | if (!providerRequest.isLocationSettingsIgnored() && !isThrottlingExempt( |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1548 | record.mReceiver.mCallerIdentity)) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1549 | if (!record.mIsForegroundUid) { |
| 1550 | interval = Math.max(interval, backgroundThrottleInterval); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1551 | } |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1552 | if (interval != locationRequest.getInterval()) { |
| 1553 | locationRequest = new LocationRequest(locationRequest); |
| 1554 | locationRequest.setInterval(interval); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | record.mRequest = locationRequest; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1559 | requests.add(locationRequest); |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1560 | if (!locationRequest.isLowPowerMode()) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1561 | providerRequest.setLowPowerMode(false); |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1562 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1563 | if (interval < providerRequest.getInterval()) { |
| 1564 | providerRequest.setInterval(interval); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1565 | } |
| 1566 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1567 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1568 | providerRequest.setLocationRequests(requests); |
| 1569 | |
| 1570 | if (providerRequest.getInterval() < Long.MAX_VALUE) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1571 | // calculate who to blame for power |
| 1572 | // This is somewhat arbitrary. We pick a threshold interval |
| 1573 | // that is slightly higher that the minimum interval, and |
| 1574 | // spread the blame across all applications with a request |
| 1575 | // under that threshold. |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1576 | // TODO: overflow |
| 1577 | long thresholdInterval = (providerRequest.getInterval() + 1000) * 3 / 2; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1578 | for (UpdateRecord record : records) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1579 | if (mUserInfoHelper.isCurrentUserOrProfile( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1580 | UserHandle.getUserId(record.mReceiver.mCallerIdentity.mUid))) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1581 | LocationRequest locationRequest = record.mRequest; |
Svet Ganov | e998c73 | 2016-06-10 00:12:38 -0700 | [diff] [blame] | 1582 | |
| 1583 | // Don't assign battery blame for update records whose |
| 1584 | // client has no permission to receive location data. |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1585 | if (!providerRequest.getLocationRequests().contains(locationRequest)) { |
Svet Ganov | e998c73 | 2016-06-10 00:12:38 -0700 | [diff] [blame] | 1586 | continue; |
| 1587 | } |
| 1588 | |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1589 | if (locationRequest.getInterval() <= thresholdInterval) { |
David Christie | e55c968 | 2013-08-22 10:10:34 -0700 | [diff] [blame] | 1590 | if (record.mReceiver.mWorkSource != null |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1591 | && isValidWorkSource(record.mReceiver.mWorkSource)) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1592 | providerRequest.getWorkSource().add(record.mReceiver.mWorkSource); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1593 | } else { |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1594 | // Assign blame to caller if there's no WorkSource associated with |
| 1595 | // the request or if it's invalid. |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1596 | providerRequest.getWorkSource().add( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1597 | record.mReceiver.mCallerIdentity.mUid, |
| 1598 | record.mReceiver.mCallerIdentity.mPackageName); |
David Christie | 82edc9b | 2013-07-19 11:31:42 -0700 | [diff] [blame] | 1599 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 1600 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1601 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1602 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | } |
| 1604 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1605 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1606 | manager.setRequest(providerRequest.build()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1609 | /** |
| 1610 | * Whether a given {@code WorkSource} associated with a Location request is valid. |
| 1611 | */ |
| 1612 | private static boolean isValidWorkSource(WorkSource workSource) { |
| 1613 | if (workSource.size() > 0) { |
| 1614 | // If the WorkSource has one or more non-chained UIDs, make sure they're accompanied |
| 1615 | // by tags. |
Suprabh Shukla | f7cffa7 | 2019-11-08 17:03:03 -0800 | [diff] [blame] | 1616 | return workSource.getPackageName(0) != null; |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1617 | } else { |
| 1618 | // For now, make sure callers have supplied an attribution tag for use with |
| 1619 | // AppOpsManager. This might be relaxed in the future. |
Suprabh Shukla | f7cffa7 | 2019-11-08 17:03:03 -0800 | [diff] [blame] | 1620 | final List<WorkChain> workChains = workSource.getWorkChains(); |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 1621 | return workChains != null && !workChains.isEmpty() && |
| 1622 | workChains.get(0).getAttributionTag() != null; |
| 1623 | } |
| 1624 | } |
| 1625 | |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1626 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1627 | public String[] getBackgroundThrottlingWhitelist() { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1628 | return mSettingsHelper.getBackgroundThrottlePackageWhitelist().toArray(new String[0]); |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Soonil Nagarkar | 5140e4f | 2019-02-14 17:21:33 -0800 | [diff] [blame] | 1631 | @Override |
| 1632 | public String[] getIgnoreSettingsWhitelist() { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1633 | return mSettingsHelper.getIgnoreSettingsPackageWhitelist().toArray(new String[0]); |
Soonil Nagarkar | 5140e4f | 2019-02-14 17:21:33 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1636 | private boolean isThrottlingExempt(CallerIdentity callerIdentity) { |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1637 | if (callerIdentity.mUid == Process.SYSTEM_UID) { |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1641 | if (mSettingsHelper.getBackgroundThrottlePackageWhitelist().contains( |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 1642 | callerIdentity.mPackageName)) { |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 1643 | return true; |
| 1644 | } |
| 1645 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1646 | return mLocalService.isProviderPackage(callerIdentity.mPackageName); |
Soonil Nagarkar | 2f1f7e8 | 2017-01-24 12:52:10 -0800 | [diff] [blame] | 1647 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1648 | } |
| 1649 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1650 | private boolean isSettingsExempt(UpdateRecord record) { |
Soonil Nagarkar | 397ad58 | 2019-01-23 22:47:57 -0800 | [diff] [blame] | 1651 | if (!record.mRealRequest.isLocationSettingsIgnored()) { |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1655 | if (mSettingsHelper.getIgnoreSettingsPackageWhitelist().contains( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1656 | record.mReceiver.mCallerIdentity.mPackageName)) { |
Soonil Nagarkar | 397ad58 | 2019-01-23 22:47:57 -0800 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1660 | return mLocalService.isProviderPackage(record.mReceiver.mCallerIdentity.mPackageName); |
Soonil Nagarkar | 397ad58 | 2019-01-23 22:47:57 -0800 | [diff] [blame] | 1661 | |
Soonil Nagarkar | 397ad58 | 2019-01-23 22:47:57 -0800 | [diff] [blame] | 1662 | } |
| 1663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | private class UpdateRecord { |
| 1665 | final String mProvider; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1666 | private final LocationRequest mRealRequest; // original request from client |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 1667 | LocationRequest mRequest; // possibly throttled version of the request |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1668 | private final Receiver mReceiver; |
| 1669 | private boolean mIsForegroundUid; |
| 1670 | private Location mLastFixBroadcast; |
Soonil Nagarkar | ae6ce77 | 2019-05-01 13:16:17 -0700 | [diff] [blame] | 1671 | private Throwable mStackTrace; // for debugging only |
Soonil Nagarkar | 95768ce | 2019-11-05 15:22:44 -0800 | [diff] [blame] | 1672 | private long mExpirationRealtimeMs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | |
| 1674 | /** |
| 1675 | * Note: must be constructed with lock held. |
| 1676 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1677 | private UpdateRecord(String provider, LocationRequest request, Receiver receiver) { |
Soonil Nagarkar | 95768ce | 2019-11-05 15:22:44 -0800 | [diff] [blame] | 1678 | mExpirationRealtimeMs = request.getExpirationRealtimeMs(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1679 | mProvider = provider; |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 1680 | mRealRequest = request; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1681 | mRequest = request; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | mReceiver = receiver; |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1683 | mIsForegroundUid = mAppForegroundHelper.isAppForeground(mReceiver.mCallerIdentity.mUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | |
Soonil Nagarkar | ae6ce77 | 2019-05-01 13:16:17 -0700 | [diff] [blame] | 1685 | if (D && receiver.mCallerIdentity.mPid == Process.myPid()) { |
| 1686 | mStackTrace = new Throwable(); |
| 1687 | } |
| 1688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider); |
| 1690 | if (records == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1691 | records = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | mRecordsByProvider.put(provider, records); |
| 1693 | } |
| 1694 | if (!records.contains(this)) { |
| 1695 | records.add(this); |
| 1696 | } |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 1697 | |
| 1698 | // Update statistics for historical location requests by package/provider |
| 1699 | mRequestStatistics.startRequesting( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1700 | mReceiver.mCallerIdentity.mPackageName, provider, request.getInterval(), |
Wyatt Riley | f7075e0 | 2018-04-12 17:54:26 -0700 | [diff] [blame] | 1701 | mIsForegroundUid); |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * Method to be called when record changes foreground/background |
| 1706 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1707 | private void updateForeground(boolean isForeground) { |
Wyatt Riley | f7075e0 | 2018-04-12 17:54:26 -0700 | [diff] [blame] | 1708 | mIsForegroundUid = isForeground; |
| 1709 | mRequestStatistics.updateForeground( |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 1710 | mReceiver.mCallerIdentity.mPackageName, mProvider, isForeground); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /** |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 1714 | * 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] | 1715 | */ |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1716 | private void disposeLocked(boolean removeReceiver) { |
Hongyi Zhang | 700137e | 2019-05-23 21:19:36 -0700 | [diff] [blame] | 1717 | String packageName = mReceiver.mCallerIdentity.mPackageName; |
| 1718 | mRequestStatistics.stopRequesting(packageName, mProvider); |
| 1719 | |
| 1720 | mLocationUsageLogger.logLocationApiUsage( |
| 1721 | LocationStatsEnums.USAGE_ENDED, |
| 1722 | LocationStatsEnums.API_REQUEST_LOCATION_UPDATES, |
| 1723 | packageName, |
| 1724 | mRealRequest, |
| 1725 | mReceiver.isListener(), |
| 1726 | mReceiver.isPendingIntent(), |
Hongyi Zhang | 0e9ea75 | 2019-06-09 22:49:25 -0700 | [diff] [blame] | 1727 | /* geofence= */ null, |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1728 | mAppForegroundHelper.getImportance(mReceiver.mCallerIdentity.mUid)); |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 1729 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1730 | // remove from mRecordsByProvider |
| 1731 | ArrayList<UpdateRecord> globalRecords = mRecordsByProvider.get(this.mProvider); |
| 1732 | if (globalRecords != null) { |
| 1733 | globalRecords.remove(this); |
| 1734 | } |
| 1735 | |
| 1736 | if (!removeReceiver) return; // the caller will handle the rest |
| 1737 | |
| 1738 | // remove from Receiver#mUpdateRecords |
| 1739 | HashMap<String, UpdateRecord> receiverRecords = mReceiver.mUpdateRecords; |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1740 | receiverRecords.remove(this.mProvider); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1741 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1742 | // and also remove the Receiver if it has no more update records |
| 1743 | if (receiverRecords.size() == 0) { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1744 | removeUpdatesLocked(mReceiver); |
Mike Lockwood | 3a76fd6 | 2009-09-01 07:26:56 -0400 | [diff] [blame] | 1745 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | @Override |
| 1749 | public String toString() { |
Soonil Nagarkar | ae6ce77 | 2019-05-01 13:16:17 -0700 | [diff] [blame] | 1750 | StringBuilder b = new StringBuilder("UpdateRecord["); |
| 1751 | b.append(mProvider).append(" "); |
| 1752 | b.append(mReceiver.mCallerIdentity.mPackageName); |
| 1753 | b.append("(").append(mReceiver.mCallerIdentity.mUid); |
| 1754 | if (mIsForegroundUid) { |
| 1755 | b.append(" foreground"); |
| 1756 | } else { |
| 1757 | b.append(" background"); |
| 1758 | } |
| 1759 | b.append(") "); |
| 1760 | b.append(mRealRequest).append(" ").append(mReceiver.mWorkSource); |
| 1761 | |
| 1762 | if (mStackTrace != null) { |
| 1763 | ByteArrayOutputStream tmp = new ByteArrayOutputStream(); |
| 1764 | mStackTrace.printStackTrace(new PrintStream(tmp)); |
| 1765 | b.append("\n\n").append(tmp.toString()).append("\n"); |
| 1766 | } |
| 1767 | |
| 1768 | b.append("]"); |
| 1769 | return b.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1773 | @GuardedBy("mLock") |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1774 | private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1775 | String packageName, @Nullable String featureId, WorkSource workSource, |
| 1776 | boolean hideFromAppOps, @NonNull String listenerIdentifier) { |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1777 | IBinder binder = listener.asBinder(); |
| 1778 | Receiver receiver = mReceivers.get(binder); |
| 1779 | if (receiver == null) { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1780 | receiver = new Receiver(listener, null, pid, uid, packageName, featureId, workSource, |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1781 | hideFromAppOps, listenerIdentifier); |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1782 | if (!receiver.linkToListenerDeathNotificationLocked( |
| 1783 | receiver.getListener().asBinder())) { |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1784 | return null; |
| 1785 | } |
Wen Jing | cb3ab22 | 2014-03-27 13:42:59 +0800 | [diff] [blame] | 1786 | mReceivers.put(binder, receiver); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1787 | } |
| 1788 | return receiver; |
| 1789 | } |
| 1790 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1791 | @GuardedBy("mLock") |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1792 | private Receiver getReceiverLocked(PendingIntent intent, int pid, int uid, String packageName, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1793 | @Nullable String featureId, WorkSource workSource, boolean hideFromAppOps, |
| 1794 | @NonNull String listenerIdentifier) { |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1795 | Receiver receiver = mReceivers.get(intent); |
| 1796 | if (receiver == null) { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1797 | receiver = new Receiver(null, intent, pid, uid, packageName, featureId, workSource, |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1798 | hideFromAppOps, listenerIdentifier); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1799 | mReceivers.put(intent, receiver); |
| 1800 | } |
| 1801 | return receiver; |
| 1802 | } |
| 1803 | |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1804 | /** |
| 1805 | * Creates a LocationRequest based upon the supplied LocationRequest that to meets resolution |
| 1806 | * and consistency requirements. |
| 1807 | * |
| 1808 | * @param request the LocationRequest from which to create a sanitized version |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1809 | * @return a version of request that meets the given resolution and consistency requirements |
| 1810 | * @hide |
| 1811 | */ |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1812 | private LocationRequest createSanitizedRequest(LocationRequest request, int resolutionLevel, |
| 1813 | boolean callerHasLocationHardwarePermission) { |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1814 | LocationRequest sanitizedRequest = new LocationRequest(request); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1815 | if (!callerHasLocationHardwarePermission) { |
| 1816 | // allow setting low power mode only for callers with location hardware permission |
| 1817 | sanitizedRequest.setLowPowerMode(false); |
| 1818 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1819 | if (resolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 1820 | switch (sanitizedRequest.getQuality()) { |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 1821 | case LocationRequest.ACCURACY_FINE: |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1822 | sanitizedRequest.setQuality(LocationRequest.ACCURACY_BLOCK); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 1823 | break; |
| 1824 | case LocationRequest.POWER_HIGH: |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1825 | sanitizedRequest.setQuality(LocationRequest.POWER_LOW); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 1826 | break; |
| 1827 | } |
| 1828 | // throttle |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1829 | if (sanitizedRequest.getInterval() < LocationFudger.FASTEST_INTERVAL_MS) { |
| 1830 | sanitizedRequest.setInterval(LocationFudger.FASTEST_INTERVAL_MS); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 1831 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1832 | if (sanitizedRequest.getFastestInterval() < LocationFudger.FASTEST_INTERVAL_MS) { |
| 1833 | sanitizedRequest.setFastestInterval(LocationFudger.FASTEST_INTERVAL_MS); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 1834 | } |
Nick Pelly | 74fa7ea | 2012-08-13 19:36:38 -0700 | [diff] [blame] | 1835 | } |
Nick Pelly | 4e31c4f | 2012-08-13 19:35:39 -0700 | [diff] [blame] | 1836 | // make getFastestInterval() the minimum of interval and fastest interval |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1837 | if (sanitizedRequest.getFastestInterval() > sanitizedRequest.getInterval()) { |
Soonil Nagarkar | 397ad58 | 2019-01-23 22:47:57 -0800 | [diff] [blame] | 1838 | sanitizedRequest.setFastestInterval(request.getInterval()); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1839 | } |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1840 | return sanitizedRequest; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 1841 | } |
| 1842 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1843 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1844 | public void requestLocationUpdates(LocationRequest request, ILocationListener listener, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1845 | PendingIntent intent, String packageName, String featureId, |
| 1846 | String listenerIdentifier) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 1847 | Objects.requireNonNull(listenerIdentifier); |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1848 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1849 | enforceCallingOrSelfLocationPermission(); |
| 1850 | enforceCallingOrSelfPackageName(packageName); |
| 1851 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1852 | synchronized (mLock) { |
| 1853 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1854 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1855 | WorkSource workSource = request.getWorkSource(); |
| 1856 | if (workSource != null && !workSource.isEmpty()) { |
| 1857 | mContext.enforceCallingOrSelfPermission( |
| 1858 | Manifest.permission.UPDATE_DEVICE_STATS, null); |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 1859 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1860 | boolean hideFromAppOps = request.getHideFromAppOps(); |
| 1861 | if (hideFromAppOps) { |
| 1862 | mContext.enforceCallingOrSelfPermission( |
| 1863 | Manifest.permission.UPDATE_APP_OPS_STATS, null); |
| 1864 | } |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1865 | if (request.isLocationSettingsIgnored()) { |
| 1866 | mContext.enforceCallingOrSelfPermission( |
| 1867 | Manifest.permission.WRITE_SECURE_SETTINGS, null); |
| 1868 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1869 | boolean callerHasLocationHardwarePermission = |
| 1870 | mContext.checkCallingPermission(android.Manifest.permission.LOCATION_HARDWARE) |
| 1871 | == PERMISSION_GRANTED; |
| 1872 | LocationRequest sanitizedRequest = createSanitizedRequest(request, |
| 1873 | allowedResolutionLevel, |
| 1874 | callerHasLocationHardwarePermission); |
| 1875 | |
| 1876 | final int pid = Binder.getCallingPid(); |
| 1877 | final int uid = Binder.getCallingUid(); |
| 1878 | |
| 1879 | long identity = Binder.clearCallingIdentity(); |
| 1880 | try { |
| 1881 | |
| 1882 | // We don't check for MODE_IGNORED here; we will do that when we go to deliver |
| 1883 | // a location. |
| 1884 | checkLocationAccess(pid, uid, packageName, allowedResolutionLevel); |
| 1885 | |
| 1886 | if (intent == null && listener == null) { |
| 1887 | throw new IllegalArgumentException("need either listener or intent"); |
| 1888 | } else if (intent != null && listener != null) { |
| 1889 | throw new IllegalArgumentException( |
| 1890 | "cannot register both listener and intent"); |
| 1891 | } |
| 1892 | |
Hongyi Zhang | 700137e | 2019-05-23 21:19:36 -0700 | [diff] [blame] | 1893 | mLocationUsageLogger.logLocationApiUsage( |
| 1894 | LocationStatsEnums.USAGE_STARTED, |
| 1895 | LocationStatsEnums.API_REQUEST_LOCATION_UPDATES, |
| 1896 | packageName, request, listener != null, intent != null, |
Hongyi Zhang | 0e9ea75 | 2019-06-09 22:49:25 -0700 | [diff] [blame] | 1897 | /* geofence= */ null, |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1898 | mAppForegroundHelper.getImportance(uid)); |
Hongyi Zhang | 700137e | 2019-05-23 21:19:36 -0700 | [diff] [blame] | 1899 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1900 | Receiver receiver; |
| 1901 | if (intent != null) { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1902 | receiver = getReceiverLocked(intent, pid, uid, packageName, featureId, |
| 1903 | workSource, hideFromAppOps, listenerIdentifier); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1904 | } else { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1905 | receiver = getReceiverLocked(listener, pid, uid, packageName, featureId, |
| 1906 | workSource, hideFromAppOps, listenerIdentifier); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1907 | } |
| 1908 | requestLocationUpdatesLocked(sanitizedRequest, receiver, uid, packageName); |
| 1909 | } finally { |
| 1910 | Binder.restoreCallingIdentity(identity); |
| 1911 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1912 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1913 | } |
| 1914 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1915 | @GuardedBy("mLock") |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1916 | private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver, |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 1917 | int uid, String packageName) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1918 | // Figure out the provider. Either its explicitly request (legacy use cases), or |
| 1919 | // use the fused provider |
| 1920 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
| 1921 | String name = request.getProvider(); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 1922 | if (name == null) { |
| 1923 | throw new IllegalArgumentException("provider name must not be null"); |
| 1924 | } |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 1925 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 1926 | LocationProviderManager manager = getLocationProviderManager(name); |
| 1927 | if (manager == null) { |
Victoria Lease | b30f383 | 2013-10-13 12:15:40 -0700 | [diff] [blame] | 1928 | throw new IllegalArgumentException("provider doesn't exist: " + name); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1931 | UpdateRecord record = new UpdateRecord(name, request, receiver); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1932 | if (D) { |
| 1933 | Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver)) |
| 1934 | + " " + name + " " + request + " from " + packageName + "(" + uid + " " |
| 1935 | + (record.mIsForegroundUid ? "foreground" : "background") |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1936 | + (isThrottlingExempt(receiver.mCallerIdentity) |
Soonil Nagarkar | 681d711 | 2017-02-23 17:14:16 -0800 | [diff] [blame] | 1937 | ? " [whitelisted]" : "") + ")"); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 1938 | } |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 1939 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1940 | UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record); |
| 1941 | if (oldRecord != null) { |
| 1942 | oldRecord.disposeLocked(false); |
| 1943 | } |
| 1944 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1945 | if (!manager.isEnabled() && !isSettingsExempt(record)) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1946 | // Notify the listener that updates are currently disabled - but only if the request |
| 1947 | // does not ignore location settings |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1948 | receiver.callProviderEnabledLocked(name, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1949 | } |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 1950 | |
| 1951 | applyRequirementsLocked(name); |
| 1952 | |
David Christie | 0b83745 | 2013-07-29 16:02:13 -0700 | [diff] [blame] | 1953 | // Update the monitoring here just in case multiple location requests were added to the |
| 1954 | // same receiver (this request may be high power and the initial might not have been). |
| 1955 | receiver.updateMonitoring(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 1958 | @Override |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1959 | public void removeUpdates(ILocationListener listener, PendingIntent intent, |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1960 | String packageName) { |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 1961 | enforceCallingOrSelfPackageName(packageName); |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 1962 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1963 | int pid = Binder.getCallingPid(); |
| 1964 | int uid = Binder.getCallingUid(); |
| 1965 | |
| 1966 | if (intent == null && listener == null) { |
| 1967 | throw new IllegalArgumentException("need either listener or intent"); |
| 1968 | } else if (intent != null && listener != null) { |
| 1969 | throw new IllegalArgumentException("cannot register both listener and intent"); |
| 1970 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1971 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1972 | synchronized (mLock) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1973 | Receiver receiver; |
| 1974 | if (intent != null) { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1975 | receiver = getReceiverLocked(intent, pid, uid, packageName, null, null, false, ""); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1976 | } else { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 1977 | receiver = getReceiverLocked(listener, pid, uid, packageName, null, null, false, |
| 1978 | ""); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1979 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 1980 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1981 | long identity = Binder.clearCallingIdentity(); |
| 1982 | try { |
| 1983 | removeUpdatesLocked(receiver); |
| 1984 | } finally { |
| 1985 | Binder.restoreCallingIdentity(identity); |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 1986 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 1990 | @GuardedBy("mLock") |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 1991 | private void removeUpdatesLocked(Receiver receiver) { |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 1992 | if (D) Log.i(TAG, "remove " + Integer.toHexString(System.identityHashCode(receiver))); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1993 | |
| 1994 | if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1995 | receiver.unlinkFromListenerDeathNotificationLocked( |
| 1996 | receiver.getListener().asBinder()); |
Soonil Nagarkar | b46c167 | 2019-02-06 12:49:10 -0800 | [diff] [blame] | 1997 | receiver.clearPendingBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1998 | } |
| 1999 | |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 2000 | receiver.updateMonitoring(false); |
| 2001 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2002 | // Record which providers were associated with this listener |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2003 | HashSet<String> providers = new HashSet<>(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2004 | HashMap<String, UpdateRecord> oldRecords = receiver.mUpdateRecords; |
| 2005 | if (oldRecords != null) { |
| 2006 | // Call dispose() on the obsolete update records. |
| 2007 | for (UpdateRecord record : oldRecords.values()) { |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2008 | // Update statistics for historical location requests by package/provider |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2009 | record.disposeLocked(false); |
| 2010 | } |
| 2011 | // Accumulate providers |
| 2012 | providers.addAll(oldRecords.keySet()); |
| 2013 | } |
| 2014 | |
| 2015 | // update provider |
| 2016 | for (String provider : providers) { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2017 | applyRequirementsLocked(provider); |
| 2018 | } |
| 2019 | } |
| 2020 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2021 | @Override |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2022 | public Location getLastLocation(LocationRequest r, String packageName, String featureId) { |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2023 | enforceCallingOrSelfLocationPermission(); |
| 2024 | enforceCallingOrSelfPackageName(packageName); |
| 2025 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2026 | synchronized (mLock) { |
| 2027 | LocationRequest request = r != null ? r : DEFAULT_LOCATION_REQUEST; |
| 2028 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2029 | // 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] | 2030 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2031 | final int pid = Binder.getCallingPid(); |
| 2032 | final int uid = Binder.getCallingUid(); |
| 2033 | final long identity = Binder.clearCallingIdentity(); |
| 2034 | try { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2035 | if (mSettingsHelper.isLocationPackageBlacklisted(UserHandle.getUserId(uid), |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 2036 | packageName)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2037 | if (D) { |
| 2038 | Log.d(TAG, "not returning last loc for blacklisted app: " |
| 2039 | + packageName); |
| 2040 | } |
| 2041 | return null; |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2042 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2043 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2044 | // Figure out the provider. Either its explicitly request (deprecated API's), |
| 2045 | // or use the fused provider |
| 2046 | String name = request.getProvider(); |
| 2047 | if (name == null) name = LocationManager.FUSED_PROVIDER; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2048 | LocationProviderManager manager = getLocationProviderManager(name); |
| 2049 | if (manager == null) return null; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2050 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2051 | // only the current user or location providers may get location this way |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2052 | if (!mUserInfoHelper.isCurrentUserOrProfile(UserHandle.getUserId(uid)) |
| 2053 | && !mLocalService.isProviderPackage(packageName)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2054 | return null; |
| 2055 | } |
| 2056 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2057 | if (!manager.isEnabled()) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2058 | return null; |
| 2059 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2060 | |
| 2061 | Location location; |
| 2062 | if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 2063 | // Make sure that an app with coarse permissions can't get frequent location |
| 2064 | // updates by calling LocationManager.getLastKnownLocation repeatedly. |
| 2065 | location = mLastLocationCoarseInterval.get(name); |
| 2066 | } else { |
| 2067 | location = mLastLocation.get(name); |
| 2068 | } |
| 2069 | if (location == null) { |
| 2070 | return null; |
| 2071 | } |
| 2072 | |
| 2073 | // Don't return stale location to apps with foreground-only location permission. |
| 2074 | String op = resolutionLevelToOpStr(allowedResolutionLevel); |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2075 | long locationAgeMs = TimeUnit.NANOSECONDS.toMillis( |
| 2076 | SystemClock.elapsedRealtime() - location.getElapsedRealtimeNanos()); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2077 | if (locationAgeMs > mSettingsHelper.getMaxLastLocationAgeMs() |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2078 | && (mAppOps.unsafeCheckOp(op, uid, packageName) |
| 2079 | == AppOpsManager.MODE_FOREGROUND)) { |
| 2080 | return null; |
| 2081 | } |
| 2082 | |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 2083 | Location lastLocation = null; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2084 | if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 2085 | Location noGPSLocation = location.getExtraLocation( |
| 2086 | Location.EXTRA_NO_GPS_LOCATION); |
| 2087 | if (noGPSLocation != null) { |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 2088 | lastLocation = new Location(mLocationFudger.getOrCreate(noGPSLocation)); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2089 | } |
| 2090 | } else { |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 2091 | lastLocation = new Location(location); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2092 | } |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 2093 | // Don't report location access if there is no last location to deliver. |
| 2094 | if (lastLocation != null) { |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2095 | if (!reportLocationAccessNoThrow(pid, uid, packageName, featureId, |
| 2096 | allowedResolutionLevel, null)) { |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 2097 | if (D) { |
| 2098 | Log.d(TAG, "not returning last loc for no op app: " + packageName); |
| 2099 | } |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2100 | lastLocation = null; |
Wei Wang | 16276a4 | 2019-02-04 18:23:31 -0800 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | return lastLocation; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2104 | } finally { |
| 2105 | Binder.restoreCallingIdentity(identity); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2106 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2110 | @Override |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2111 | public boolean getCurrentLocation(LocationRequest locationRequest, |
| 2112 | ICancellationSignal remoteCancellationSignal, ILocationListener listener, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2113 | String packageName, String featureId, String listenerIdentifier) { |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2114 | // side effect of validating locationRequest and packageName |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2115 | Location lastLocation = getLastLocation(locationRequest, packageName, featureId); |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2116 | if (lastLocation != null) { |
| 2117 | long locationAgeMs = TimeUnit.NANOSECONDS.toMillis( |
| 2118 | SystemClock.elapsedRealtimeNanos() - lastLocation.getElapsedRealtimeNanos()); |
| 2119 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2120 | if (locationAgeMs < MAX_CURRENT_LOCATION_AGE_MS) { |
| 2121 | try { |
| 2122 | listener.onLocationChanged(lastLocation); |
| 2123 | return true; |
| 2124 | } catch (RemoteException e) { |
| 2125 | Log.w(TAG, e); |
| 2126 | return false; |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2127 | } |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2128 | } |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2129 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2130 | if (!mAppForegroundHelper.isAppForeground(Binder.getCallingUid())) { |
| 2131 | if (locationAgeMs < mSettingsHelper.getBackgroundThrottleIntervalMs()) { |
| 2132 | // not allowed to request new locations, so we can't return anything |
| 2133 | return false; |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2134 | } |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2138 | requestLocationUpdates(locationRequest, listener, null, packageName, featureId, |
| 2139 | listenerIdentifier); |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2140 | CancellationSignal cancellationSignal = CancellationSignal.fromTransport( |
| 2141 | remoteCancellationSignal); |
| 2142 | if (cancellationSignal != null) { |
| 2143 | cancellationSignal.setOnCancelListener( |
| 2144 | () -> removeUpdates(listener, null, packageName)); |
| 2145 | } |
| 2146 | return true; |
| 2147 | } |
| 2148 | |
| 2149 | @Override |
Chad Brubaker | f113333 | 2019-03-15 14:13:59 -0700 | [diff] [blame] | 2150 | public LocationTime getGnssTimeMillis() { |
| 2151 | synchronized (mLock) { |
| 2152 | Location location = mLastLocation.get(LocationManager.GPS_PROVIDER); |
| 2153 | if (location == null) { |
| 2154 | return null; |
| 2155 | } |
| 2156 | long currentNanos = SystemClock.elapsedRealtimeNanos(); |
| 2157 | long deltaMs = (currentNanos - location.getElapsedRealtimeNanos()) / 1000000L; |
| 2158 | return new LocationTime(location.getTime() + deltaMs, currentNanos); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2163 | public boolean injectLocation(Location location) { |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2164 | mContext.enforceCallingPermission(android.Manifest.permission.LOCATION_HARDWARE, |
| 2165 | "Location Hardware permission not granted to inject location"); |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2166 | mContext.enforceCallingPermission(ACCESS_FINE_LOCATION, |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2167 | "Access Fine Location permission not granted to inject Location"); |
| 2168 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2169 | synchronized (mLock) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2170 | LocationProviderManager manager = getLocationProviderManager(location.getProvider()); |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2171 | if (manager == null || !manager.isEnabled()) { |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2172 | return false; |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2173 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2174 | |
| 2175 | // NOTE: If last location is already available, location is not injected. If |
| 2176 | // provider's normal source (like a GPS chipset) have already provided an output |
| 2177 | // there is no need to inject this location. |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2178 | if (mLastLocation.get(manager.getName()) != null) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2179 | return false; |
| 2180 | } |
| 2181 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2182 | updateLastLocationLocked(location, manager.getName()); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2183 | return true; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2184 | } |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2185 | } |
| 2186 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2187 | @Override |
| 2188 | public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2189 | String packageName, String featureId, String listenerIdentifier) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2190 | Objects.requireNonNull(listenerIdentifier); |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2191 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2192 | mContext.enforceCallingOrSelfPermission(ACCESS_FINE_LOCATION, null); |
| 2193 | enforceCallingOrSelfPackageName(packageName); |
| 2194 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2195 | if (request == null) request = DEFAULT_LOCATION_REQUEST; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2196 | int allowedResolutionLevel = getCallerAllowedResolutionLevel(); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2197 | if (intent == null) { |
| 2198 | throw new IllegalArgumentException("invalid pending intent: " + null); |
Victoria Lease | 56e675b | 2012-11-05 19:25:06 -0800 | [diff] [blame] | 2199 | } |
Soonil Nagarkar | 8c4655e | 2019-02-27 13:00:56 -0800 | [diff] [blame] | 2200 | // Require that caller can manage given document |
| 2201 | boolean callerHasLocationHardwarePermission = |
| 2202 | mContext.checkCallingPermission(android.Manifest.permission.LOCATION_HARDWARE) |
| 2203 | == PERMISSION_GRANTED; |
| 2204 | LocationRequest sanitizedRequest = createSanitizedRequest(request, |
| 2205 | allowedResolutionLevel, |
| 2206 | callerHasLocationHardwarePermission); |
| 2207 | |
| 2208 | if (D) { |
| 2209 | Log.d(TAG, "requestGeofence: " + sanitizedRequest + " " + geofence + " " + intent); |
| 2210 | } |
| 2211 | |
| 2212 | // geo-fence manager uses the public location API, need to clear identity |
| 2213 | int uid = Binder.getCallingUid(); |
| 2214 | if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) { |
| 2215 | // temporary measure until geofences work for secondary users |
| 2216 | Log.w(TAG, "proximity alerts are currently available only to the primary user"); |
| 2217 | return; |
| 2218 | } |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2219 | |
| 2220 | mLocationUsageLogger.logLocationApiUsage( |
| 2221 | LocationStatsEnums.USAGE_STARTED, |
| 2222 | LocationStatsEnums.API_REQUEST_GEOFENCE, |
| 2223 | packageName, |
| 2224 | request, |
| 2225 | /* hasListener= */ false, |
| 2226 | true, |
| 2227 | geofence, |
| 2228 | mAppForegroundHelper.getImportance(uid)); |
| 2229 | |
Soonil Nagarkar | 8c4655e | 2019-02-27 13:00:56 -0800 | [diff] [blame] | 2230 | long identity = Binder.clearCallingIdentity(); |
| 2231 | try { |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2232 | mGeofenceManager.addFence(sanitizedRequest, geofence, intent, allowedResolutionLevel, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2233 | uid, packageName, featureId, listenerIdentifier); |
Soonil Nagarkar | 8c4655e | 2019-02-27 13:00:56 -0800 | [diff] [blame] | 2234 | } finally { |
| 2235 | Binder.restoreCallingIdentity(identity); |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2236 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | @Override |
| 2240 | public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2241 | if (intent == null) { |
| 2242 | throw new IllegalArgumentException("invalid pending intent: " + null); |
| 2243 | } |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2244 | enforceCallingOrSelfPackageName(packageName); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2245 | |
| 2246 | if (D) Log.d(TAG, "removeGeofence: " + geofence + " " + intent); |
| 2247 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2248 | mLocationUsageLogger.logLocationApiUsage( |
| 2249 | LocationStatsEnums.USAGE_ENDED, |
| 2250 | LocationStatsEnums.API_REQUEST_GEOFENCE, |
| 2251 | packageName, |
| 2252 | /* LocationRequest= */ null, |
| 2253 | /* hasListener= */ false, |
| 2254 | true, |
| 2255 | geofence, |
| 2256 | mAppForegroundHelper.getImportance(Binder.getCallingUid())); |
| 2257 | |
Nick Pelly | 2b7a0d0 | 2012-08-17 15:09:44 -0700 | [diff] [blame] | 2258 | // geo-fence manager uses the public location API, need to clear identity |
| 2259 | long identity = Binder.clearCallingIdentity(); |
| 2260 | try { |
| 2261 | mGeofenceManager.removeFence(geofence, intent); |
| 2262 | } finally { |
| 2263 | Binder.restoreCallingIdentity(identity); |
| 2264 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2267 | @Override |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2268 | public boolean registerGnssStatusCallback(IGnssStatusListener listener, String packageName, |
| 2269 | String featureId) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2270 | return mGnssManagerService != null && mGnssManagerService.registerGnssStatusCallback( |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2271 | listener, packageName, featureId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2274 | @Override |
Anil Admal | 98d49b7 | 2019-02-06 15:26:33 -0800 | [diff] [blame] | 2275 | public void unregisterGnssStatusCallback(IGnssStatusListener listener) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2276 | if (mGnssManagerService != null) mGnssManagerService.unregisterGnssStatusCallback(listener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2279 | @Override |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2280 | public boolean addGnssMeasurementsListener(IGnssMeasurementsListener listener, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2281 | String packageName, String featureId, String listenerIdentifier) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2282 | Objects.requireNonNull(listenerIdentifier); |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2283 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2284 | return mGnssManagerService != null && mGnssManagerService.addGnssMeasurementsListener( |
| 2285 | listener, packageName, featureId, listenerIdentifier); |
Anil Admal | 52686f8 | 2019-02-13 19:04:10 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | @Override |
| 2289 | public void removeGnssMeasurementsListener(IGnssMeasurementsListener listener) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2290 | if (mGnssManagerService != null) { |
| 2291 | mGnssManagerService.removeGnssMeasurementsListener( |
| 2292 | listener); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2293 | } |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | @Override |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 2297 | public void injectGnssMeasurementCorrections( |
| 2298 | GnssMeasurementCorrections measurementCorrections, String packageName) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2299 | if (mGnssManagerService != null) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2300 | mGnssManagerService.injectGnssMeasurementCorrections(measurementCorrections, |
| 2301 | packageName); |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | @Override |
Anil Admal | 9934978 | 2019-03-19 18:58:42 -0700 | [diff] [blame] | 2306 | public long getGnssCapabilities(String packageName) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2307 | return mGnssManagerService == null ? 0L : mGnssManagerService.getGnssCapabilities( |
| 2308 | packageName); |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | @Override |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2312 | public boolean addGnssNavigationMessageListener(IGnssNavigationMessageListener listener, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2313 | String packageName, String featureId, String listenerIdentifier) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2314 | Objects.requireNonNull(listenerIdentifier); |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2315 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2316 | return mGnssManagerService != null && mGnssManagerService.addGnssNavigationMessageListener( |
| 2317 | listener, packageName, featureId, listenerIdentifier); |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2321 | public void removeGnssNavigationMessageListener(IGnssNavigationMessageListener listener) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2322 | if (mGnssManagerService != null) { |
| 2323 | mGnssManagerService.removeGnssNavigationMessageListener( |
| 2324 | listener); |
| 2325 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | @Override |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2329 | public boolean sendExtraCommand(String providerName, String command, Bundle extras) { |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2330 | Objects.requireNonNull(providerName); |
| 2331 | Objects.requireNonNull(command); |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 2332 | |
| 2333 | mContext.enforceCallingOrSelfPermission( |
| 2334 | Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS, null); |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2335 | enforceCallingOrSelfLocationPermission(); |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 2336 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2337 | mLocationUsageLogger.logLocationApiUsage( |
| 2338 | LocationStatsEnums.USAGE_STARTED, |
| 2339 | LocationStatsEnums.API_SEND_EXTRA_COMMAND, |
| 2340 | providerName); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2341 | |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2342 | LocationProviderManager manager = getLocationProviderManager(providerName); |
| 2343 | if (manager != null) { |
| 2344 | manager.sendExtraCommand(Binder.getCallingUid(), Binder.getCallingPid(), command, |
| 2345 | extras); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2346 | } |
Soonil Nagarkar | bfd1f68 | 2020-01-16 10:50:14 -0800 | [diff] [blame] | 2347 | |
| 2348 | mLocationUsageLogger.logLocationApiUsage( |
| 2349 | LocationStatsEnums.USAGE_ENDED, |
| 2350 | LocationStatsEnums.API_SEND_EXTRA_COMMAND, |
| 2351 | providerName); |
| 2352 | |
| 2353 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2356 | @Override |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2357 | public ProviderProperties getProviderProperties(String providerName) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2358 | LocationProviderManager manager = getLocationProviderManager(providerName); |
| 2359 | if (manager == null) { |
| 2360 | return null; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2361 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2362 | return manager.getProperties(); |
Jason Monk | b71218a | 2015-06-17 14:44:39 -0400 | [diff] [blame] | 2363 | } |
| 2364 | |
Wei Wang | 980b7c2 | 2018-12-06 17:53:00 -0800 | [diff] [blame] | 2365 | @Override |
Soonil Nagarkar | 0d77ea6 | 2019-01-31 14:36:56 -0800 | [diff] [blame] | 2366 | public boolean isProviderPackage(String packageName) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2367 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_DEVICE_CONFIG, null); |
| 2368 | return mLocalService.isProviderPackage(packageName); |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2371 | @Override |
Wei Wang | 9f52d55 | 2019-10-02 14:49:02 -0700 | [diff] [blame] | 2372 | public List<String> getProviderPackages(String providerName) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2373 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_DEVICE_CONFIG, null); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2374 | LocationProviderManager manager = getLocationProviderManager(providerName); |
| 2375 | return manager == null ? Collections.emptyList() : new ArrayList<>(manager.getPackages()); |
Wei Wang | 9f52d55 | 2019-10-02 14:49:02 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | @Override |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2379 | public void setExtraLocationControllerPackage(String packageName) { |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2380 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 2381 | Manifest.permission.LOCATION_HARDWARE + " permission required"); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2382 | synchronized (mLock) { |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2383 | mExtraLocationControllerPackage = packageName; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2384 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | @Override |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2388 | public String getExtraLocationControllerPackage() { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2389 | synchronized (mLock) { |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2390 | return mExtraLocationControllerPackage; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2391 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | @Override |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2395 | public void setExtraLocationControllerPackageEnabled(boolean enabled) { |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2396 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 2397 | Manifest.permission.LOCATION_HARDWARE + " permission required"); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2398 | synchronized (mLock) { |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2399 | mExtraLocationControllerPackageEnabled = enabled; |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2400 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | @Override |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2404 | public boolean isExtraLocationControllerPackageEnabled() { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2405 | synchronized (mLock) { |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2406 | return mExtraLocationControllerPackageEnabled |
| 2407 | && (mExtraLocationControllerPackage != null); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2408 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2412 | public boolean isLocationEnabledForUser(int userId) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2413 | if (UserHandle.getCallingUserId() != userId) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2414 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS, |
| 2415 | null); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2416 | } |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2417 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2418 | return mSettingsHelper.isLocationEnabled(userId); |
Soonil Nagarkar | b2fcddd | 2019-01-03 15:20:06 -0800 | [diff] [blame] | 2419 | } |
| 2420 | |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2421 | @Override |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2422 | public boolean isProviderEnabledForUser(String providerName, int userId) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2423 | if (UserHandle.getCallingUserId() != userId) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2424 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS, |
| 2425 | null); |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2428 | // Fused provider is accessed indirectly via criteria rather than the provider-based APIs, |
| 2429 | // so we discourage its use |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2430 | if (FUSED_PROVIDER.equals(providerName)) return false; |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2431 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2432 | synchronized (mLock) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2433 | LocationProviderManager manager = getLocationProviderManager(providerName); |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2434 | return manager != null && manager.isEnabled(userId); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2435 | } |
Maggie | 2a9409e | 2018-03-21 11:47:28 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2438 | @GuardedBy("mLock") |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2439 | private static boolean shouldBroadcastSafeLocked( |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 2440 | Location loc, Location lastLoc, UpdateRecord record, long now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 | // Always broadcast the first update |
| 2442 | if (lastLoc == null) { |
| 2443 | return true; |
| 2444 | } |
| 2445 | |
Nick Pelly | f1be686 | 2012-05-15 10:53:42 -0700 | [diff] [blame] | 2446 | // Check whether sufficient time has passed |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2447 | long minTime = record.mRealRequest.getFastestInterval(); |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2448 | long deltaMs = TimeUnit.NANOSECONDS.toMillis( |
| 2449 | loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos()); |
| 2450 | if (deltaMs < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2451 | return false; |
| 2452 | } |
| 2453 | |
| 2454 | // Check whether sufficient distance has been traveled |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2455 | double minDistance = record.mRealRequest.getSmallestDisplacement(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2456 | if (minDistance > 0.0) { |
| 2457 | if (loc.distanceTo(lastLoc) <= minDistance) { |
| 2458 | return false; |
| 2459 | } |
| 2460 | } |
| 2461 | |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 2462 | // Check whether sufficient number of udpates is left |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2463 | if (record.mRealRequest.getNumUpdates() <= 0) { |
Laurent Tu | 75defb6 | 2012-11-01 16:21:52 -0700 | [diff] [blame] | 2464 | return false; |
| 2465 | } |
| 2466 | |
| 2467 | // Check whether the expiry date has passed |
Soonil Nagarkar | 95768ce | 2019-11-05 15:22:44 -0800 | [diff] [blame] | 2468 | return record.mExpirationRealtimeMs >= now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2471 | @GuardedBy("mLock") |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2472 | private void handleLocationChangedLocked(Location location, LocationProviderManager manager) { |
| 2473 | if (!mProviderManagers.contains(manager)) { |
| 2474 | Log.w(TAG, "received location from unknown provider: " + manager.getName()); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2475 | return; |
| 2476 | } |
| 2477 | if (!location.isComplete()) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2478 | Log.w(TAG, "dropping incomplete location from " + manager.getName() + " provider: " |
| 2479 | + location); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2480 | return; |
| 2481 | } |
| 2482 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2483 | // notify passive provider |
| 2484 | if (manager != mPassiveManager) { |
| 2485 | mPassiveManager.updateLocation(new Location(location)); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2486 | } |
| 2487 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2488 | if (D) Log.d(TAG, "incoming location: " + location); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2489 | long now = SystemClock.elapsedRealtime(); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2490 | |
| 2491 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2492 | // only update last location for locations that come from enabled providers |
| 2493 | if (manager.isEnabled()) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2494 | updateLastLocationLocked(location, manager.getName()); |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 2495 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2496 | |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2497 | // Update last known coarse interval location if enough time has passed. |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 2498 | Location lastLocationCoarseInterval = mLastLocationCoarseInterval.get( |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2499 | manager.getName()); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2500 | if (lastLocationCoarseInterval == null) { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2501 | lastLocationCoarseInterval = new Location(location); |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 2502 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2503 | if (manager.isEnabled()) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2504 | mLastLocationCoarseInterval.put(manager.getName(), lastLocationCoarseInterval); |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 2505 | } |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2506 | } |
Soonil Nagarkar | 905e722 | 2019-10-01 12:03:29 -0700 | [diff] [blame] | 2507 | long timeDeltaMs = TimeUnit.NANOSECONDS.toMillis(location.getElapsedRealtimeNanos() |
| 2508 | - lastLocationCoarseInterval.getElapsedRealtimeNanos()); |
| 2509 | if (timeDeltaMs > LocationFudger.FASTEST_INTERVAL_MS) { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2510 | lastLocationCoarseInterval.set(location); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2511 | } |
| 2512 | // Don't ever return a coarse location that is more recent than the allowed update |
| 2513 | // interval (i.e. don't allow an app to keep registering and unregistering for |
| 2514 | // location updates to overcome the minimum interval). |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2515 | Location noGPSLocation = |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2516 | lastLocationCoarseInterval.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION); |
| 2517 | |
Laurent Tu | 60ec50a | 2012-10-04 17:00:10 -0700 | [diff] [blame] | 2518 | // Skip if there are no UpdateRecords for this provider. |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2519 | ArrayList<UpdateRecord> records = mRecordsByProvider.get(manager.getName()); |
Laurent Tu | 60ec50a | 2012-10-04 17:00:10 -0700 | [diff] [blame] | 2520 | if (records == null || records.size() == 0) return; |
| 2521 | |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2522 | // Fetch coarse location |
| 2523 | Location coarseLocation = null; |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2524 | if (noGPSLocation != null) { |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2525 | coarseLocation = mLocationFudger.getOrCreate(noGPSLocation); |
| 2526 | } |
| 2527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2528 | ArrayList<Receiver> deadReceivers = null; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2529 | ArrayList<UpdateRecord> deadUpdateRecords = null; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2530 | |
Soonil Nagarkar | 94749f7 | 2018-11-08 11:46:43 -0800 | [diff] [blame] | 2531 | // Broadcast location to all listeners |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2532 | for (UpdateRecord r : records) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2533 | Receiver receiver = r.mReceiver; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 2534 | boolean receiverDead = false; |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 2535 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2536 | if (!manager.isEnabled() && !isSettingsExempt(r)) { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 2537 | continue; |
| 2538 | } |
| 2539 | |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 2540 | int receiverUserId = UserHandle.getUserId(receiver.mCallerIdentity.mUid); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2541 | if (!mUserInfoHelper.isCurrentUserOrProfile(receiverUserId) |
Soonil Nagarkar | 0d77ea6 | 2019-01-31 14:36:56 -0800 | [diff] [blame] | 2542 | && !isProviderPackage(receiver.mCallerIdentity.mPackageName)) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2543 | if (D) { |
Victoria Lease | 269518e | 2012-10-29 08:25:39 -0700 | [diff] [blame] | 2544 | Log.d(TAG, "skipping loc update for background user " + receiverUserId + |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 2545 | " (app: " + receiver.mCallerIdentity.mPackageName + ")"); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 2546 | } |
| 2547 | continue; |
| 2548 | } |
| 2549 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2550 | if (mSettingsHelper.isLocationPackageBlacklisted(receiverUserId, |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 2551 | receiver.mCallerIdentity.mPackageName)) { |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2552 | if (D) { |
| 2553 | Log.d(TAG, "skipping loc update for blacklisted app: " + |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 2554 | receiver.mCallerIdentity.mPackageName); |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 2555 | } |
Nick Pelly | 4035f5a | 2012-08-17 14:43:49 -0700 | [diff] [blame] | 2556 | continue; |
| 2557 | } |
| 2558 | |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2559 | Location notifyLocation; |
Victoria Lease | 37425c3 | 2012-10-16 16:08:48 -0700 | [diff] [blame] | 2560 | if (receiver.mAllowedResolutionLevel < RESOLUTION_LEVEL_FINE) { |
| 2561 | notifyLocation = coarseLocation; // use coarse location |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2562 | } else { |
Soonil Nagarkar | be93e9c | 2019-01-08 13:05:03 -0800 | [diff] [blame] | 2563 | notifyLocation = location; // use fine location |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2564 | } |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2565 | if (notifyLocation != null) { |
| 2566 | Location lastLoc = r.mLastFixBroadcast; |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2567 | if ((lastLoc == null) |
| 2568 | || shouldBroadcastSafeLocked(notifyLocation, lastLoc, r, now)) { |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2569 | if (lastLoc == null) { |
| 2570 | lastLoc = new Location(notifyLocation); |
| 2571 | r.mLastFixBroadcast = lastLoc; |
| 2572 | } else { |
| 2573 | lastLoc.set(notifyLocation); |
| 2574 | } |
Wei Wang | 8486fc7 | 2019-03-07 18:49:21 -0800 | [diff] [blame] | 2575 | // Report location access before delivering location to the client. This will |
| 2576 | // note location delivery to appOps, so it should be called only when a |
| 2577 | // location is really being delivered to the client. |
| 2578 | if (!reportLocationAccessNoThrow( |
| 2579 | receiver.mCallerIdentity.mPid, |
| 2580 | receiver.mCallerIdentity.mUid, |
| 2581 | receiver.mCallerIdentity.mPackageName, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2582 | receiver.mCallerIdentity.mFeatureId, |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 2583 | receiver.mAllowedResolutionLevel, |
| 2584 | "Location sent to " + receiver.mCallerIdentity.mListenerIdentifier)) { |
Wei Wang | 8486fc7 | 2019-03-07 18:49:21 -0800 | [diff] [blame] | 2585 | if (D) { |
| 2586 | Log.d(TAG, "skipping loc update for no op app: " |
| 2587 | + receiver.mCallerIdentity.mPackageName); |
| 2588 | } |
| 2589 | continue; |
| 2590 | } |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2591 | if (!receiver.callLocationChangedLocked(notifyLocation)) { |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2592 | Slog.w(TAG, "RemoteException calling onLocationChanged on " |
| 2593 | + receiver); |
Victoria Lease | 09016ab | 2012-09-16 12:33:15 -0700 | [diff] [blame] | 2594 | receiverDead = true; |
| 2595 | } |
Soonil Nagarkar | d4def0c | 2017-05-23 15:54:55 -0700 | [diff] [blame] | 2596 | r.mRealRequest.decrementNumUpdates(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2600 | // track expired records |
Soonil Nagarkar | 95768ce | 2019-11-05 15:22:44 -0800 | [diff] [blame] | 2601 | if (r.mRealRequest.getNumUpdates() <= 0 || r.mExpirationRealtimeMs < now) { |
Soonil Nagarkar | 39d614a | 2019-10-31 12:10:31 -0700 | [diff] [blame] | 2602 | // notify the client it can remove this listener |
| 2603 | r.mReceiver.callRemovedLocked(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2604 | if (deadUpdateRecords == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2605 | deadUpdateRecords = new ArrayList<>(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2606 | } |
| 2607 | deadUpdateRecords.add(r); |
| 2608 | } |
| 2609 | // track dead receivers |
| 2610 | if (receiverDead) { |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 2611 | if (deadReceivers == null) { |
Soonil Nagarkar | 7decfb6 | 2017-01-18 12:18:49 -0800 | [diff] [blame] | 2612 | deadReceivers = new ArrayList<>(); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 2613 | } |
| 2614 | if (!deadReceivers.contains(receiver)) { |
| 2615 | deadReceivers.add(receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2616 | } |
| 2617 | } |
| 2618 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2619 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2620 | // remove dead records and receivers outside the loop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2621 | if (deadReceivers != null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2622 | for (Receiver receiver : deadReceivers) { |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2623 | removeUpdatesLocked(receiver); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | if (deadUpdateRecords != null) { |
| 2627 | for (UpdateRecord r : deadUpdateRecords) { |
| 2628 | r.disposeLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2629 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2630 | applyRequirementsLocked(manager); |
Victoria Lease | 8b38b29 | 2012-12-04 15:04:43 -0800 | [diff] [blame] | 2631 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2632 | } |
| 2633 | |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2634 | @GuardedBy("mLock") |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2635 | private void updateLastLocationLocked(Location location, String provider) { |
Ram Periathiruvadi | 8671fea | 2017-12-08 18:35:10 -0800 | [diff] [blame] | 2636 | Location noGPSLocation = location.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION); |
| 2637 | Location lastNoGPSLocation; |
| 2638 | Location lastLocation = mLastLocation.get(provider); |
| 2639 | if (lastLocation == null) { |
| 2640 | lastLocation = new Location(provider); |
| 2641 | mLastLocation.put(provider, lastLocation); |
| 2642 | } else { |
| 2643 | lastNoGPSLocation = lastLocation.getExtraLocation(Location.EXTRA_NO_GPS_LOCATION); |
| 2644 | if (noGPSLocation == null && lastNoGPSLocation != null) { |
| 2645 | // New location has no no-GPS location: adopt last no-GPS location. This is set |
| 2646 | // directly into location because we do not want to notify COARSE clients. |
| 2647 | location.setExtraLocation(Location.EXTRA_NO_GPS_LOCATION, lastNoGPSLocation); |
| 2648 | } |
| 2649 | } |
| 2650 | lastLocation.set(location); |
| 2651 | } |
| 2652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | // Geocoder |
| 2654 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2655 | @Override |
Mike Lockwood | e15735a | 2010-09-20 17:48:47 -0400 | [diff] [blame] | 2656 | public boolean geocoderIsPresent() { |
Mark Vandevoorde | 01ac80b | 2010-05-21 15:43:26 -0700 | [diff] [blame] | 2657 | return mGeocodeProvider != null; |
| 2658 | } |
| 2659 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2660 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | public String getFromLocation(double latitude, double longitude, int maxResults, |
Mike Lockwood | 3490140 | 2010-01-04 12:14:21 -0500 | [diff] [blame] | 2662 | GeocoderParams params, List<Address> addrs) { |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 2663 | if (mGeocodeProvider != null) { |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 2664 | return mGeocodeProvider.getFromLocation(latitude, longitude, maxResults, |
| 2665 | params, addrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | } |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 2667 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2668 | } |
| 2669 | |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 2670 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2671 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2672 | public String getFromLocationName(String locationName, |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 2673 | double lowerLeftLatitude, double lowerLeftLongitude, |
| 2674 | double upperRightLatitude, double upperRightLongitude, int maxResults, |
Mike Lockwood | 3490140 | 2010-01-04 12:14:21 -0500 | [diff] [blame] | 2675 | GeocoderParams params, List<Address> addrs) { |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 2676 | |
| 2677 | if (mGeocodeProvider != null) { |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 2678 | return mGeocodeProvider.getFromLocationName(locationName, lowerLeftLatitude, |
| 2679 | lowerLeftLongitude, upperRightLatitude, upperRightLongitude, |
| 2680 | maxResults, params, addrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | } |
Mike Lockwood | a55c321 | 2009-04-15 11:10:11 -0400 | [diff] [blame] | 2682 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | // Mock Providers |
| 2686 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2687 | @Override |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2688 | public void addTestProvider(String provider, ProviderProperties properties, |
| 2689 | String packageName) { |
| 2690 | if (mAppOps.checkOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(), packageName) |
| 2691 | != AppOpsManager.MODE_ALLOWED) { |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 2692 | return; |
| 2693 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2694 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2695 | synchronized (mLock) { |
| 2696 | LocationProviderManager manager = getLocationProviderManager(provider); |
| 2697 | if (manager == null) { |
| 2698 | manager = new LocationProviderManager(provider); |
| 2699 | mProviderManagers.add(manager); |
| 2700 | } |
| 2701 | |
| 2702 | manager.setMockProvider(new MockProvider(mContext, properties)); |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | @Override |
| 2707 | public void removeTestProvider(String provider, String packageName) { |
| 2708 | if (mAppOps.checkOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(), packageName) |
| 2709 | != AppOpsManager.MODE_ALLOWED) { |
| 2710 | return; |
Mike Lockwood | a4903f2 | 2010-02-17 06:42:23 -0500 | [diff] [blame] | 2711 | } |
| 2712 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2713 | synchronized (mLock) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2714 | LocationProviderManager manager = getLocationProviderManager(provider); |
| 2715 | if (manager == null) { |
| 2716 | return; |
| 2717 | } |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2718 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2719 | manager.setMockProvider(null); |
| 2720 | if (!manager.hasProvider()) { |
| 2721 | mProviderManagers.remove(manager); |
| 2722 | mLastLocation.remove(manager.getName()); |
| 2723 | mLastLocationCoarseInterval.remove(manager.getName()); |
Mike Lockwood | 7566c1d | 2009-08-25 10:05:18 -0700 | [diff] [blame] | 2724 | } |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2725 | } |
Ji-Hwan Lee | 26bdb8f | 2014-04-21 20:48:19 +0900 | [diff] [blame] | 2726 | } |
| 2727 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2728 | @Override |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2729 | public void setTestProviderLocation(String provider, Location location, String packageName) { |
| 2730 | if (mAppOps.checkOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(), packageName) |
| 2731 | != AppOpsManager.MODE_ALLOWED) { |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 2732 | return; |
| 2733 | } |
| 2734 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2735 | LocationProviderManager manager = getLocationProviderManager(provider); |
| 2736 | if (manager == null) { |
| 2737 | throw new IllegalArgumentException("provider doesn't exist: " + provider); |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2738 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2739 | |
| 2740 | manager.setMockProviderLocation(location); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2741 | } |
| 2742 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2743 | @Override |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2744 | public void setTestProviderEnabled(String provider, boolean enabled, String packageName) { |
| 2745 | if (mAppOps.checkOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(), packageName) |
| 2746 | != AppOpsManager.MODE_ALLOWED) { |
Svet Ganov | f7e9cf4 | 2015-05-13 10:40:31 -0700 | [diff] [blame] | 2747 | return; |
| 2748 | } |
| 2749 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2750 | LocationProviderManager manager = getLocationProviderManager(provider); |
| 2751 | if (manager == null) { |
| 2752 | throw new IllegalArgumentException("provider doesn't exist: " + provider); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2753 | } |
| 2754 | |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2755 | manager.setMockProviderAllowed(enabled); |
Soonil Nagarkar | 90da1ab | 2019-01-04 16:26:59 -0800 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | @Override |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 2759 | @NonNull |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2760 | public List<LocationRequest> getTestProviderCurrentRequests(String provider, |
| 2761 | String packageName) { |
| 2762 | if (mAppOps.checkOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(), packageName) |
| 2763 | != AppOpsManager.MODE_ALLOWED) { |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 2764 | return Collections.emptyList(); |
| 2765 | } |
| 2766 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2767 | LocationProviderManager manager = getLocationProviderManager(provider); |
| 2768 | if (manager == null) { |
| 2769 | throw new IllegalArgumentException("provider doesn't exist: " + provider); |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 2770 | } |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2771 | |
| 2772 | return manager.getMockProviderRequests(); |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2777 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) { |
| 2778 | return; |
| 2779 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 2780 | |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2781 | IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
| 2782 | |
Soonil Nagarkar | 6825774 | 2019-01-09 19:42:34 +0000 | [diff] [blame] | 2783 | synchronized (mLock) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2784 | if (mGnssManagerService != null && args.length > 0 && args[0].equals("--gnssmetrics")) { |
| 2785 | mGnssManagerService.dump(fd, pw, args); |
Blake Kragten | 47014d2 | 2019-12-06 14:18:38 -0800 | [diff] [blame] | 2786 | return; |
Siddharth Ray | bb608c8 | 2017-03-16 11:33:34 -0700 | [diff] [blame] | 2787 | } |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2788 | |
| 2789 | ipw.println("Location Manager State:"); |
| 2790 | ipw.increaseIndent(); |
| 2791 | ipw.print("Current System Time: " |
WyattRiley | ba6072f | 2019-04-18 07:37:52 -0700 | [diff] [blame] | 2792 | + TimeUtils.logTimeOfDay(System.currentTimeMillis())); |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2793 | ipw.println(", Current Elapsed Time: " |
WyattRiley | ba6072f | 2019-04-18 07:37:52 -0700 | [diff] [blame] | 2794 | + TimeUtils.formatDuration(SystemClock.elapsedRealtime())); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 2795 | |
| 2796 | ipw.println("User Info:"); |
| 2797 | ipw.increaseIndent(); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2798 | mUserInfoHelper.dump(fd, ipw, args); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 2799 | ipw.decreaseIndent(); |
| 2800 | |
| 2801 | ipw.println("Location Settings:"); |
| 2802 | ipw.increaseIndent(); |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2803 | mSettingsHelper.dump(fd, ipw, args); |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 2804 | ipw.decreaseIndent(); |
| 2805 | |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2806 | ipw.println("Battery Saver Location Mode: " |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 2807 | + locationPowerSaveModeToString(mBatterySaverMode)); |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2808 | |
| 2809 | ipw.println("Location Listeners:"); |
| 2810 | ipw.increaseIndent(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2811 | for (Receiver receiver : mReceivers.values()) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2812 | ipw.println(receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2813 | } |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2814 | ipw.decreaseIndent(); |
| 2815 | |
| 2816 | ipw.println("Active Records by Provider:"); |
| 2817 | ipw.increaseIndent(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2818 | for (Map.Entry<String, ArrayList<UpdateRecord>> entry : mRecordsByProvider.entrySet()) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2819 | ipw.println(entry.getKey() + ":"); |
| 2820 | ipw.increaseIndent(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2821 | for (UpdateRecord record : entry.getValue()) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2822 | ipw.println(record); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | } |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2824 | ipw.decreaseIndent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | } |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2826 | ipw.decreaseIndent(); |
Anil Admal | 98d49b7 | 2019-02-06 15:26:33 -0800 | [diff] [blame] | 2827 | |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2828 | ipw.println("Historical Records by Provider:"); |
| 2829 | ipw.increaseIndent(); |
WyattRiley | d6b82a6 | 2019-12-30 18:12:50 -0800 | [diff] [blame] | 2830 | TreeMap<PackageProviderKey, PackageStatistics> sorted = new TreeMap<>(); |
| 2831 | sorted.putAll(mRequestStatistics.statistics); |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2832 | for (Map.Entry<PackageProviderKey, PackageStatistics> entry |
WyattRiley | d6b82a6 | 2019-12-30 18:12:50 -0800 | [diff] [blame] | 2833 | : sorted.entrySet()) { |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2834 | PackageProviderKey key = entry.getKey(); |
WyattRiley | d6b82a6 | 2019-12-30 18:12:50 -0800 | [diff] [blame] | 2835 | ipw.println(key.providerName + ": " + key.packageName + ": " + entry.getValue()); |
David Christie | 2ff96af | 2014-01-30 16:09:37 -0800 | [diff] [blame] | 2836 | } |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2837 | ipw.decreaseIndent(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 2838 | |
WyattRiley | fa1ef8d3 | 2019-12-30 17:19:55 -0800 | [diff] [blame] | 2839 | mRequestStatistics.history.dump(ipw); |
| 2840 | |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2841 | ipw.println("Last Known Locations:"); |
| 2842 | ipw.increaseIndent(); |
| 2843 | for (Map.Entry<String, Location> entry : mLastLocation.entrySet()) { |
| 2844 | ipw.println(entry.getKey() + ": " + entry.getValue()); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2845 | } |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2846 | ipw.decreaseIndent(); |
| 2847 | |
| 2848 | ipw.println("Last Known Coarse Locations:"); |
| 2849 | ipw.increaseIndent(); |
| 2850 | for (Map.Entry<String, Location> entry : mLastLocationCoarseInterval.entrySet()) { |
| 2851 | ipw.println(entry.getKey() + ": " + entry.getValue()); |
| 2852 | } |
| 2853 | ipw.decreaseIndent(); |
David Christie | 1b9b7b1 | 2013-04-15 15:31:11 -0700 | [diff] [blame] | 2854 | |
Tyler Trephan | 2f3ee9a | 2019-03-21 10:54:22 -0700 | [diff] [blame] | 2855 | if (mGeofenceManager != null) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2856 | ipw.println("Geofences:"); |
| 2857 | ipw.increaseIndent(); |
| 2858 | mGeofenceManager.dump(ipw); |
| 2859 | ipw.decreaseIndent(); |
Tyler Trephan | 2f3ee9a | 2019-03-21 10:54:22 -0700 | [diff] [blame] | 2860 | } |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 2861 | |
Wei Wang | 114922a | 2019-01-30 18:19:35 -0800 | [diff] [blame] | 2862 | if (mExtraLocationControllerPackage != null) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2863 | ipw.println("Location Controller Extra Package: " + mExtraLocationControllerPackage |
| 2864 | + (mExtraLocationControllerPackageEnabled ? " [enabled]" : "[disabled]")); |
Wei Wang | 980b7c2 | 2018-12-06 17:53:00 -0800 | [diff] [blame] | 2865 | } |
| 2866 | |
Tyler Trephan | 2f3ee9a | 2019-03-21 10:54:22 -0700 | [diff] [blame] | 2867 | if (mLocationFudger != null) { |
Soonil Nagarkar | 1c57255 | 2019-07-10 13:31:47 -0700 | [diff] [blame] | 2868 | ipw.println("Location Fudger:"); |
| 2869 | ipw.increaseIndent(); |
| 2870 | mLocationFudger.dump(fd, ipw, args); |
| 2871 | ipw.decreaseIndent(); |
Tyler Trephan | 2f3ee9a | 2019-03-21 10:54:22 -0700 | [diff] [blame] | 2872 | } |
Anil Admal | 98d49b7 | 2019-02-06 15:26:33 -0800 | [diff] [blame] | 2873 | |
Soonil Nagarkar | 9ec0185 | 2020-01-10 13:42:38 -0800 | [diff] [blame] | 2874 | ipw.println("Location Providers:"); |
| 2875 | ipw.increaseIndent(); |
| 2876 | for (LocationProviderManager manager : mProviderManagers) { |
| 2877 | manager.dump(fd, ipw, args); |
| 2878 | } |
| 2879 | ipw.decreaseIndent(); |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2880 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 2881 | if (mGnssManagerService != null) { |
| 2882 | ipw.println("GNSS:"); |
| 2883 | ipw.increaseIndent(); |
| 2884 | mGnssManagerService.dump(fd, ipw, args); |
| 2885 | ipw.decreaseIndent(); |
| 2886 | } |
Anil Admal | 98d49b7 | 2019-02-06 15:26:33 -0800 | [diff] [blame] | 2887 | } |
| 2888 | } |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2889 | |
| 2890 | private class LocalService extends LocationManagerInternal { |
| 2891 | |
| 2892 | @Override |
| 2893 | public void requestSetProviderAllowed(String provider, boolean allowed) { |
| 2894 | Preconditions.checkArgument(provider != null, "invalid null provider"); |
| 2895 | |
| 2896 | synchronized (mLock) { |
| 2897 | LocationProviderManager manager = getLocationProviderManager(provider); |
| 2898 | if (manager != null) { |
| 2899 | manager.requestSetAllowed(allowed); |
| 2900 | } |
| 2901 | } |
| 2902 | } |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 2903 | |
| 2904 | @Override |
| 2905 | public boolean isProviderPackage(String packageName) { |
| 2906 | for (LocationProviderManager manager : mProviderManagers) { |
| 2907 | if (manager.getPackages().contains(packageName)) { |
| 2908 | return true; |
| 2909 | } |
| 2910 | } |
| 2911 | return false; |
| 2912 | } |
| 2913 | |
| 2914 | @Override |
| 2915 | public void sendNiResponse(int notifId, int userResponse) { |
| 2916 | if (mGnssManagerService != null) { |
| 2917 | mGnssManagerService.sendNiResponse(notifId, userResponse); |
| 2918 | } |
| 2919 | } |
Soonil Nagarkar | 980ce6a | 2020-01-23 18:06:31 -0800 | [diff] [blame] | 2920 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2921 | } |