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