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