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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java

Issue 1326593006: Add the Physical Web to Chrome on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add UrlManager class Created 5 years, 3 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/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);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698