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 | |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 19 | import android.car.hardware.power.CarPowerManager; |
| 20 | import android.car.hardware.power.CarPowerManager.CarPowerStateListener; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 21 | import android.car.hardware.power.CarPowerManager.CarPowerStateListenerWithCompletion; |
Ying Zheng | 9fc9940 | 2018-09-19 14:23:59 -0700 | [diff] [blame] | 22 | import android.car.userlib.CarUserManagerHelper; |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 27 | import android.location.Location; |
| 28 | import android.location.LocationManager; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.HandlerThread; |
Gregory Clark | 756ed81 | 2018-02-06 18:19:27 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 33 | import android.util.AtomicFile; |
| 34 | import android.util.JsonReader; |
| 35 | import android.util.JsonWriter; |
| 36 | import android.util.Log; |
| 37 | |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 38 | import com.android.car.systeminterface.SystemInterface; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
| 40 | |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 41 | import java.io.File; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 42 | import java.io.FileInputStream; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 43 | import java.io.FileNotFoundException; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 44 | import java.io.FileOutputStream; |
| 45 | import java.io.IOException; |
| 46 | import java.io.InputStreamReader; |
| 47 | import java.io.OutputStreamWriter; |
| 48 | import java.io.PrintWriter; |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 49 | import java.util.concurrent.CompletableFuture; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * This service stores the last known location from {@link LocationManager} when a car is parked |
| 53 | * and restores the location when the car is powered on. |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 54 | */ |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 55 | public class CarLocationService extends BroadcastReceiver implements |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 56 | CarServiceBase, CarPowerStateListenerWithCompletion { |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 57 | private static final String TAG = "CarLocationService"; |
| 58 | private static final String FILENAME = "location_cache.json"; |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 59 | private static final boolean DBG = true; |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 60 | // The accuracy for the stored timestamp |
| 61 | private static final long GRANULARITY_ONE_DAY_MS = 24 * 60 * 60 * 1000L; |
| 62 | // The time-to-live for the cached location |
| 63 | 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] | 64 | // The maximum number of times to try injecting a location |
| 65 | private static final int MAX_LOCATION_INJECTION_ATTEMPTS = 10; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 66 | |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 67 | // Used internally for mHandlerThread synchronization |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 68 | private final Object mLock = new Object(); |
| 69 | |
| 70 | private final Context mContext; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 71 | private final CarUserManagerHelper mCarUserManagerHelper; |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 72 | private int mTaskCount = 0; |
| 73 | private HandlerThread mHandlerThread; |
| 74 | private Handler mHandler; |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 75 | private CarPowerManager mCarPowerManager; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 76 | |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 77 | public CarLocationService( |
| 78 | Context context, |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 79 | CarUserManagerHelper carUserManagerHelper) { |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 80 | logd("constructed"); |
| 81 | mContext = context; |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 82 | mCarUserManagerHelper = carUserManagerHelper; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void init() { |
| 87 | logd("init"); |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 88 | IntentFilter filter = new IntentFilter(); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 89 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 90 | filter.addAction(LocationManager.MODE_CHANGED_ACTION); |
Soonil Nagarkar | c956316 | 2019-03-04 18:53:39 -0800 | [diff] [blame] | 91 | filter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION); |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 92 | mContext.registerReceiver(this, filter); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 93 | mCarPowerManager = CarLocalServices.createCarPowerManager(mContext); |
| 94 | if (mCarPowerManager != null) { // null case happens for testing. |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 95 | mCarPowerManager.setListenerWithCompletion(CarLocationService.this); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 96 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void release() { |
| 101 | logd("release"); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 102 | if (mCarPowerManager != null) { |
| 103 | mCarPowerManager.clearListener(); |
| 104 | } |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 105 | mContext.unregisterReceiver(this); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void dump(PrintWriter writer) { |
| 110 | writer.println(TAG); |
| 111 | writer.println("Context: " + mContext); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 112 | writer.println("MAX_LOCATION_INJECTION_ATTEMPTS: " + MAX_LOCATION_INJECTION_ATTEMPTS); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 116 | public void onStateChanged(int state, CompletableFuture<Void> future) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 117 | logd("onStateChanged: " + state); |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 118 | switch (state) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 119 | case CarPowerStateListener.SHUTDOWN_PREPARE: |
Gregory Clark | 5b9a9cd | 2018-09-10 13:21:22 -0700 | [diff] [blame] | 120 | asyncOperation(() -> { |
| 121 | storeLocation(); |
| 122 | // Notify the CarPowerManager that it may proceed to shutdown or suspend. |
Gregory Clark | 71ed672 | 2018-09-24 13:10:16 -0700 | [diff] [blame] | 123 | if (future != null) { |
| 124 | future.complete(null); |
| 125 | } |
Gregory Clark | 5b9a9cd | 2018-09-10 13:21:22 -0700 | [diff] [blame] | 126 | }); |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 127 | break; |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame^] | 128 | case CarPowerStateListener.SUSPEND_EXIT: |
| 129 | deleteCacheFile(); |
| 130 | if (future != null) { |
| 131 | future.complete(null); |
| 132 | } |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 133 | default: |
Gregory Clark | 5b9a9cd | 2018-09-10 13:21:22 -0700 | [diff] [blame] | 134 | // This service does not need to do any work for these events but should still |
| 135 | // notify the CarPowerManager that it may proceed. |
Gregory Clark | 71ed672 | 2018-09-24 13:10:16 -0700 | [diff] [blame] | 136 | if (future != null) { |
| 137 | future.complete(null); |
| 138 | } |
Serik Beketayev | 6892054 | 2018-09-06 13:29:58 -0700 | [diff] [blame] | 139 | break; |
| 140 | } |
Gregory Clark | 26fa601 | 2018-03-14 18:38:56 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Gregory Clark | 26fa601 | 2018-03-14 18:38:56 -0700 | [diff] [blame] | 143 | @Override |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 144 | public void onReceive(Context context, Intent intent) { |
Gregory Clark | 26fa601 | 2018-03-14 18:38:56 -0700 | [diff] [blame] | 145 | logd("onReceive " + intent); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 146 | String action = intent.getAction(); |
Gregory Clark | 6057550 | 2019-04-08 22:42:40 -0700 | [diff] [blame] | 147 | if (action == Intent.ACTION_USER_SWITCHED) { |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 148 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 149 | logd("USER_SWITCHED: " + userHandle); |
| 150 | if (mCarUserManagerHelper.isHeadlessSystemUser() |
| 151 | && userHandle > UserHandle.USER_SYSTEM) { |
| 152 | asyncOperation(() -> loadLocation()); |
| 153 | } |
| 154 | } else if (action == LocationManager.MODE_CHANGED_ACTION |
| 155 | && shouldCheckLocationPermissions()) { |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 156 | LocationManager locationManager = |
| 157 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 158 | boolean locationEnabled = locationManager.isLocationEnabled(); |
| 159 | logd("isLocationEnabled(): " + locationEnabled); |
| 160 | if (!locationEnabled) { |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame^] | 161 | deleteCacheFile(); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 162 | } |
Soonil Nagarkar | c956316 | 2019-03-04 18:53:39 -0800 | [diff] [blame] | 163 | } else if (action == LocationManager.PROVIDERS_CHANGED_ACTION |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 164 | && shouldCheckLocationPermissions()) { |
| 165 | LocationManager locationManager = |
| 166 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 167 | boolean gpsEnabled = |
| 168 | locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); |
| 169 | logd("isProviderEnabled('gps'): " + gpsEnabled); |
| 170 | if (!gpsEnabled) { |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame^] | 171 | deleteCacheFile(); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Tells whether or not we should check location permissions for the sake of deleting the |
| 178 | * location cache file when permissions are lacking. If the system user is headless but the |
| 179 | * current user is still the system user, then we should not respond to a lack of location |
| 180 | * permissions. |
| 181 | */ |
| 182 | private boolean shouldCheckLocationPermissions() { |
| 183 | return !(mCarUserManagerHelper.isHeadlessSystemUser() |
| 184 | && mCarUserManagerHelper.isCurrentProcessSystemUser()); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Gets the last known location from the LocationManager and store it in a file. |
| 189 | */ |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 190 | private void storeLocation() { |
| 191 | LocationManager locationManager = |
| 192 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 193 | Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
| 194 | if (location == null) { |
| 195 | logd("Not storing null location"); |
| 196 | } else { |
| 197 | logd("Storing location: " + location); |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 198 | AtomicFile atomicFile = new AtomicFile(getLocationCacheFile()); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 199 | FileOutputStream fos = null; |
| 200 | try { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 201 | fos = atomicFile.startWrite(); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 202 | try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(fos, "UTF-8"))) { |
| 203 | jsonWriter.beginObject(); |
| 204 | jsonWriter.name("provider").value(location.getProvider()); |
| 205 | jsonWriter.name("latitude").value(location.getLatitude()); |
| 206 | jsonWriter.name("longitude").value(location.getLongitude()); |
| 207 | if (location.hasAltitude()) { |
| 208 | jsonWriter.name("altitude").value(location.getAltitude()); |
| 209 | } |
| 210 | if (location.hasSpeed()) { |
| 211 | jsonWriter.name("speed").value(location.getSpeed()); |
| 212 | } |
| 213 | if (location.hasBearing()) { |
| 214 | jsonWriter.name("bearing").value(location.getBearing()); |
| 215 | } |
| 216 | if (location.hasAccuracy()) { |
| 217 | jsonWriter.name("accuracy").value(location.getAccuracy()); |
| 218 | } |
| 219 | if (location.hasVerticalAccuracy()) { |
| 220 | jsonWriter.name("verticalAccuracy").value( |
| 221 | location.getVerticalAccuracyMeters()); |
| 222 | } |
| 223 | if (location.hasSpeedAccuracy()) { |
| 224 | jsonWriter.name("speedAccuracy").value( |
| 225 | location.getSpeedAccuracyMetersPerSecond()); |
| 226 | } |
| 227 | if (location.hasBearingAccuracy()) { |
| 228 | jsonWriter.name("bearingAccuracy").value( |
| 229 | location.getBearingAccuracyDegrees()); |
| 230 | } |
| 231 | if (location.isFromMockProvider()) { |
| 232 | jsonWriter.name("isFromMockProvider").value(true); |
| 233 | } |
| 234 | long currentTime = location.getTime(); |
| 235 | // Round the time down to only be accurate within one day. |
| 236 | jsonWriter.name("captureTime").value( |
| 237 | currentTime - currentTime % GRANULARITY_ONE_DAY_MS); |
| 238 | jsonWriter.endObject(); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 239 | } |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 240 | atomicFile.finishWrite(fos); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 241 | } catch (IOException e) { |
| 242 | Log.e(TAG, "Unable to write to disk", e); |
| 243 | atomicFile.failWrite(fos); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Reads a previously stored location and attempts to inject it into the LocationManager. |
| 250 | */ |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 251 | private void loadLocation() { |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 252 | Location location = readLocationFromCacheFile(); |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 253 | logd("Read location from timestamp " + location.getTime()); |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 254 | long currentTime = System.currentTimeMillis(); |
| 255 | if (location.getTime() + TTL_THIRTY_DAYS_MS < currentTime) { |
| 256 | logd("Location expired."); |
Gregory Clark | 63a4ad2 | 2019-05-23 18:36:33 -0700 | [diff] [blame^] | 257 | deleteCacheFile(); |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 258 | } else { |
| 259 | location.setTime(currentTime); |
| 260 | long elapsedTime = SystemClock.elapsedRealtimeNanos(); |
| 261 | location.setElapsedRealtimeNanos(elapsedTime); |
| 262 | if (location.isComplete()) { |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 263 | injectLocation(location, 1); |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | private Location readLocationFromCacheFile() { |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 269 | Location location = new Location((String) null); |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 270 | AtomicFile atomicFile = new AtomicFile(getLocationCacheFile()); |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 271 | try (FileInputStream fis = atomicFile.openRead()) { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 272 | JsonReader reader = new JsonReader(new InputStreamReader(fis, "UTF-8")); |
| 273 | reader.beginObject(); |
| 274 | while (reader.hasNext()) { |
| 275 | String name = reader.nextName(); |
| 276 | if (name.equals("provider")) { |
| 277 | location.setProvider(reader.nextString()); |
| 278 | } else if (name.equals("latitude")) { |
| 279 | location.setLatitude(reader.nextDouble()); |
| 280 | } else if (name.equals("longitude")) { |
| 281 | location.setLongitude(reader.nextDouble()); |
| 282 | } else if (name.equals("altitude")) { |
| 283 | location.setAltitude(reader.nextDouble()); |
| 284 | } else if (name.equals("speed")) { |
| 285 | location.setSpeed((float) reader.nextDouble()); |
| 286 | } else if (name.equals("bearing")) { |
| 287 | location.setBearing((float) reader.nextDouble()); |
| 288 | } else if (name.equals("accuracy")) { |
| 289 | location.setAccuracy((float) reader.nextDouble()); |
| 290 | } else if (name.equals("verticalAccuracy")) { |
| 291 | location.setVerticalAccuracyMeters((float) reader.nextDouble()); |
| 292 | } else if (name.equals("speedAccuracy")) { |
| 293 | location.setSpeedAccuracyMetersPerSecond((float) reader.nextDouble()); |
| 294 | } else if (name.equals("bearingAccuracy")) { |
| 295 | location.setBearingAccuracyDegrees((float) reader.nextDouble()); |
| 296 | } else if (name.equals("isFromMockProvider")) { |
| 297 | location.setIsFromMockProvider(reader.nextBoolean()); |
Gregory Clark | e70eb5b | 2018-02-07 18:29:24 -0800 | [diff] [blame] | 298 | } else if (name.equals("captureTime")) { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 299 | location.setTime(reader.nextLong()); |
| 300 | } else { |
| 301 | reader.skipValue(); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 302 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 303 | } |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 304 | reader.endObject(); |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 305 | } catch (FileNotFoundException e) { |
| 306 | Log.d(TAG, "Location cache file not found."); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 307 | } catch (IOException e) { |
| 308 | Log.e(TAG, "Unable to read from disk", e); |
| 309 | } catch (NumberFormatException | IllegalStateException e) { |
| 310 | Log.e(TAG, "Unexpected format", e); |
| 311 | } |
Gregory Clark | 67259c2 | 2018-03-09 15:30:21 -0800 | [diff] [blame] | 312 | return location; |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 315 | private void deleteCacheFile() { |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 316 | boolean deleted = getLocationCacheFile().delete(); |
| 317 | logd("Deleted cache file: " + deleted); |
Gregory Clark | 29b7f0f | 2018-02-16 17:53:40 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 320 | /** |
| 321 | * Attempts to inject the location multiple times in case the LocationManager was not fully |
| 322 | * initialized or has not updated its handle to the current user yet. |
| 323 | */ |
| 324 | private void injectLocation(Location location, int attemptCount) { |
| 325 | LocationManager locationManager = |
| 326 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 327 | boolean success = locationManager.injectLocation(location); |
| 328 | logd("Injected location " + location + " with result " + success + " on attempt " |
| 329 | + attemptCount); |
| 330 | if (success) { |
| 331 | return; |
| 332 | } else if (attemptCount <= MAX_LOCATION_INJECTION_ATTEMPTS) { |
| 333 | asyncOperation(() -> { |
| 334 | injectLocation(location, attemptCount + 1); |
| 335 | }, 200 * attemptCount); |
| 336 | } else { |
| 337 | logd("No location injected."); |
| 338 | } |
| 339 | } |
| 340 | |
Gregory Clark | a440e81 | 2019-02-14 16:05:51 -0800 | [diff] [blame] | 341 | private File getLocationCacheFile() { |
| 342 | SystemInterface systemInterface = CarLocalServices.getService(SystemInterface.class); |
| 343 | File file = new File(systemInterface.getSystemCarDir(), FILENAME); |
| 344 | logd("File: " + file); |
| 345 | return file; |
| 346 | } |
| 347 | |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 348 | @VisibleForTesting |
| 349 | void asyncOperation(Runnable operation) { |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 350 | asyncOperation(operation, 0); |
| 351 | } |
| 352 | |
| 353 | private void asyncOperation(Runnable operation, long delayMillis) { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 354 | synchronized (mLock) { |
| 355 | // Create a new HandlerThread if this is the first task to queue. |
| 356 | if (++mTaskCount == 1) { |
| 357 | mHandlerThread = new HandlerThread("CarLocationServiceThread"); |
| 358 | mHandlerThread.start(); |
| 359 | mHandler = new Handler(mHandlerThread.getLooper()); |
| 360 | } |
| 361 | } |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 362 | mHandler.postDelayed(() -> { |
Gregory Clark | 3f9b56d | 2018-01-31 13:02:41 -0800 | [diff] [blame] | 363 | try { |
| 364 | operation.run(); |
| 365 | } finally { |
| 366 | synchronized (mLock) { |
| 367 | // Quit the thread when the task queue is empty. |
| 368 | if (--mTaskCount == 0) { |
| 369 | mHandler.getLooper().quit(); |
| 370 | mHandler = null; |
| 371 | mHandlerThread = null; |
| 372 | } |
| 373 | } |
| 374 | } |
Gregory Clark | a63ba02 | 2018-06-07 16:42:12 -0700 | [diff] [blame] | 375 | }, delayMillis); |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | private static void logd(String msg) { |
| 379 | if (DBG) { |
| 380 | Log.d(TAG, msg); |
| 381 | } |
| 382 | } |
Gregory Clark | d813606 | 2017-12-11 14:27:53 -0800 | [diff] [blame] | 383 | } |