Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUIManager.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUIManager.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUIManager.java |
| index 261023eb674897a202f09bb5ac535be03d9179e2..0af4089ec86de5990e459667fd81d02c0330e932 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUIManager.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUIManager.java |
| @@ -12,6 +12,7 @@ |
| import android.content.res.Resources; |
| import android.graphics.Bitmap; |
| import android.graphics.Color; |
| +import android.os.Bundle; |
| import android.support.v4.app.NotificationCompat; |
| import android.util.Log; |
| @@ -20,6 +21,8 @@ |
| import org.chromium.chrome.browser.preferences.Preferences; |
| import org.chromium.chrome.browser.preferences.PreferencesLauncher; |
| import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; |
| +import org.chromium.chrome.browser.preferences.website.WebsitePreferences; |
| +import org.chromium.chrome.browser.preferences.website.WebsiteSettingsCategoryFilter; |
| import org.chromium.chrome.browser.widget.RoundedIconGenerator; |
| /** |
| @@ -121,6 +124,46 @@ public static boolean dispatchNotificationEvent(Intent intent) { |
| return false; |
| } |
| + public static void launchNotificationPreferences(Context context, Intent incomingIntent) { |
|
Peter Beverloo
2015/02/24 17:48:03
Please add a docblock comment since this is a publ
Michael van Ouwerkerk
2015/02/25 18:12:44
Done.
|
| + // Use the application context because it lives longer. When using he given context, it |
| + // may be stopped before the preferences intent is handled. |
| + Context applicationContext = context.getApplicationContext(); |
| + |
| + // If the user touched the settings cog on a flipped notification there will be a |
| + // notification tag extra. From the tag we can read the origin of the notification, and use |
| + // that to open the settings screen for that specific origin. |
| + String notificationTag = |
| + incomingIntent.getStringExtra(NotificationConstants.EXTRA_NOTIFICATION_TAG); |
| + boolean launchSingleSitePreferences = notificationTag != null; |
|
Peter Beverloo
2015/02/24 17:48:02
nit: launchSinceWebsitePreferences to match the fr
Michael van Ouwerkerk
2015/02/25 18:12:45
Done.
|
| + |
| + String fragmentName = launchSingleSitePreferences ? SingleWebsitePreferences.class.getName() |
| + : WebsitePreferences.class.getName(); |
| + Intent preferencesIntent = |
| + PreferencesLauncher.createIntentForSettingsPage(applicationContext, fragmentName); |
| + |
| + Bundle fragmentArguments; |
| + if (launchSingleSitePreferences) { |
| + String origin = |
| + notificationTag.split(NotificationConstants.NOTIFICATION_TAG_SEPARATOR)[0]; |
| + fragmentArguments = SingleWebsitePreferences.createFragmentArgsForSite(origin); |
| + } else { |
| + fragmentArguments = new Bundle(); |
| + fragmentArguments.putString(WebsitePreferences.EXTRA_CATEGORY, |
| + WebsiteSettingsCategoryFilter.FILTER_PUSH_NOTIFICATIONS); |
| + fragmentArguments.putString(WebsitePreferences.EXTRA_TITLE, |
| + applicationContext.getResources().getString( |
| + R.string.push_notifications_permission_title)); |
| + } |
| + preferencesIntent.putExtra(Preferences.EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArguments); |
| + |
| + // When coming from the gear on a flipped notification the intent will not be handled |
|
Peter Beverloo
2015/02/24 17:48:03
s/*/When coming from the gear on a flipped notific
Michael van Ouwerkerk
2015/02/25 18:12:44
Done.
|
| + // correctly without this flag. |
| + if (launchSingleSitePreferences) |
| + preferencesIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| + |
| + applicationContext.startActivity(preferencesIntent); |
| + } |
| + |
| private PendingIntent getPendingIntent(String action, String notificationId, int platformId, |
| byte[] notificationData) { |
| Intent intent = new Intent(action); |
| @@ -133,9 +176,19 @@ private PendingIntent getPendingIntent(String action, String notificationId, int |
| PendingIntent.FLAG_UPDATE_CURRENT); |
| } |
| + private static String makePlatformTag(String tag, int platformId, String origin) { |
| + // The given tag may contain the separator character, so add it last to make reading the |
| + // preceding origin token reliable. If no tag was specified (it is the default empty |
| + // string), make the platform tag unique by appending the platform id. |
| + String platformTag = origin + NotificationConstants.NOTIFICATION_TAG_SEPARATOR + tag; |
|
Peter Beverloo
2015/02/24 17:48:03
Use TextUtils.isEmpty(tag) please, to make sure th
Michael van Ouwerkerk
2015/02/25 18:12:44
Done.
|
| + if (tag.isEmpty()) platformTag += platformId; |
| + return platformTag; |
| + } |
| + |
| /** |
| * Displays a notification with the given |notificationId|, |title|, |body| and |icon|. |
| * |
| + * @param tag A string identifier for this notification. |
| * @param notificationId Unique id provided by the Chrome Notification system. |
| * @param title Title to be displayed in the notification. |
| * @param body Message to be displayed in the notification. Will be trimmed to one line of |
| @@ -147,8 +200,8 @@ private PendingIntent getPendingIntent(String action, String notificationId, int |
| * @return The id using which the notification can be identified. |
| */ |
| @CalledByNative |
| - private int displayNotification(String notificationId, String title, String body, Bitmap icon, |
| - String origin, byte[] notificationData) { |
| + private int displayNotification(String tag, String notificationId, String title, String body, |
| + Bitmap icon, String origin, byte[] notificationData) { |
| if (icon == null || icon.getWidth() == 0) { |
| icon = getIconGenerator().generateIconForUrl(origin, true); |
| } |
| @@ -181,7 +234,8 @@ private int displayNotification(String notificationId, String title, String body |
| pendingSettingsIntent) |
| .setSubText(origin); |
| - mNotificationManager.notify(mLastNotificationId, notificationBuilder.build()); |
| + String platformTag = makePlatformTag(tag, mLastNotificationId, origin); |
| + mNotificationManager.notify(platformTag, mLastNotificationId, notificationBuilder.build()); |
| return mLastNotificationId++; |
| } |
| @@ -213,11 +267,12 @@ private RoundedIconGenerator getIconGenerator() { |
| } |
| /** |
| - * Closes the notification identified by |platformId|. |
| + * Closes the notification identified by |tag|, |platformId|, and |origin|. |
| */ |
| @CalledByNative |
| - private void closeNotification(int platformId) { |
| - mNotificationManager.cancel(platformId); |
| + private void closeNotification(String tag, int platformId, String origin) { |
| + String platformTag = makePlatformTag(tag, platformId, origin); |
| + mNotificationManager.cancel(platformTag, platformId); |
| } |
| private boolean onNotificationClicked(String notificationId, int platformId, |