Add an API for deleting databases.
Add SQLiteDatabase.deleteDatabase to delete a database and all
of its auxiliary files.
This method is also used by Context.deleteDatabase and resolves issues
where auxiliary files would get left behind.
Bug: 5972489
Change-Id: I3633b9b4978972a1d240ac0a9861420ecf989149
diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java
index 515658f..2f3dc06 100644
--- a/core/java/android/database/sqlite/SQLiteDatabase.java
+++ b/core/java/android/database/sqlite/SQLiteDatabase.java
@@ -36,6 +36,7 @@
import dalvik.system.CloseGuard;
import java.io.File;
+import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -678,6 +679,40 @@
}
/**
+ * Deletes a database including its journal file and other auxiliary files
+ * that may have been created by the database engine.
+ *
+ * @param file The database file path.
+ * @return True if the database was successfully deleted.
+ */
+ public static boolean deleteDatabase(File file) {
+ if (file == null) {
+ throw new IllegalArgumentException("file must not be null");
+ }
+
+ boolean deleted = false;
+ deleted |= file.delete();
+ deleted |= new File(file.getPath() + "-journal").delete();
+ deleted |= new File(file.getPath() + "-shm").delete();
+ deleted |= new File(file.getPath() + "-wal").delete();
+
+ File dir = file.getParentFile();
+ if (dir != null) {
+ final String prefix = file.getName() + "-mj";
+ final FileFilter filter = new FileFilter() {
+ @Override
+ public boolean accept(File candidate) {
+ return candidate.getName().startsWith(prefix);
+ }
+ };
+ for (File masterJournal : dir.listFiles(filter)) {
+ deleted |= masterJournal.delete();
+ }
+ }
+ return deleted;
+ }
+
+ /**
* Reopens the database in read-write mode.
* If the database is already read-write, does nothing.
*