2010-09-26 23:16:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2011-05-11 20:40:52 +00:00
|
|
|
import android.app.Notification.Builder;
|
2010-09-26 23:16:21 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2011-05-03 21:42:26 +00:00
|
|
|
import android.content.ContentResolver;
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
import android.content.ContentUris;
|
2010-09-26 23:16:21 +00:00
|
|
|
import android.content.Context;
|
2010-10-18 20:14:20 +00:00
|
|
|
import android.content.Intent;
|
2011-05-03 21:42:26 +00:00
|
|
|
import android.database.ContentObserver;
|
2011-05-10 18:36:57 +00:00
|
|
|
import android.database.Cursor;
|
2010-09-26 23:16:21 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
2011-05-03 21:42:26 +00:00
|
|
|
import android.os.Handler;
|
2011-05-10 18:36:57 +00:00
|
|
|
import android.os.Looper;
|
|
|
|
import android.os.Process;
|
2012-12-11 18:37:35 +00:00
|
|
|
import android.provider.Settings;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
2010-09-26 23:16:21 +00:00
|
|
|
import android.text.TextUtils;
|
2013-10-17 20:50:38 +00:00
|
|
|
import android.text.format.DateUtils;
|
2010-09-26 23:16:21 +00:00
|
|
|
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
import com.android.email.activity.setup.AccountSecurity;
|
2014-07-11 17:08:20 +00:00
|
|
|
import com.android.email.activity.setup.HeadlessAccountSettingsLoader;
|
2012-06-28 17:40:46 +00:00
|
|
|
import com.android.email.provider.EmailProvider;
|
2013-10-04 17:01:13 +00:00
|
|
|
import com.android.email.service.EmailServiceUtils;
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
import com.android.emailcommon.provider.Account;
|
|
|
|
import com.android.emailcommon.provider.EmailContent;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Attachment;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Message;
|
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
2012-06-28 17:40:46 +00:00
|
|
|
import com.android.emailcommon.utility.EmailAsyncTask;
|
2012-12-11 18:37:35 +00:00
|
|
|
import com.android.mail.preferences.FolderPreferences;
|
2012-06-28 17:40:46 +00:00
|
|
|
import com.android.mail.providers.Folder;
|
|
|
|
import com.android.mail.providers.UIProvider;
|
2013-08-30 00:37:27 +00:00
|
|
|
import com.android.mail.utils.Clock;
|
2012-12-11 18:37:35 +00:00
|
|
|
import com.android.mail.utils.LogTag;
|
|
|
|
import com.android.mail.utils.LogUtils;
|
|
|
|
import com.android.mail.utils.NotificationUtils;
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
|
2011-05-03 21:42:26 +00:00
|
|
|
import java.util.HashMap;
|
2011-05-11 22:29:24 +00:00
|
|
|
import java.util.HashSet;
|
2012-12-11 18:37:35 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2011-05-03 21:42:26 +00:00
|
|
|
|
2010-09-26 23:16:21 +00:00
|
|
|
/**
|
|
|
|
* Class that manages notifications.
|
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
public class EmailNotificationController implements NotificationController {
|
2012-12-11 18:37:35 +00:00
|
|
|
private static final String LOG_TAG = LogTag.getLogTag();
|
2012-06-28 17:40:46 +00:00
|
|
|
|
2011-03-22 00:08:16 +00:00
|
|
|
private static final int NOTIFICATION_ID_ATTACHMENT_WARNING = 3;
|
|
|
|
private static final int NOTIFICATION_ID_PASSWORD_EXPIRING = 4;
|
|
|
|
private static final int NOTIFICATION_ID_PASSWORD_EXPIRED = 5;
|
2010-10-18 20:14:20 +00:00
|
|
|
|
2012-06-28 17:40:46 +00:00
|
|
|
private static final int NOTIFICATION_ID_BASE_MASK = 0xF0000000;
|
2010-10-18 20:14:20 +00:00
|
|
|
private static final int NOTIFICATION_ID_BASE_LOGIN_WARNING = 0x20000000;
|
2012-06-28 17:40:46 +00:00
|
|
|
private static final int NOTIFICATION_ID_BASE_SECURITY_NEEDED = 0x30000000;
|
|
|
|
private static final int NOTIFICATION_ID_BASE_SECURITY_CHANGED = 0x40000000;
|
2010-09-26 23:16:21 +00:00
|
|
|
|
2011-05-11 22:29:24 +00:00
|
|
|
private static NotificationThread sNotificationThread;
|
|
|
|
private static Handler sNotificationHandler;
|
2014-09-07 20:36:33 +00:00
|
|
|
private static EmailNotificationController sInstance;
|
2010-09-26 23:16:21 +00:00
|
|
|
private final Context mContext;
|
|
|
|
private final NotificationManager mNotificationManager;
|
2010-12-03 22:44:47 +00:00
|
|
|
private final Clock mClock;
|
2012-06-28 17:40:46 +00:00
|
|
|
/** Maps account id to its observer */
|
2012-12-11 18:37:35 +00:00
|
|
|
private final Map<Long, ContentObserver> mNotificationMap =
|
|
|
|
new HashMap<Long, ContentObserver>();
|
2011-05-11 22:29:24 +00:00
|
|
|
private ContentObserver mAccountObserver;
|
2010-09-26 23:16:21 +00:00
|
|
|
|
|
|
|
/** Constructor */
|
2014-09-07 20:36:33 +00:00
|
|
|
protected EmailNotificationController(Context context, Clock clock) {
|
2010-09-26 23:16:21 +00:00
|
|
|
mContext = context.getApplicationContext();
|
2012-08-23 05:25:42 +00:00
|
|
|
EmailContent.init(context);
|
2010-09-26 23:16:21 +00:00
|
|
|
mNotificationManager = (NotificationManager) context.getSystemService(
|
|
|
|
Context.NOTIFICATION_SERVICE);
|
2010-12-03 22:44:47 +00:00
|
|
|
mClock = clock;
|
2010-09-26 23:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Singleton access */
|
2014-09-07 20:36:33 +00:00
|
|
|
public static synchronized EmailNotificationController getInstance(Context context) {
|
2010-09-26 23:16:21 +00:00
|
|
|
if (sInstance == null) {
|
2014-09-07 20:36:33 +00:00
|
|
|
sInstance = new EmailNotificationController(context, Clock.INSTANCE);
|
2010-09-26 23:16:21 +00:00
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
2011-07-28 20:55:10 +00:00
|
|
|
/**
|
|
|
|
* Return whether or not a notification, based on the passed-in id, needs to be "ongoing"
|
|
|
|
* @param notificationId the notification id to check
|
|
|
|
* @return whether or not the notification must be "ongoing"
|
|
|
|
*/
|
2012-12-11 18:37:35 +00:00
|
|
|
private static boolean needsOngoingNotification(int notificationId) {
|
2011-07-28 20:55:10 +00:00
|
|
|
// "Security needed" must be ongoing so that the user doesn't close it; otherwise, sync will
|
|
|
|
// be prevented until a reboot. Consider also doing this for password expired.
|
2012-06-28 17:40:46 +00:00
|
|
|
return (notificationId & NOTIFICATION_ID_BASE_MASK) == NOTIFICATION_ID_BASE_SECURITY_NEEDED;
|
2011-07-28 20:55:10 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 20:58:36 +00:00
|
|
|
/**
|
2012-12-11 18:37:35 +00:00
|
|
|
* Returns a {@link android.support.v4.app.NotificationCompat.Builder} for an event with the
|
|
|
|
* given account. The account contains specific rules on ring tone usage and these will be used
|
|
|
|
* to modify the notification behaviour.
|
2010-12-01 20:58:36 +00:00
|
|
|
*
|
2012-06-28 17:40:46 +00:00
|
|
|
* @param accountId The id of the account this notification is being built for.
|
2011-04-28 18:09:41 +00:00
|
|
|
* @param ticker Text displayed when the notification is first shown. May be {@code null}.
|
|
|
|
* @param title The first line of text. May NOT be {@code null}.
|
|
|
|
* @param contentText The second line of text. May NOT be {@code null}.
|
|
|
|
* @param intent The intent to start if the user clicks on the notification.
|
2012-12-11 18:37:35 +00:00
|
|
|
* @param number A number to display using {@link Builder#setNumber(int)}. May be {@code null}.
|
2011-05-03 21:42:26 +00:00
|
|
|
* @param enableAudio If {@code false}, do not play any sound. Otherwise, play sound according
|
|
|
|
* to the settings for the given account.
|
2011-04-28 18:09:41 +00:00
|
|
|
* @return A {@link Notification} that can be sent to the notification service.
|
2010-12-01 20:58:36 +00:00
|
|
|
*/
|
2012-12-11 18:37:35 +00:00
|
|
|
private NotificationCompat.Builder createBaseAccountNotificationBuilder(long accountId,
|
2014-08-19 17:49:28 +00:00
|
|
|
String ticker, CharSequence title, String contentText, Intent intent,
|
2011-07-28 20:55:10 +00:00
|
|
|
Integer number, boolean enableAudio, boolean ongoing) {
|
2010-12-01 20:58:36 +00:00
|
|
|
// Pending Intent
|
2011-02-17 00:38:18 +00:00
|
|
|
PendingIntent pending = null;
|
|
|
|
if (intent != null) {
|
2011-04-28 18:09:41 +00:00
|
|
|
pending = PendingIntent.getActivity(
|
|
|
|
mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2011-02-17 00:38:18 +00:00
|
|
|
}
|
2010-12-01 20:58:36 +00:00
|
|
|
|
2011-04-28 18:09:41 +00:00
|
|
|
// NOTE: the ticker is not shown for notifications in the Holo UX
|
2012-12-11 18:37:35 +00:00
|
|
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
|
2011-04-28 18:09:41 +00:00
|
|
|
.setContentTitle(title)
|
|
|
|
.setContentText(contentText)
|
|
|
|
.setContentIntent(pending)
|
|
|
|
.setNumber(number == null ? 0 : number)
|
2014-08-16 00:54:08 +00:00
|
|
|
.setSmallIcon(R.drawable.ic_notification_mail_24dp)
|
2011-04-28 18:09:41 +00:00
|
|
|
.setWhen(mClock.getTime())
|
2011-07-28 20:55:10 +00:00
|
|
|
.setTicker(ticker)
|
|
|
|
.setOngoing(ongoing);
|
2011-05-03 21:42:26 +00:00
|
|
|
|
|
|
|
if (enableAudio) {
|
2012-06-28 17:40:46 +00:00
|
|
|
Account account = Account.restoreAccountWithId(mContext, accountId);
|
2011-05-03 21:42:26 +00:00
|
|
|
setupSoundAndVibration(builder, account);
|
|
|
|
}
|
2010-12-01 20:58:36 +00:00
|
|
|
|
2012-06-28 17:40:46 +00:00
|
|
|
return builder;
|
2011-04-28 18:09:41 +00:00
|
|
|
}
|
2010-12-01 20:58:36 +00:00
|
|
|
|
2011-04-28 18:09:41 +00:00
|
|
|
/**
|
|
|
|
* Generic notifier for any account. Uses notification rules from account.
|
|
|
|
*
|
2012-06-28 17:40:46 +00:00
|
|
|
* @param accountId The account id this notification is being built for.
|
2011-04-28 18:09:41 +00:00
|
|
|
* @param ticker Text displayed when the notification is first shown. May be {@code null}.
|
|
|
|
* @param title The first line of text. May NOT be {@code null}.
|
|
|
|
* @param contentText The second line of text. May NOT be {@code null}.
|
|
|
|
* @param intent The intent to start if the user clicks on the notification.
|
|
|
|
* @param notificationId The ID of the notification to register with the service.
|
|
|
|
*/
|
2012-06-28 17:40:46 +00:00
|
|
|
private void showNotification(long accountId, String ticker, String title,
|
2011-04-28 18:09:41 +00:00
|
|
|
String contentText, Intent intent, int notificationId) {
|
2012-12-11 18:37:35 +00:00
|
|
|
final NotificationCompat.Builder builder = createBaseAccountNotificationBuilder(accountId,
|
2014-08-19 17:49:28 +00:00
|
|
|
ticker, title, contentText, intent, null, true,
|
2012-06-28 17:40:46 +00:00
|
|
|
needsOngoingNotification(notificationId));
|
2012-12-11 18:37:35 +00:00
|
|
|
mNotificationManager.notify(notificationId, builder.build());
|
2010-12-01 20:58:36 +00:00
|
|
|
}
|
|
|
|
|
2010-09-26 23:16:21 +00:00
|
|
|
/**
|
2011-05-10 18:36:57 +00:00
|
|
|
* Tells the notification controller if it should be watching for changes to the message table.
|
|
|
|
* This is the main life cycle method for message notifications. When we stop observing
|
|
|
|
* database changes, we save the state [e.g. message ID and count] of the most recent
|
|
|
|
* notification shown to the user. And, when we start observing database changes, we restore
|
|
|
|
* the saved state.
|
2010-09-26 23:16:21 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2012-12-11 18:37:35 +00:00
|
|
|
public void watchForMessages() {
|
2011-05-12 17:13:45 +00:00
|
|
|
ensureHandlerExists();
|
2011-05-10 18:36:57 +00:00
|
|
|
// Run this on the message notification handler
|
2011-05-11 22:29:24 +00:00
|
|
|
sNotificationHandler.post(new Runnable() {
|
2011-05-10 18:36:57 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
|
|
|
|
|
|
|
// otherwise, start new observers for all notified accounts
|
2011-05-12 17:13:45 +00:00
|
|
|
registerMessageNotification(Account.ACCOUNT_ID_COMBINED_VIEW);
|
2011-05-11 22:29:24 +00:00
|
|
|
// If we're already observing account changes, don't do anything else
|
|
|
|
if (mAccountObserver == null) {
|
2012-12-11 18:37:35 +00:00
|
|
|
LogUtils.i(LOG_TAG, "Observing account changes for notifications");
|
2011-05-11 22:29:24 +00:00
|
|
|
mAccountObserver = new AccountContentObserver(sNotificationHandler, mContext);
|
|
|
|
resolver.registerContentObserver(Account.NOTIFIER_URI, true, mAccountObserver);
|
2011-05-10 18:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2010-09-26 23:16:21 +00:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:13:45 +00:00
|
|
|
/**
|
|
|
|
* Ensures the notification handler exists and is ready to handle requests.
|
|
|
|
*/
|
2013-10-17 20:50:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Notifications jump around too much because we get too many content updates.
|
|
|
|
* We should try to make the provider generate fewer updates instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static final int NOTIFICATION_DELAYED_MESSAGE = 0;
|
|
|
|
private static final long NOTIFICATION_DELAY = 15 * DateUtils.SECOND_IN_MILLIS;
|
|
|
|
// True if we're coalescing notification updates
|
|
|
|
private static boolean sNotificationDelayedMessagePending;
|
|
|
|
// True if accounts have changed and we need to refresh everything
|
|
|
|
private static boolean sRefreshAllNeeded;
|
|
|
|
// Set of accounts we need to regenerate notifications for
|
|
|
|
private static final HashSet<Long> sRefreshAccountSet = new HashSet<Long>();
|
|
|
|
// These should all be accessed on-thread, but just in case...
|
|
|
|
private static final Object sNotificationDelayedMessageLock = new Object();
|
|
|
|
|
2011-05-12 17:13:45 +00:00
|
|
|
private static synchronized void ensureHandlerExists() {
|
|
|
|
if (sNotificationThread == null) {
|
|
|
|
sNotificationThread = new NotificationThread();
|
2013-10-17 20:50:38 +00:00
|
|
|
sNotificationHandler = new Handler(sNotificationThread.getLooper(),
|
|
|
|
new Handler.Callback() {
|
|
|
|
@Override
|
|
|
|
public boolean handleMessage(final android.os.Message message) {
|
|
|
|
/**
|
|
|
|
* To reduce spamming the notifications, we quiesce updates for a few
|
|
|
|
* seconds to batch them up, then handle them here.
|
|
|
|
*/
|
|
|
|
LogUtils.d(LOG_TAG, "Delayed notification processing");
|
|
|
|
synchronized (sNotificationDelayedMessageLock) {
|
|
|
|
sNotificationDelayedMessagePending = false;
|
|
|
|
final Context context = (Context)message.obj;
|
|
|
|
if (sRefreshAllNeeded) {
|
|
|
|
sRefreshAllNeeded = false;
|
|
|
|
refreshAllNotificationsInternal(context);
|
|
|
|
}
|
|
|
|
for (final Long accountId : sRefreshAccountSet) {
|
|
|
|
refreshNotificationsForAccountInternal(context, accountId);
|
|
|
|
}
|
|
|
|
sRefreshAccountSet.clear();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2011-05-12 17:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-26 23:16:21 +00:00
|
|
|
/**
|
2012-06-28 17:40:46 +00:00
|
|
|
* Registers an observer for changes to mailboxes in the given account.
|
2011-05-10 18:36:57 +00:00
|
|
|
* NOTE: This must be called on the notification handler thread.
|
|
|
|
* @param accountId The ID of the account to register the observer for. May be
|
2011-05-12 17:13:45 +00:00
|
|
|
* {@link Account#ACCOUNT_ID_COMBINED_VIEW} to register observers for all
|
|
|
|
* accounts that allow for user notification.
|
2010-09-26 23:16:21 +00:00
|
|
|
*/
|
2012-12-11 18:37:35 +00:00
|
|
|
private void registerMessageNotification(final long accountId) {
|
2011-05-10 18:36:57 +00:00
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
2011-05-12 17:13:45 +00:00
|
|
|
if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
|
2011-05-10 18:36:57 +00:00
|
|
|
Cursor c = resolver.query(
|
|
|
|
Account.CONTENT_URI, EmailContent.ID_PROJECTION,
|
2012-12-11 18:37:35 +00:00
|
|
|
null, null, null);
|
2011-05-10 18:36:57 +00:00
|
|
|
try {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
long id = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
|
|
|
|
registerMessageNotification(id);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
} else {
|
2011-07-20 01:19:59 +00:00
|
|
|
ContentObserver obs = mNotificationMap.get(accountId);
|
|
|
|
if (obs != null) return; // we're already observing; nothing to do
|
2012-12-11 18:37:35 +00:00
|
|
|
LogUtils.i(LOG_TAG, "Registering for notifications for account " + accountId);
|
2011-05-10 18:36:57 +00:00
|
|
|
ContentObserver observer = new MessageContentObserver(
|
2012-06-28 17:40:46 +00:00
|
|
|
sNotificationHandler, mContext, accountId);
|
2011-05-10 18:36:57 +00:00
|
|
|
resolver.registerContentObserver(Message.NOTIFIER_URI, true, observer);
|
2011-07-20 01:19:59 +00:00
|
|
|
mNotificationMap.put(accountId, observer);
|
2011-05-11 22:29:24 +00:00
|
|
|
// Now, ping the observer for any initial notifications
|
2011-07-20 01:19:59 +00:00
|
|
|
observer.onChange(true);
|
2011-05-03 21:42:26 +00:00
|
|
|
}
|
2011-05-10 18:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters the observer for the given account. If the specified account does not have
|
2011-05-11 22:29:24 +00:00
|
|
|
* a registered observer, no action is performed. This will not clear any existing notification
|
|
|
|
* for the specified account. Use {@link NotificationManager#cancel(int)}.
|
2011-05-10 18:36:57 +00:00
|
|
|
* NOTE: This must be called on the notification handler thread.
|
2011-05-12 17:13:45 +00:00
|
|
|
* @param accountId The ID of the account to unregister from. To unregister all accounts that
|
|
|
|
* have observers, specify an ID of {@link Account#ACCOUNT_ID_COMBINED_VIEW}.
|
2011-05-10 18:36:57 +00:00
|
|
|
*/
|
2012-12-11 18:37:35 +00:00
|
|
|
private void unregisterMessageNotification(final long accountId) {
|
2011-05-10 18:36:57 +00:00
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
2011-05-12 17:13:45 +00:00
|
|
|
if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
|
2012-12-11 18:37:35 +00:00
|
|
|
LogUtils.i(LOG_TAG, "Unregistering notifications for all accounts");
|
2011-05-10 18:36:57 +00:00
|
|
|
// cancel all existing message observers
|
2011-07-20 01:19:59 +00:00
|
|
|
for (ContentObserver observer : mNotificationMap.values()) {
|
2011-05-10 18:36:57 +00:00
|
|
|
resolver.unregisterContentObserver(observer);
|
|
|
|
}
|
|
|
|
mNotificationMap.clear();
|
2011-05-03 21:42:26 +00:00
|
|
|
} else {
|
2012-12-11 18:37:35 +00:00
|
|
|
LogUtils.i(LOG_TAG, "Unregistering notifications for account " + accountId);
|
2011-07-20 01:19:59 +00:00
|
|
|
ContentObserver observer = mNotificationMap.remove(accountId);
|
|
|
|
if (observer != null) {
|
2011-05-10 18:36:57 +00:00
|
|
|
resolver.unregisterContentObserver(observer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 17:40:46 +00:00
|
|
|
public static final String EXTRA_ACCOUNT = "account";
|
|
|
|
public static final String EXTRA_CONVERSATION = "conversationUri";
|
|
|
|
public static final String EXTRA_FOLDER = "folder";
|
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
/** Sets up the notification's sound and vibration based upon account details. */
|
|
|
|
private void setupSoundAndVibration(
|
|
|
|
NotificationCompat.Builder builder, Account account) {
|
|
|
|
String ringtoneUri = Settings.System.DEFAULT_NOTIFICATION_URI.toString();
|
|
|
|
boolean vibrate = false;
|
2012-07-30 20:12:07 +00:00
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
// Use the Inbox notification preferences
|
|
|
|
final Cursor accountCursor = mContext.getContentResolver().query(EmailProvider.uiUri(
|
|
|
|
"uiaccount", account.mId), UIProvider.ACCOUNTS_PROJECTION, null, null, null);
|
2012-09-23 19:13:48 +00:00
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
com.android.mail.providers.Account uiAccount = null;
|
2012-09-23 19:13:48 +00:00
|
|
|
try {
|
2012-12-11 18:37:35 +00:00
|
|
|
if (accountCursor.moveToFirst()) {
|
2014-07-11 15:17:54 +00:00
|
|
|
uiAccount = com.android.mail.providers.Account.builder().buildFrom(accountCursor);
|
2012-09-23 19:13:48 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2012-12-11 18:37:35 +00:00
|
|
|
accountCursor.close();
|
2012-09-23 19:13:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
if (uiAccount != null) {
|
|
|
|
final Cursor folderCursor =
|
|
|
|
mContext.getContentResolver().query(uiAccount.settings.defaultInbox,
|
|
|
|
UIProvider.FOLDERS_PROJECTION, null, null, null);
|
2010-09-26 23:16:21 +00:00
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
if (folderCursor == null) {
|
2013-03-08 07:00:47 +00:00
|
|
|
// This can happen when the notification is for the security policy notification
|
|
|
|
// that happens before the account is setup
|
|
|
|
LogUtils.w(LOG_TAG, "Null folder cursor for mailbox %s",
|
2012-12-11 18:37:35 +00:00
|
|
|
uiAccount.settings.defaultInbox);
|
2013-03-08 07:00:47 +00:00
|
|
|
} else {
|
|
|
|
Folder folder = null;
|
|
|
|
try {
|
|
|
|
if (folderCursor.moveToFirst()) {
|
|
|
|
folder = new Folder(folderCursor);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
folderCursor.close();
|
2012-06-28 17:40:46 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 07:00:47 +00:00
|
|
|
if (folder != null) {
|
|
|
|
final FolderPreferences folderPreferences = new FolderPreferences(
|
2013-10-11 23:44:57 +00:00
|
|
|
mContext, uiAccount.getEmailAddress(), folder, true /* inbox */);
|
2010-09-26 23:16:21 +00:00
|
|
|
|
2013-03-08 07:00:47 +00:00
|
|
|
ringtoneUri = folderPreferences.getNotificationRingtoneUri();
|
|
|
|
vibrate = folderPreferences.isNotificationVibrateEnabled();
|
|
|
|
} else {
|
|
|
|
LogUtils.e(LOG_TAG,
|
|
|
|
"Null folder for mailbox %s", uiAccount.settings.defaultInbox);
|
|
|
|
}
|
2012-12-11 18:37:35 +00:00
|
|
|
}
|
2010-12-03 22:44:47 +00:00
|
|
|
} else {
|
2012-12-11 18:37:35 +00:00
|
|
|
LogUtils.e(LOG_TAG, "Null uiAccount for account id %d", account.mId);
|
2010-12-03 22:44:47 +00:00
|
|
|
}
|
2010-09-26 23:16:21 +00:00
|
|
|
|
2011-04-28 18:09:41 +00:00
|
|
|
int defaults = Notification.DEFAULT_LIGHTS;
|
2012-11-29 02:14:06 +00:00
|
|
|
if (vibrate) {
|
2011-04-28 18:09:41 +00:00
|
|
|
defaults |= Notification.DEFAULT_VIBRATE;
|
2010-09-26 23:16:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 00:59:05 +00:00
|
|
|
builder.setSound(TextUtils.isEmpty(ringtoneUri) ? null : Uri.parse(ringtoneUri))
|
2011-04-28 18:09:41 +00:00
|
|
|
.setDefaults(defaults);
|
2010-09-26 23:16:21 +00:00
|
|
|
}
|
2010-10-18 20:14:20 +00:00
|
|
|
|
|
|
|
/**
|
2011-04-28 18:09:41 +00:00
|
|
|
* Show (or update) a notification that the given attachment could not be forwarded. This
|
|
|
|
* is a very unusual case, and perhaps we shouldn't even send a notification. For now,
|
|
|
|
* it's helpful for debugging.
|
|
|
|
*
|
2011-02-17 00:38:18 +00:00
|
|
|
* NOTE: DO NOT CALL THIS METHOD FROM THE UI THREAD (DATABASE ACCESS)
|
2010-10-18 20:14:20 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2014-07-11 17:08:20 +00:00
|
|
|
public void showDownloadForwardFailedNotificationSynchronous(Attachment attachment) {
|
|
|
|
final Message message = Message.restoreMessageWithId(mContext, attachment.mMessageKey);
|
2012-06-28 17:40:46 +00:00
|
|
|
if (message == null) return;
|
2014-07-11 17:08:20 +00:00
|
|
|
final Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, message.mMailboxKey);
|
2012-06-28 17:40:46 +00:00
|
|
|
showNotification(mailbox.mAccountKey,
|
2010-10-18 20:14:20 +00:00
|
|
|
mContext.getString(R.string.forward_download_failed_ticker),
|
2011-02-17 00:38:18 +00:00
|
|
|
mContext.getString(R.string.forward_download_failed_title),
|
|
|
|
attachment.mFileName,
|
|
|
|
null,
|
|
|
|
NOTIFICATION_ID_ATTACHMENT_WARNING);
|
2010-10-18 20:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-28 18:09:41 +00:00
|
|
|
* Returns a notification ID for login failed notifications for the given account account.
|
2010-10-18 20:14:20 +00:00
|
|
|
*/
|
2012-12-11 18:37:35 +00:00
|
|
|
private static int getLoginFailedNotificationId(long accountId) {
|
2010-10-18 20:14:20 +00:00
|
|
|
return NOTIFICATION_ID_BASE_LOGIN_WARNING + (int)accountId;
|
|
|
|
}
|
|
|
|
|
2011-02-17 00:38:18 +00:00
|
|
|
/**
|
2011-04-28 18:09:41 +00:00
|
|
|
* Show (or update) a notification that there was a login failure for the given account.
|
|
|
|
*
|
2011-02-17 00:38:18 +00:00
|
|
|
* NOTE: DO NOT CALL THIS METHOD FROM THE UI THREAD (DATABASE ACCESS)
|
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2014-07-11 17:08:20 +00:00
|
|
|
public void showLoginFailedNotificationSynchronous(long accountId, boolean incoming) {
|
2010-10-18 20:14:20 +00:00
|
|
|
final Account account = Account.restoreAccountWithId(mContext, accountId);
|
|
|
|
if (account == null) return;
|
2014-07-11 17:08:20 +00:00
|
|
|
final Mailbox mailbox = Mailbox.restoreMailboxOfType(mContext, accountId,
|
2012-06-28 17:40:46 +00:00
|
|
|
Mailbox.TYPE_INBOX);
|
|
|
|
if (mailbox == null) return;
|
2014-07-11 17:08:20 +00:00
|
|
|
|
|
|
|
final Intent settingsIntent;
|
|
|
|
if (incoming) {
|
|
|
|
settingsIntent = new Intent(Intent.ACTION_VIEW,
|
2014-09-07 20:36:33 +00:00
|
|
|
EmailProvider.getIncomingSettingsUri(accountId));
|
2014-07-11 17:08:20 +00:00
|
|
|
} else {
|
|
|
|
settingsIntent = new Intent(Intent.ACTION_VIEW,
|
|
|
|
HeadlessAccountSettingsLoader.getOutgoingSettingsUri(accountId));
|
|
|
|
}
|
2012-06-28 17:40:46 +00:00
|
|
|
showNotification(mailbox.mAccountKey,
|
2010-10-18 20:14:20 +00:00
|
|
|
mContext.getString(R.string.login_failed_ticker, account.mDisplayName),
|
2011-02-17 00:38:18 +00:00
|
|
|
mContext.getString(R.string.login_failed_title),
|
|
|
|
account.getDisplayName(),
|
2014-07-11 17:08:20 +00:00
|
|
|
settingsIntent,
|
|
|
|
getLoginFailedNotificationId(accountId));
|
2010-10-18 20:14:20 +00:00
|
|
|
}
|
|
|
|
|
2011-04-28 18:09:41 +00:00
|
|
|
/**
|
|
|
|
* Cancels the login failed notification for the given account.
|
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2010-10-18 20:14:20 +00:00
|
|
|
public void cancelLoginFailedNotification(long accountId) {
|
|
|
|
mNotificationManager.cancel(getLoginFailedNotificationId(accountId));
|
|
|
|
}
|
2011-03-22 00:08:16 +00:00
|
|
|
|
|
|
|
/**
|
2011-04-28 18:09:41 +00:00
|
|
|
* Show (or update) a notification that the user's password is expiring. The given account
|
|
|
|
* is used to update the display text, but, all accounts share the same notification ID.
|
2011-03-22 00:08:16 +00:00
|
|
|
*
|
2011-04-28 18:09:41 +00:00
|
|
|
* NOTE: DO NOT CALL THIS METHOD FROM THE UI THREAD (DATABASE ACCESS)
|
2011-03-22 00:08:16 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2014-07-11 17:08:20 +00:00
|
|
|
public void showPasswordExpiringNotificationSynchronous(long accountId) {
|
|
|
|
final Account account = Account.restoreAccountWithId(mContext, accountId);
|
2011-03-22 00:08:16 +00:00
|
|
|
if (account == null) return;
|
2011-04-28 18:09:41 +00:00
|
|
|
|
2014-07-11 17:08:20 +00:00
|
|
|
final Intent intent = AccountSecurity.actionDevicePasswordExpirationIntent(mContext,
|
2011-03-22 00:08:16 +00:00
|
|
|
accountId, false);
|
2014-07-11 17:08:20 +00:00
|
|
|
final String accountName = account.getDisplayName();
|
|
|
|
final String ticker =
|
2011-04-28 18:09:41 +00:00
|
|
|
mContext.getString(R.string.password_expire_warning_ticker_fmt, accountName);
|
2014-07-11 17:08:20 +00:00
|
|
|
final String title = mContext.getString(R.string.password_expire_warning_content_title);
|
2012-06-28 17:40:46 +00:00
|
|
|
showNotification(accountId, ticker, title, accountName, intent,
|
2011-03-22 00:08:16 +00:00
|
|
|
NOTIFICATION_ID_PASSWORD_EXPIRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-28 18:09:41 +00:00
|
|
|
* Show (or update) a notification that the user's password has expired. The given account
|
|
|
|
* is used to update the display text, but, all accounts share the same notification ID.
|
2011-03-22 00:08:16 +00:00
|
|
|
*
|
2011-04-28 18:09:41 +00:00
|
|
|
* NOTE: DO NOT CALL THIS METHOD FROM THE UI THREAD (DATABASE ACCESS)
|
2011-03-22 00:08:16 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2014-07-11 17:08:20 +00:00
|
|
|
public void showPasswordExpiredNotificationSynchronous(long accountId) {
|
|
|
|
final Account account = Account.restoreAccountWithId(mContext, accountId);
|
2011-03-22 00:08:16 +00:00
|
|
|
if (account == null) return;
|
2011-04-28 18:09:41 +00:00
|
|
|
|
2014-07-11 17:08:20 +00:00
|
|
|
final Intent intent = AccountSecurity.actionDevicePasswordExpirationIntent(mContext,
|
2011-03-22 00:08:16 +00:00
|
|
|
accountId, true);
|
2014-07-11 17:08:20 +00:00
|
|
|
final String accountName = account.getDisplayName();
|
|
|
|
final String ticker = mContext.getString(R.string.password_expired_ticker);
|
|
|
|
final String title = mContext.getString(R.string.password_expired_content_title);
|
2012-06-28 17:40:46 +00:00
|
|
|
showNotification(accountId, ticker, title, accountName, intent,
|
2011-04-28 18:09:41 +00:00
|
|
|
NOTIFICATION_ID_PASSWORD_EXPIRED);
|
2011-03-22 00:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-28 18:09:41 +00:00
|
|
|
* Cancels any password expire notifications [both expired & expiring].
|
2011-03-22 00:08:16 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2011-03-22 00:08:16 +00:00
|
|
|
public void cancelPasswordExpirationNotifications() {
|
2011-05-10 18:36:57 +00:00
|
|
|
mNotificationManager.cancel(NOTIFICATION_ID_PASSWORD_EXPIRING);
|
|
|
|
mNotificationManager.cancel(NOTIFICATION_ID_PASSWORD_EXPIRED);
|
2011-03-22 00:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-28 17:40:46 +00:00
|
|
|
* Show (or update) a security needed notification. If tapped, the user is taken to a
|
|
|
|
* dialog asking whether he wants to update his settings.
|
2011-03-22 00:08:16 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2011-03-22 00:08:16 +00:00
|
|
|
public void showSecurityNeededNotification(Account account) {
|
|
|
|
Intent intent = AccountSecurity.actionUpdateSecurityIntent(mContext, account.mId, true);
|
2011-04-28 18:09:41 +00:00
|
|
|
String accountName = account.getDisplayName();
|
|
|
|
String ticker =
|
2012-06-28 17:40:46 +00:00
|
|
|
mContext.getString(R.string.security_needed_ticker_fmt, accountName);
|
|
|
|
String title = mContext.getString(R.string.security_notification_content_update_title);
|
|
|
|
showNotification(account.mId, ticker, title, accountName, intent,
|
|
|
|
(int)(NOTIFICATION_ID_BASE_SECURITY_NEEDED + account.mId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show (or update) a security changed notification. If tapped, the user is taken to the
|
|
|
|
* account settings screen where he can view the list of enforced policies
|
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2012-06-28 17:40:46 +00:00
|
|
|
public void showSecurityChangedNotification(Account account) {
|
2014-07-11 17:08:20 +00:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW,
|
2014-09-07 20:36:33 +00:00
|
|
|
EmailProvider.getIncomingSettingsUri(account.getId()));
|
2014-07-11 17:08:20 +00:00
|
|
|
final String accountName = account.getDisplayName();
|
|
|
|
final String ticker =
|
2012-06-28 17:40:46 +00:00
|
|
|
mContext.getString(R.string.security_changed_ticker_fmt, accountName);
|
2014-07-11 17:08:20 +00:00
|
|
|
final String title =
|
|
|
|
mContext.getString(R.string.security_notification_content_change_title);
|
2012-06-28 17:40:46 +00:00
|
|
|
showNotification(account.mId, ticker, title, accountName, intent,
|
|
|
|
(int)(NOTIFICATION_ID_BASE_SECURITY_CHANGED + account.mId));
|
2011-03-22 00:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-28 17:40:46 +00:00
|
|
|
* Show (or update) a security unsupported notification. If tapped, the user is taken to the
|
|
|
|
* account settings screen where he can view the list of unsupported policies
|
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2012-06-28 17:40:46 +00:00
|
|
|
public void showSecurityUnsupportedNotification(Account account) {
|
2014-07-11 17:08:20 +00:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW,
|
2014-09-07 20:36:33 +00:00
|
|
|
EmailProvider.getIncomingSettingsUri(account.getId()));
|
2014-07-11 17:08:20 +00:00
|
|
|
final String accountName = account.getDisplayName();
|
|
|
|
final String ticker =
|
2012-06-28 17:40:46 +00:00
|
|
|
mContext.getString(R.string.security_unsupported_ticker_fmt, accountName);
|
2014-07-11 17:08:20 +00:00
|
|
|
final String title =
|
|
|
|
mContext.getString(R.string.security_notification_content_unsupported_title);
|
2012-06-28 17:40:46 +00:00
|
|
|
showNotification(account.mId, ticker, title, accountName, intent,
|
|
|
|
(int)(NOTIFICATION_ID_BASE_SECURITY_NEEDED + account.mId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels all security needed notifications.
|
2011-03-22 00:08:16 +00:00
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
2011-03-22 00:08:16 +00:00
|
|
|
public void cancelSecurityNeededNotification() {
|
2012-06-28 17:40:46 +00:00
|
|
|
EmailAsyncTask.runAsyncParallel(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Cursor c = mContext.getContentResolver().query(Account.CONTENT_URI,
|
|
|
|
Account.ID_PROJECTION, null, null, null);
|
|
|
|
try {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
long id = c.getLong(Account.ID_PROJECTION_COLUMN);
|
|
|
|
mNotificationManager.cancel(
|
|
|
|
(int)(NOTIFICATION_ID_BASE_SECURITY_NEEDED + id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}});
|
2011-03-22 00:08:16 +00:00
|
|
|
}
|
2011-05-03 21:42:26 +00:00
|
|
|
|
2013-05-23 01:36:07 +00:00
|
|
|
/**
|
|
|
|
* Cancels all notifications for the specified account id. This includes new mail notifications,
|
|
|
|
* as well as special login/security notifications.
|
|
|
|
*/
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
|
|
|
public void cancelNotifications(final Context context, final Account account) {
|
2013-10-04 19:27:20 +00:00
|
|
|
final EmailServiceUtils.EmailServiceInfo serviceInfo
|
|
|
|
= EmailServiceUtils.getServiceInfoForAccount(context, account.mId);
|
|
|
|
if (serviceInfo == null) {
|
|
|
|
LogUtils.d(LOG_TAG, "Can't cancel notification for missing account %d", account.mId);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-04 17:01:13 +00:00
|
|
|
final android.accounts.Account notifAccount
|
2013-10-04 19:27:20 +00:00
|
|
|
= account.getAccountManagerAccount(serviceInfo.accountType);
|
|
|
|
|
2013-10-04 17:01:13 +00:00
|
|
|
NotificationUtils.clearAccountNotifications(context, notifAccount);
|
2013-05-23 01:36:07 +00:00
|
|
|
|
|
|
|
final NotificationManager notificationManager = getInstance(context).mNotificationManager;
|
|
|
|
|
|
|
|
notificationManager.cancel((int) (NOTIFICATION_ID_BASE_LOGIN_WARNING + account.mId));
|
|
|
|
notificationManager.cancel((int) (NOTIFICATION_ID_BASE_SECURITY_NEEDED + account.mId));
|
|
|
|
notificationManager.cancel((int) (NOTIFICATION_ID_BASE_SECURITY_CHANGED + account.mId));
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:50:38 +00:00
|
|
|
private static void refreshNotificationsForAccount(final Context context,
|
|
|
|
final long accountId) {
|
|
|
|
synchronized (sNotificationDelayedMessageLock) {
|
|
|
|
if (sNotificationDelayedMessagePending) {
|
|
|
|
sRefreshAccountSet.add(accountId);
|
|
|
|
} else {
|
|
|
|
ensureHandlerExists();
|
|
|
|
sNotificationHandler.sendMessageDelayed(
|
|
|
|
android.os.Message.obtain(sNotificationHandler,
|
|
|
|
NOTIFICATION_DELAYED_MESSAGE, context), NOTIFICATION_DELAY);
|
|
|
|
sNotificationDelayedMessagePending = true;
|
|
|
|
refreshNotificationsForAccountInternal(context, accountId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void refreshNotificationsForAccountInternal(final Context context,
|
|
|
|
final long accountId) {
|
2013-11-21 20:27:14 +00:00
|
|
|
final Uri accountUri = EmailProvider.uiUri("uiaccount", accountId);
|
|
|
|
|
2013-10-17 20:50:38 +00:00
|
|
|
final ContentResolver contentResolver = context.getContentResolver();
|
|
|
|
|
2013-11-21 20:27:14 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 20:36:33 +00:00
|
|
|
@Override
|
|
|
|
public void handleUpdateNotificationIntent(Context context, Intent intent) {
|
2013-11-21 20:27:14 +00:00
|
|
|
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);
|
2013-10-17 20:50:38 +00:00
|
|
|
|
|
|
|
if (accountCursor == null) {
|
2013-11-21 20:27:14 +00:00
|
|
|
LogUtils.e(LOG_TAG, "Null account cursor for account " + accountUri);
|
2013-10-17 20:50:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
com.android.mail.providers.Account account = null;
|
|
|
|
try {
|
|
|
|
if (accountCursor.moveToFirst()) {
|
2014-07-11 15:17:54 +00:00
|
|
|
account = com.android.mail.providers.Account.builder().buildFrom(accountCursor);
|
2013-10-17 20:50:38 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
accountCursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account == null) {
|
2013-11-21 20:27:14 +00:00
|
|
|
LogUtils.d(LOG_TAG, "Tried to create a notification for a missing account "
|
|
|
|
+ accountUri);
|
2013-10-17 20:50:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-21 20:27:14 +00:00
|
|
|
final Cursor folderCursor = contentResolver.query(folderUri, UIProvider.FOLDERS_PROJECTION,
|
|
|
|
null, null, null);
|
2013-10-17 20:50:38 +00:00
|
|
|
|
2013-11-21 20:27:14 +00:00
|
|
|
if (folderCursor == null) {
|
|
|
|
LogUtils.e(LOG_TAG, "Null folder cursor for account " + accountUri + ", mailbox "
|
|
|
|
+ folderUri);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-17 20:50:38 +00:00
|
|
|
|
2013-11-21 20:27:14 +00:00
|
|
|
Folder folder = null;
|
|
|
|
try {
|
|
|
|
if (folderCursor.moveToFirst()) {
|
|
|
|
folder = new Folder(folderCursor);
|
|
|
|
} else {
|
|
|
|
LogUtils.e(LOG_TAG, "Empty folder cursor for account " + accountUri + ", mailbox "
|
|
|
|
+ folderUri);
|
|
|
|
return;
|
2013-10-17 20:50:38 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2013-11-21 20:27:14 +00:00
|
|
|
folderCursor.close();
|
2013-10-17 20:50:38 +00:00
|
|
|
}
|
2013-11-21 20:27:14 +00:00
|
|
|
|
|
|
|
// 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.
|
2014-05-20 21:40:08 +00:00
|
|
|
NotificationUtils.sendSetNewEmailIndicatorIntent(context, unreadCount, unseenCount,
|
|
|
|
account, folder, true /* getAttention */);
|
2013-10-17 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void refreshAllNotifications(final Context context) {
|
|
|
|
synchronized (sNotificationDelayedMessageLock) {
|
|
|
|
if (sNotificationDelayedMessagePending) {
|
|
|
|
sRefreshAllNeeded = true;
|
|
|
|
} else {
|
|
|
|
ensureHandlerExists();
|
|
|
|
sNotificationHandler.sendMessageDelayed(
|
|
|
|
android.os.Message.obtain(sNotificationHandler,
|
|
|
|
NOTIFICATION_DELAYED_MESSAGE, context), NOTIFICATION_DELAY);
|
|
|
|
sNotificationDelayedMessagePending = true;
|
|
|
|
refreshAllNotificationsInternal(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void refreshAllNotificationsInternal(final Context context) {
|
2014-05-20 21:40:08 +00:00
|
|
|
NotificationUtils.resendNotifications(
|
2014-08-29 16:46:44 +00:00
|
|
|
context, false, null, null, null /* ContactFetcher */);
|
2013-10-17 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
2011-05-03 21:42:26 +00:00
|
|
|
/**
|
|
|
|
* Observer invoked whenever a message we're notifying the user about changes.
|
|
|
|
*/
|
|
|
|
private static class MessageContentObserver extends ContentObserver {
|
|
|
|
private final Context mContext;
|
2011-05-10 18:36:57 +00:00
|
|
|
private final long mAccountId;
|
2011-05-03 21:42:26 +00:00
|
|
|
|
2011-05-10 18:36:57 +00:00
|
|
|
public MessageContentObserver(
|
2012-12-11 18:37:35 +00:00
|
|
|
final Handler handler, final Context context, final long accountId) {
|
2011-05-10 18:36:57 +00:00
|
|
|
super(handler);
|
2011-05-03 21:42:26 +00:00
|
|
|
mContext = context;
|
|
|
|
mAccountId = accountId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-11 18:37:35 +00:00
|
|
|
public void onChange(final boolean selfChange) {
|
2013-10-17 20:50:38 +00:00
|
|
|
refreshNotificationsForAccount(mContext, mAccountId);
|
2012-06-28 17:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-11 22:29:24 +00:00
|
|
|
/**
|
|
|
|
* Observer invoked whenever an account is modified. This could mean the user changed the
|
|
|
|
* notification settings.
|
|
|
|
*/
|
|
|
|
private static class AccountContentObserver extends ContentObserver {
|
|
|
|
private final Context mContext;
|
2012-12-11 18:37:35 +00:00
|
|
|
public AccountContentObserver(final Handler handler, final Context context) {
|
2011-05-11 22:29:24 +00:00
|
|
|
super(handler);
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-11 18:37:35 +00:00
|
|
|
public void onChange(final boolean selfChange) {
|
2011-05-11 22:29:24 +00:00
|
|
|
final ContentResolver resolver = mContext.getContentResolver();
|
2012-06-28 17:40:46 +00:00
|
|
|
final Cursor c = resolver.query(Account.CONTENT_URI, EmailContent.ID_PROJECTION,
|
2012-12-11 18:37:35 +00:00
|
|
|
null, null, null);
|
|
|
|
final Set<Long> newAccountList = new HashSet<Long>();
|
|
|
|
final Set<Long> removedAccountList = new HashSet<Long>();
|
2011-05-11 22:29:24 +00:00
|
|
|
if (c == null) {
|
|
|
|
// Suspender time ... theoretically, this will never happen
|
2012-12-11 18:37:35 +00:00
|
|
|
LogUtils.wtf(LOG_TAG, "#onChange(); NULL response for account id query");
|
2011-05-11 22:29:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
long accountId = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
|
|
|
|
newAccountList.add(accountId);
|
|
|
|
}
|
|
|
|
} finally {
|
2013-06-25 22:15:02 +00:00
|
|
|
c.close();
|
2011-05-11 22:29:24 +00:00
|
|
|
}
|
|
|
|
// NOTE: Looping over three lists is not necessarily the most efficient. However, the
|
|
|
|
// account lists are going to be very small, so, this will not be necessarily bad.
|
|
|
|
// Cycle through existing notification list and adjust as necessary
|
2012-12-11 18:37:35 +00:00
|
|
|
for (final long accountId : sInstance.mNotificationMap.keySet()) {
|
2011-05-11 22:29:24 +00:00
|
|
|
if (!newAccountList.remove(accountId)) {
|
|
|
|
// account id not in the current set of notifiable accounts
|
|
|
|
removedAccountList.add(accountId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// A new account was added to the notification list
|
2012-12-11 18:37:35 +00:00
|
|
|
for (final long accountId : newAccountList) {
|
2011-05-11 22:29:24 +00:00
|
|
|
sInstance.registerMessageNotification(accountId);
|
|
|
|
}
|
|
|
|
// An account was removed from the notification list
|
2012-12-11 18:37:35 +00:00
|
|
|
for (final long accountId : removedAccountList) {
|
2011-05-11 22:29:24 +00:00
|
|
|
sInstance.unregisterMessageNotification(accountId);
|
|
|
|
}
|
2012-12-11 18:37:35 +00:00
|
|
|
|
2013-10-17 20:50:38 +00:00
|
|
|
refreshAllNotifications(mContext);
|
2011-05-11 22:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-10 18:36:57 +00:00
|
|
|
/**
|
|
|
|
* Thread to handle all notification actions through its own {@link Looper}.
|
|
|
|
*/
|
|
|
|
private static class NotificationThread implements Runnable {
|
|
|
|
/** Lock to ensure proper initialization */
|
|
|
|
private final Object mLock = new Object();
|
|
|
|
/** The {@link Looper} that handles messages for this thread */
|
|
|
|
private Looper mLooper;
|
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
public NotificationThread() {
|
2011-05-10 18:36:57 +00:00
|
|
|
new Thread(null, this, "EmailNotification").start();
|
|
|
|
synchronized (mLock) {
|
|
|
|
while (mLooper == null) {
|
|
|
|
try {
|
|
|
|
mLock.wait();
|
|
|
|
} catch (InterruptedException ex) {
|
2013-10-03 20:26:55 +00:00
|
|
|
// Loop around and wait again
|
2011-05-10 18:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (mLock) {
|
|
|
|
Looper.prepare();
|
|
|
|
mLooper = Looper.myLooper();
|
|
|
|
mLock.notifyAll();
|
|
|
|
}
|
|
|
|
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
|
|
|
Looper.loop();
|
|
|
|
}
|
2012-12-11 18:37:35 +00:00
|
|
|
|
|
|
|
public Looper getLooper() {
|
2011-05-10 18:36:57 +00:00
|
|
|
return mLooper;
|
|
|
|
}
|
2011-05-03 21:42:26 +00:00
|
|
|
}
|
2010-09-26 23:16:21 +00:00
|
|
|
}
|