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 |
index 07b4dc23f5896e19961e0efc3cd25b64f711b128..d99a59de9f15ad84c5f2cf4bae447ef2e5ba9feb 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/UrlManager.java |
@@ -20,12 +20,14 @@ import android.os.SystemClock; |
import android.support.v4.app.NotificationCompat; |
import org.chromium.base.Log; |
+import org.chromium.base.ObserverList; |
import org.chromium.base.VisibleForTesting; |
import org.chromium.chrome.R; |
import org.chromium.chrome.browser.notifications.NotificationConstants; |
import org.chromium.chrome.browser.notifications.NotificationManagerProxy; |
import org.chromium.chrome.browser.notifications.NotificationManagerProxyImpl; |
+import java.util.ArrayList; |
import java.util.Arrays; |
import java.util.Collection; |
import java.util.Collections; |
@@ -57,6 +59,19 @@ class UrlManager { |
private final Context mContext; |
private NotificationManagerProxy mNotificationManager; |
private PwsClient mPwsClient; |
+ private final ObserverList<Listener> mObservers; |
+ |
+ /** |
+ * Interface for observers that should be notified when the nearby URL list changes. |
+ */ |
+ public interface Listener { |
+ /** |
+ * Callback called when one or more URLs are added to the URL list. |
+ * @param urls A set of strings containing nearby URLs resolvable with our resolution |
+ * service. |
+ */ |
+ void onDisplayableUrlsAdded(Collection<String> urls); |
+ } |
/** |
* Construct the UrlManager. |
@@ -67,6 +82,7 @@ class UrlManager { |
mNotificationManager = new NotificationManagerProxyImpl( |
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); |
mPwsClient = new PwsClientImpl(); |
+ mObservers = new ObserverList<Listener>(); |
initSharedPreferences(); |
} |
@@ -83,6 +99,22 @@ class UrlManager { |
} |
/** |
+ * Add an observer to be notified on changes to the nearby URL list. |
+ * @param observer The observer to add. |
+ */ |
+ public void addObserver(Listener observer) { |
+ mObservers.addObserver(observer); |
+ } |
+ |
+ /** |
+ * Remove an observer from the observer list. |
+ * @param observer The observer to remove. |
+ */ |
+ public void removeObserver(Listener observer) { |
+ mObservers.removeObserver(observer); |
+ } |
+ |
+ /** |
* Add a URL to the store of URLs. |
* This method additionally updates the Physical Web notification. |
* @param url The URL to add. |
@@ -93,8 +125,21 @@ class UrlManager { |
boolean isOnboarding = PhysicalWeb.isOnboarding(mContext); |
Set<String> nearbyUrls = getCachedNearbyUrls(); |
- boolean isUrlListEmptyBefore = (isOnboarding && nearbyUrls.isEmpty()) |
- || (!isOnboarding && getUrls().isEmpty()); |
+ // A URL is displayable if it is both nearby and resolved through our resolution service. |
+ // When new displayable URLs are found we tell our observers. In onboarding mode we do not |
+ // use our resolution service so the displayable list should always be empty. However, we |
+ // still want to track the nearby URL count so we can show an opt-in notification. |
+ // In normal operation, both the notification and observers are updated for changes to the |
+ // displayable list. |
+ |
+ int displayableUrlsBefore; |
+ int notificationUrlsBefore; |
+ if (isOnboarding) { |
+ displayableUrlsBefore = 0; |
+ notificationUrlsBefore = nearbyUrls.size(); |
+ } else { |
+ displayableUrlsBefore = notificationUrlsBefore = getUrls().size(); |
+ } |
nearbyUrls.add(url); |
putCachedNearbyUrls(nearbyUrls); |
@@ -103,8 +148,17 @@ class UrlManager { |
resolveUrl(url); |
} |
- boolean isUrlListEmptyAfter = !isOnboarding && getUrls().isEmpty(); |
- updateNotification(isUrlListEmptyBefore, isUrlListEmptyAfter); |
+ int displayableUrlsAfter; |
+ int notificationUrlsAfter; |
+ if (isOnboarding) { |
+ displayableUrlsAfter = 0; |
+ notificationUrlsAfter = nearbyUrls.size(); |
+ } else { |
+ displayableUrlsAfter = notificationUrlsAfter = getUrls().size(); |
+ } |
+ |
+ updateNotification(notificationUrlsBefore == 0, notificationUrlsAfter == 0); |
+ notifyDisplayableUrlsChanged(displayableUrlsBefore, displayableUrlsAfter, url); |
} |
/** |
@@ -120,9 +174,8 @@ class UrlManager { |
nearbyUrls.remove(url); |
putCachedNearbyUrls(nearbyUrls); |
- boolean isUrlListEmptyAfter = (isOnboarding && nearbyUrls.isEmpty()) |
- || (!isOnboarding && getUrls().isEmpty()); |
- updateNotification(false, isUrlListEmptyAfter); |
+ int notificationUrlsAfter = isOnboarding ? nearbyUrls.size() : getUrls().size(); |
+ updateNotification(false, notificationUrlsAfter == 0); |
} |
/** |
@@ -177,11 +230,14 @@ class UrlManager { |
private void addResolvedUrl(String url) { |
Log.d(TAG, "PWS resolved: %s", url); |
Set<String> resolvedUrls = getCachedResolvedUrls(); |
- boolean isUrlListEmptyBefore = getUrls().isEmpty(); |
+ int displayableUrlsBefore = getUrls().size(); |
resolvedUrls.add(url); |
putCachedResolvedUrls(resolvedUrls); |
- updateNotification(isUrlListEmptyBefore, getUrls().isEmpty()); |
+ |
+ int displayableUrlsAfter = getUrls().size(); |
+ updateNotification(displayableUrlsBefore == 0, displayableUrlsAfter == 0); |
+ notifyDisplayableUrlsChanged(displayableUrlsBefore, displayableUrlsAfter, url); |
} |
private void removeResolvedUrl(String url) { |
@@ -377,6 +433,18 @@ class UrlManager { |
alarmManager.cancel(pendingIntent); |
} |
+ private void notifyDisplayableUrlsChanged(int displayCountBefore, int displayCountAfter, |
+ String url) { |
+ if (displayCountAfter > displayCountBefore) { |
+ Collection<String> urls = new ArrayList<String>(); |
+ urls.add(url); |
+ Collection<String> wrappedUrls = Collections.unmodifiableCollection(urls); |
+ for (Listener observer : mObservers) { |
+ observer.onDisplayableUrlsAdded(wrappedUrls); |
+ } |
+ } |
+ } |
+ |
@VisibleForTesting |
void overridePwsClientForTesting(PwsClient pwsClient) { |
mPwsClient = pwsClient; |