Remove NotificationService; use Account columns for data storage

Bug: 5023662
Change-Id: I84df3b474dd6320327851003af985144cc16348e
This commit is contained in:
Marc Blank 2011-07-19 18:19:59 -07:00
parent c6df1d605f
commit aca9426581
7 changed files with 70 additions and 100 deletions

View File

@ -333,12 +333,6 @@
>
</service>
<service
android:name=".service.NotificationService"
android:enabled="false"
>
</service>
<!--Required stanza to register the PopImapAuthenticatorService with AccountManager -->
<service
android:name=".service.PopImapAuthenticatorService"

View File

@ -109,6 +109,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
public String mSecuritySyncKey;
public String mSignature;
public long mPolicyKey;
public long mNotifiedMessageId;
public int mNotifiedMessageCount;
// Convenience for creating/working with an account
public transient HostAuth mHostAuthRecv;
@ -135,6 +137,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
public static final int CONTENT_SECURITY_SYNC_KEY_COLUMN = 15;
public static final int CONTENT_SIGNATURE_COLUMN = 16;
public static final int CONTENT_POLICY_KEY = 17;
public static final int CONTENT_NOTIFIED_MESSAGE_ID = 18;
public static final int CONTENT_NOTIFIED_MESSAGE_COUNT = 19;
public static final String[] CONTENT_PROJECTION = new String[] {
RECORD_ID, AccountColumns.DISPLAY_NAME,
@ -144,7 +148,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
AccountColumns.COMPATIBILITY_UUID, AccountColumns.SENDER_NAME,
AccountColumns.RINGTONE_URI, AccountColumns.PROTOCOL_VERSION,
AccountColumns.NEW_MESSAGE_COUNT, AccountColumns.SECURITY_SYNC_KEY,
AccountColumns.SIGNATURE, AccountColumns.POLICY_KEY
AccountColumns.SIGNATURE, AccountColumns.POLICY_KEY,
AccountColumns.NOTIFIED_MESSAGE_ID, AccountColumns.NOTIFIED_MESSAGE_COUNT
};
public static final int CONTENT_MAILBOX_TYPE_COLUMN = 1;
@ -249,6 +254,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
mSecuritySyncKey = cursor.getString(CONTENT_SECURITY_SYNC_KEY_COLUMN);
mSignature = cursor.getString(CONTENT_SIGNATURE_COLUMN);
mPolicyKey = cursor.getLong(CONTENT_POLICY_KEY);
mNotifiedMessageId = cursor.getLong(CONTENT_NOTIFIED_MESSAGE_ID);
mNotifiedMessageCount = cursor.getInt(CONTENT_NOTIFIED_MESSAGE_COUNT);
}
private long getId(Uri u) {
@ -830,6 +837,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
values.put(AccountColumns.SECURITY_SYNC_KEY, mSecuritySyncKey);
values.put(AccountColumns.SIGNATURE, mSignature);
values.put(AccountColumns.POLICY_KEY, mPolicyKey);
values.put(AccountColumns.NOTIFIED_MESSAGE_ID, mNotifiedMessageId);
values.put(AccountColumns.NOTIFIED_MESSAGE_COUNT, mNotifiedMessageCount);
return values;
}
@ -881,6 +890,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
dest.writeString(mSecuritySyncKey);
dest.writeString(mSignature);
dest.writeLong(mPolicyKey);
dest.writeLong(mNotifiedMessageId);
dest.writeInt(mNotifiedMessageCount);
if (mHostAuthRecv != null) {
dest.writeByte((byte)1);
@ -920,6 +931,8 @@ public final class Account extends EmailContent implements AccountColumns, Parce
mSecuritySyncKey = in.readString();
mSignature = in.readString();
mPolicyKey = in.readLong();
mNotifiedMessageId = in.readLong();
mNotifiedMessageCount = in.readInt();
mHostAuthRecv = null;
if (in.readByte() == 1) {

View File

@ -1316,6 +1316,10 @@ public abstract class EmailContent {
public static final String SIGNATURE = "signature";
// A foreign key into the Policy table
public static final String POLICY_KEY = "policyKey";
// The last notified message id
public static final String NOTIFIED_MESSAGE_ID = "notifiedMessageId";
// The most recent notified message count
public static final String NOTIFIED_MESSAGE_COUNT = "notifiedMessageCount";
}
public interface QuickResponseColumns {

View File

@ -16,19 +16,6 @@
package com.android.email;
import com.android.email.activity.ShortcutPicker;
import com.android.email.activity.MessageCompose;
import com.android.email.service.AttachmentDownloadService;
import com.android.email.service.MailService;
import com.android.email.service.NotificationService;
import com.android.email.widget.WidgetConfiguration;
import com.android.emailcommon.Logging;
import com.android.emailcommon.TempDirectory;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.service.EmailServiceProxy;
import com.android.emailcommon.utility.EmailAsyncTask;
import com.android.emailcommon.utility.Utility;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
@ -37,6 +24,18 @@ import android.content.pm.PackageManager;
import android.database.Cursor;
import android.util.Log;
import com.android.email.activity.MessageCompose;
import com.android.email.activity.ShortcutPicker;
import com.android.email.service.AttachmentDownloadService;
import com.android.email.service.MailService;
import com.android.email.widget.WidgetConfiguration;
import com.android.emailcommon.Logging;
import com.android.emailcommon.TempDirectory;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.service.EmailServiceProxy;
import com.android.emailcommon.utility.EmailAsyncTask;
import com.android.emailcommon.utility.Utility;
public class Email extends Application {
/**
* If this is enabled there will be additional logging information sent to
@ -162,11 +161,6 @@ public class Email extends Application {
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(context, NotificationService.class),
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
if (enabled && pm.getComponentEnabledSetting(
new ComponentName(context, MailService.class)) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
@ -179,7 +173,7 @@ public class Email extends Application {
// Start/stop the various services depending on whether there are any accounts
startOrStopService(enabled, context, new Intent(context, AttachmentDownloadService.class));
startOrStopService(enabled, context, new Intent(context, NotificationService.class));
NotificationController.getInstance(context).watchForMessages(enabled);
}
/**

View File

@ -22,6 +22,7 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
@ -35,7 +36,6 @@ import android.os.Looper;
import android.os.Process;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import com.android.email.activity.ContactStatusLoader;
@ -46,6 +46,7 @@ import com.android.emailcommon.Logging;
import com.android.emailcommon.mail.Address;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
import com.android.emailcommon.provider.EmailContent.Attachment;
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
import com.android.emailcommon.provider.EmailContent.Message;
@ -88,7 +89,7 @@ public class NotificationController {
// for now since the assumption is that we only ever look for changes in an account's
// INBOX. We should adjust our logic to use the mailbox ID instead.
/** Maps account id to the message data */
private final HashMap<Long, MessageData> mNotificationMap;
private final HashMap<Long, ContentObserver> 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<Long, MessageData>();
mNotificationMap = new HashMap<Long, ContentObserver>();
}
/** Singleton access */
@ -208,7 +209,6 @@ public class NotificationController {
@Override
public void run() {
ContentResolver resolver = mContext.getContentResolver();
HashMap<Long, long[]> 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}.
*/

View File

@ -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

View File

@ -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;
}
}