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

Side by Side 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: Handled jdduke's comments Created 5 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.physicalweb;
6
7 import android.app.Notification;
8 import android.content.Context;
9 import android.content.SharedPreferences;
10 import android.content.res.Resources;
11 import android.support.v4.app.NotificationCompat;
12 import android.support.v4.app.NotificationManagerCompat;
13
14 import org.chromium.chrome.R;
15 import org.chromium.chrome.browser.notifications.NotificationConstants;
16
17 import java.util.HashSet;
18 import java.util.Set;
19
20 /**
21 * This class stores URLs and surfaces notifications to the user.
22 */
23 public class UrlManager {
24 private static final String TAG = "PhysicalWeb";
25 private static final String PREFS_NAME = "org.chromium.chrome.browser.physic alweb.URL_CACHE";
26 private static final String PREFS_VERSION_KEY = "version";
27 private static final String PREFS_URLS_KEY = "urls";
28 private static final int PREFS_VERSION = 1;
29 private static UrlManager sInstance = null;
30 private final Context mContext;
31 private final NotificationManagerCompat mNotificationManager;
32
33 /**
34 * Construct the UrlManager.
35 * @param context An instance of android.content.Context
36 */
37 public UrlManager(Context context) {
38 mContext = context;
39 mNotificationManager = NotificationManagerCompat.from(context);
40 }
41
42 /**
43 * Get a singleton instance of this class.
44 * @param context An instance of android.content.Context.
45 * @return A singleton instance of this class.
46 */
47 public static UrlManager getInstance(Context context) {
48 if (sInstance == null) {
49 sInstance = new UrlManager(context);
50 }
51 return sInstance;
52 }
53
54 /**
55 * Add a URL to the store of URLs.
56 * This method additionally updates the Physical Web notification.
57 * @param url The URL to add.
58 */
59 public void addUrl(String url) {
60 Set<String> urls = getCachedUrls();
61 urls.add(url);
62 putCachedUrls(urls);
63 updateNotification(urls);
64 }
65
66 /**
67 * Remove a URL to the store of URLs.
68 * This method additionally updates the Physical Web notification.
69 * @param url The URL to remove.
70 */
71 public void removeUrl(String url) {
72 Set<String> urls = getCachedUrls();
73 urls.remove(url);
74 putCachedUrls(urls);
75 updateNotification(urls);
76 }
77
78 private Set<String> getCachedUrls() {
79 // Check the version.
80 SharedPreferences prefs = mContext.getSharedPreferences(PREFS_NAME, Cont ext.MODE_PRIVATE);
81 int prefsVersion = prefs.getInt(PREFS_VERSION_KEY, 0);
82 if (prefsVersion != PREFS_VERSION) {
83 return new HashSet<String>();
84 }
85
86 // Restore the cached urls.
87 return prefs.getStringSet(PREFS_URLS_KEY, new HashSet<String>());
88 }
89
90 private void putCachedUrls(Set<String> urls) {
91 // Write the version.
92 SharedPreferences prefs = mContext.getSharedPreferences(PREFS_NAME, Cont ext.MODE_PRIVATE);
93 SharedPreferences.Editor editor = prefs.edit();
94 editor.putInt(PREFS_VERSION_KEY, PREFS_VERSION);
95
96 // Write the urls.
97 editor.putStringSet(PREFS_URLS_KEY, urls);
98 editor.apply();
99 }
100
101 private void updateNotification(Set<String> urls) {
102 if (urls.isEmpty()) {
103 mNotificationManager.cancel(NotificationConstants.NOTIFICATION_ID_PH YSICAL_WEB);
104 return;
105 }
106
107 // Get values to display.
108 String displayUrl = urls.iterator().next();
109 Resources resources = mContext.getResources();
110 String title = resources.getQuantityString(R.plurals.physical_web_notifi cation_title,
111 urls.size(), urls.size());
112
113 // Create the notification.
114 Notification notification = new NotificationCompat.Builder(mContext)
115 .setSmallIcon(R.drawable.ic_physical_web_notification)
116 .setContentTitle(title)
117 .setContentText(displayUrl)
118 .setPriority(NotificationCompat.PRIORITY_MIN)
119 .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
120 .build();
121 mNotificationManager.notify(NotificationConstants.NOTIFICATION_ID_PHYSIC AL_WEB,
122 notification);
123 }
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698