| 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.SharedPreferences; | 7 import android.content.SharedPreferences; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 | 9 |
| 10 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.chrome.browser.ChromeFeatureList; | 11 import org.chromium.chrome.browser.ChromeFeatureList; |
| 12 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; | 12 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; |
| 13 import org.chromium.chrome.browser.profiles.Profile; |
| 14 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
| 13 import org.chromium.components.location.LocationUtils; | 15 import org.chromium.components.location.LocationUtils; |
| 14 | 16 |
| 15 /** | 17 /** |
| 16 * This class provides the basic interface to the Physical Web feature. | 18 * This class provides the basic interface to the Physical Web feature. |
| 17 */ | 19 */ |
| 18 public class PhysicalWeb { | 20 public class PhysicalWeb { |
| 19 public static final int OPTIN_NOTIFY_MAX_TRIES = 1; | 21 public static final int OPTIN_NOTIFY_MAX_TRIES = 1; |
| 20 private static final String PREF_PHYSICAL_WEB_NOTIFY_COUNT = "physical_web_n
otify_count"; | 22 private static final String PREF_PHYSICAL_WEB_NOTIFY_COUNT = "physical_web_n
otify_count"; |
| 21 private static final String PREF_IGNORE_OTHER_CLIENTS = "physical_web_ignore
_other_clients"; | 23 private static final String PREF_IGNORE_OTHER_CLIENTS = "physical_web_ignore
_other_clients"; |
| 22 private static final String FEATURE_NAME = "PhysicalWeb"; | 24 private static final String FEATURE_NAME = "PhysicalWeb"; |
| 23 private static final String IGNORE_OTHER_CLIENTS_FEATURE_NAME = "PhysicalWeb
IgnoreOtherClients"; | 25 private static final String IGNORE_OTHER_CLIENTS_FEATURE_NAME = "PhysicalWeb
IgnoreOtherClients"; |
| 24 private static final int MIN_ANDROID_VERSION = 18; | 26 private static final int MIN_ANDROID_VERSION = 18; |
| 25 | 27 |
| 26 /** | 28 /** |
| 27 * Evaluate whether the environment is one in which the Physical Web should | 29 * Evaluates whether the environment is one in which the Physical Web should |
| 28 * be enabled. | 30 * be enabled. |
| 29 * @return true if the PhysicalWeb should be enabled | 31 * @return true if the PhysicalWeb should be enabled |
| 30 */ | 32 */ |
| 31 public static boolean featureIsEnabled() { | 33 public static boolean featureIsEnabled() { |
| 32 return ChromeFeatureList.isEnabled(FEATURE_NAME) | 34 return ChromeFeatureList.isEnabled(FEATURE_NAME) |
| 33 && Build.VERSION.SDK_INT >= MIN_ANDROID_VERSION; | 35 && Build.VERSION.SDK_INT >= MIN_ANDROID_VERSION; |
| 34 } | 36 } |
| 35 | 37 |
| 36 /** | 38 /** |
| 37 * Checks whether the Physical Web preference is switched to On. | 39 * Checks whether the Physical Web preference is switched to On. |
| 38 * | 40 * |
| 39 * @return boolean {@code true} if the preference is On. | 41 * @return boolean {@code true} if the preference is On. |
| 40 */ | 42 */ |
| 41 public static boolean isPhysicalWebPreferenceEnabled() { | 43 public static boolean isPhysicalWebPreferenceEnabled() { |
| 42 return PrivacyPreferencesManager.getInstance().isPhysicalWebEnabled(); | 44 return PrivacyPreferencesManager.getInstance().isPhysicalWebEnabled(); |
| 43 } | 45 } |
| 44 | 46 |
| 45 /** | 47 /** |
| 46 * Checks whether the Physical Web onboard flow is active and the user has | 48 * Checks whether the Physical Web onboard flow is active and the user has |
| 47 * not yet elected to either enable or decline the feature. | 49 * not yet elected to either enable or decline the feature. |
| 48 * | 50 * |
| 49 * @return boolean {@code true} if onboarding is complete. | 51 * @return boolean {@code true} if onboarding is complete. |
| 50 */ | 52 */ |
| 51 public static boolean isOnboarding() { | 53 public static boolean isOnboarding() { |
| 52 return PrivacyPreferencesManager.getInstance().isPhysicalWebOnboarding()
; | 54 return PrivacyPreferencesManager.getInstance().isPhysicalWebOnboarding()
; |
| 53 } | 55 } |
| 54 | 56 |
| 55 /** | 57 /** |
| 56 * Start the Physical Web feature. | 58 * Starts the Physical Web feature. |
| 57 * At the moment, this only enables URL discovery over BLE. | 59 * At the moment, this only enables URL discovery over BLE. |
| 58 */ | 60 */ |
| 59 public static void startPhysicalWeb() { | 61 public static void startPhysicalWeb() { |
| 60 // Only subscribe to Nearby if we have the location permission. | 62 // Only subscribe to Nearby if we have the location permission. |
| 61 LocationUtils locationUtils = LocationUtils.getInstance(); | 63 LocationUtils locationUtils = LocationUtils.getInstance(); |
| 62 if (locationUtils.hasAndroidLocationPermission() | 64 if (locationUtils.hasAndroidLocationPermission() |
| 63 && locationUtils.isSystemLocationSettingEnabled()) { | 65 && locationUtils.isSystemLocationSettingEnabled()) { |
| 64 new NearbyBackgroundSubscription(NearbySubscription.SUBSCRIBE).run()
; | 66 new NearbyBackgroundSubscription(NearbySubscription.SUBSCRIBE).run()
; |
| 65 } | 67 } |
| 66 } | 68 } |
| 67 | 69 |
| 68 /** | 70 /** |
| 69 * Stop the Physical Web feature. | 71 * Stops the Physical Web feature. |
| 70 */ | 72 */ |
| 71 public static void stopPhysicalWeb() { | 73 public static void stopPhysicalWeb() { |
| 72 new NearbyBackgroundSubscription(NearbySubscription.UNSUBSCRIBE, new Run
nable() { | 74 new NearbyBackgroundSubscription(NearbySubscription.UNSUBSCRIBE, new Run
nable() { |
| 73 @Override | 75 @Override |
| 74 public void run() { | 76 public void run() { |
| 75 // 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. |
| 76 UrlManager.getInstance().clearAllUrls(); | 78 UrlManager.getInstance().clearAllUrls(); |
| 77 } | 79 } |
| 78 }).run(); | 80 }).run(); |
| 79 } | 81 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 101 * 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 |
| 102 * has been shown. | 104 * has been shown. |
| 103 * @return an integer representing the high-priority notifification display
count. | 105 * @return an integer representing the high-priority notifification display
count. |
| 104 */ | 106 */ |
| 105 public static int getOptInNotifyCount() { | 107 public static int getOptInNotifyCount() { |
| 106 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); | 108 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); |
| 107 return sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); | 109 return sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); |
| 108 } | 110 } |
| 109 | 111 |
| 110 /** | 112 /** |
| 111 * Perform various Physical Web operations that should happen on startup. | 113 * Performs various Physical Web operations that should happen on startup. |
| 112 */ | 114 */ |
| 113 public static void onChromeStart() { | 115 public static void onChromeStart() { |
| 114 // The PhysicalWebUma calls in this method should be called only when th
e native library is | 116 if (!featureIsEnabled()) { |
| 115 // loaded. This is always the case on chrome startup. | 117 stopPhysicalWeb(); |
| 116 if (featureIsEnabled() && (isPhysicalWebPreferenceEnabled() || isOnboard
ing())) { | 118 return; |
| 119 } |
| 120 |
| 121 // If this user is in the default state, we need to check if we should e
nable Physical Web. |
| 122 if (isOnboarding() && shouldAutoEnablePhysicalWeb()) { |
| 123 PrivacyPreferencesManager.getInstance().setPhysicalWebEnabled(true); |
| 124 } |
| 125 |
| 126 if (isPhysicalWebPreferenceEnabled()) { |
| 117 boolean ignoreOtherClients = | 127 boolean ignoreOtherClients = |
| 118 ChromeFeatureList.isEnabled(IGNORE_OTHER_CLIENTS_FEATURE_NAM
E); | 128 ChromeFeatureList.isEnabled(IGNORE_OTHER_CLIENTS_FEATURE_NAM
E); |
| 119 ContextUtils.getAppSharedPreferences().edit() | 129 ContextUtils.getAppSharedPreferences().edit() |
| 120 .putBoolean(PREF_IGNORE_OTHER_CLIENTS, ignoreOtherClients) | 130 .putBoolean(PREF_IGNORE_OTHER_CLIENTS, ignoreOtherClients) |
| 121 .apply(); | 131 .apply(); |
| 122 startPhysicalWeb(); | 132 startPhysicalWeb(); |
| 133 // The PhysicalWebUma call in this method should be called only when
the native library |
| 134 // is loaded. This is always the case on chrome startup. |
| 123 PhysicalWebUma.uploadDeferredMetrics(); | 135 PhysicalWebUma.uploadDeferredMetrics(); |
| 124 } else { | |
| 125 stopPhysicalWeb(); | |
| 126 } | 136 } |
| 127 } | 137 } |
| 138 |
| 139 /** |
| 140 * Checks if this device should have Physical Web automatically enabled. |
| 141 */ |
| 142 private static boolean shouldAutoEnablePhysicalWeb() { |
| 143 LocationUtils locationUtils = LocationUtils.getInstance(); |
| 144 return locationUtils.isSystemLocationSettingEnabled() |
| 145 && locationUtils.hasAndroidLocationPermission() |
| 146 && TemplateUrlService.getInstance().isDefaultSearchEngineGoogle(
) |
| 147 && !Profile.getLastUsedProfile().isOffTheRecord(); |
| 148 } |
| 128 } | 149 } |
| OLD | NEW |