1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-25 09:08:49 -05:00

Fixed issue 1205: Attachment storage location setting now takes effect immediatly. LocalStore is also better at handling when attachment are stored in a mix of internal and external storage (migration state after a setting change)

This commit is contained in:
Bao-Long Nguyen-Trong 2010-02-13 19:07:10 +00:00
parent f2f3b4263d
commit e435d9d616
2 changed files with 1109 additions and 1069 deletions

File diff suppressed because it is too large Load Diff

View File

@ -38,7 +38,7 @@ public class LocalStore extends Store implements Serializable
private static final int DB_VERSION = 33;
private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED, Flag.X_DESTROYED, Flag.SEEN };
private Account mAccount;
private boolean mStoreAttachmentsOnSdCard;
private String mPath;
private SQLiteDatabase mDb;
private File mInternalAttachmentsDir = null;
@ -70,23 +70,6 @@ public class LocalStore extends Store implements Serializable
{
mApplication = application;
//Store should be passed their store
//but let's not break the interface now
for (Account account : Preferences.getPreferences(mApplication).getAccounts())
{
if (_uri.equals(account.getLocalStoreUri()))
{
mAccount = account;
break;
}
}
if (mAccount == null)
{
//Should not happend
throw new IllegalArgumentException("No account found: uri=" + _uri);
}
URI uri = null;
try
{
@ -124,14 +107,24 @@ public class LocalStore extends Store implements Serializable
String externalAttachmentsPath = "/sdcard" + mPath.substring("//data".length());
mExternalAttachmentsDir = new File(externalAttachmentsPath + "_att");
if (mAccount.isStoreAttachmentOnSdCard()
&& !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
Account account = null;
for (Account aAccount : Preferences.getPreferences(mApplication).getAccounts())
{
if (!mExternalAttachmentsDir.exists())
if (_uri.equals(aAccount.getLocalStoreUri()))
{
mExternalAttachmentsDir.mkdirs();
account = aAccount;
break;
}
}
if (account == null)
{
//Should not happend
throw new IllegalArgumentException("No account found: uri=" + _uri);
}
else
{
this.setStoreAttachmentsOnSdCard(account.isStoreAttachmentOnSdCard());
}
mDb = SQLiteDatabase.openOrCreateDatabase(mPath, null);
if (mDb.getVersion() != DB_VERSION)
@ -269,7 +262,7 @@ public class LocalStore extends Store implements Serializable
long attachmentLength = 0;
attachmentLength =+ getSize(mInternalAttachmentsDir);
if (usesExternalAttachmentDir())
if (isExternalAttachmentsDirReady())
{
attachmentLength =+ getSize(mExternalAttachmentsDir);
}
@ -429,7 +422,7 @@ public class LocalStore extends Store implements Serializable
}
delete(mInternalAttachmentsDir);
if (usesExternalAttachmentDir())
if (isExternalAttachmentsDirReady())
{
delete(mExternalAttachmentsDir);
}
@ -486,7 +479,7 @@ public class LocalStore extends Store implements Serializable
mDb.update("attachments", cv, null, null);
}
pruneCachedAttachments(force, mInternalAttachmentsDir);
if (usesExternalAttachmentDir())
if (isExternalAttachmentsDirReady())
{
pruneCachedAttachments(force, mExternalAttachmentsDir);
}
@ -2624,35 +2617,70 @@ public class LocalStore extends Store implements Serializable
private File getAttachmentsDir()
{
if (usesExternalAttachmentDir())
if (mStoreAttachmentsOnSdCard)
{
if (!mExternalAttachmentsDir.exists())
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
mExternalAttachmentsDir.mkdirs();
if (!mExternalAttachmentsDir.exists())
{
mExternalAttachmentsDir.mkdirs();
}
return mExternalAttachmentsDir;
}
else
{
throw new IllegalStateException("SD card not mounted");
}
return mExternalAttachmentsDir;
}
else {
return mInternalAttachmentsDir;
}
}
private boolean usesExternalAttachmentDir()
public boolean isStoreAttachmentsOnSdCard()
{
if (mAccount.isStoreAttachmentOnSdCard()) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
throw new IllegalStateException("SDCard not mounted");
return mStoreAttachmentsOnSdCard;
}
}
else
{
return true;
}
public void setStoreAttachmentsOnSdCard(boolean storeAttachmentsOnSdCard)
{
this.mStoreAttachmentsOnSdCard = storeAttachmentsOnSdCard;
if (K9.DEBUG)
{
Log.v(K9.LOG_TAG, "mStoreAttachmentsOnSdCard: " + mStoreAttachmentsOnSdCard);
}
else
if (storeAttachmentsOnSdCard
&& !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
&& !mExternalAttachmentsDir.exists())
{
mExternalAttachmentsDir.mkdirs();
}
}
/**
* Checks if the dir is eardy to use and if we need it.
*
* @exception IllegalStateException If the dir is not ready (SD card not mounted)
* and we expect it to be (mStoreAttachmentsOnSdCard is true)
*/
private boolean isExternalAttachmentsDirReady()
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
if (!mExternalAttachmentsDir.exists())
{
mExternalAttachmentsDir.mkdirs();
}
return true;
}
else if (!mStoreAttachmentsOnSdCard)
{
return false;
}
else
{
throw new IllegalStateException("SD card not mounted");
}
}
}