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