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