From 5bf193844ce12afcf5e330e82b4b7c6d89f693eb Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Wed, 3 Mar 2010 08:26:31 -0800 Subject: [PATCH] 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 --- src/com/android/exchange/SyncManager.java | 35 ++++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/com/android/exchange/SyncManager.java b/src/com/android/exchange/SyncManager.java index f9bdc9709..b367a7a10 100644 --- a/src/com/android/exchange/SyncManager.java +++ b/src/com/android/exchange/SyncManager.java @@ -649,16 +649,17 @@ public class SyncManager extends Service implements Runnable { CalendarSyncAdapter.CALENDAR_SELECTION, new String[] {account.mEmailAddress, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE}, null); - // Save its id and its sync events status - try { - if (c.moveToFirst()) { - mCalendarId = c.getLong(0); - mSyncEvents = c.getLong(1); + if (c != null) { + // Save its id and its sync events status + try { + if (c.moveToFirst()) { + mCalendarId = c.getLong(0); + mSyncEvents = c.getLong(1); + } + } finally { + c.close(); } - } finally { - c.close(); } - } @Override @@ -668,25 +669,25 @@ public class SyncManager extends Service implements Runnable { Cursor c = mResolver.query(Calendars.CONTENT_URI, new String[] {Calendars.SYNC_EVENTS}, Calendars._ID + "=?", new String[] {Long.toString(mCalendarId)}, null); + if (c == null) return; // Get its sync events; if it's changed, we've got work to do try { if (c.moveToFirst()) { long newSyncEvents = c.getLong(0); if (newSyncEvents != mSyncEvents) { // The user has changed sync state; let SyncManager know - android.accounts.Account account = - new android.accounts.Account(mAccountName, - Email.EXCHANGE_ACCOUNT_MANAGER_TYPE); + android.accounts.Account account = new android.accounts.Account( + mAccountName, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE); ContentResolver.setSyncAutomatically(account, Calendar.AUTHORITY, newSyncEvents != 0); if (newSyncEvents == 0) { // For some reason, CalendarProvider deletes all Events in this // case; this means that we have to reset our sync key - Mailbox mailbox = Mailbox.restoreMailboxOfType(INSTANCE, mAccountId, - Mailbox.TYPE_CALENDAR); + Mailbox mailbox = Mailbox.restoreMailboxOfType(INSTANCE, + mAccountId, Mailbox.TYPE_CALENDAR); EasSyncService service = new EasSyncService(INSTANCE, mailbox); - CalendarSyncAdapter adapter = - new CalendarSyncAdapter(mailbox, service); + CalendarSyncAdapter adapter = new CalendarSyncAdapter(mailbox, + service); try { adapter.setSyncKey("0", false); } catch (IOException e) { @@ -694,8 +695,8 @@ public class SyncManager extends Service implements Runnable { } ContentValues cv = new ContentValues(); cv.put(Mailbox.SYNC_KEY, "0"); - mResolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, - mailbox.mId), cv, null, null); + mResolver.update(ContentUris.withAppendedId( + Mailbox.CONTENT_URI, mailbox.mId), cv, null, null); // TODO Stop sync in progress?? }