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