diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java b/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java index 62c11718a..4fbf2d2e6 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java @@ -86,7 +86,7 @@ Stream listFilesRecursively(File directory) { return Stream.empty(); } File[] files = directory.listFiles(); - if (files == null){ + if (files == null) { return Stream.empty(); } Stream directories = Stream.of(files).filter(File::isDirectory); @@ -94,9 +94,9 @@ Stream listFilesRecursively(File directory) { return Stream.concat(plainFiles, directories.flatMap(this::listFilesRecursively)); } - Stream listDirectories(File directory){ + Stream listDirectories(File directory) { File[] files = directory.listFiles(); - if (files == null){ + if (files == null) { return Stream.empty(); } return Stream.of(files).filter(File::isDirectory); diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java b/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java index f5de29378..188d6e5bf 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java @@ -44,7 +44,7 @@ public StartTypeAwareSpanStorage( this.rootDir = rootDir; this.spanDir = fileUtils.getSpansDirectory(rootDir); this.uniqueId = UUID.randomUUID().toString(); - //if new id then background span directory with old ids can be deleted + // if new id then background span directory with old ids can be deleted cleanUpUnsentBackgroundSpan(); } @@ -67,7 +67,9 @@ public Stream getPendingFiles() { } private boolean isAppForeground() { - return (visibleScreenTracker.getCurrentlyVisibleScreen() != null && !visibleScreenTracker.getCurrentlyVisibleScreen().equals("unknown")) || visibleScreenTracker.getPreviouslyVisibleScreen() != null; + return (visibleScreenTracker.getCurrentlyVisibleScreen() != null + && !visibleScreenTracker.getCurrentlyVisibleScreen().equals("unknown")) + || visibleScreenTracker.getPreviouslyVisibleScreen() != null; } private void moveBackgroundSpanToPendingSpan() { @@ -98,14 +100,10 @@ public File provideSpanFile() { private File getSpanFile() { if (!isAppForeground()) { - Log.d( - LOG_TAG, - "Creating background span " + uniqueId); + Log.d(LOG_TAG, "Creating background span " + uniqueId); return getCurrentSessionBackgroundFile(); } - Log.d( - LOG_TAG, - "Creating foreground span " + uniqueId); + Log.d(LOG_TAG, "Creating foreground span " + uniqueId); return spanDir; } @@ -119,17 +117,20 @@ private File ensureDirExist(File pathToReturn) { return rootDir; } - private void cleanUpUnsentBackgroundSpan(){ - fileUtils.listDirectories(new File(spanDir, "background/")) - .filter(file -> { - String path = file.getPath(); - String pathId = path.substring(path.lastIndexOf("/") + 1); - return !pathId.equals(uniqueId); - }) - .forEach(file -> { - Log.d(SplunkRum.LOG_TAG, "Cleaning up " + file.getPath()); - fileUtils.listFilesRecursively(file).forEach(fileUtils::safeDelete); - fileUtils.safeDelete(file); - }); + private void cleanUpUnsentBackgroundSpan() { + fileUtils + .listDirectories(new File(spanDir, "background/")) + .filter( + file -> { + String path = file.getPath(); + String pathId = path.substring(path.lastIndexOf("/") + 1); + return !pathId.equals(uniqueId); + }) + .forEach( + file -> { + Log.d(SplunkRum.LOG_TAG, "Cleaning up " + file.getPath()); + fileUtils.listFilesRecursively(file).forEach(fileUtils::safeDelete); + fileUtils.safeDelete(file); + }); } } diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java index b84e6b632..75930386e 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java @@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -31,7 +30,6 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -52,7 +50,7 @@ void setup() { } @Test - void constructor_onNewId_shouldCleanOldBackgroundFiles(){ + void constructor_onNewId_shouldCleanOldBackgroundFiles() { File file = mock(); when(file.getPath()).thenReturn("files/spans/background/123"); when(fileUtils.listDirectories(any())).thenReturn(Stream.of(file)); @@ -73,7 +71,8 @@ void getPendingFiles_givenInBackground_shouldReturnForegoundOnlySpan() { } @Test - void getPendingFiles_givenPrevouslyInBackground_shouldMoveBackgroundSpanToForegroundSpanForSending() { + void + getPendingFiles_givenPrevouslyInBackground_shouldMoveBackgroundSpanToForegroundSpanForSending() { when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn("MainActivity"); when(visibleScreenTracker.getCurrentlyVisibleScreen()).thenReturn("MainActivity");