Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0423432100199f76cb445d4ab7340bb0642a163a |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.physicalweb; |
| + |
| +import android.app.Notification; |
| +import android.content.Context; |
| +import android.content.SharedPreferences; |
| +import android.content.res.Resources; |
| +import android.support.v4.app.NotificationCompat; |
| +import android.support.v4.app.NotificationManagerCompat; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.chrome.R; |
| + |
| +import java.util.HashSet; |
| +import java.util.Set; |
| +import java.util.concurrent.atomic.AtomicReference; |
| + |
| +/** |
| + * This class stores URLs and surfaces notifications to the user. |
| + */ |
| +public class UrlManager { |
| + private static AtomicReference<UrlManager> sInstance = new AtomicReference<UrlManager>(); |
|
nyquist
2015/09/15 18:16:46
final
cco3
2015/09/16 17:41:29
Done.
|
| + private static final String TAG = "cr.PhysicalWeb"; |
| + private static final String PREFS_NAME = "org.chromium.chrome.browser.physicalweb.URL_CACHE"; |
| + private static final String PREFS_VERSION_KEY = "version"; |
| + private static final String PREFS_URLS_KEY = "urls"; |
| + private static final int PREFS_VERSION = 1; |
| + private static final int NOTIFICATION_ID = 23; |
| + private Context mContext; |
|
nyquist
2015/09/15 18:16:46
final
cco3
2015/09/16 17:41:29
Done.
|
| + private NotificationManagerCompat mNotificationManager; |
|
nyquist
2015/09/15 18:16:46
final
cco3
2015/09/16 17:41:28
Done.
|
| + |
| + public UrlManager(Context context) { |
| + mContext = context; |
| + mNotificationManager = NotificationManagerCompat.from(context); |
| + } |
| + |
| + public static UrlManager getInstance(Context context) { |
| + if (sInstance.get() == null) { |
| + sInstance.compareAndSet(null, new UrlManager(context)); |
| + } |
| + return sInstance.get(); |
| + } |
| + |
| + public void addUrl(String url) { |
| + Log.d(TAG, "Adding URL: " + url); |
| + Set<String> urls = getCachedUrls(); |
| + urls.add(url); |
| + putCachedUrls(urls); |
| + updateNotification(urls); |
| + } |
| + |
| + public void removeUrl(String url) { |
| + Log.d(TAG, "Removing URL: " + url); |
| + Set<String> urls = getCachedUrls(); |
| + urls.remove(url); |
| + putCachedUrls(urls); |
| + updateNotification(urls); |
| + } |
| + |
| + private Set<String> getCachedUrls() { |
| + // Check the version |
| + SharedPreferences prefs = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
| + int prefsVersion = prefs.getInt(PREFS_VERSION_KEY, 0); |
| + if (prefsVersion != PREFS_VERSION) { |
| + return new HashSet<String>(); |
| + } |
| + |
| + // Restore the cached urls |
| + return prefs.getStringSet(PREFS_URLS_KEY, new HashSet<String>()); |
| + } |
| + |
| + private void putCachedUrls(Set<String> urls) { |
| + // Write the version |
| + SharedPreferences prefs = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
| + SharedPreferences.Editor editor = prefs.edit(); |
| + editor.putInt(PREFS_VERSION_KEY, PREFS_VERSION); |
| + |
| + // Write the urls |
| + editor.putStringSet(PREFS_URLS_KEY, urls); |
| + editor.apply(); |
| + } |
| + |
| + private void updateNotification(Set<String> urls) { |
| + if (urls.isEmpty()) { |
| + mNotificationManager.cancelAll(); |
|
nyquist
2015/09/15 18:16:46
Does this cancel other notifications from Chrome,
cco3
2015/09/16 17:41:28
Done.
|
| + return; |
| + } |
| + |
| + // Get values to display |
| + String displayUrl = urls.iterator().next(); |
| + Resources resources = mContext.getResources(); |
| + String title = resources.getQuantityString(R.plurals.physical_web_notification_title, |
| + urls.size(), urls.size()); |
| + |
| + //PendingIntent pendingIntent = createReturnToAppPendingIntent(); |
|
nyquist
2015/09/15 18:16:46
Remove commented out line.
cco3
2015/09/16 17:41:29
Done.
|
| + Notification notification = new NotificationCompat.Builder(mContext) |
| + .setSmallIcon(R.drawable.ic_physical_web_notification) |
| + .setContentTitle(title) |
| + .setContentText(displayUrl) |
| + //.setContentIntent(pendingIntent) |
|
nyquist
2015/09/15 18:16:46
Remove commented out line.
cco3
2015/09/16 17:41:28
Done.
|
| + .setPriority(NotificationCompat.PRIORITY_MIN) |
| + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
| + .build(); |
| + mNotificationManager.notify(NOTIFICATION_ID, notification); |
| + } |
| +} |