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