diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 32f757986..28a6ea731 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -333,12 +333,6 @@ > - - - mNotificationMap; + private final HashMap mNotificationMap; private ContentObserver mAccountObserver; /** * Suspend notifications for this account. If {@link Account#NO_ACCOUNT}, no @@ -107,7 +108,7 @@ public class NotificationController { mGenericSenderIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_contact_picture); mClock = clock; - mNotificationMap = new HashMap(); + mNotificationMap = new HashMap(); } /** Singleton access */ @@ -208,7 +209,6 @@ public class NotificationController { @Override public void run() { ContentResolver resolver = mContext.getContentResolver(); - HashMap table; if (!watch) { unregisterMessageNotification(Account.ACCOUNT_ID_COMBINED_VIEW); if (mAccountObserver != null) { @@ -300,10 +300,9 @@ public class NotificationController { c.close(); } } else { - MessageData data = mNotificationMap.get(accountId); - if (data != null) return; // we're already observing; nothing to do + ContentObserver obs = mNotificationMap.get(accountId); + if (obs != null) return; // we're already observing; nothing to do - data = new MessageData(); Mailbox mailbox = Mailbox.restoreMailboxOfType(mContext, accountId, Mailbox.TYPE_INBOX); if (mailbox == null) { Log.w(Logging.LOG_TAG, "Could not load INBOX for account id: " + accountId); @@ -312,10 +311,9 @@ public class NotificationController { ContentObserver observer = new MessageContentObserver( sNotificationHandler, mContext, mailbox.mId, accountId); resolver.registerContentObserver(Message.NOTIFIER_URI, true, observer); - data.mObserver = observer; - mNotificationMap.put(accountId, data); + mNotificationMap.put(accountId, observer); // Now, ping the observer for any initial notifications - data.mObserver.onChange(true); + observer.onChange(true); } } @@ -331,15 +329,13 @@ public class NotificationController { ContentResolver resolver = mContext.getContentResolver(); if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) { // cancel all existing message observers - for (MessageData data : mNotificationMap.values()) { - ContentObserver observer = data.mObserver; + for (ContentObserver observer : mNotificationMap.values()) { resolver.unregisterContentObserver(observer); } mNotificationMap.clear(); } else { - MessageData data = mNotificationMap.remove(accountId); - if (data != null) { - ContentObserver observer = data.mObserver; + ContentObserver observer = mNotificationMap.remove(accountId); + if (observer != null) { resolver.unregisterContentObserver(observer); } } @@ -592,14 +588,15 @@ public class NotificationController { return; } - MessageData data = sInstance.mNotificationMap.get(mAccountId); - if (data == null) { + ContentObserver observer = sInstance.mNotificationMap.get(mAccountId); + if (observer == null) { // notification for a mailbox that we aren't observing; this should not happen Log.e(Logging.LOG_TAG, "Received notifiaction when observer data was null"); return; } - long oldMessageId = data.mNotifiedMessageId; - int oldMessageCount = data.mNotifiedMessageCount; + Account account = Account.restoreAccountWithId(mContext, mAccountId); + long oldMessageId = account.mNotifiedMessageId; + int oldMessageCount = account.mNotifiedMessageCount; ContentResolver resolver = mContext.getContentResolver(); long lastSeenMessageId = Utility.getFirstRowLong( @@ -646,8 +643,12 @@ public class NotificationController { sInstance.getNewMessageNotificationId(mAccountId), n); } } - data.mNotifiedMessageId = newMessageId; - data.mNotifiedMessageCount = newMessageCount; + // Save away the new values + ContentValues cv = new ContentValues(); + cv.put(AccountColumns.NOTIFIED_MESSAGE_ID, newMessageId); + cv.put(AccountColumns.NOTIFIED_MESSAGE_COUNT, newMessageCount); + resolver.update(ContentUris.withAppendedId(Account.CONTENT_URI, mAccountId), cv, + null, null); } finally { c.close(); } @@ -710,16 +711,6 @@ public class NotificationController { } } - /** Information about the message(s) we're notifying the user about. */ - private static class MessageData { - /** The database observer */ - ContentObserver mObserver; - /** Message ID used in the user notification */ - long mNotifiedMessageId; - /** Message count used in the user notification */ - int mNotifiedMessageCount; - } - /** * Thread to handle all notification actions through its own {@link Looper}. */ diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index c4df0987b..27b248784 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -159,8 +159,9 @@ public class EmailProvider extends ContentProvider { // Version 25: Added QuickResponse table // Version 26: Update IMAP accounts to add FLAG_SUPPORTS_SEARCH flag // Version 27: Add protocolSearchInfo to Message table + // Version 28: Add notifiedMessageId and notifiedMessageCount to Account - public static final int DATABASE_VERSION = 27; + public static final int DATABASE_VERSION = 28; // Any changes to the database format *must* include update-in-place code. // Original version: 2 @@ -612,7 +613,9 @@ public class EmailProvider extends ContentProvider { + AccountColumns.SECURITY_FLAGS + " integer, " + AccountColumns.SECURITY_SYNC_KEY + " text, " + AccountColumns.SIGNATURE + " text, " - + AccountColumns.POLICY_KEY + " integer" + + AccountColumns.POLICY_KEY + " integer, " + + AccountColumns.NOTIFIED_MESSAGE_ID + " integer, " + + AccountColumns.NOTIFIED_MESSAGE_COUNT + " integer" + ");"; db.execSQL("create table " + Account.TABLE_NAME + s); // Deleting an account deletes associated Mailboxes and HostAuth's @@ -1330,6 +1333,18 @@ public class EmailProvider extends ContentProvider { } oldVersion = 27; } + if (oldVersion == 27) { + try { + db.execSQL("alter table " + Account.TABLE_NAME + + " add column " + Account.NOTIFIED_MESSAGE_ID + " integer;"); + db.execSQL("alter table " + Account.TABLE_NAME + + " add column " + Account.NOTIFIED_MESSAGE_COUNT + " integer;"); + } catch (SQLException e) { + // Shouldn't be needed unless we're debugging and interrupt the process + Log.w(TAG, "Exception upgrading EmailProvider.db from 27 to 27 " + e); + } + oldVersion = 28; + } } @Override diff --git a/src/com/android/email/service/NotificationService.java b/src/com/android/email/service/NotificationService.java deleted file mode 100644 index caba9bf60..000000000 --- a/src/com/android/email/service/NotificationService.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.email.service; - -import com.android.email.NotificationController; - -import android.app.Service; -import android.content.Intent; -import android.os.IBinder; - -public class NotificationService extends Service { - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - NotificationController.getInstance(this).watchForMessages(true); - return START_STICKY; - } - - @Override - public void onDestroy() { - NotificationController.getInstance(this).watchForMessages(false); - } - - @Override - public IBinder onBind(Intent intent) { - return null; - } -} \ No newline at end of file