| 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.content.Context; | |
| 8 import android.content.SharedPreferences; | 7 import android.content.SharedPreferences; |
| 9 import android.os.Build; | 8 import android.os.Build; |
| 10 | 9 |
| 11 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.chrome.browser.ChromeApplication; | |
| 13 import org.chromium.chrome.browser.ChromeFeatureList; | 11 import org.chromium.chrome.browser.ChromeFeatureList; |
| 14 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; | 12 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; |
| 15 | 13 |
| 16 /** | 14 /** |
| 17 * This class provides the basic interface to the Physical Web feature. | 15 * This class provides the basic interface to the Physical Web feature. |
| 18 */ | 16 */ |
| 19 public class PhysicalWeb { | 17 public class PhysicalWeb { |
| 20 public static final int OPTIN_NOTIFY_MAX_TRIES = 1; | 18 public static final int OPTIN_NOTIFY_MAX_TRIES = 1; |
| 21 private static final String PREF_PHYSICAL_WEB_NOTIFY_COUNT = "physical_web_n
otify_count"; | 19 private static final String PREF_PHYSICAL_WEB_NOTIFY_COUNT = "physical_web_n
otify_count"; |
| 22 private static final String PREF_IGNORE_OTHER_CLIENTS = "physical_web_ignore
_other_clients"; | 20 private static final String PREF_IGNORE_OTHER_CLIENTS = "physical_web_ignore
_other_clients"; |
| 23 private static final String FEATURE_NAME = "PhysicalWeb"; | 21 private static final String FEATURE_NAME = "PhysicalWeb"; |
| 24 private static final String IGNORE_OTHER_CLIENTS_FEATURE_NAME = "PhysicalWeb
IgnoreOtherClients"; | 22 private static final String IGNORE_OTHER_CLIENTS_FEATURE_NAME = "PhysicalWeb
IgnoreOtherClients"; |
| 25 private static final int MIN_ANDROID_VERSION = 18; | 23 private static final int MIN_ANDROID_VERSION = 18; |
| 26 | 24 |
| 27 /** | 25 /** |
| 28 * Evaluate whether the environment is one in which the Physical Web should | 26 * Evaluate whether the environment is one in which the Physical Web should |
| 29 * be enabled. | 27 * be enabled. |
| 30 * @return true if the PhysicalWeb should be enabled | 28 * @return true if the PhysicalWeb should be enabled |
| 31 */ | 29 */ |
| 32 public static boolean featureIsEnabled() { | 30 public static boolean featureIsEnabled() { |
| 33 return ChromeFeatureList.isEnabled(FEATURE_NAME) | 31 return ChromeFeatureList.isEnabled(FEATURE_NAME) |
| 34 && Build.VERSION.SDK_INT >= MIN_ANDROID_VERSION; | 32 && Build.VERSION.SDK_INT >= MIN_ANDROID_VERSION; |
| 35 } | 33 } |
| 36 | 34 |
| 37 /** | 35 /** |
| 38 * Checks whether the Physical Web preference is switched to On. | 36 * Checks whether the Physical Web preference is switched to On. |
| 39 * | 37 * |
| 40 * @param context An instance of android.content.Context | |
| 41 * @return boolean {@code true} if the preference is On. | 38 * @return boolean {@code true} if the preference is On. |
| 42 */ | 39 */ |
| 43 public static boolean isPhysicalWebPreferenceEnabled(Context context) { | 40 public static boolean isPhysicalWebPreferenceEnabled() { |
| 44 return PrivacyPreferencesManager.getInstance().isPhysicalWebEnabled(); | 41 return PrivacyPreferencesManager.getInstance().isPhysicalWebEnabled(); |
| 45 } | 42 } |
| 46 | 43 |
| 47 /** | 44 /** |
| 48 * Checks whether the Physical Web onboard flow is active and the user has | 45 * Checks whether the Physical Web onboard flow is active and the user has |
| 49 * not yet elected to either enable or decline the feature. | 46 * not yet elected to either enable or decline the feature. |
| 50 * | 47 * |
| 51 * @param context An instance of android.content.Context | |
| 52 * @return boolean {@code true} if onboarding is complete. | 48 * @return boolean {@code true} if onboarding is complete. |
| 53 */ | 49 */ |
| 54 public static boolean isOnboarding(Context context) { | 50 public static boolean isOnboarding() { |
| 55 return PrivacyPreferencesManager.getInstance().isPhysicalWebOnboarding()
; | 51 return PrivacyPreferencesManager.getInstance().isPhysicalWebOnboarding()
; |
| 56 } | 52 } |
| 57 | 53 |
| 58 /** | 54 /** |
| 59 * Start the Physical Web feature. | 55 * Start the Physical Web feature. |
| 60 * At the moment, this only enables URL discovery over BLE. | 56 * At the moment, this only enables URL discovery over BLE. |
| 61 * @param application An instance of {@link ChromeApplication}, used to get
the | |
| 62 * appropriate PhysicalWebBleClient implementation. | |
| 63 */ | 57 */ |
| 64 public static void startPhysicalWeb(final ChromeApplication application) { | 58 public static void startPhysicalWeb() { |
| 65 PhysicalWebBleClient.getInstance(application).backgroundSubscribe(new Ru
nnable() { | 59 PhysicalWebBleClient.getInstance().backgroundSubscribe(new Runnable() { |
| 66 @Override | 60 @Override |
| 67 public void run() { | 61 public void run() { |
| 68 // We need to clear the list of nearby URLs so that they can be
repopulated by the | 62 // We need to clear the list of nearby URLs so that they can be
repopulated by the |
| 69 // new subscription, but we don't know whether we are already su
bscribed, so we need | 63 // new subscription, but we don't know whether we are already su
bscribed, so we need |
| 70 // to pass a callback so that we can clear as soon as we are res
ubscribed. | 64 // to pass a callback so that we can clear as soon as we are res
ubscribed. |
| 71 UrlManager.getInstance(application).clearNearbyUrls(); | 65 UrlManager.getInstance().clearNearbyUrls(); |
| 72 } | 66 } |
| 73 }); | 67 }); |
| 74 } | 68 } |
| 75 | 69 |
| 76 /** | 70 /** |
| 77 * Stop the Physical Web feature. | 71 * Stop the Physical Web feature. |
| 78 * @param application An instance of {@link ChromeApplication}, used to get
the | |
| 79 * appropriate PhysicalWebBleClient implementation. | |
| 80 */ | 72 */ |
| 81 public static void stopPhysicalWeb(final ChromeApplication application) { | 73 public static void stopPhysicalWeb() { |
| 82 PhysicalWebBleClient.getInstance(application).backgroundUnsubscribe(new
Runnable() { | 74 PhysicalWebBleClient.getInstance().backgroundUnsubscribe(new Runnable()
{ |
| 83 @Override | 75 @Override |
| 84 public void run() { | 76 public void run() { |
| 85 // This isn't absolutely necessary, but it's nice to clean up al
l our shared prefs. | 77 // This isn't absolutely necessary, but it's nice to clean up al
l our shared prefs. |
| 86 UrlManager.getInstance(application).clearAllUrls(); | 78 UrlManager.getInstance().clearAllUrls(); |
| 87 } | 79 } |
| 88 }); | 80 }); |
| 89 } | 81 } |
| 90 | 82 |
| 91 /** | 83 /** |
| 92 * Returns true if we should fire notifications regardless of the existence
of other Physical | 84 * Returns true if we should fire notifications regardless of the existence
of other Physical |
| 93 * Web clients. | 85 * Web clients. |
| 94 * This method is for use when the native library is not available. | 86 * This method is for use when the native library is not available. |
| 95 */ | 87 */ |
| 96 public static boolean shouldIgnoreOtherClients() { | 88 public static boolean shouldIgnoreOtherClients() { |
| 97 return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IGNORE_OTH
ER_CLIENTS, false); | 89 return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IGNORE_OTH
ER_CLIENTS, false); |
| 98 } | 90 } |
| 99 | 91 |
| 100 /** | 92 /** |
| 101 * Increments a value tracking how many times we've shown the Physical Web | 93 * Increments a value tracking how many times we've shown the Physical Web |
| 102 * opt-in notification. | 94 * opt-in notification. |
| 103 * | |
| 104 * @param context An instance of android.content.Context | |
| 105 */ | 95 */ |
| 106 public static void recordOptInNotification(Context context) { | 96 public static void recordOptInNotification() { |
| 107 SharedPreferences sharedPreferences = | 97 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); |
| 108 ContextUtils.getAppSharedPreferences(); | |
| 109 int value = sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); | 98 int value = sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); |
| 110 sharedPreferences.edit().putInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, value +
1).apply(); | 99 sharedPreferences.edit().putInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, value +
1).apply(); |
| 111 } | 100 } |
| 112 | 101 |
| 113 /** | 102 /** |
| 114 * Gets the current count of how many times a high-priority opt-in notificat
ion | 103 * Gets the current count of how many times a high-priority opt-in notificat
ion |
| 115 * has been shown. | 104 * has been shown. |
| 116 * | |
| 117 * @param context An instance of android.content.Context | |
| 118 * @return an integer representing the high-priority notifification display
count. | 105 * @return an integer representing the high-priority notifification display
count. |
| 119 */ | 106 */ |
| 120 public static int getOptInNotifyCount(Context context) { | 107 public static int getOptInNotifyCount() { |
| 121 SharedPreferences sharedPreferences = | 108 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); |
| 122 ContextUtils.getAppSharedPreferences(); | |
| 123 return sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); | 109 return sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); |
| 124 } | 110 } |
| 125 | 111 |
| 126 /** | 112 /** |
| 127 * Perform various Physical Web operations that should happen on startup. | 113 * Perform various Physical Web operations that should happen on startup. |
| 128 * @param application An instance of {@link ChromeApplication}. | |
| 129 */ | 114 */ |
| 130 public static void onChromeStart(ChromeApplication application) { | 115 public static void onChromeStart() { |
| 131 // The PhysicalWebUma calls in this method should be called only when th
e native library is | 116 // The PhysicalWebUma calls in this method should be called only when th
e native library is |
| 132 // loaded. This is always the case on chrome startup. | 117 // loaded. This is always the case on chrome startup. |
| 133 if (featureIsEnabled() | 118 if (featureIsEnabled() && (isPhysicalWebPreferenceEnabled() || isOnboard
ing())) { |
| 134 && (isPhysicalWebPreferenceEnabled(application) || isOnboarding(
application))) { | |
| 135 boolean ignoreOtherClients = | 119 boolean ignoreOtherClients = |
| 136 ChromeFeatureList.isEnabled(IGNORE_OTHER_CLIENTS_FEATURE_NAM
E); | 120 ChromeFeatureList.isEnabled(IGNORE_OTHER_CLIENTS_FEATURE_NAM
E); |
| 137 ContextUtils.getAppSharedPreferences().edit() | 121 ContextUtils.getAppSharedPreferences().edit() |
| 138 .putBoolean(PREF_IGNORE_OTHER_CLIENTS, ignoreOtherClients) | 122 .putBoolean(PREF_IGNORE_OTHER_CLIENTS, ignoreOtherClients) |
| 139 .apply(); | 123 .apply(); |
| 140 startPhysicalWeb(application); | 124 startPhysicalWeb(); |
| 141 PhysicalWebUma.uploadDeferredMetrics(application); | 125 PhysicalWebUma.uploadDeferredMetrics(); |
| 142 } else { | 126 } else { |
| 143 stopPhysicalWeb(application); | 127 stopPhysicalWeb(); |
| 144 } | 128 } |
| 145 } | 129 } |
| 146 } | 130 } |
| OLD | NEW |