OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
6 | 6 |
7 import android.app.Notification; | 7 import android.app.Notification; |
| 8 import android.app.NotificationManager; |
8 import android.app.PendingIntent; | 9 import android.app.PendingIntent; |
9 import android.content.Context; | 10 import android.content.Context; |
10 import android.content.Intent; | 11 import android.content.Intent; |
11 import android.content.SharedPreferences; | 12 import android.content.SharedPreferences; |
12 import android.content.res.Resources; | 13 import android.content.res.Resources; |
13 import android.graphics.Bitmap; | 14 import android.graphics.Bitmap; |
14 import android.graphics.BitmapFactory; | 15 import android.graphics.BitmapFactory; |
15 import android.os.SystemClock; | 16 import android.os.SystemClock; |
16 import android.support.v4.app.NotificationCompat; | 17 import android.support.v4.app.NotificationCompat; |
17 import android.support.v4.app.NotificationManagerCompat; | |
18 | 18 |
19 import org.chromium.base.Log; | 19 import org.chromium.base.Log; |
| 20 import org.chromium.base.VisibleForTesting; |
20 import org.chromium.chrome.R; | 21 import org.chromium.chrome.R; |
21 import org.chromium.chrome.browser.notifications.NotificationConstants; | 22 import org.chromium.chrome.browser.notifications.NotificationConstants; |
| 23 import org.chromium.chrome.browser.notifications.NotificationManagerProxy; |
| 24 import org.chromium.chrome.browser.notifications.NotificationManagerProxyImpl; |
22 | 25 |
23 import java.util.Arrays; | 26 import java.util.Arrays; |
24 import java.util.Collection; | 27 import java.util.Collection; |
25 import java.util.Collections; | 28 import java.util.Collections; |
26 import java.util.HashSet; | 29 import java.util.HashSet; |
27 import java.util.Set; | 30 import java.util.Set; |
28 | 31 |
29 /** | 32 /** |
30 * This class stores URLs which are discovered by scanning for Physical Web beac
ons, and updates a | 33 * This class stores URLs which are discovered by scanning for Physical Web beac
ons, and updates a |
31 * Notification as the set changes. | 34 * Notification as the set changes. |
32 * | 35 * |
33 * There are two sets of URLs maintained: | 36 * There are two sets of URLs maintained: |
34 * - Those which are currently nearby, as tracked by calls to addUrl/removeUrl | 37 * - Those which are currently nearby, as tracked by calls to addUrl/removeUrl |
35 * - Those which have ever resolved through the Physical Web Service (e.g. are k
nown to produce | 38 * - Those which have ever resolved through the Physical Web Service (e.g. are k
nown to produce |
36 * good results). | 39 * good results). |
37 * | 40 * |
38 * Whenever either list changes, we update the Physical Web Notification, based
on the intersection | 41 * Whenever either list changes, we update the Physical Web Notification, based
on the intersection |
39 * of currently-nearby and known-resolved URLs. | 42 * of currently-nearby and known-resolved URLs. |
40 */ | 43 */ |
41 class UrlManager { | 44 class UrlManager { |
42 private static final String TAG = "PhysicalWeb"; | 45 private static final String TAG = "PhysicalWeb"; |
43 private static final String PREFS_NAME = "org.chromium.chrome.browser.physic
alweb.URL_CACHE"; | 46 private static final String PREFS_NAME = "org.chromium.chrome.browser.physic
alweb.URL_CACHE"; |
44 private static final String PREFS_VERSION_KEY = "version"; | 47 private static final String PREFS_VERSION_KEY = "version"; |
45 private static final String PREFS_NEARBY_URLS_KEY = "nearby_urls"; | 48 private static final String PREFS_NEARBY_URLS_KEY = "nearby_urls"; |
46 private static final String PREFS_RESOLVED_URLS_KEY = "resolved_urls"; | 49 private static final String PREFS_RESOLVED_URLS_KEY = "resolved_urls"; |
47 private static final String DEPRECATED_PREFS_URLS_KEY = "urls"; | 50 private static final String DEPRECATED_PREFS_URLS_KEY = "urls"; |
48 private static final int PREFS_VERSION = 2; | 51 private static final int PREFS_VERSION = 2; |
49 private static UrlManager sInstance = null; | 52 private static UrlManager sInstance = null; |
50 private final Context mContext; | 53 private final Context mContext; |
51 private final NotificationManagerCompat mNotificationManager; | 54 private NotificationManagerProxy mNotificationManager; |
52 private final PwsClient mPwsClient; | 55 private PwsClient mPwsClient; |
53 | 56 |
54 /** | 57 /** |
55 * Construct the UrlManager. | 58 * Construct the UrlManager. |
56 * @param context An instance of android.content.Context | 59 * @param context An instance of android.content.Context |
57 */ | 60 */ |
58 public UrlManager(Context context) { | 61 public UrlManager(Context context) { |
59 mContext = context; | 62 mContext = context; |
60 mNotificationManager = NotificationManagerCompat.from(context); | 63 mNotificationManager = new NotificationManagerProxyImpl( |
61 mPwsClient = new PwsClient(); | 64 (NotificationManager) context.getSystemService(Context.NOTIFICAT
ION_SERVICE)); |
| 65 mPwsClient = new PwsClientImpl(); |
62 initSharedPreferences(); | 66 initSharedPreferences(); |
63 } | 67 } |
64 | 68 |
65 /** | 69 /** |
66 * Get a singleton instance of this class. | 70 * Get a singleton instance of this class. |
67 * @param context An instance of android.content.Context. | 71 * @param context An instance of android.content.Context. |
68 * @return A singleton instance of this class. | 72 * @return A singleton instance of this class. |
69 */ | 73 */ |
70 public static UrlManager getInstance(Context context) { | 74 public static UrlManager getInstance(Context context) { |
71 if (sInstance == null) { | 75 if (sInstance == null) { |
72 sInstance = new UrlManager(context); | 76 sInstance = new UrlManager(context); |
73 } | 77 } |
74 return sInstance; | 78 return sInstance; |
75 } | 79 } |
76 | 80 |
77 /** | 81 /** |
78 * Add a URL to the store of URLs. | 82 * Add a URL to the store of URLs. |
79 * This method additionally updates the Physical Web notification. | 83 * This method additionally updates the Physical Web notification. |
80 * @param url The URL to add. | 84 * @param url The URL to add. |
81 */ | 85 */ |
| 86 @VisibleForTesting |
82 public void addUrl(String url) { | 87 public void addUrl(String url) { |
83 Log.d(TAG, "URL found: " + url); | 88 Log.d(TAG, "URL found: " + url); |
84 boolean isOnboarding = PhysicalWeb.isOnboarding(mContext); | 89 boolean isOnboarding = PhysicalWeb.isOnboarding(mContext); |
85 Set<String> nearbyUrls = getCachedNearbyUrls(); | 90 Set<String> nearbyUrls = getCachedNearbyUrls(); |
86 | 91 |
87 boolean wasUrlListEmpty = (isOnboarding && nearbyUrls.isEmpty()) | 92 boolean wasUrlListEmpty = (isOnboarding && nearbyUrls.isEmpty()) |
88 || (!isOnboarding && getUrls().isEmpty()); | 93 || (!isOnboarding && getUrls().isEmpty()); |
89 | 94 |
90 nearbyUrls.add(url); | 95 nearbyUrls.add(url); |
91 putCachedNearbyUrls(nearbyUrls); | 96 putCachedNearbyUrls(nearbyUrls); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) | 340 .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
336 .setAutoCancel(true) | 341 .setAutoCancel(true) |
337 .build(); | 342 .build(); |
338 mNotificationManager.notify(NotificationConstants.NOTIFICATION_ID_PHYSIC
AL_WEB, | 343 mNotificationManager.notify(NotificationConstants.NOTIFICATION_ID_PHYSIC
AL_WEB, |
339 notification); | 344 notification); |
340 } | 345 } |
341 | 346 |
342 private void clearNotification() { | 347 private void clearNotification() { |
343 mNotificationManager.cancel(NotificationConstants.NOTIFICATION_ID_PHYSIC
AL_WEB); | 348 mNotificationManager.cancel(NotificationConstants.NOTIFICATION_ID_PHYSIC
AL_WEB); |
344 } | 349 } |
| 350 |
| 351 @VisibleForTesting |
| 352 void overridePwsClientForTesting(PwsClient pwsClient) { |
| 353 mPwsClient = pwsClient; |
| 354 } |
| 355 |
| 356 @VisibleForTesting |
| 357 void overrideNotificationManagerForTesting( |
| 358 NotificationManagerProxy notificationManager) { |
| 359 mNotificationManager = notificationManager; |
| 360 } |
345 } | 361 } |
OLD | NEW |