2010-12-18 17:56:40 -05:00
|
|
|
package com.fsck.k9.mail.store;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteException;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.helper.Utility;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class LockableDatabase {
|
2010-12-18 17:56:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback interface for DB operations. Concept is similar to Spring
|
|
|
|
* HibernateCallback.
|
|
|
|
*
|
|
|
|
* @param <T>
|
|
|
|
* Return value type for {@link #doDbWork(SQLiteDatabase)}
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static interface DbCallback<T> {
|
2010-12-18 17:56:40 -05:00
|
|
|
/**
|
|
|
|
* @param db
|
|
|
|
* The locked database on which the work should occur. Never
|
|
|
|
* <code>null</code>.
|
|
|
|
* @return Any relevant data. Can be <code>null</code>.
|
|
|
|
* @throws WrappedException
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
*/
|
|
|
|
T doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static interface SchemaDefinition {
|
2010-12-18 17:56:40 -05:00
|
|
|
int getVersion();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param db Never <code>null</code>.
|
|
|
|
*/
|
|
|
|
void doDbUpgrade(SQLiteDatabase db);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Workaround exception wrapper used to keep the inner exception generated
|
|
|
|
* in a {@link DbCallback}.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected static class WrappedException extends RuntimeException {
|
2010-12-18 17:56:40 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 8184421232587399369L;
|
2010-12-24 13:55:05 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public WrappedException(final Exception cause) {
|
2010-12-18 17:56:40 -05:00
|
|
|
super(cause);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the DB on mount and close the DB on unmount
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private class StorageListener implements StorageManager.StorageListener {
|
2010-12-18 17:56:40 -05:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onUnmount(final String providerId) {
|
|
|
|
if (!providerId.equals(mStorageProviderId)) {
|
2010-12-18 17:56:40 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (K9.DEBUG) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.d(K9.LOG_TAG, "LockableDatabase: Closing DB " + uUid + " due to unmount event on StorageProvider: " + providerId);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
lockWrite();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
mDb.close();
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (UnavailableStorageException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.w(K9.LOG_TAG, "Unable to writelock on unmount", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onMount(final String providerId) {
|
|
|
|
if (!providerId.equals(mStorageProviderId)) {
|
2010-12-18 17:56:40 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (K9.DEBUG) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.d(K9.LOG_TAG, "LockableDatabase: Opening DB " + uUid + " due to mount event on StorageProvider: " + providerId);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
openOrCreateDataspace(mApplication);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (UnavailableStorageException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Unable to open DB on mount", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String mStorageProviderId;
|
|
|
|
|
|
|
|
private SQLiteDatabase mDb;
|
|
|
|
/**
|
|
|
|
* Reentrant read lock
|
|
|
|
*/
|
|
|
|
private final Lock mReadLock;
|
|
|
|
/**
|
|
|
|
* Reentrant write lock (if you lock it 2x from the same thread, you have to
|
|
|
|
* unlock it 2x to release it)
|
|
|
|
*/
|
|
|
|
private final Lock mWriteLock;
|
|
|
|
|
|
|
|
{
|
|
|
|
final ReadWriteLock lock = new ReentrantReadWriteLock(true);
|
|
|
|
mReadLock = lock.readLock();
|
|
|
|
mWriteLock = lock.writeLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private final StorageListener mStorageListener = new StorageListener();
|
|
|
|
|
|
|
|
private Application mApplication;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link ThreadLocal} to check whether a DB transaction is occuring in the
|
|
|
|
* current {@link Thread}.
|
|
|
|
*
|
|
|
|
* @see #execute(boolean, DbCallback)
|
|
|
|
*/
|
|
|
|
private ThreadLocal<Boolean> inTransaction = new ThreadLocal<Boolean>();
|
|
|
|
|
|
|
|
private SchemaDefinition mSchemaDefinition;
|
|
|
|
|
|
|
|
private String uUid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param application
|
|
|
|
* Never <code>null</code>.
|
|
|
|
* @param uUid
|
|
|
|
* Never <code>null</code>.
|
|
|
|
* @param schemaDefinition
|
|
|
|
* Never <code>null</code
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public LockableDatabase(final Application application, final String uUid, final SchemaDefinition schemaDefinition) {
|
2010-12-18 17:56:40 -05:00
|
|
|
this.mApplication = application;
|
|
|
|
this.uUid = uUid;
|
|
|
|
this.mSchemaDefinition = schemaDefinition;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setStorageProviderId(String mStorageProviderId) {
|
2010-12-18 17:56:40 -05:00
|
|
|
this.mStorageProviderId = mStorageProviderId;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getStorageProviderId() {
|
2010-12-18 17:56:40 -05:00
|
|
|
return mStorageProviderId;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private StorageManager getStorageManager() {
|
2010-12-18 17:56:40 -05:00
|
|
|
return StorageManager.getInstance(mApplication);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock the storage for shared operations (concurrent threads are allowed to
|
|
|
|
* run simultaneously).
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* You <strong>have to</strong> invoke {@link #unlockRead()} when you're
|
|
|
|
* done with the storage.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
* If storage can't be locked because it is not available
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void lockRead() throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
mReadLock.lock();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
getStorageManager().lockProvider(mStorageProviderId);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (UnavailableStorageException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
mReadLock.unlock();
|
|
|
|
throw e;
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (RuntimeException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
mReadLock.unlock();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void unlockRead() {
|
2010-12-18 17:56:40 -05:00
|
|
|
getStorageManager().unlockProvider(mStorageProviderId);
|
|
|
|
mReadLock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock the storage for exclusive access (other threads aren't allowed to
|
|
|
|
* run simultaneously)
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* You <strong>have to</strong> invoke {@link #unlockWrite()} when you're
|
|
|
|
* done with the storage.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
* If storage can't be locked because it is not available.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void lockWrite() throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
lockWrite(mStorageProviderId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock the storage for exclusive access (other threads aren't allowed to
|
|
|
|
* run simultaneously)
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* You <strong>have to</strong> invoke {@link #unlockWrite()} when you're
|
|
|
|
* done with the storage.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param providerId
|
|
|
|
* Never <code>null</code>.
|
|
|
|
*
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
* If storage can't be locked because it is not available.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void lockWrite(final String providerId) throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
mWriteLock.lock();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
getStorageManager().lockProvider(providerId);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (UnavailableStorageException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
mWriteLock.unlock();
|
|
|
|
throw e;
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (RuntimeException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
mWriteLock.unlock();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void unlockWrite() {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite(mStorageProviderId);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void unlockWrite(final String providerId) {
|
2010-12-18 17:56:40 -05:00
|
|
|
getStorageManager().unlockProvider(providerId);
|
|
|
|
mWriteLock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a DB callback in a shared context (doesn't prevent concurrent
|
|
|
|
* shared executions), taking care of locking the DB storage.
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* Can be instructed to start a transaction if none is currently active in
|
|
|
|
* the current thread. Callback will participe in any active transaction (no
|
|
|
|
* inner transaction created).
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param transactional
|
|
|
|
* <code>true</code> the callback must be executed in a
|
|
|
|
* transactional context.
|
|
|
|
* @param callback
|
|
|
|
* Never <code>null</code>.
|
|
|
|
*
|
|
|
|
* @param <T>
|
|
|
|
* @return Whatever {@link DbCallback#doDbWork(SQLiteDatabase)} returns.
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public <T> T execute(final boolean transactional, final DbCallback<T> callback) throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
lockRead();
|
|
|
|
final boolean doTransaction = transactional && inTransaction.get() == null;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
final boolean debug = K9.DEBUG;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (doTransaction) {
|
2010-12-18 17:56:40 -05:00
|
|
|
inTransaction.set(Boolean.TRUE);
|
|
|
|
mDb.beginTransaction();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
final T result = callback.doDbWork(mDb);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (doTransaction) {
|
2010-12-18 17:56:40 -05:00
|
|
|
mDb.setTransactionSuccessful();
|
|
|
|
}
|
|
|
|
return result;
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
|
|
|
if (doTransaction) {
|
2010-12-18 17:56:40 -05:00
|
|
|
final long begin;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (debug) {
|
2010-12-18 17:56:40 -05:00
|
|
|
begin = System.currentTimeMillis();
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-12-18 17:56:40 -05:00
|
|
|
begin = 0l;
|
|
|
|
}
|
|
|
|
// not doing endTransaction in the same 'finally' block of unlockRead() because endTransaction() may throw an exception
|
|
|
|
mDb.endTransaction();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (debug) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.v(K9.LOG_TAG, "LockableDatabase: Transaction ended, took " + Long.toString(System.currentTimeMillis() - begin) + "ms / " + new Exception().getStackTrace()[1].toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
|
|
|
if (doTransaction) {
|
2010-12-18 17:56:40 -05:00
|
|
|
inTransaction.set(null);
|
|
|
|
}
|
|
|
|
unlockRead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param newProviderId
|
|
|
|
* Never <code>null</code>.
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void switchProvider(final String newProviderId) throws MessagingException {
|
|
|
|
if (newProviderId.equals(mStorageProviderId)) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.v(K9.LOG_TAG, "LockableDatabase: Ignoring provider switch request as they are equal: " + newProviderId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String oldProviderId = mStorageProviderId;
|
|
|
|
lockWrite(oldProviderId);
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
lockWrite(newProviderId);
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
mDb.close();
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (Exception e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.i(K9.LOG_TAG, "Unable to close DB on local store migration", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
final StorageManager storageManager = getStorageManager();
|
|
|
|
|
|
|
|
// create new path
|
|
|
|
prepareStorage(newProviderId);
|
|
|
|
|
|
|
|
// move all database files
|
|
|
|
Utility.moveRecursive(storageManager.getDatabase(uUid, oldProviderId), storageManager.getDatabase(uUid, newProviderId));
|
|
|
|
// move all attachment files
|
|
|
|
Utility.moveRecursive(storageManager.getAttachmentDirectory(uUid, oldProviderId), storageManager.getAttachmentDirectory(uUid, newProviderId));
|
|
|
|
|
|
|
|
mStorageProviderId = newProviderId;
|
|
|
|
|
|
|
|
// re-initialize this class with the new Uri
|
|
|
|
openOrCreateDataspace(mApplication);
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite(newProviderId);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite(oldProviderId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void open() throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
lockWrite();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
openOrCreateDataspace(mApplication);
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite();
|
|
|
|
}
|
|
|
|
StorageManager.getInstance(mApplication).addListener(mStorageListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param application
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void openOrCreateDataspace(final Application application) throws UnavailableStorageException {
|
2010-12-24 13:55:05 -05:00
|
|
|
|
2010-12-18 17:56:40 -05:00
|
|
|
lockWrite();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
final File databaseFile = prepareStorage(mStorageProviderId);
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
mDb = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (SQLiteException e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
// try to gracefully handle DB corruption - see issue 2537
|
|
|
|
Log.w(K9.LOG_TAG, "Unable to open DB " + databaseFile + " - removing file and retrying", e);
|
|
|
|
databaseFile.delete();
|
|
|
|
mDb = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mDb.getVersion() != mSchemaDefinition.getVersion()) {
|
2010-12-18 17:56:40 -05:00
|
|
|
mSchemaDefinition.doDbUpgrade(mDb);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param providerId
|
|
|
|
* Never <code>null</code>.
|
|
|
|
* @return DB file.
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected File prepareStorage(final String providerId) throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
final StorageManager storageManager = getStorageManager();
|
2010-12-24 13:55:05 -05:00
|
|
|
|
2010-12-18 17:56:40 -05:00
|
|
|
final File databaseFile;
|
|
|
|
final File databaseParentDir;
|
|
|
|
databaseFile = storageManager.getDatabase(uUid, providerId);
|
|
|
|
databaseParentDir = databaseFile.getParentFile();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (databaseParentDir.isFile()) {
|
2011-09-27 00:03:13 -04:00
|
|
|
// should be safe to unconditionally delete clashing file: user is not supposed to mess with our directory
|
2010-12-18 17:56:40 -05:00
|
|
|
databaseParentDir.delete();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!databaseParentDir.exists()) {
|
|
|
|
if (!databaseParentDir.mkdirs()) {
|
2010-12-18 17:56:40 -05:00
|
|
|
// Android seems to be unmounting the storage...
|
|
|
|
throw new UnavailableStorageException("Unable to access: " + databaseParentDir);
|
|
|
|
}
|
|
|
|
Utility.touchFile(databaseParentDir, ".nomedia");
|
|
|
|
}
|
2010-12-24 13:55:05 -05:00
|
|
|
|
2010-12-18 17:56:40 -05:00
|
|
|
final File attachmentDir;
|
|
|
|
final File attachmentParentDir;
|
|
|
|
attachmentDir = storageManager
|
|
|
|
.getAttachmentDirectory(uUid, providerId);
|
|
|
|
attachmentParentDir = attachmentDir.getParentFile();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!attachmentParentDir.exists()) {
|
2010-12-18 17:56:40 -05:00
|
|
|
attachmentParentDir.mkdirs();
|
|
|
|
Utility.touchFile(attachmentParentDir, ".nomedia");
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!attachmentDir.exists()) {
|
2010-12-18 17:56:40 -05:00
|
|
|
attachmentDir.mkdirs();
|
|
|
|
}
|
|
|
|
return databaseFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the backing database.
|
2010-12-24 13:55:05 -05:00
|
|
|
*
|
2010-12-18 17:56:40 -05:00
|
|
|
* @throws UnavailableStorageException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void delete() throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
delete(false);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void recreate() throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
delete(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param recreate
|
|
|
|
* <code>true</code> if the DB should be recreated after delete
|
|
|
|
* @throws UnavailableStorageException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private void delete(final boolean recreate) throws UnavailableStorageException {
|
2010-12-18 17:56:40 -05:00
|
|
|
lockWrite();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
mDb.close();
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (Exception e) {
|
2010-12-24 13:55:05 -05:00
|
|
|
|
2010-12-18 17:56:40 -05:00
|
|
|
}
|
|
|
|
final StorageManager storageManager = getStorageManager();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
final File attachmentDirectory = storageManager.getAttachmentDirectory(uUid, mStorageProviderId);
|
|
|
|
final File[] attachments = attachmentDirectory.listFiles();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (File attachment : attachments) {
|
|
|
|
if (attachment.exists()) {
|
2010-12-18 17:56:40 -05:00
|
|
|
attachment.delete();
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (attachmentDirectory.exists()) {
|
2010-12-18 17:56:40 -05:00
|
|
|
attachmentDirectory.delete();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (Exception e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-12-18 17:56:40 -05:00
|
|
|
storageManager.getDatabase(uUid, mStorageProviderId).delete();
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (Exception e) {
|
2010-12-18 17:56:40 -05:00
|
|
|
Log.i(K9.LOG_TAG, "LockableDatabase: delete(): Unable to delete backing DB file", e);
|
|
|
|
}
|
2010-12-24 13:55:05 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (recreate) {
|
2010-12-18 17:56:40 -05:00
|
|
|
openOrCreateDataspace(mApplication);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-12-18 17:56:40 -05:00
|
|
|
// stop waiting for mount/unmount events
|
|
|
|
getStorageManager().removeListener(mStorageListener);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} finally {
|
2010-12-18 17:56:40 -05:00
|
|
|
unlockWrite();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|