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