Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d87f7e1f authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Clear storage on bluetooth factory reset

This CL makes BT factory reset removes all files/directories under
- /data/misc/bluedroid/
- /data/misc/bluetooth/

Bug: 359583862
Bug: 373284699
Test: atest AdapterServiceTest
Test: Manual, clicked 'Reset Bluetooth & Wifi' and checked
      the cache/hash files are deleted.
Change-Id: I969ae986e1afd32e91528424dc27a3016a0e8b58
parent c1bc65e3
Loading
Loading
Loading
Loading
+52 −0
Original line number Diff line number Diff line
@@ -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;
@@ -5880,6 +5886,10 @@ public class AdapterService extends Service {
            mBtCompanionManager.factoryReset();
        }

        if (Flags.gattClearCacheOnFactoryReset()) {
            clearStorage();
        }

        return mNativeInterface.factoryReset();
    }

@@ -6970,4 +6980,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);
        }
    }
}
+40 −0
Original line number Diff line number Diff line
@@ -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;
@@ -1033,4 +1036,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);
        }
    }
}