Implement a resyncMailbox Method

Deletes all messages in a mailbox, resets the sync key and requests a sync.

Only supports Mail mailboxes for now.

Change-Id: Iab8c051631f38687c02a22cb80ce4f50c2d9c6f1
This commit is contained in:
Alon Albert 2013-09-19 08:22:12 -07:00
parent e3a4a1b25d
commit 29d886ced7
2 changed files with 61 additions and 0 deletions

View File

@ -748,6 +748,8 @@ public abstract class EmailContent {
public static final String PER_ACCOUNT_FAVORITE_SELECTION =
ACCOUNT_KEY_SELECTION + " AND " + ALL_FAVORITE_SELECTION;
public static final String MAILBOX_SELECTION = MAILBOX_KEY + "=?";
// _id field is in AbstractContent
public String mDisplayName;
public long mTimeStamp;

View File

@ -17,16 +17,21 @@
package com.android.emailcommon.provider;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import com.android.emailcommon.Logging;
@ -35,6 +40,8 @@ import com.android.emailcommon.provider.EmailContent.MailboxColumns;
import com.android.emailcommon.utility.Utility;
import com.android.mail.utils.LogUtils;
import java.util.ArrayList;
public class Mailbox extends EmailContent implements MailboxColumns, Parcelable {
/**
* Sync extras key when syncing a mailbox to specify which mailbox to sync.
@ -831,4 +838,56 @@ public class Mailbox extends EmailContent implements MailboxColumns, Parcelable
return EmailContent.AUTHORITY;
}
}
public static void resyncMailbox(
final ContentResolver cr,
final android.accounts.Account account,
final long mailboxId) {
final Cursor cursor = cr.query(Mailbox.CONTENT_URI,
new String[]{
Mailbox.TYPE,
Mailbox.SERVER_ID,
},
Mailbox.RECORD_ID + "=?",
new String[] {String.valueOf(mailboxId)},
null);
if (cursor == null || cursor.getCount() == 0) {
LogUtils.w(Logging.LOG_TAG, "Mailbox %d not found", mailboxId);
return;
}
try {
cursor.moveToFirst();
final int type = cursor.getInt(0);
if (type >= TYPE_NOT_EMAIL) {
throw new IllegalArgumentException(
String.format("Mailbox %d is not an Email mailbox", mailboxId));
}
final String serverId = cursor.getString(1);
if (TextUtils.isEmpty(serverId)) {
throw new IllegalArgumentException(
String.format("Mailbox %d has no server id", mailboxId));
}
final ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(Message.CONTENT_URI)
.withSelection(Message.MAILBOX_SELECTION,
new String[]{String.valueOf(mailboxId)})
.build());
ops.add(ContentProviderOperation.newUpdate(
ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId))
.withValue(Mailbox.SYNC_KEY, "0").build());
cr.applyBatch(AUTHORITY, ops);
final Bundle extras = new Bundle();
extras.putLong(SYNC_EXTRA_MAILBOX_ID, mailboxId);
extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
ContentResolver.requestSync(account, AUTHORITY, extras);
} catch (RemoteException e) {
LogUtils.w(Logging.LOG_TAG, e, "Failed to wipe mailbox %d", mailboxId);
} catch (OperationApplicationException e) {
LogUtils.w(Logging.LOG_TAG, e, "Failed to wipe mailbox %d", mailboxId);
} finally {
cursor.close();
}
}
}