Fix bug #1997284 re: single default account

* Fixed the bug and uncommented the relevant tests in ProviderTests
This commit is contained in:
Marc Blank 2009-07-22 15:55:45 -07:00
parent 9e2c6bd5f2
commit 531ae9d25f
3 changed files with 70 additions and 13 deletions

View File

@ -17,6 +17,7 @@
package com.android.email.provider;
import com.android.email.R;
import com.android.exchange.EmailContent.AccountColumns;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
@ -1173,6 +1174,34 @@ public abstract class EmailContent {
return id;
}
/**
* Override update to enforce a single default account, and do it atomically
*/
public int update(Context context, ContentValues cv) {
if (cv.containsKey(AccountColumns.IS_DEFAULT) &&
cv.getAsBoolean(AccountColumns.IS_DEFAULT)) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ContentValues cv1 = new ContentValues();
cv1.put(AccountColumns.IS_DEFAULT, 0);
// Clear the default flag in all accounts
ops.add(ContentProviderOperation.newUpdate(CONTENT_URI).withValues(cv1).build());
// Update this account
ops.add(ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(CONTENT_URI, mId))
.withValues(cv).build());
try {
context.getContentResolver().applyBatch(EmailProvider.EMAIL_AUTHORITY, ops);
return 1;
} catch (RemoteException e) {
// There is nothing to be done here; fail by returning 0
} catch (OperationApplicationException e) {
// There is nothing to be done here; fail by returning 0
}
return 0;
}
return super.update(context, cv);
}
/*
* Override this so that we can store the HostAuth's first and link them to the Account
* (non-Javadoc)

View File

@ -17,6 +17,7 @@ package com.android.exchange;
import com.android.email.R;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.EmailContent.AccountColumns;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
@ -1164,7 +1165,35 @@ public abstract class EmailContent {
}
return acct;
}
/**
* Override update to enforce a single default account, and do it atomically
*/
public int update(Context context, ContentValues cv) {
if (cv.containsKey(AccountColumns.IS_DEFAULT) &&
cv.getAsBoolean(AccountColumns.IS_DEFAULT)) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ContentValues cv1 = new ContentValues();
cv1.put(AccountColumns.IS_DEFAULT, 0);
// Clear the default flag in all accounts
ops.add(ContentProviderOperation.newUpdate(CONTENT_URI).withValues(cv1).build());
// Update this account
ops.add(ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(CONTENT_URI, mId))
.withValues(cv).build());
try {
context.getContentResolver().applyBatch(EmailProvider.EMAIL_AUTHORITY, ops);
return 1;
} catch (RemoteException e) {
// There is nothing to be done here; fail by returning 0
} catch (OperationApplicationException e) {
// There is nothing to be done here; fail by returning 0
}
return 0;
}
return super.update(context, cv);
}
/*
* Override this so that we can store the HostAuth's first and link them to the Account
* (non-Javadoc)

View File

@ -681,28 +681,27 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertEquals(account1Id, defaultAccountId);
// TODO: Reenable these when the single-default-account logic is fixed
// updateIsDefault(account2, true);
// defaultAccountId = Account.getDefaultAccountId(mMockContext);
// assertEquals(account2Id, defaultAccountId);
//
// updateIsDefault(account3, true);
// defaultAccountId = Account.getDefaultAccountId(mMockContext);
// assertEquals(account3Id, defaultAccountId);
updateIsDefault(account2, true);
defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertEquals(account2Id, defaultAccountId);
updateIsDefault(account3, true);
defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertEquals(account3Id, defaultAccountId);
// Now delete a non-default account and confirm no change
Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id);
mMockContext.getContentResolver().delete(uri, null, null);
// defaultAccountId = Account.getDefaultAccountId(mMockContext);
// assertEquals(account3Id, defaultAccountId);
defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertEquals(account3Id, defaultAccountId);
// Now confirm deleting the default account and it switches to another one
uri = ContentUris.withAppendedId(Account.CONTENT_URI, account3Id);
mMockContext.getContentResolver().delete(uri, null, null);
// defaultAccountId = Account.getDefaultAccountId(mMockContext);
// assertEquals(account2Id, defaultAccountId);
defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertEquals(account2Id, defaultAccountId);
// Now delete the final account and confirm there are no default accounts again
uri = ContentUris.withAppendedId(Account.CONTENT_URI, account2Id);