Harden SyncManager against CalendarProvider query results

* Unlike EmailProvider, CalendarProvider can return null from queries.
* Put checks for null around code using the result of query to
  prevent NPE's

Change-Id: Ia9e8e2f44406dce07dcb2ffa40c22933ec2d4f07
This commit is contained in:
Marc Blank 2010-03-03 08:26:31 -08:00
parent 4476b2a1da
commit 5bf193844c

View File

@ -649,6 +649,7 @@ public class SyncManager extends Service implements Runnable {
CalendarSyncAdapter.CALENDAR_SELECTION, CalendarSyncAdapter.CALENDAR_SELECTION,
new String[] {account.mEmailAddress, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE}, new String[] {account.mEmailAddress, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE},
null); null);
if (c != null) {
// Save its id and its sync events status // Save its id and its sync events status
try { try {
if (c.moveToFirst()) { if (c.moveToFirst()) {
@ -658,7 +659,7 @@ public class SyncManager extends Service implements Runnable {
} finally { } finally {
c.close(); c.close();
} }
}
} }
@Override @Override
@ -668,25 +669,25 @@ public class SyncManager extends Service implements Runnable {
Cursor c = mResolver.query(Calendars.CONTENT_URI, Cursor c = mResolver.query(Calendars.CONTENT_URI,
new String[] {Calendars.SYNC_EVENTS}, Calendars._ID + "=?", new String[] {Calendars.SYNC_EVENTS}, Calendars._ID + "=?",
new String[] {Long.toString(mCalendarId)}, null); new String[] {Long.toString(mCalendarId)}, null);
if (c == null) return;
// Get its sync events; if it's changed, we've got work to do // Get its sync events; if it's changed, we've got work to do
try { try {
if (c.moveToFirst()) { if (c.moveToFirst()) {
long newSyncEvents = c.getLong(0); long newSyncEvents = c.getLong(0);
if (newSyncEvents != mSyncEvents) { if (newSyncEvents != mSyncEvents) {
// The user has changed sync state; let SyncManager know // The user has changed sync state; let SyncManager know
android.accounts.Account account = android.accounts.Account account = new android.accounts.Account(
new android.accounts.Account(mAccountName, mAccountName, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
ContentResolver.setSyncAutomatically(account, Calendar.AUTHORITY, ContentResolver.setSyncAutomatically(account, Calendar.AUTHORITY,
newSyncEvents != 0); newSyncEvents != 0);
if (newSyncEvents == 0) { if (newSyncEvents == 0) {
// For some reason, CalendarProvider deletes all Events in this // For some reason, CalendarProvider deletes all Events in this
// case; this means that we have to reset our sync key // case; this means that we have to reset our sync key
Mailbox mailbox = Mailbox.restoreMailboxOfType(INSTANCE, mAccountId, Mailbox mailbox = Mailbox.restoreMailboxOfType(INSTANCE,
Mailbox.TYPE_CALENDAR); mAccountId, Mailbox.TYPE_CALENDAR);
EasSyncService service = new EasSyncService(INSTANCE, mailbox); EasSyncService service = new EasSyncService(INSTANCE, mailbox);
CalendarSyncAdapter adapter = CalendarSyncAdapter adapter = new CalendarSyncAdapter(mailbox,
new CalendarSyncAdapter(mailbox, service); service);
try { try {
adapter.setSyncKey("0", false); adapter.setSyncKey("0", false);
} catch (IOException e) { } catch (IOException e) {
@ -694,8 +695,8 @@ public class SyncManager extends Service implements Runnable {
} }
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();
cv.put(Mailbox.SYNC_KEY, "0"); cv.put(Mailbox.SYNC_KEY, "0");
mResolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, mResolver.update(ContentUris.withAppendedId(
mailbox.mId), cv, null, null); Mailbox.CONTENT_URI, mailbox.mId), cv, null, null);
// TODO Stop sync in progress?? // TODO Stop sync in progress??
} }