Use update notification intent to trigger notifications

This ensures the SuppressNotificationReceiver object quiesces the notification while we're viewing the folder

b/11789666

Change-Id: I98f388844b29458e7ea7deee398f7d8536b1919c
This commit is contained in:
Tony Mantler 2013-11-21 12:27:14 -08:00
parent 6b9c648105
commit 8196f82ce2
4 changed files with 102 additions and 49 deletions

View File

@ -682,6 +682,11 @@
<action android:name="com.android.mail.action.CLEAR_NEW_MAIL_NOTIFICATIONS" />
<data android:scheme="content" />
</intent-filter>
<intent-filter>
<action android:name="com.android.mail.action.update_notification"
android:priority="-10"/>
<data android:mimeType="@string/application_mime_type" />
</intent-filter>
</service>
<service android:name="com.android.mail.NotificationActionIntentService"

View File

@ -18,6 +18,7 @@ package com.android.email;
import android.content.Intent;
import com.android.mail.MailIntentService;
import com.android.mail.providers.UIProvider;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils;
@ -35,6 +36,10 @@ public class EmailIntentService extends MailIntentService {
protected void onHandleIntent(final Intent intent) {
super.onHandleIntent(intent);
if (UIProvider.ACTION_UPDATE_NOTIFICATION.equals(intent.getAction())) {
NotificationController.handleUpdateNotificationIntent(this, intent);
}
LogUtils.v(LOG_TAG, "Handling intent %s", intent);
}
}

View File

@ -592,14 +592,73 @@ public class NotificationController {
private static void refreshNotificationsForAccountInternal(final Context context,
final long accountId) {
final Uri accountUri = EmailProvider.uiUri("uiaccount", accountId);
final ContentResolver contentResolver = context.getContentResolver();
final Cursor accountCursor = contentResolver.query(
EmailProvider.uiUri("uiaccount", accountId), UIProvider.ACCOUNTS_PROJECTION,
null, null, null);
final Cursor mailboxCursor = contentResolver.query(
ContentUris.withAppendedId(EmailContent.MAILBOX_NOTIFICATION_URI, accountId),
null, null, null, null);
try {
while (mailboxCursor.moveToNext()) {
final long mailboxId =
mailboxCursor.getLong(EmailContent.NOTIFICATION_MAILBOX_ID_COLUMN);
if (mailboxId == 0) continue;
final int unseenCount = mailboxCursor.getInt(
EmailContent.NOTIFICATION_MAILBOX_UNSEEN_COUNT_COLUMN);
final int unreadCount;
// If nothing is unseen, clear the notification
if (unseenCount == 0) {
unreadCount = 0;
} else {
unreadCount = mailboxCursor.getInt(
EmailContent.NOTIFICATION_MAILBOX_UNREAD_COUNT_COLUMN);
}
final Uri folderUri = EmailProvider.uiUri("uifolder", mailboxId);
LogUtils.d(LOG_TAG, "Changes to account " + accountId + ", folder: "
+ mailboxId + ", unreadCount: " + unreadCount + ", unseenCount: "
+ unseenCount);
final Intent intent = new Intent(UIProvider.ACTION_UPDATE_NOTIFICATION);
intent.setPackage(context.getPackageName());
intent.setType(EmailProvider.EMAIL_APP_MIME_TYPE);
intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_ACCOUNT, accountUri);
intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_FOLDER, folderUri);
intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNREAD_COUNT,
unreadCount);
intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNSEEN_COUNT,
unseenCount);
context.sendOrderedBroadcast(intent, null);
}
} finally {
mailboxCursor.close();
}
}
public static void handleUpdateNotificationIntent(Context context, Intent intent) {
final Uri accountUri =
intent.getParcelableExtra(UIProvider.UpdateNotificationExtras.EXTRA_ACCOUNT);
final Uri folderUri =
intent.getParcelableExtra(UIProvider.UpdateNotificationExtras.EXTRA_FOLDER);
final int unreadCount = intent.getIntExtra(
UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNREAD_COUNT, 0);
final int unseenCount = intent.getIntExtra(
UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNSEEN_COUNT, 0);
final ContentResolver contentResolver = context.getContentResolver();
final Cursor accountCursor = contentResolver.query(accountUri,
UIProvider.ACCOUNTS_PROJECTION, null, null, null);
if (accountCursor == null) {
LogUtils.e(LOG_TAG, "Null account cursor for account id %d", accountId);
LogUtils.e(LOG_TAG, "Null account cursor for account " + accountUri);
return;
}
@ -613,58 +672,37 @@ public class NotificationController {
}
if (account == null) {
LogUtils.d(LOG_TAG, "Tried to create a notification for a missing account %d",
accountId);
LogUtils.d(LOG_TAG, "Tried to create a notification for a missing account "
+ accountUri);
return;
}
final Cursor mailboxCursor = contentResolver.query(
ContentUris.withAppendedId(EmailContent.MAILBOX_NOTIFICATION_URI, accountId),
null, null, null, null);
final Cursor folderCursor = contentResolver.query(folderUri, UIProvider.FOLDERS_PROJECTION,
null, null, null);
if (folderCursor == null) {
LogUtils.e(LOG_TAG, "Null folder cursor for account " + accountUri + ", mailbox "
+ folderUri);
return;
}
Folder folder = null;
try {
while (mailboxCursor.moveToNext()) {
final long mailboxId =
mailboxCursor.getLong(EmailContent.NOTIFICATION_MAILBOX_ID_COLUMN);
if (mailboxId == 0) continue;
final int unreadCount = mailboxCursor.getInt(
EmailContent.NOTIFICATION_MAILBOX_UNREAD_COUNT_COLUMN);
final int unseenCount = mailboxCursor.getInt(
EmailContent.NOTIFICATION_MAILBOX_UNSEEN_COUNT_COLUMN);
final Cursor folderCursor = contentResolver.query(
EmailProvider.uiUri("uifolder", mailboxId),
UIProvider.FOLDERS_PROJECTION, null, null, null);
if (folderCursor == null) {
LogUtils.e(LOG_TAG, "Null folder cursor for account %d, mailbox %d",
accountId, mailboxId);
continue;
}
Folder folder = null;
try {
if (folderCursor.moveToFirst()) {
folder = new Folder(folderCursor);
} else {
LogUtils.e(LOG_TAG, "Empty folder cursor for account %d, mailbox %d",
accountId, mailboxId);
continue;
}
} finally {
folderCursor.close();
}
LogUtils.d(LOG_TAG, "Changes to account " + account.name + ", folder: "
+ folder.name + ", unreadCount: " + unreadCount + ", unseenCount: "
+ unseenCount);
NotificationUtils.setNewEmailIndicator(context, unreadCount, unseenCount,
account, folder, true);
if (folderCursor.moveToFirst()) {
folder = new Folder(folderCursor);
} else {
LogUtils.e(LOG_TAG, "Empty folder cursor for account " + accountUri + ", mailbox "
+ folderUri);
return;
}
} finally {
mailboxCursor.close();
folderCursor.close();
}
// TODO: we don't always want getAttention to be true, but we don't necessarily have a
// good heuristic for when it should or shouldn't be.
NotificationUtils.setNewEmailIndicator(context, unreadCount, unseenCount, account, folder,
true /* getAttention */);
}
private static void refreshAllNotifications(final Context context) {

View File

@ -34,6 +34,7 @@ import android.provider.ContactsContract;
import android.text.TextUtils;
import android.text.format.DateUtils;
import com.android.email.EmailIntentService;
import com.android.email.Preferences;
import com.android.email.R;
import com.android.email.SecurityPolicy;
@ -45,6 +46,7 @@ import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
import com.android.emailcommon.provider.HostAuth;
import com.android.mail.providers.UIProvider;
import com.android.mail.utils.LogUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
@ -136,6 +138,9 @@ public class EmailBroadcastProcessorService extends IntentService {
AccountSettings.actionSettingsWithDebug(this);
} else if (AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION.equals(broadcastAction)) {
onSystemAccountChanged();
} else if (UIProvider.ACTION_UPDATE_NOTIFICATION.equals((broadcastAction))) {
broadcastIntent.setClass(this, EmailIntentService.class);
startService(broadcastIntent);
}
} else if (ACTION_DEVICE_POLICY_ADMIN.equals(action)) {
int message = intent.getIntExtra(EXTRA_DEVICE_POLICY_ADMIN, -1);