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