Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.car; |
| 18 | |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
| 20 | import android.car.ICarUserService; |
| 21 | import android.car.ILocationManagerProxy; |
Gregory Clark | 18a0ef9 | 2019-05-23 20:00:49 -0700 | [diff] [blame] | 22 | import android.car.drivingstate.CarDrivingStateEvent; |
| 23 | import android.car.drivingstate.ICarDrivingStateChangeListener; |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 24 | import android.car.hardware.power.CarPowerManager; |
| 25 | import android.car.hardware.power.CarPowerManager.CarPowerStateListener; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 26 | import android.car.hardware.power.CarPowerManager.CarPowerStateListenerWithCompletion; |
Ying Zheng | 9fc9940 | 2018-09-19 14:23:59 -0700 | [diff] [blame] | 27 | import android.car.userlib.CarUserManagerHelper; |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 32 | import android.location.Location; |
| 33 | import android.location.LocationManager; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.HandlerThread; |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
Gregory Clark | 756ed81 | 2018-02-06 18:19:27 -0800 | [diff] [blame] | 37 | import android.os.SystemClock; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 38 | import android.os.UserHandle; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 39 | import android.util.AtomicFile; |
| 40 | import android.util.JsonReader; |
| 41 | import android.util.JsonWriter; |
| 42 | import android.util.Log; |
| 43 | |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 44 | import com.android.car.systeminterface.SystemInterface; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 45 | import com.android.internal.annotations.VisibleForTesting; |
| 46 | |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 47 | import java.io.File; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 48 | import java.io.FileInputStream; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 49 | import java.io.FileNotFoundException; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 50 | import java.io.FileOutputStream; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStreamReader; |
| 53 | import java.io.OutputStreamWriter; |
| 54 | import java.io.PrintWriter; |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 55 | import java.util.concurrent.CompletableFuture; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * This service stores the last known location from {@link LocationManager} when a car is parked |
| 59 | * and restores the location when the car is powered on. |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 60 | */ |
Gregory Clark | 18a0ef9 | 2019-05-23 20:00:49 -0700 | [diff] [blame] | 61 | public class CarLocationService extends BroadcastReceiver implements CarServiceBase, |
| 62 | CarPowerStateListenerWithCompletion { |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 63 | private static final String TAG = "CarLocationService"; |
| 64 | private static final String FILENAME = "location_cache.json"; |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 65 | private static final boolean DBG = true; |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 66 | // The accuracy for the stored timestamp |
| 67 | private static final long GRANULARITY_ONE_DAY_MS = 24 * 60 * 60 * 1000L; |
| 68 | // The time-to-live for the cached location |
| 69 | private static final long TTL_THIRTY_DAYS_MS = 30 * GRANULARITY_ONE_DAY_MS; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 70 | // The maximum number of times to try injecting a location |
| 71 | private static final int MAX_LOCATION_INJECTION_ATTEMPTS = 10; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 72 | |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 73 | // Used internally for mHandlerThread synchronization |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 74 | private final Object mLock = new Object(); |
| 75 | |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 76 | // Used internally for mILocationManagerProxy synchronization |
| 77 | private final Object mLocationManagerProxyLock = new Object(); |
| 78 | |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 79 | private final Context mContext; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 80 | private final CarUserManagerHelper mCarUserManagerHelper; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 81 | private int mTaskCount = 0; |
| 82 | private HandlerThread mHandlerThread; |
| 83 | private Handler mHandler; |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 84 | private CarPowerManager mCarPowerManager; |
Gregory Clark | 18a0ef9 | 2019-05-23 20:00:49 -0700 | [diff] [blame] | 85 | private CarDrivingStateService mCarDrivingStateService; |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 86 | private PerUserCarServiceHelper mPerUserCarServiceHelper; |
| 87 | |
| 88 | // Allows us to interact with the {@link LocationManager} as the foreground user. |
| 89 | private ILocationManagerProxy mILocationManagerProxy; |
| 90 | |
| 91 | // Maintains mILocationManagerProxy for the current foreground user. |
| 92 | private final PerUserCarServiceHelper.ServiceCallback mUserServiceCallback = |
| 93 | new PerUserCarServiceHelper.ServiceCallback() { |
| 94 | @Override |
| 95 | public void onServiceConnected(ICarUserService carUserService) { |
| 96 | logd("Connected to PerUserCarService"); |
| 97 | if (carUserService == null) { |
| 98 | logd("ICarUserService is null. Cannot get location manager proxy"); |
| 99 | return; |
| 100 | } |
| 101 | synchronized (mLocationManagerProxyLock) { |
| 102 | try { |
| 103 | mILocationManagerProxy = carUserService.getLocationManagerProxy(); |
| 104 | } catch (RemoteException e) { |
| 105 | Log.e(TAG, "RemoteException from ICarUserService", e); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | int currentUser = ActivityManager.getCurrentUser(); |
| 110 | logd("Current user: " + currentUser); |
| 111 | if (mCarUserManagerHelper.isHeadlessSystemUser() |
| 112 | && currentUser > UserHandle.USER_SYSTEM) { |
| 113 | asyncOperation(() -> loadLocation()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void onPreUnbind() { |
| 119 | logd("Before Unbinding from PerCarUserService"); |
| 120 | synchronized (mLocationManagerProxyLock) { |
| 121 | mILocationManagerProxy = null; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onServiceDisconnected() { |
| 127 | logd("Disconnected from PerUserCarService"); |
| 128 | synchronized (mLocationManagerProxyLock) { |
| 129 | mILocationManagerProxy = null; |
| 130 | } |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | private final ICarDrivingStateChangeListener mICarDrivingStateChangeEventListener = |
| 135 | new ICarDrivingStateChangeListener.Stub() { |
| 136 | @Override |
| 137 | public void onDrivingStateChanged(CarDrivingStateEvent event) { |
| 138 | logd("onDrivingStateChanged " + event); |
| 139 | if (event != null |
| 140 | && event.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING) { |
| 141 | deleteCacheFile(); |
| 142 | if (mCarDrivingStateService != null) { |
| 143 | mCarDrivingStateService.unregisterDrivingStateChangeListener( |
| 144 | mICarDrivingStateChangeEventListener); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | }; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 149 | |
Gregory Clark | 18a0ef9 | 2019-05-23 20:00:49 -0700 | [diff] [blame] | 150 | public CarLocationService(Context context, CarUserManagerHelper carUserManagerHelper) { |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 151 | logd("constructed"); |
| 152 | mContext = context; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 153 | mCarUserManagerHelper = carUserManagerHelper; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void init() { |
| 158 | logd("init"); |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 159 | IntentFilter filter = new IntentFilter(); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 160 | filter.addAction(LocationManager.MODE_CHANGED_ACTION); |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 161 | mContext.registerReceiver(this, filter); |
Gregory Clark | 18a0ef9 | 2019-05-23 20:00:49 -0700 | [diff] [blame] | 162 | mCarDrivingStateService = CarLocalServices.getService(CarDrivingStateService.class); |
| 163 | if (mCarDrivingStateService != null) { |
| 164 | CarDrivingStateEvent event = mCarDrivingStateService.getCurrentDrivingState(); |
| 165 | if (event != null && event.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING) { |
| 166 | deleteCacheFile(); |
| 167 | } else { |
| 168 | mCarDrivingStateService.registerDrivingStateChangeListener( |
| 169 | mICarDrivingStateChangeEventListener); |
| 170 | } |
| 171 | } |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 172 | mCarPowerManager = CarLocalServices.createCarPowerManager(mContext); |
| 173 | if (mCarPowerManager != null) { // null case happens for testing. |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 174 | mCarPowerManager.setListenerWithCompletion(CarLocationService.this); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 175 | } |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 176 | mPerUserCarServiceHelper = CarLocalServices.getService(PerUserCarServiceHelper.class); |
| 177 | if (mPerUserCarServiceHelper != null) { |
| 178 | mPerUserCarServiceHelper.registerServiceCallback(mUserServiceCallback); |
| 179 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void release() { |
| 184 | logd("release"); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 185 | if (mCarPowerManager != null) { |
| 186 | mCarPowerManager.clearListener(); |
| 187 | } |
Gregory Clark | 18a0ef9 | 2019-05-23 20:00:49 -0700 | [diff] [blame] | 188 | if (mCarDrivingStateService != null) { |
| 189 | mCarDrivingStateService.unregisterDrivingStateChangeListener( |
| 190 | mICarDrivingStateChangeEventListener); |
| 191 | } |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 192 | if (mPerUserCarServiceHelper != null) { |
| 193 | mPerUserCarServiceHelper.unregisterServiceCallback(mUserServiceCallback); |
| 194 | } |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 195 | mContext.unregisterReceiver(this); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void dump(PrintWriter writer) { |
| 200 | writer.println(TAG); |
| 201 | writer.println("Context: " + mContext); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 202 | writer.println("MAX_LOCATION_INJECTION_ATTEMPTS: " + MAX_LOCATION_INJECTION_ATTEMPTS); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 206 | public void onStateChanged(int state, CompletableFuture<Void> future) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 207 | logd("onStateChanged: " + state); |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 208 | switch (state) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 209 | case CarPowerStateListener.SHUTDOWN_PREPARE: |
Gregory Clark | 5b9a9cd | 2018-09-10 13:21:22 -0700 | [diff] [blame] | 210 | asyncOperation(() -> { |
| 211 | storeLocation(); |
| 212 | // Notify the CarPowerManager that it may proceed to shutdown or suspend. |
Gregory Clark | 71ed672 | 2018-09-24 13:10:16 -0700 | [diff] [blame] | 213 | if (future != null) { |
| 214 | future.complete(null); |
| 215 | } |
Gregory Clark | 5b9a9cd | 2018-09-10 13:21:22 -0700 | [diff] [blame] | 216 | }); |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 217 | break; |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame] | 218 | case CarPowerStateListener.SUSPEND_EXIT: |
Gregory Clark | dfb084f | 2019-07-15 16:41:57 -0700 | [diff] [blame^] | 219 | if (mCarDrivingStateService != null) { |
| 220 | CarDrivingStateEvent event = mCarDrivingStateService.getCurrentDrivingState(); |
| 221 | if (event != null |
| 222 | && event.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING) { |
| 223 | deleteCacheFile(); |
| 224 | } else { |
| 225 | logd("Registering to receive driving state."); |
| 226 | mCarDrivingStateService.registerDrivingStateChangeListener( |
| 227 | mICarDrivingStateChangeEventListener); |
| 228 | } |
| 229 | } |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame] | 230 | if (future != null) { |
| 231 | future.complete(null); |
| 232 | } |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 233 | default: |
Gregory Clark | 5b9a9cd | 2018-09-10 13:21:22 -0700 | [diff] [blame] | 234 | // This service does not need to do any work for these events but should still |
| 235 | // notify the CarPowerManager that it may proceed. |
Gregory Clark | 71ed672 | 2018-09-24 13:10:16 -0700 | [diff] [blame] | 236 | if (future != null) { |
| 237 | future.complete(null); |
| 238 | } |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 239 | break; |
| 240 | } |
Gregory Clark | 26fa601 | 2018-03-14 18:38:56 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Gregory Clark | 26fa601 | 2018-03-14 18:38:56 -0700 | [diff] [blame] | 243 | @Override |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 244 | public void onReceive(Context context, Intent intent) { |
Gregory Clark | 26fa601 | 2018-03-14 18:38:56 -0700 | [diff] [blame] | 245 | logd("onReceive " + intent); |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 246 | // If the system user is headless but the current user is still the system user, then we |
| 247 | // should not delete the location cache file due to missing location permissions. |
| 248 | if (isCurrentUserHeadlessSystemUser()) { |
| 249 | logd("Current user is headless system user."); |
| 250 | return; |
| 251 | } |
| 252 | synchronized (mLocationManagerProxyLock) { |
| 253 | if (mILocationManagerProxy == null) { |
| 254 | logd("Null location manager."); |
| 255 | return; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 256 | } |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 257 | String action = intent.getAction(); |
| 258 | try { |
| 259 | if (action == LocationManager.MODE_CHANGED_ACTION) { |
| 260 | boolean locationEnabled = mILocationManagerProxy.isLocationEnabled(); |
| 261 | logd("isLocationEnabled(): " + locationEnabled); |
| 262 | if (!locationEnabled) { |
| 263 | deleteCacheFile(); |
| 264 | } |
| 265 | } else { |
| 266 | logd("Unexpected intent."); |
| 267 | } |
| 268 | } catch (RemoteException e) { |
| 269 | Log.e(TAG, "RemoteException from ILocationManagerProxy", e); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 274 | /** Tells whether the current foreground user is the headless system user. */ |
| 275 | private boolean isCurrentUserHeadlessSystemUser() { |
| 276 | int currentUserId = ActivityManager.getCurrentUser(); |
| 277 | return mCarUserManagerHelper.isHeadlessSystemUser() && currentUserId == 0; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 281 | * Gets the last known location from the location manager proxy and store it in a file. |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 282 | */ |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 283 | private void storeLocation() { |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 284 | Location location = null; |
| 285 | synchronized (mLocationManagerProxyLock) { |
| 286 | if (mILocationManagerProxy == null) { |
| 287 | logd("Null location manager proxy."); |
| 288 | return; |
| 289 | } |
| 290 | try { |
| 291 | location = mILocationManagerProxy.getLastKnownLocation( |
| 292 | LocationManager.GPS_PROVIDER); |
| 293 | } catch (RemoteException e) { |
| 294 | Log.e(TAG, "RemoteException from ILocationManagerProxy", e); |
| 295 | } |
| 296 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 297 | if (location == null) { |
| 298 | logd("Not storing null location"); |
| 299 | } else { |
| 300 | logd("Storing location: " + location); |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 301 | AtomicFile atomicFile = new AtomicFile(getLocationCacheFile()); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 302 | FileOutputStream fos = null; |
| 303 | try { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 304 | fos = atomicFile.startWrite(); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 305 | try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(fos, "UTF-8"))) { |
| 306 | jsonWriter.beginObject(); |
| 307 | jsonWriter.name("provider").value(location.getProvider()); |
| 308 | jsonWriter.name("latitude").value(location.getLatitude()); |
| 309 | jsonWriter.name("longitude").value(location.getLongitude()); |
| 310 | if (location.hasAltitude()) { |
| 311 | jsonWriter.name("altitude").value(location.getAltitude()); |
| 312 | } |
| 313 | if (location.hasSpeed()) { |
| 314 | jsonWriter.name("speed").value(location.getSpeed()); |
| 315 | } |
| 316 | if (location.hasBearing()) { |
| 317 | jsonWriter.name("bearing").value(location.getBearing()); |
| 318 | } |
| 319 | if (location.hasAccuracy()) { |
| 320 | jsonWriter.name("accuracy").value(location.getAccuracy()); |
| 321 | } |
| 322 | if (location.hasVerticalAccuracy()) { |
| 323 | jsonWriter.name("verticalAccuracy").value( |
| 324 | location.getVerticalAccuracyMeters()); |
| 325 | } |
| 326 | if (location.hasSpeedAccuracy()) { |
| 327 | jsonWriter.name("speedAccuracy").value( |
| 328 | location.getSpeedAccuracyMetersPerSecond()); |
| 329 | } |
| 330 | if (location.hasBearingAccuracy()) { |
| 331 | jsonWriter.name("bearingAccuracy").value( |
| 332 | location.getBearingAccuracyDegrees()); |
| 333 | } |
| 334 | if (location.isFromMockProvider()) { |
| 335 | jsonWriter.name("isFromMockProvider").value(true); |
| 336 | } |
| 337 | long currentTime = location.getTime(); |
| 338 | // Round the time down to only be accurate within one day. |
| 339 | jsonWriter.name("captureTime").value( |
| 340 | currentTime - currentTime % GRANULARITY_ONE_DAY_MS); |
| 341 | jsonWriter.endObject(); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 342 | } |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 343 | atomicFile.finishWrite(fos); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 344 | } catch (IOException e) { |
| 345 | Log.e(TAG, "Unable to write to disk", e); |
| 346 | atomicFile.failWrite(fos); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 351 | /** |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 352 | * Reads a previously stored location and attempts to inject it into the location manager proxy. |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 353 | */ |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 354 | private void loadLocation() { |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 355 | Location location = readLocationFromCacheFile(); |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 356 | logd("Read location from timestamp " + location.getTime()); |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 357 | long currentTime = System.currentTimeMillis(); |
| 358 | if (location.getTime() + TTL_THIRTY_DAYS_MS < currentTime) { |
| 359 | logd("Location expired."); |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame] | 360 | deleteCacheFile(); |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 361 | } else { |
| 362 | location.setTime(currentTime); |
| 363 | long elapsedTime = SystemClock.elapsedRealtimeNanos(); |
| 364 | location.setElapsedRealtimeNanos(elapsedTime); |
| 365 | if (location.isComplete()) { |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 366 | injectLocation(location, 1); |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private Location readLocationFromCacheFile() { |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 372 | Location location = new Location((String) null); |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 373 | AtomicFile atomicFile = new AtomicFile(getLocationCacheFile()); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 374 | try (FileInputStream fis = atomicFile.openRead()) { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 375 | JsonReader reader = new JsonReader(new InputStreamReader(fis, "UTF-8")); |
| 376 | reader.beginObject(); |
| 377 | while (reader.hasNext()) { |
| 378 | String name = reader.nextName(); |
| 379 | if (name.equals("provider")) { |
| 380 | location.setProvider(reader.nextString()); |
| 381 | } else if (name.equals("latitude")) { |
| 382 | location.setLatitude(reader.nextDouble()); |
| 383 | } else if (name.equals("longitude")) { |
| 384 | location.setLongitude(reader.nextDouble()); |
| 385 | } else if (name.equals("altitude")) { |
| 386 | location.setAltitude(reader.nextDouble()); |
| 387 | } else if (name.equals("speed")) { |
| 388 | location.setSpeed((float) reader.nextDouble()); |
| 389 | } else if (name.equals("bearing")) { |
| 390 | location.setBearing((float) reader.nextDouble()); |
| 391 | } else if (name.equals("accuracy")) { |
| 392 | location.setAccuracy((float) reader.nextDouble()); |
| 393 | } else if (name.equals("verticalAccuracy")) { |
| 394 | location.setVerticalAccuracyMeters((float) reader.nextDouble()); |
| 395 | } else if (name.equals("speedAccuracy")) { |
| 396 | location.setSpeedAccuracyMetersPerSecond((float) reader.nextDouble()); |
| 397 | } else if (name.equals("bearingAccuracy")) { |
| 398 | location.setBearingAccuracyDegrees((float) reader.nextDouble()); |
| 399 | } else if (name.equals("isFromMockProvider")) { |
| 400 | location.setIsFromMockProvider(reader.nextBoolean()); |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 401 | } else if (name.equals("captureTime")) { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 402 | location.setTime(reader.nextLong()); |
| 403 | } else { |
| 404 | reader.skipValue(); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 405 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 406 | } |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 407 | reader.endObject(); |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 408 | } catch (FileNotFoundException e) { |
| 409 | Log.d(TAG, "Location cache file not found."); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 410 | } catch (IOException e) { |
| 411 | Log.e(TAG, "Unable to read from disk", e); |
| 412 | } catch (NumberFormatException | IllegalStateException e) { |
| 413 | Log.e(TAG, "Unexpected format", e); |
| 414 | } |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 415 | return location; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 418 | private void deleteCacheFile() { |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 419 | boolean deleted = getLocationCacheFile().delete(); |
| 420 | logd("Deleted cache file: " + deleted); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 423 | /** |
| 424 | * Attempts to inject the location multiple times in case the LocationManager was not fully |
| 425 | * initialized or has not updated its handle to the current user yet. |
| 426 | */ |
| 427 | private void injectLocation(Location location, int attemptCount) { |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 428 | boolean success = false; |
| 429 | synchronized (mLocationManagerProxyLock) { |
| 430 | if (mILocationManagerProxy == null) { |
| 431 | logd("Null location manager proxy."); |
| 432 | } else { |
| 433 | try { |
| 434 | success = mILocationManagerProxy.injectLocation(location); |
| 435 | } catch (RemoteException e) { |
| 436 | Log.e(TAG, "RemoteException from ILocationManagerProxy", e); |
| 437 | } |
| 438 | } |
| 439 | } |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 440 | logd("Injected location " + location + " with result " + success + " on attempt " |
| 441 | + attemptCount); |
| 442 | if (success) { |
| 443 | return; |
| 444 | } else if (attemptCount <= MAX_LOCATION_INJECTION_ATTEMPTS) { |
| 445 | asyncOperation(() -> { |
| 446 | injectLocation(location, attemptCount + 1); |
| 447 | }, 200 * attemptCount); |
| 448 | } else { |
| 449 | logd("No location injected."); |
| 450 | } |
| 451 | } |
| 452 | |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 453 | private File getLocationCacheFile() { |
| 454 | SystemInterface systemInterface = CarLocalServices.getService(SystemInterface.class); |
| 455 | File file = new File(systemInterface.getSystemCarDir(), FILENAME); |
| 456 | logd("File: " + file); |
| 457 | return file; |
| 458 | } |
| 459 | |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 460 | @VisibleForTesting |
| 461 | void asyncOperation(Runnable operation) { |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 462 | asyncOperation(operation, 0); |
| 463 | } |
| 464 | |
| 465 | private void asyncOperation(Runnable operation, long delayMillis) { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 466 | synchronized (mLock) { |
| 467 | // Create a new HandlerThread if this is the first task to queue. |
| 468 | if (++mTaskCount == 1) { |
| 469 | mHandlerThread = new HandlerThread("CarLocationServiceThread"); |
| 470 | mHandlerThread.start(); |
| 471 | mHandler = new Handler(mHandlerThread.getLooper()); |
| 472 | } |
| 473 | } |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 474 | mHandler.postDelayed(() -> { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 475 | try { |
| 476 | operation.run(); |
| 477 | } finally { |
| 478 | synchronized (mLock) { |
| 479 | // Quit the thread when the task queue is empty. |
| 480 | if (--mTaskCount == 0) { |
| 481 | mHandler.getLooper().quit(); |
| 482 | mHandler = null; |
| 483 | mHandlerThread = null; |
| 484 | } |
| 485 | } |
| 486 | } |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 487 | }, delayMillis); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | private static void logd(String msg) { |
| 491 | if (DBG) { |
| 492 | Log.d(TAG, msg); |
| 493 | } |
| 494 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 495 | } |