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