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