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