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