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