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