1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
ash / system / phonehub / onboarding_nudge_controller.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/phonehub/onboarding_nudge_controller.h"
#include <algorithm>
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/public/cpp/system/anchored_nudge_manager.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/phonehub/phone_hub_metrics.h"
#include "ash/system/phonehub/phone_hub_ui_controller.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/clock.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/multidevice/remote_device_ref.h"
#include "chromeos/ash/components/phonehub/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"
namespace ash {
namespace {
PrefService* GetPrefService() {
return Shell::Get()->session_controller()->GetActivePrefService();
}
} // namespace
// static
void OnboardingNudgeController::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterTimePref(kPhoneHubNudgeLastShownTime, base::Time());
registry->RegisterIntegerPref(kPhoneHubNudgeTotalAppearances, 0);
registry->RegisterTimePref(kPhoneHubNudgeLastActionTime, base::Time());
registry->RegisterTimePref(kPhoneHubNudgeLastClickTime, base::Time());
registry->RegisterListPref(kSyncedDevices);
}
OnboardingNudgeController::OnboardingNudgeController(
views::View* anchored_view,
base::RepeatingClosure stop_animation_callback,
base::RepeatingClosure start_animation_callback,
base::Clock* clock)
: anchored_view_(anchored_view),
stop_animation_callback_(std::move(stop_animation_callback)),
start_animation_callback_(std::move(start_animation_callback)),
clock_(clock) {}
OnboardingNudgeController::~OnboardingNudgeController() = default;
void OnboardingNudgeController::ShowNudgeIfNeeded() {
if (!IsInPhoneHubNudgeExperimentGroup()) {
return;
}
if (!ShouldShowNudge()) {
return;
}
PA_LOG(INFO)
<< "Phone Hub onboarding nudge is being shown for text experiment group "
<< (features::kPhoneHubNotifierTextGroup.Get() ==
features::PhoneHubNotifierTextGroup::kNotifierTextGroupA
? "A."
: "B.");
std::u16string nudge_text = l10n_util::GetStringUTF16(
features::kPhoneHubNotifierTextGroup.Get() ==
features::PhoneHubNotifierTextGroup::kNotifierTextGroupA
? IDS_ASH_MULTI_DEVICE_SETUP_NOTIFIER_TEXT_WITH_PHONE_HUB
: IDS_ASH_MULTI_DEVICE_SETUP_NOTIFIER_TEXT_WITHOUT_PHONE_HUB);
AnchoredNudgeData nudge_data = {kPhoneHubNudgeId, NudgeCatalogName::kPhoneHub,
nudge_text, anchored_view_};
nudge_data.anchored_to_shelf = true;
nudge_data.hover_changed_callback =
base::BindRepeating(&OnboardingNudgeController::OnNudgeHoverStateChanged,
weak_ptr_factory_.GetWeakPtr());
nudge_data.click_callback =
base::BindRepeating(&OnboardingNudgeController::OnNudgeClicked,
weak_ptr_factory_.GetWeakPtr());
nudge_data.dismiss_callback = stop_animation_callback_.Then(
base::BindRepeating(&OnboardingNudgeController::OnNudgeDismissed,
weak_ptr_factory_.GetWeakPtr()));
AnchoredNudgeManager::Get()->Show(nudge_data);
if (AnchoredNudgeManager::Get()->IsNudgeShown(kPhoneHubNudgeId)) {
start_animation_callback_.Run();
PrefService* pref_service = GetPrefService();
pref_service->SetTime(kPhoneHubNudgeLastShownTime, clock_->Now());
pref_service->SetInteger(
kPhoneHubNudgeTotalAppearances,
pref_service->GetInteger(kPhoneHubNudgeTotalAppearances) + 1);
base::UmaHistogramCounts100("MultiDeviceSetup.NudgeShown", 1);
}
}
void OnboardingNudgeController::HideNudge() {
if (!IsInPhoneHubNudgeExperimentGroup()) {
return;
}
if (AnchoredNudgeManager::Get()->IsNudgeShown(kPhoneHubNudgeId)) {
// `HideNudge()` is only invoked when Phone Hub icon is clicked. If the
// nudge is visible, it should be counted as interaction.
PrefService* pref_service = GetPrefService();
base::Time time = clock_->Now();
pref_service->SetTime(kPhoneHubNudgeLastActionTime, time);
pref_service->SetTime(kPhoneHubNudgeLastClickTime, time);
is_phone_hub_icon_clicked_ = true;
AnchoredNudgeManager::Get()->Cancel(kPhoneHubNudgeId);
}
}
void OnboardingNudgeController::MaybeRecordNudgeAction() {
if (!IsInPhoneHubNudgeExperimentGroup()) {
return;
}
AnchoredNudgeManager::Get()->MaybeRecordNudgeAction(
NudgeCatalogName::kPhoneHub);
}
void OnboardingNudgeController::OnNudgeHoverStateChanged(bool is_hovering) {
if (!IsInPhoneHubNudgeExperimentGroup()) {
return;
}
if (is_hovering) {
PrefService* pref_service = GetPrefService();
pref_service->SetTime(kPhoneHubNudgeLastActionTime, clock_->Now());
}
}
void OnboardingNudgeController::OnNudgeClicked() {
if (!IsInPhoneHubNudgeExperimentGroup()) {
return;
}
is_nudge_clicked_ = true;
// Action can be click or hover so define `kPhoneHubNudgeLastActionTime` on
// click, but `kPhoneHubNudgeLastClickTime` should be set separately.
PrefService* pref_service = GetPrefService();
base::Time time = clock_->Now();
pref_service->SetTime(kPhoneHubNudgeLastActionTime, time);
pref_service->SetTime(kPhoneHubNudgeLastClickTime, time);
}
void OnboardingNudgeController::OnNudgeDismissed() {
if (!IsInPhoneHubNudgeExperimentGroup()) {
return;
}
if (is_nudge_clicked_ || is_phone_hub_icon_clicked_) {
PrefService* pref_service = GetPrefService();
base::UmaHistogramTimes(
"MultiDeviceSetup.NudgeActionDuration",
pref_service->GetTime(kPhoneHubNudgeLastClickTime) -
pref_service->GetTime(kPhoneHubNudgeLastShownTime));
base::UmaHistogramCounts100(
"MultiDeviceSetup.NudgeShownTimesBeforeActed",
pref_service->GetInteger(kPhoneHubNudgeTotalAppearances));
if (is_nudge_clicked_) {
base::UmaHistogramEnumeration(
"MultiDeviceSetup.NudgeInteracted",
phone_hub_metrics::MultideviceSetupNudgeInteraction::kNudgeClicked);
}
if (is_phone_hub_icon_clicked_) {
base::UmaHistogramEnumeration(
"MultiDeviceSetup.NudgeInteracted",
phone_hub_metrics::MultideviceSetupNudgeInteraction::
kPhoneHubIconClicked);
}
}
is_nudge_clicked_ = false;
is_phone_hub_icon_clicked_ = false;
}
void OnboardingNudgeController::OnEligiblePhoneHubHostFound(
const multidevice::RemoteDeviceRefList eligible_devices) {
bool new_host_found = false;
for (const multidevice::RemoteDeviceRef& device : eligible_devices) {
if (!IsDeviceStoredInPref(device)) {
AddToEligibleDevicesPref(device);
new_host_found = true;
}
}
if (new_host_found) {
// Rest pref values to show the nudge again when user gets a new eligible
// phone.
ResetNudgePrefs();
}
}
void OnboardingNudgeController::AddToEligibleDevicesPref(
const multidevice::RemoteDeviceRef& device) {
PrefService* pref_service = GetPrefService();
const base::Value::List& devices_in_pref =
pref_service->GetList(kSyncedDevices);
base::Value::List updated_device_list = devices_in_pref.Clone();
updated_device_list.Append(device.instance_id());
pref_service->SetList(kSyncedDevices, updated_device_list.Clone());
}
void OnboardingNudgeController::ResetNudgePrefs() {
PrefService* pref_service = GetPrefService();
if (pref_service->FindPreference(phonehub::prefs::kHideOnboardingUi)) {
pref_service->SetBoolean(phonehub::prefs::kHideOnboardingUi, false);
}
pref_service->SetInteger(kPhoneHubNudgeTotalAppearances, 0);
pref_service->SetTime(kPhoneHubNudgeLastShownTime, base::Time());
pref_service->SetTime(kPhoneHubNudgeLastActionTime, base::Time());
pref_service->SetTime(kPhoneHubNudgeLastClickTime, base::Time());
}
bool OnboardingNudgeController::IsDeviceStoredInPref(
const multidevice::RemoteDeviceRef& device) {
PrefService* pref_service = GetPrefService();
const base::Value::List& devices_in_pref =
pref_service->GetList(kSyncedDevices);
return base::Contains(devices_in_pref, base::Value(device.instance_id()));
}
bool OnboardingNudgeController::IsInPhoneHubNudgeExperimentGroup() {
return features::IsPhoneHubOnboardingNotifierRevampEnabled() &&
features::kPhoneHubOnboardingNotifierUseNudge.Get();
}
bool OnboardingNudgeController::ShouldShowNudge() {
PrefService* pref_service = GetPrefService();
if (!pref_service->GetTime(kPhoneHubNudgeLastClickTime).is_null()) {
// User has taken actions on the nudge. We do not show it to them again.
return false;
}
if (pref_service->GetInteger(kPhoneHubNudgeTotalAppearances) >=
features::kPhoneHubNudgeTotalAppearancesAllowed.Get()) {
PA_LOG(INFO) << "Nudge has been shown "
<< features::kPhoneHubNudgeTotalAppearancesAllowed.Get()
<< " times. Do not show again.";
return false;
}
// Nudge has not been shown before.
if (pref_service->GetTime(kPhoneHubNudgeLastShownTime).is_null()) {
return true;
}
if ((clock_->Now() - pref_service->GetTime(kPhoneHubNudgeLastShownTime)) >=
features::kPhoneHubNudgeDelay.Get()) {
return true;
}
PA_LOG(INFO) << "Nudge was shown less than "
<< features::kPhoneHubNudgeDelay.Get()
<< " hours ago. Not being shown this time.";
return false;
}
} // namespace ash