Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotificationHelper.java

Issue 2616893003: Hide notifications after their deadline (Closed)
Patch Set: import annotation Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotificationHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotificationHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotificationHelper.java
index dcf20b80496c2c3e9fbb682c36fdafd8d236df9d..337a3a997a905b558711614896dae30c57f4c9ae 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotificationHelper.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotificationHelper.java
@@ -4,13 +4,18 @@
package org.chromium.chrome.browser.ntp;
+import android.annotation.TargetApi;
+import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
+import android.os.Build;
import android.provider.Browser;
+import android.service.notification.StatusBarNotification;
import android.support.v4.app.NotificationCompat;
import org.chromium.base.ContextUtils;
@@ -27,10 +32,21 @@ import org.chromium.chrome.browser.document.ChromeLauncherActivity;
*/
public class ContentSuggestionsNotificationHelper {
private static final String NOTIFICATION_TAG = "ContentSuggestionsNotification";
- private static final int NOTIFICATION_ID = 0;
+ private static final String NOTIFICATION_ID_EXTRA = "notification_id";
private ContentSuggestionsNotificationHelper() {} // Prevent instantiation
+ /**
+ * Removes the notification after a timeout period.
+ */
+ public static final class TimeoutReceiver extends BroadcastReceiver {
+ public void onReceive(Context context, Intent intent) {
+ int id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1);
+ if (id < 0) return;
+ hideNotification(id);
+ }
+ }
+
@CalledByNative
private static void openUrl(String url) {
Context context = ContextUtils.getApplicationContext();
@@ -45,11 +61,26 @@ public class ContentSuggestionsNotificationHelper {
context.startActivity(intent);
}
+ @TargetApi(Build.VERSION_CODES.M)
@CalledByNative
- private static void showNotification(String url, String title, String text, Bitmap image) {
+ private static void showNotification(
+ String url, String title, String text, Bitmap image, long timeoutAtMillis) {
+ // Post notification.
Context context = ContextUtils.getApplicationContext();
NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+
+ // Find an available notification ID.
+ int nextId = 0;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ for (StatusBarNotification activeNotification : manager.getActiveNotifications()) {
+ if (activeNotification.getTag() != NOTIFICATION_TAG) continue;
+ if (activeNotification.getId() >= nextId) {
+ nextId = activeNotification.getId() + 1;
+ }
+ }
+ }
+
Intent intent = new Intent()
.setAction(Intent.ACTION_VIEW)
.setData(Uri.parse(url))
@@ -65,14 +96,42 @@ public class ContentSuggestionsNotificationHelper {
.setGroup(NOTIFICATION_TAG)
.setLargeIcon(image)
.setSmallIcon(R.drawable.ic_chrome);
- manager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, builder.build());
+ manager.notify(NOTIFICATION_TAG, nextId, builder.build());
+
+ // Set timeout.
+ if (timeoutAtMillis != Long.MAX_VALUE) {
+ AlarmManager alarmManager =
+ (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ Intent timeoutIntent = new Intent(context, TimeoutReceiver.class)
+ .setData(Uri.parse(url))
+ .putExtra(NOTIFICATION_ID_EXTRA, nextId);
+ alarmManager.set(AlarmManager.RTC, timeoutAtMillis,
+ PendingIntent.getBroadcast(
+ context, 0, timeoutIntent, PendingIntent.FLAG_UPDATE_CURRENT));
+ }
+ }
+
+ private static void hideNotification(int id) {
+ Context context = ContextUtils.getApplicationContext();
+ NotificationManager manager =
+ (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ manager.cancel(NOTIFICATION_TAG, id);
}
+ @TargetApi(Build.VERSION_CODES.M)
@CalledByNative
- private static void hideNotification() {
+ private static void hideAllNotifications() {
Context context = ContextUtils.getApplicationContext();
NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- manager.cancel(NOTIFICATION_TAG, NOTIFICATION_ID);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ for (StatusBarNotification activeNotification : manager.getActiveNotifications()) {
+ if (activeNotification.getTag() == NOTIFICATION_TAG) {
+ manager.cancel(NOTIFICATION_TAG, activeNotification.getId());
+ }
+ }
+ } else {
+ manager.cancel(NOTIFICATION_TAG, 0);
+ }
}
}
« no previous file with comments | « chrome/android/java/AndroidManifest.xml ('k') | chrome/browser/android/ntp/content_suggestions_notification_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698