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