From 6a21a9a61394681dcb03878aaa8aacf67a9660ad Mon Sep 17 00:00:00 2001 From: yingying Date: Fri, 27 Dec 2013 18:37:07 +0800 Subject: UnifiedEmail: Support customize the notification icon for different account. - Add one xml file to provider the different notification icon defined. - Show the defined icon for the notification if the account customize it. - If the account didn't defined the special icon, it will show as default. Change-Id: Ic565dbcb8426c22bc6302f261d52f0595d92c4c5 --- res/drawable/stat_notify_email_189.xml | 32 ++++++ res/xml/notify_icon_providers.xml | 40 +++++++ .../emailcommon/utility/NotifyIconUtilities.java | 115 +++++++++++++++++++++ .../mail/utils/NotificationActionUtils.java | 7 +- src/com/android/mail/utils/NotificationUtils.java | 11 +- 5 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 res/drawable/stat_notify_email_189.xml create mode 100644 res/xml/notify_icon_providers.xml create mode 100644 src/com/android/emailcommon/utility/NotifyIconUtilities.java diff --git a/res/drawable/stat_notify_email_189.xml b/res/drawable/stat_notify_email_189.xml new file mode 100644 index 000000000..56bcd29fc --- /dev/null +++ b/res/drawable/stat_notify_email_189.xml @@ -0,0 +1,32 @@ + + + + + \ No newline at end of file diff --git a/res/xml/notify_icon_providers.xml b/res/xml/notify_icon_providers.xml new file mode 100644 index 000000000..4b6a6ca76 --- /dev/null +++ b/res/xml/notify_icon_providers.xml @@ -0,0 +1,40 @@ + + + + + + + + diff --git a/src/com/android/emailcommon/utility/NotifyIconUtilities.java b/src/com/android/emailcommon/utility/NotifyIconUtilities.java new file mode 100644 index 000000000..016d691f1 --- /dev/null +++ b/src/com/android/emailcommon/utility/NotifyIconUtilities.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2014, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright (C) 2009 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.emailcommon.utility; + +import android.content.Context; +import android.content.res.XmlResourceParser; + +import com.android.mail.utils.LogUtils; + +public class NotifyIconUtilities { + private static final String LOG_TAG = "NotifyIconUtilities"; + + private final static String ADDRESS_SEPARATOR = "@"; + /** Pattern to match any part of a domain */ + private final static String WILD_STRING = "*"; + /** Will match any, single character */ + private final static char WILD_CHARACTER = '?'; + private final static String DOMAIN_SEPARATOR = "\\."; + + public static int findNotifyIconForAccountDomain(Context context, int iconProviders, + String address, int defaultIcon) { + LogUtils.i(LOG_TAG, "find the notify icon for account: ", address); + + int notify_icon_res = defaultIcon; + String domain = address.split(ADDRESS_SEPARATOR)[1]; + try { + XmlResourceParser xml = context.getResources().getXml(iconProviders); + int xmlEventType; + boolean foundNotifyIcon = false; + while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) { + if (xmlEventType == XmlResourceParser.START_TAG + && "provider".equals(xml.getName())) { + String providerDomain = xml.getAttributeValue(null, "domain"); + try { + if (matchProvider(domain, providerDomain)) { + String notify_icon_res_name = xml.getAttributeValue(null, "icon"); + int resId = context.getResources(). + getIdentifier(notify_icon_res_name, "drawable", + context.getPackageName()); + if (resId > 0) { + notify_icon_res = resId; + } + foundNotifyIcon = true; + } + } catch (IllegalArgumentException e) { + LogUtils.w(LOG_TAG, "providers line: " + xml.getLineNumber() + + "; Domain contains multiple globals"); + } + } else if (xmlEventType == XmlResourceParser.END_TAG + && "provider".equals(xml.getName()) + && foundNotifyIcon) { + return notify_icon_res; + } + } + } catch (Exception e) { + LogUtils.e(LOG_TAG, "Error while trying to load provider settings.", e); + } + return notify_icon_res; + } + + /** + * Returns true if the string s1 matches the string s2. The string + * s2 may contain any number of wildcards -- a '?' character -- and/or asterisk + * characters -- '*'. Wildcards match any single character, while the asterisk matches a domain + * part (i.e. substring demarcated by a period, '.') + */ + private static boolean matchProvider(String testDomain, String providerDomain) { + String[] testParts = testDomain.split(DOMAIN_SEPARATOR); + String[] providerParts = providerDomain.split(DOMAIN_SEPARATOR); + if (testParts.length != providerParts.length) { + return false; + } + for (int i = 0; i < testParts.length; i++) { + String testPart = testParts[i].toLowerCase(); + String providerPart = providerParts[i].toLowerCase(); + if (!providerPart.equals(WILD_STRING) && + !matchWithWildcards(testPart, providerPart)) { + return false; + } + } + return true; + } + + private static boolean matchWithWildcards(String testPart, String providerPart) { + int providerLength = providerPart.length(); + if (testPart.length() != providerLength){ + return false; + } + for (int i = 0; i < providerLength; i++) { + char testChar = testPart.charAt(i); + char providerChar = providerPart.charAt(i); + if (testChar != providerChar && providerChar != WILD_CHARACTER) { + return false; + } + } + return true; + } +} diff --git a/src/com/android/mail/utils/NotificationActionUtils.java b/src/com/android/mail/utils/NotificationActionUtils.java index 9443537b6..be99729e8 100644 --- a/src/com/android/mail/utils/NotificationActionUtils.java +++ b/src/com/android/mail/utils/NotificationActionUtils.java @@ -32,6 +32,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.widget.RemoteViews; +import com.android.emailcommon.utility.NotifyIconUtilities; import com.android.mail.MailIntentService; import com.android.mail.NotificationActionIntentService; import com.android.mail.R; @@ -523,7 +524,11 @@ public class NotificationActionUtils { final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); - builder.setSmallIcon(R.drawable.stat_notify_email); + int iconResId = NotifyIconUtilities.findNotifyIconForAccountDomain(context, + R.xml.notify_icon_providers, notificationAction.getAccount().getEmailAddress(), + R.drawable.stat_notify_email); + + builder.setSmallIcon(iconResId); builder.setWhen(notificationAction.getWhen()); final RemoteViews undoView = diff --git a/src/com/android/mail/utils/NotificationUtils.java b/src/com/android/mail/utils/NotificationUtils.java index 9a0caf945..4d3dfcc1c 100644 --- a/src/com/android/mail/utils/NotificationUtils.java +++ b/src/com/android/mail/utils/NotificationUtils.java @@ -41,6 +41,7 @@ import android.text.style.TextAppearanceSpan; import android.util.Pair; import android.util.SparseArray; +import com.android.emailcommon.utility.NotifyIconUtilities; import com.android.mail.EmailAddress; import com.android.mail.MailIntentService; import com.android.mail.R; @@ -130,7 +131,7 @@ public class NotificationUtils { extends ConcurrentHashMap> { private static final String NOTIFICATION_PART_SEPARATOR = " "; - private static final int NUM_NOTIFICATION_PARTS= 4; + private static final int NUM_NOTIFICATION_PARTS = 4; /** * Retuns the unread count for the given NotificationKey. @@ -228,7 +229,8 @@ public class NotificationUtils { if (unreadCount != null && unseenCount != null) { final String[] partValues = new String[] { key.account.uri.toString(), key.folder.folderUri.fullUri.toString(), - unreadCount.toString(), unseenCount.toString()}; + unreadCount.toString(), unseenCount.toString() + }; notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues)); } } @@ -524,8 +526,11 @@ public class NotificationUtils { // We now have all we need to create the notification and the pending intent PendingIntent clickIntent; + int iconResId = NotifyIconUtilities.findNotifyIconForAccountDomain(context, + R.xml.notify_icon_providers, account.getEmailAddress(), + R.drawable.stat_notify_email); NotificationCompat.Builder notification = new NotificationCompat.Builder(context); - notification.setSmallIcon(R.drawable.stat_notify_email); + notification.setSmallIcon(iconResId); notification.setTicker(account.name); final long when; -- cgit v1.2.3