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.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.bluetooth.BluetoothAdapter; | 8 import android.bluetooth.BluetoothAdapter; |
9 import android.bluetooth.BluetoothManager; | 9 import android.bluetooth.BluetoothManager; |
10 import android.content.Context; | 10 import android.content.Context; |
11 import android.content.Intent; | 11 import android.content.Intent; |
12 import android.content.SharedPreferences; | |
13 import android.net.Uri; | 12 import android.net.Uri; |
| 13 import android.os.AsyncTask; |
14 import android.os.Build; | 14 import android.os.Build; |
15 | 15 |
16 import org.chromium.base.ContextUtils; | 16 import org.chromium.base.ContextUtils; |
17 import org.chromium.chrome.browser.ChromeFeatureList; | 17 import org.chromium.chrome.browser.ChromeFeatureList; |
18 import org.chromium.chrome.browser.IntentHandler; | 18 import org.chromium.chrome.browser.IntentHandler; |
19 import org.chromium.chrome.browser.UrlConstants; | 19 import org.chromium.chrome.browser.UrlConstants; |
20 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; | 20 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; |
21 import org.chromium.chrome.browser.profiles.Profile; | 21 import org.chromium.chrome.browser.profiles.Profile; |
22 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | 22 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
23 import org.chromium.components.location.LocationUtils; | 23 import org.chromium.components.location.LocationUtils; |
24 | 24 |
25 /** | 25 /** |
26 * This class provides the basic interface to the Physical Web feature. | 26 * This class provides the basic interface to the Physical Web feature. |
27 */ | 27 */ |
28 public class PhysicalWeb { | 28 public class PhysicalWeb { |
29 public static final int OPTIN_NOTIFY_MAX_TRIES = 1; | 29 public static final int OPTIN_NOTIFY_MAX_TRIES = 1; |
30 private static final String PHYSICAL_WEB_SHARING_PREFERENCE = "physical_web_
sharing"; | 30 private static final String PHYSICAL_WEB_SHARING_PREFERENCE = "physical_web_
sharing"; |
31 private static final String PREF_PHYSICAL_WEB_NOTIFY_COUNT = "physical_web_n
otify_count"; | |
32 private static final String FEATURE_NAME = "PhysicalWeb"; | 31 private static final String FEATURE_NAME = "PhysicalWeb"; |
33 private static final String PHYSICAL_WEB_SHARING_FEATURE_NAME = "PhysicalWeb
Sharing"; | 32 private static final String PHYSICAL_WEB_SHARING_FEATURE_NAME = "PhysicalWeb
Sharing"; |
34 private static final int MIN_ANDROID_VERSION = 18; | 33 private static final int MIN_ANDROID_VERSION = 18; |
35 | 34 |
36 /** | 35 /** |
37 * Evaluates whether the environment is one in which the Physical Web should | 36 * Evaluates whether the environment is one in which the Physical Web should |
38 * be enabled. | 37 * be enabled. |
39 * @return true if the PhysicalWeb should be enabled | 38 * @return true if the PhysicalWeb should be enabled |
40 */ | 39 */ |
41 public static boolean featureIsEnabled() { | 40 public static boolean featureIsEnabled() { |
(...skipping 18 matching lines...) Expand all Loading... |
60 public static boolean sharingIsEnabled() { | 59 public static boolean sharingIsEnabled() { |
61 return ChromeFeatureList.isEnabled(PHYSICAL_WEB_SHARING_FEATURE_NAME); | 60 return ChromeFeatureList.isEnabled(PHYSICAL_WEB_SHARING_FEATURE_NAME); |
62 } | 61 } |
63 | 62 |
64 /** | 63 /** |
65 * Checks whether the user has consented to use the Sharing feature. | 64 * Checks whether the user has consented to use the Sharing feature. |
66 * | 65 * |
67 * @return boolean {@code true} if the feature is enabled | 66 * @return boolean {@code true} if the feature is enabled |
68 */ | 67 */ |
69 public static boolean sharingIsOptedIn() { | 68 public static boolean sharingIsOptedIn() { |
70 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); | 69 return ContextUtils.getAppSharedPreferences() |
71 return sharedPrefs.getBoolean(PHYSICAL_WEB_SHARING_PREFERENCE, false); | 70 .getBoolean(PHYSICAL_WEB_SHARING_PREFERENCE, false); |
72 } | 71 } |
73 | 72 |
74 /** | 73 /** |
75 * Sets the preference that the user has opted into use the Sharing feature. | 74 * Sets the preference that the user has opted into use the Sharing feature. |
76 */ | 75 */ |
77 public static void setSharingOptedIn() { | 76 public static void setSharingOptedIn() { |
78 ContextUtils.getAppSharedPreferences() | 77 ContextUtils.getAppSharedPreferences() |
79 .edit() | 78 .edit() |
80 .putBoolean(PHYSICAL_WEB_SHARING_PREFERENCE, true) | 79 .putBoolean(PHYSICAL_WEB_SHARING_PREFERENCE, true) |
81 .apply(); | 80 .apply(); |
82 } | 81 } |
83 | 82 |
84 /** | 83 /** |
85 * Checks whether the Physical Web onboard flow is active and the user has | 84 * Checks whether the Physical Web onboard flow is active and the user has |
86 * not yet elected to either enable or decline the feature. | 85 * not yet elected to either enable or decline the feature. |
87 * | 86 * |
88 * @return boolean {@code true} if onboarding is complete. | 87 * @return boolean {@code true} if onboarding is complete. |
89 */ | 88 */ |
90 public static boolean isOnboarding() { | 89 public static boolean isOnboarding() { |
91 return PrivacyPreferencesManager.getInstance().isPhysicalWebOnboarding()
; | 90 return PrivacyPreferencesManager.getInstance().isPhysicalWebOnboarding()
; |
92 } | 91 } |
93 | 92 |
94 /** | 93 /** |
95 * Increments a value tracking how many times we've shown the Physical Web | |
96 * opt-in notification. | |
97 */ | |
98 public static void recordOptInNotification() { | |
99 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); | |
100 int value = sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); | |
101 sharedPreferences.edit().putInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, value +
1).apply(); | |
102 } | |
103 | |
104 /** | |
105 * Gets the current count of how many times a high-priority opt-in notificat
ion | |
106 * has been shown. | |
107 * @return an integer representing the high-priority notifification display
count. | |
108 */ | |
109 public static int getOptInNotifyCount() { | |
110 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); | |
111 return sharedPreferences.getInt(PREF_PHYSICAL_WEB_NOTIFY_COUNT, 0); | |
112 } | |
113 | |
114 /** | |
115 * Performs various Physical Web operations that should happen on startup. | 94 * Performs various Physical Web operations that should happen on startup. |
116 */ | 95 */ |
117 public static void onChromeStart() { | 96 public static void onChromeStart() { |
118 // In the case that the user has disabled our flag and restarted, this i
s a minimal code | 97 // In the case that the user has disabled our flag and restarted, this i
s a minimal code |
119 // path to disable our subscription to Nearby. | 98 // path to disable our subscription to Nearby. |
120 if (!featureIsEnabled()) { | 99 if (!featureIsEnabled()) { |
121 new NearbyBackgroundSubscription(NearbySubscription.UNSUBSCRIBE).run
(); | 100 new NearbyBackgroundSubscription(NearbySubscription.UNSUBSCRIBE).run
(); |
122 return; | 101 return; |
123 } | 102 } |
124 | 103 |
125 // If this user is in the default state, we need to check if we should e
nable Physical Web. | 104 // If this user is in the default state, we need to check if we should e
nable Physical Web. |
126 if (isOnboarding() && shouldAutoEnablePhysicalWeb()) { | 105 if (isOnboarding() && shouldAutoEnablePhysicalWeb()) { |
127 PrivacyPreferencesManager.getInstance().setPhysicalWebEnabled(true); | 106 PrivacyPreferencesManager.getInstance().setPhysicalWebEnabled(true); |
128 } | 107 } |
129 | 108 |
130 updateScans(); | 109 updateScans(); |
131 // The PhysicalWebUma call in this method should be called only when the
native library | 110 // The PhysicalWebUma call in this method should be called only when the
native library |
132 // is loaded. This is always the case on chrome startup. | 111 // is loaded. This is always the case on chrome startup. |
133 PhysicalWebUma.uploadDeferredMetrics(); | 112 PhysicalWebUma.uploadDeferredMetrics(); |
| 113 |
| 114 // We can remove this block after M60. |
| 115 new AsyncTask<Void, Void, Void>() { |
| 116 @Override |
| 117 protected Void doInBackground(Void... params) { |
| 118 ContextUtils.getAppSharedPreferences().edit() |
| 119 .remove("physical_web_notify_count") |
| 120 .apply(); |
| 121 return null; |
| 122 } |
| 123 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
134 } | 124 } |
135 | 125 |
136 /** | 126 /** |
137 * Checks if this device should have Physical Web automatically enabled. | 127 * Checks if this device should have Physical Web automatically enabled. |
138 */ | 128 */ |
139 private static boolean shouldAutoEnablePhysicalWeb() { | 129 private static boolean shouldAutoEnablePhysicalWeb() { |
140 LocationUtils locationUtils = LocationUtils.getInstance(); | 130 LocationUtils locationUtils = LocationUtils.getInstance(); |
141 return locationUtils.isSystemLocationSettingEnabled() | 131 return locationUtils.isSystemLocationSettingEnabled() |
142 && locationUtils.hasAndroidLocationPermission() | 132 && locationUtils.hasAndroidLocationPermission() |
143 && TemplateUrlService.getInstance().isDefaultSearchEngineGoogle(
) | 133 && TemplateUrlService.getInstance().isDefaultSearchEngineGoogle(
) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 if (!locationUtils.hasAndroidLocationPermission() | 175 if (!locationUtils.hasAndroidLocationPermission() |
186 || !locationUtils.isSystemLocationSettingEnabled() | 176 || !locationUtils.isSystemLocationSettingEnabled() |
187 || !isPhysicalWebPreferenceEnabled()) { | 177 || !isPhysicalWebPreferenceEnabled()) { |
188 new NearbyBackgroundSubscription(NearbySubscription.UNSUBSCRIBE).run
(); | 178 new NearbyBackgroundSubscription(NearbySubscription.UNSUBSCRIBE).run
(); |
189 return; | 179 return; |
190 } | 180 } |
191 | 181 |
192 new NearbyBackgroundSubscription(NearbySubscription.SUBSCRIBE).run(); | 182 new NearbyBackgroundSubscription(NearbySubscription.SUBSCRIBE).run(); |
193 } | 183 } |
194 } | 184 } |
OLD | NEW |