From e2e3da262c36555a6d9a8acef3e824ae7f28d667 Mon Sep 17 00:00:00 2001 From: Christian Grubert Date: Sun, 8 May 2016 19:26:27 +0200 Subject: [PATCH 1/5] new option: just show notification for contacts --- k9mail/src/main/java/com/fsck/k9/Account.java | 12 +++++++++++ .../k9/activity/setup/AccountSettings.java | 6 ++++++ .../k9/controller/MessagingController.java | 6 ++++++ .../java/com/fsck/k9/helper/Contacts.java | 20 +++++++++++++++++++ k9mail/src/main/res/values/strings.xml | 2 ++ .../res/xml/account_settings_preferences.xml | 8 ++++++++ 6 files changed, 54 insertions(+) diff --git a/k9mail/src/main/java/com/fsck/k9/Account.java b/k9mail/src/main/java/com/fsck/k9/Account.java index 2b17d5f54..9129fcf08 100644 --- a/k9mail/src/main/java/com/fsck/k9/Account.java +++ b/k9mail/src/main/java/com/fsck/k9/Account.java @@ -181,6 +181,7 @@ public class Account implements BaseAccount, StoreConfig { private boolean mNotifyNewMail; private FolderMode mFolderNotifyNewMailMode; private boolean mNotifySelfNewMail; + private boolean mNotifyContactsMailOnly; private String mInboxFolderName; private String mDraftsFolderName; private String mSentFolderName; @@ -289,6 +290,7 @@ public class Account implements BaseAccount, StoreConfig { mFolderNotifyNewMailMode = FolderMode.ALL; mNotifySync = true; mNotifySelfNewMail = true; + mNotifyContactsMailOnly = false; mFolderDisplayMode = FolderMode.NOT_SECOND_CLASS; mFolderSyncMode = FolderMode.FIRST_CLASS; mFolderPushMode = FolderMode.FIRST_CLASS; @@ -397,6 +399,7 @@ public class Account implements BaseAccount, StoreConfig { mFolderNotifyNewMailMode = getEnumStringPref(storage, mUuid + ".folderNotifyNewMailMode", FolderMode.ALL); mNotifySelfNewMail = storage.getBoolean(mUuid + ".notifySelfNewMail", true); + mNotifyContactsMailOnly = storage.getBoolean(mUuid + ".notifyContactsMailOnly", false); mNotifySync = storage.getBoolean(mUuid + ".notifyMailCheck", false); mDeletePolicy = DeletePolicy.fromInt(storage.getInt(mUuid + ".deletePolicy", DeletePolicy.NEVER.setting)); mInboxFolderName = storage.getString(mUuid + ".inboxFolderName", INBOX); @@ -684,6 +687,7 @@ public class Account implements BaseAccount, StoreConfig { editor.putBoolean(mUuid + ".notifyNewMail", mNotifyNewMail); editor.putString(mUuid + ".folderNotifyNewMailMode", mFolderNotifyNewMailMode.name()); editor.putBoolean(mUuid + ".notifySelfNewMail", mNotifySelfNewMail); + editor.putBoolean(mUuid + ".notifyContactsMailOnly", mNotifyContactsMailOnly); editor.putBoolean(mUuid + ".notifyMailCheck", mNotifySync); editor.putInt(mUuid + ".deletePolicy", mDeletePolicy.setting); editor.putString(mUuid + ".inboxFolderName", mInboxFolderName); @@ -1252,6 +1256,14 @@ public class Account implements BaseAccount, StoreConfig { mNotifySelfNewMail = notifySelfNewMail; } + public synchronized boolean isNotifyContactsMailOnly() { + return mNotifyContactsMailOnly; + } + + public synchronized void setNotifyContactsMailOnly(boolean notifyContactsMailOnly) { + this.mNotifyContactsMailOnly = notifyContactsMailOnly; + } + public synchronized Expunge getExpungePolicy() { return mExpungePolicy; } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSettings.java b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSettings.java index 290d8f22f..1430abca6 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSettings.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSettings.java @@ -78,6 +78,7 @@ public class AccountSettings extends K9PreferenceActivity { private static final String PREFERENCE_NOTIFY = "account_notify"; private static final String PREFERENCE_NOTIFY_NEW_MAIL_MODE = "folder_notify_new_mail_mode"; private static final String PREFERENCE_NOTIFY_SELF = "account_notify_self"; + private static final String PREFERENCE_NOTIFY_CONTACTS_MAIL_ONLY = "account_notify_contacts_mail_only"; private static final String PREFERENCE_NOTIFY_SYNC = "account_notify_sync"; private static final String PREFERENCE_VIBRATE = "account_vibrate"; private static final String PREFERENCE_VIBRATE_PATTERN = "account_vibrate_pattern"; @@ -146,6 +147,7 @@ public class AccountSettings extends K9PreferenceActivity { private CheckBoxPreference mAccountNotify; private ListPreference mAccountNotifyNewMailMode; private CheckBoxPreference mAccountNotifySelf; + private CheckBoxPreference mAccountNotifyContactsMailOnly; private ListPreference mAccountShowPictures; private CheckBoxPreference mAccountNotifySync; private CheckBoxPreference mAccountVibrate; @@ -590,6 +592,9 @@ public class AccountSettings extends K9PreferenceActivity { mAccountNotifySelf = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY_SELF); mAccountNotifySelf.setChecked(mAccount.isNotifySelfNewMail()); + mAccountNotifyContactsMailOnly = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY_CONTACTS_MAIL_ONLY); + mAccountNotifyContactsMailOnly.setChecked(mAccount.isNotifyContactsMailOnly()); + mAccountNotifySync = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY_SYNC); mAccountNotifySync.setChecked(mAccount.isShowOngoing()); @@ -753,6 +758,7 @@ public class AccountSettings extends K9PreferenceActivity { mAccount.setNotifyNewMail(mAccountNotify.isChecked()); mAccount.setFolderNotifyNewMailMode(FolderMode.valueOf(mAccountNotifyNewMailMode.getValue())); mAccount.setNotifySelfNewMail(mAccountNotifySelf.isChecked()); + mAccount.setNotifyContactsMailOnly(mAccountNotifyContactsMailOnly.isChecked()); mAccount.setShowOngoing(mAccountNotifySync.isChecked()); mAccount.setDisplayCount(Integer.parseInt(mDisplayCount.getValue())); mAccount.setMaximumAutoDownloadMessageSize(Integer.parseInt(mMessageSize.getValue())); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 7514d3cac..74d70f911 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -40,6 +40,7 @@ import android.os.Build; import android.os.PowerManager; import android.os.Process; import android.os.SystemClock; +import android.provider.ContactsContract; import android.support.annotation.VisibleForTesting; import android.util.Log; @@ -55,6 +56,7 @@ import com.fsck.k9.R; import com.fsck.k9.activity.MessageReference; import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection; import com.fsck.k9.cache.EmailProviderCache; +import com.fsck.k9.helper.Contacts; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.AuthenticationFailedException; import com.fsck.k9.mail.CertificateValidationException; @@ -4313,6 +4315,10 @@ public class MessagingController implements Runnable { return false; } + if (account.isNotifyContactsMailOnly() && !Contacts.getInstance(context).containsContact(message.getFrom())) { + return false; + } + return true; } diff --git a/k9mail/src/main/java/com/fsck/k9/helper/Contacts.java b/k9mail/src/main/java/com/fsck/k9/helper/Contacts.java index 30ad0859e..7ab6dc306 100644 --- a/k9mail/src/main/java/com/fsck/k9/helper/Contacts.java +++ b/k9mail/src/main/java/com/fsck/k9/helper/Contacts.java @@ -141,6 +141,26 @@ public class Contacts { return result; } + /** + * Check whether one of the provided addresses belongs to one of the contacts. + * + * @param addresses The addresses to search in contacts + * @return true, if one address belongs to a contact. + * false, otherwise. + */ + public boolean containsContact(final Address[] addresses) { + if (addresses == null) { + return false; + } + + for (Address addr : addresses) { + if (isInContacts(addr.getAddress())) { + return true; + } + } + return false; + } + /** * Get the name of the contact an email address belongs to. * diff --git a/k9mail/src/main/res/values/strings.xml b/k9mail/src/main/res/values/strings.xml index 024cf0734..6f4358fb0 100644 --- a/k9mail/src/main/res/values/strings.xml +++ b/k9mail/src/main/res/values/strings.xml @@ -532,6 +532,8 @@ Please submit bug reports, contribute new features and ask questions at Notify in status bar while mail is checked Include outgoing mail Show a notification for messages I sent + Contacts only + Show a notification only for messages of known contacts Notification opens unread messages Searches for unread messages when Notification is opened Mark as read when opened diff --git a/k9mail/src/main/res/xml/account_settings_preferences.xml b/k9mail/src/main/res/xml/account_settings_preferences.xml index be644c511..b0e49cfbe 100644 --- a/k9mail/src/main/res/xml/account_settings_preferences.xml +++ b/k9mail/src/main/res/xml/account_settings_preferences.xml @@ -360,6 +360,14 @@ android:defaultValue="true" android:summary="@string/account_settings_notify_self_summary" /> + +