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