Loading android/app/src/com/android/bluetooth/btservice/AdapterService.java +52 −0 Original line number Diff line number Diff line Loading @@ -171,6 +171,12 @@ import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.time.Duration; import java.util.ArrayDeque; import java.util.ArrayList; Loading Loading @@ -5863,6 +5869,10 @@ public class AdapterService extends Service { mBtCompanionManager.factoryReset(); } if (Flags.gattClearCacheOnFactoryReset()) { clearStorage(); } return mNativeInterface.factoryReset(); } Loading Loading @@ -6953,4 +6963,46 @@ public class AdapterService extends Service { mPhonePolicy.onUuidsDiscovered(device, uuids); } } /** Clear storage */ void clearStorage() { deleteDirectoryContents("/data/misc/bluedroid/"); deleteDirectoryContents("/data/misc/bluetooth/"); } private void deleteDirectoryContents(String dirPath) { Path directoryPath = Paths.get(dirPath); try { Files.walkFileTree( directoryPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException ex) throws IOException { if (ex != null) { Log.e(TAG, "Error happened while removing contents. ", ex); } if (!dir.equals(directoryPath)) { try { Files.delete(dir); } catch (Exception e) { Log.e(TAG, "Error happened while removing directory: ", e); } } return FileVisitResult.CONTINUE; } }); Log.i(TAG, "deleteDirectoryContents() completed. Path: " + dirPath); } catch (Exception e) { Log.e(TAG, "Error happened while removing contents: ", e); } } } android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +40 −0 Original line number Diff line number Diff line Loading @@ -101,6 +101,9 @@ import platform.test.runner.parameterized.Parameters; import java.io.FileDescriptor; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.List; Loading Loading @@ -1031,4 +1034,41 @@ public class AdapterServiceTest { mAdapterService.dump(fd, writer, new String[] {"--proto-bin"}); mAdapterService.dump(fd, writer, new String[] {"random", "arguments"}); } @Test @EnableFlags(Flags.FLAG_GATT_CLEAR_CACHE_ON_FACTORY_RESET) public void testClearStorage() throws Exception { // clearStorage should remove all files under /data/misc/bluetooth/ && /data/misc/bluedroid/ final Path testCachePath = Paths.get("/data/misc/bluetooth/gatt_cache_a475b9a23d72"); final Path testHashPath = Paths.get("/data/misc/bluetooth/gatt_hash_400D017CB2563A6FB62A2DC4C2AEFD6F"); final Path randomFileUnderBluedroidPath = Paths.get("/data/misc/bluedroid/random_test_file.txt"); final Path randomFileUnderBluetoothPath = Paths.get("/data/misc/bluetooth/random_test_file.txt"); try { Files.createFile(testCachePath); Files.createFile(testHashPath); Files.createFile(randomFileUnderBluedroidPath); Files.createFile(randomFileUnderBluetoothPath); assertThat(Files.exists(testCachePath)).isTrue(); assertThat(Files.exists(testHashPath)).isTrue(); assertThat(Files.exists(randomFileUnderBluedroidPath)).isTrue(); assertThat(Files.exists(randomFileUnderBluetoothPath)).isTrue(); mAdapterService.clearStorage(); assertThat(Files.exists(testCachePath)).isFalse(); assertThat(Files.exists(testHashPath)).isFalse(); assertThat(Files.exists(randomFileUnderBluedroidPath)).isFalse(); assertThat(Files.exists(randomFileUnderBluetoothPath)).isFalse(); } finally { Files.deleteIfExists(testCachePath); Files.deleteIfExists(testHashPath); Files.deleteIfExists(randomFileUnderBluedroidPath); Files.deleteIfExists(randomFileUnderBluetoothPath); } } } Loading
android/app/src/com/android/bluetooth/btservice/AdapterService.java +52 −0 Original line number Diff line number Diff line Loading @@ -171,6 +171,12 @@ import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.time.Duration; import java.util.ArrayDeque; import java.util.ArrayList; Loading Loading @@ -5863,6 +5869,10 @@ public class AdapterService extends Service { mBtCompanionManager.factoryReset(); } if (Flags.gattClearCacheOnFactoryReset()) { clearStorage(); } return mNativeInterface.factoryReset(); } Loading Loading @@ -6953,4 +6963,46 @@ public class AdapterService extends Service { mPhonePolicy.onUuidsDiscovered(device, uuids); } } /** Clear storage */ void clearStorage() { deleteDirectoryContents("/data/misc/bluedroid/"); deleteDirectoryContents("/data/misc/bluetooth/"); } private void deleteDirectoryContents(String dirPath) { Path directoryPath = Paths.get(dirPath); try { Files.walkFileTree( directoryPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException ex) throws IOException { if (ex != null) { Log.e(TAG, "Error happened while removing contents. ", ex); } if (!dir.equals(directoryPath)) { try { Files.delete(dir); } catch (Exception e) { Log.e(TAG, "Error happened while removing directory: ", e); } } return FileVisitResult.CONTINUE; } }); Log.i(TAG, "deleteDirectoryContents() completed. Path: " + dirPath); } catch (Exception e) { Log.e(TAG, "Error happened while removing contents: ", e); } } }
android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +40 −0 Original line number Diff line number Diff line Loading @@ -101,6 +101,9 @@ import platform.test.runner.parameterized.Parameters; import java.io.FileDescriptor; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.List; Loading Loading @@ -1031,4 +1034,41 @@ public class AdapterServiceTest { mAdapterService.dump(fd, writer, new String[] {"--proto-bin"}); mAdapterService.dump(fd, writer, new String[] {"random", "arguments"}); } @Test @EnableFlags(Flags.FLAG_GATT_CLEAR_CACHE_ON_FACTORY_RESET) public void testClearStorage() throws Exception { // clearStorage should remove all files under /data/misc/bluetooth/ && /data/misc/bluedroid/ final Path testCachePath = Paths.get("/data/misc/bluetooth/gatt_cache_a475b9a23d72"); final Path testHashPath = Paths.get("/data/misc/bluetooth/gatt_hash_400D017CB2563A6FB62A2DC4C2AEFD6F"); final Path randomFileUnderBluedroidPath = Paths.get("/data/misc/bluedroid/random_test_file.txt"); final Path randomFileUnderBluetoothPath = Paths.get("/data/misc/bluetooth/random_test_file.txt"); try { Files.createFile(testCachePath); Files.createFile(testHashPath); Files.createFile(randomFileUnderBluedroidPath); Files.createFile(randomFileUnderBluetoothPath); assertThat(Files.exists(testCachePath)).isTrue(); assertThat(Files.exists(testHashPath)).isTrue(); assertThat(Files.exists(randomFileUnderBluedroidPath)).isTrue(); assertThat(Files.exists(randomFileUnderBluetoothPath)).isTrue(); mAdapterService.clearStorage(); assertThat(Files.exists(testCachePath)).isFalse(); assertThat(Files.exists(testHashPath)).isFalse(); assertThat(Files.exists(randomFileUnderBluedroidPath)).isFalse(); assertThat(Files.exists(randomFileUnderBluetoothPath)).isFalse(); } finally { Files.deleteIfExists(testCachePath); Files.deleteIfExists(testHashPath); Files.deleteIfExists(randomFileUnderBluedroidPath); Files.deleteIfExists(randomFileUnderBluetoothPath); } } }