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