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

Commit f4995497 authored by Hyundo Moon's avatar Hyundo Moon Committed by Gerrit Code Review
Browse files

Merge "Clear storage on bluetooth factory reset" into main

parents 2968117e d87f7e1f
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;
@@ -5863,6 +5869,10 @@ public class AdapterService extends Service {
            mBtCompanionManager.factoryReset();
        }

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

        return mNativeInterface.factoryReset();
    }

@@ -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);
        }
    }
}
+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;
@@ -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);
        }
    }
}