1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-26 01:28:50 -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

View File

@ -5,6 +5,7 @@ import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
@ -635,6 +636,17 @@ public class Account implements Serializable
saveIdentities(preferences.getPreferences(), editor); saveIdentities(preferences.getPreferences(), editor);
editor.commit(); editor.commit();
try
{
LocalStore localStore = (LocalStore)Store.getInstance(mLocalStoreUri, K9.app);
localStore.setStoreAttachmentsOnSdCard(mStoreAttachmentsOnSdCard);
}
catch (MessagingException e)
{
//Should not happen
Log.w(K9.LOG_TAG, null, e);
}
} }
public String toString() public String toString()

View File

@ -38,7 +38,7 @@ public class LocalStore extends Store implements Serializable
private static final int DB_VERSION = 33; private static final int DB_VERSION = 33;
private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED, Flag.X_DESTROYED, Flag.SEEN }; private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED, Flag.X_DESTROYED, Flag.SEEN };
private Account mAccount; private boolean mStoreAttachmentsOnSdCard;
private String mPath; private String mPath;
private SQLiteDatabase mDb; private SQLiteDatabase mDb;
private File mInternalAttachmentsDir = null; private File mInternalAttachmentsDir = null;
@ -70,23 +70,6 @@ public class LocalStore extends Store implements Serializable
{ {
mApplication = application; 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; URI uri = null;
try try
{ {
@ -124,14 +107,24 @@ public class LocalStore extends Store implements Serializable
String externalAttachmentsPath = "/sdcard" + mPath.substring("//data".length()); String externalAttachmentsPath = "/sdcard" + mPath.substring("//data".length());
mExternalAttachmentsDir = new File(externalAttachmentsPath + "_att"); mExternalAttachmentsDir = new File(externalAttachmentsPath + "_att");
if (mAccount.isStoreAttachmentOnSdCard() Account account = null;
&& !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 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); mDb = SQLiteDatabase.openOrCreateDatabase(mPath, null);
if (mDb.getVersion() != DB_VERSION) if (mDb.getVersion() != DB_VERSION)
@ -269,7 +262,7 @@ public class LocalStore extends Store implements Serializable
long attachmentLength = 0; long attachmentLength = 0;
attachmentLength =+ getSize(mInternalAttachmentsDir); attachmentLength =+ getSize(mInternalAttachmentsDir);
if (usesExternalAttachmentDir()) if (isExternalAttachmentsDirReady())
{ {
attachmentLength =+ getSize(mExternalAttachmentsDir); attachmentLength =+ getSize(mExternalAttachmentsDir);
} }
@ -429,7 +422,7 @@ public class LocalStore extends Store implements Serializable
} }
delete(mInternalAttachmentsDir); delete(mInternalAttachmentsDir);
if (usesExternalAttachmentDir()) if (isExternalAttachmentsDirReady())
{ {
delete(mExternalAttachmentsDir); delete(mExternalAttachmentsDir);
} }
@ -486,7 +479,7 @@ public class LocalStore extends Store implements Serializable
mDb.update("attachments", cv, null, null); mDb.update("attachments", cv, null, null);
} }
pruneCachedAttachments(force, mInternalAttachmentsDir); pruneCachedAttachments(force, mInternalAttachmentsDir);
if (usesExternalAttachmentDir()) if (isExternalAttachmentsDirReady())
{ {
pruneCachedAttachments(force, mExternalAttachmentsDir); pruneCachedAttachments(force, mExternalAttachmentsDir);
} }
@ -2624,7 +2617,9 @@ public class LocalStore extends Store implements Serializable
private File getAttachmentsDir() private File getAttachmentsDir()
{ {
if (usesExternalAttachmentDir()) if (mStoreAttachmentsOnSdCard)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{ {
if (!mExternalAttachmentsDir.exists()) if (!mExternalAttachmentsDir.exists())
{ {
@ -2632,27 +2627,60 @@ public class LocalStore extends Store implements Serializable
} }
return mExternalAttachmentsDir; return mExternalAttachmentsDir;
} }
else
{
throw new IllegalStateException("SD card not mounted");
}
}
else { else {
return mInternalAttachmentsDir; return mInternalAttachmentsDir;
} }
} }
private boolean usesExternalAttachmentDir() public boolean isStoreAttachmentsOnSdCard()
{ {
if (mAccount.isStoreAttachmentOnSdCard()) { return mStoreAttachmentsOnSdCard;
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
throw new IllegalStateException("SDCard not mounted");
} }
else
public void setStoreAttachmentsOnSdCard(boolean storeAttachmentsOnSdCard)
{ {
this.mStoreAttachmentsOnSdCard = storeAttachmentsOnSdCard;
if (K9.DEBUG)
{
Log.v(K9.LOG_TAG, "mStoreAttachmentsOnSdCard: " + mStoreAttachmentsOnSdCard);
}
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; return true;
} }
} else if (!mStoreAttachmentsOnSdCard)
else
{ {
return false; return false;
} }
else
{
throw new IllegalStateException("SD card not mounted");
} }
}
} }