Skip to content

Commit 53e8767

Browse files
committed
fix: make USB probing and eject flow safe
1 parent 1a5c8e7 commit 53e8767

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎app/src/main/java/com/drivehub/kamera/dashcam/DashcamStorageManager.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,20 +434,25 @@ private static boolean runWriteProbe(File dir) {
434434

435435
/** Returns null when the probe wrote and read back, otherwise why it did not. */
436436
private static String writeProbeError(File dir) {
437-
File probe = new File(dir, WRITE_PROBE_FILE_NAME);
437+
File probe = null;
438438
try {
439+
// resolve() can run concurrently from the settings screen and the recording loop.
440+
// A fixed probe name lets one caller delete the other's file and creates a false
441+
// WRITE_TEST_FAILED result, so every invocation needs its own temporary file.
442+
probe = File.createTempFile(WRITE_PROBE_FILE_NAME + ".", ".tmp", dir);
439443
try (FileOutputStream out = new FileOutputStream(probe)) {
440444
out.write("dashcam".getBytes());
441445
out.getFD().sync();
442446
}
443447
boolean ok = probe.isFile() && probe.length() > 0;
444-
// noinspection ResultOfMethodCallIgnored
445-
probe.delete();
446448
return ok ? null : "file missing or empty after write";
447449
} catch (Throwable t) {
448-
// noinspection ResultOfMethodCallIgnored
449-
probe.delete();
450450
return String.valueOf(t);
451+
} finally {
452+
if (probe != null) {
453+
// noinspection ResultOfMethodCallIgnored
454+
probe.delete();
455+
}
451456
}
452457
}
453458

‎app/src/main/java/com/drivehub/kamera/dashcam/RecordingService.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,15 @@ public int onStartCommand(Intent intent, int flags, int startId) {
203203
// Decide on the worker thread: resolve() touches the filesystem for every target
204204
// except INTERNAL_ONLY. That was harmless while the preference was pinned to
205205
// internal storage; now that it is honoured, this would be main-thread IO.
206-
usbEjectInProgress = true;
207-
shutdownRecordingServiceWithoutStopSelf();
208206
new Thread(() -> {
209207
boolean usingUsb = activeBaseIsUsb || DashcamStorageManager.resolve(this).usingUsb;
210208
if (!usingUsb) {
211-
usbEjectInProgress = false;
212209
broadcastUsbEjectReady(
213210
false, R.string.settings_dashcam_storage_eject_unavailable_message);
214-
stopForeground(true);
215-
stopSelf();
216211
return;
217212
}
213+
usbEjectInProgress = true;
214+
shutdownRecordingServiceWithoutStopSelf();
218215
boolean safeToRemove = awaitShutdownQuiescence();
219216
broadcastUsbEjectReady(
220217
safeToRemove,

0 commit comments

Comments
 (0)