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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
// Copyright 2021 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/quick_pair/ui/fast_pair/fast_pair_notification_controller.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/cross_device/logging/logging.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
using message_center::MessageCenter;
using message_center::Notification;
namespace {
const message_center::NotifierId kNotifierFastPair =
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
"ash.fastpair",
ash::NotificationCatalogName::kFastPair);
const char kFastPairErrorNotificationId[] =
"cros_fast_pair_error_notification_id";
const char kFastPairDiscoveryGuestNotificationId[] =
"cros_fast_pair_discovery_guest_notification_id";
const char kFastPairApplicationAvailableNotificationId[] =
"cros_fast_pair_application_available_notification_id";
const char kFastPairApplicationInstalledNotificationId[] =
"cros_fast_pair_application_installed_notification_id";
const char kFastPairDiscoveryUserNotificationId[] =
"cros_fast_pair_discovery_user_notification_id";
const char kFastPairPairingNotificationId[] =
"cros_fast_pair_pairing_notification_id";
const char kFastPairAssociateAccountNotificationId[] =
"cros_fast_pair_associate_account_notification_id";
const char kFastPairDiscoverySubsequentNotificationId[] =
"cros_fast_pair_discovery_subsequent_notification_id";
const char kFastPairDisplayPasskeyNotificationId[] =
"cros_fast_pair_display_passkey_notification_id";
// Values outside of the range (e.g. -1) will show an infinite loading
// progress bar.
const int kInfiniteLoadingProgressValue = -1;
// 12 seconds comes from aligning CrOS notification with Android. Android
// determined it takes 12 seconds for a device to be truly lost to the adapter.
// Within 12 seconds, a device can be perceived as lost, yet be found again.
constexpr base::TimeDelta kNotificationTimeout = base::Seconds(12);
// Creates an empty Fast Pair notification with the given id and uses the
// Bluetooth icon and FastPair notifierID.
std::unique_ptr<message_center::Notification> CreateNotification(
const std::string& id,
message_center::SystemNotificationWarningLevel warning_level,
message_center::MessageCenter* message_center) {
// Remove any existing Fast Pair notifications so only one appears at a time,
// since there isn't a case where all of them should be showing.
message_center->RemoveNotificationsForNotifierId(kNotifierFastPair);
std::unique_ptr<message_center::Notification> notification =
ash::CreateSystemNotificationPtr(
/*type=*/message_center::NOTIFICATION_TYPE_SIMPLE,
/*id=*/id,
/*title=*/std::u16string(),
/*message=*/std::u16string(),
/*display_source=*/std::u16string(), /*origin_url=*/GURL(),
/*notifier_id=*/kNotifierFastPair,
/*optional_fields=*/{},
/*delegate=*/nullptr,
/*small_image=*/ash::kNotificationBluetoothIcon,
/*warning_level=*/warning_level);
notification->set_never_timeout(true);
notification->set_priority(
message_center::NotificationPriority::MAX_PRIORITY);
return notification;
}
} // namespace
namespace ash {
namespace quick_pair {
// NotificationDelegate implementation for handling click and dismiss events on
// a notification. Used by Error, Pairing, and Discovery notifications.
class NotificationDelegate : public message_center::NotificationDelegate {
public:
explicit NotificationDelegate(
base::RepeatingClosure on_primary_click,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close,
base::RepeatingClosure on_secondary_click = base::DoNothing(),
base::OneShotTimer* expire_notification_timer = nullptr) {
on_primary_click_ = on_primary_click;
on_secondary_click_ = on_secondary_click;
on_close_ = std::move(on_close);
expire_notification_timer_ = expire_notification_timer;
}
void DismissedByTimeout() { dismissed_by_timeout_ = true; }
protected:
~NotificationDelegate() override = default;
// message_center::NotificationDelegate override:
void Click(const std::optional<int>& button_index,
const std::optional<std::u16string>& reply) override {
if (!button_index)
return;
// If the button displayed on the notification is clicked.
switch (*button_index) {
case static_cast<int>(Button::kPrimaryButton):
on_primary_click_.Run();
break;
case static_cast<int>(Button::kSecondaryButton):
on_secondary_click_.Run();
break;
}
}
// message_center::NotificationDelegate override:
void Close(bool by_user) override {
// If there is an expire notification timer, stop the timer if the user
// dismisses the notification to prevent the timer firing and removing
// notifications that might come up later.
if (expire_notification_timer_) {
CD_LOG(VERBOSE, Feature::FP)
<< __func__ << ": stopping expiration timer on notification close";
expire_notification_timer_->Stop();
}
if (dismissed_by_timeout_) {
std::move(on_close_).Run(
FastPairNotificationDismissReason::kDismissedByTimeout);
return;
} else if (by_user) {
std::move(on_close_).Run(
FastPairNotificationDismissReason::kDismissedByUser);
return;
}
std::move(on_close_).Run(FastPairNotificationDismissReason::kDismissedByOs);
}
private:
enum class Button { kPrimaryButton, kSecondaryButton };
bool dismissed_by_timeout_ = false;
base::RepeatingClosure on_primary_click_;
base::RepeatingClosure on_secondary_click_;
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close_;
raw_ptr<base::OneShotTimer, DanglingUntriaged> expire_notification_timer_;
};
FastPairNotificationController::FastPairNotificationController(
message_center::MessageCenter* message_center)
: message_center_(message_center) {
DCHECK(message_center_);
}
FastPairNotificationController::~FastPairNotificationController() = default;
void FastPairNotificationController::ShowErrorNotification(
const std::u16string& device_name,
gfx::Image device_image,
base::RepeatingClosure launch_bluetooth_pairing,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
// Because the pairing notification is pinned, we need to manually remove it
// to explicitly say it is done not by the user. This only need to be done
// for pinned notifications.
message_center_->RemoveNotification(kFastPairPairingNotificationId,
/*by_user=*/false);
std::unique_ptr<message_center::Notification> error_notification =
CreateNotification(
kFastPairErrorNotificationId,
message_center::SystemNotificationWarningLevel::CRITICAL_WARNING,
message_center_);
error_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_CONNECTION_ERROR_TITLE, device_name));
error_notification->set_message(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_CONNECTION_ERROR_MESSAGE));
message_center::ButtonInfo settings_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_SETTINGS_BUTTON));
error_notification->set_buttons({settings_button});
error_notification->set_delegate(base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/launch_bluetooth_pairing,
/*on_close=*/std::move(on_close)));
error_notification->SetImage(device_image);
message_center_->AddNotification(std::move(error_notification));
}
void FastPairNotificationController::ExtendNotification() {
// If the timer is already running, it implies that there is already a
// notification being shown for this device. Since the Mediator keeps track
// of the device for the currently shown notification, we can only get to this
// point if the notification is for the same device, which means we reset the
// timeout.
if (expire_notification_timer_.IsRunning()) {
CD_LOG(INFO, Feature::FP)
<< __func__ << " extending notification for re-discovered device";
expire_notification_timer_.Reset();
}
}
void FastPairNotificationController::ShowUserDiscoveryNotification(
const std::u16string& device_name,
const std::u16string& email_address,
gfx::Image device_image,
base::RepeatingClosure on_connect_clicked,
base::RepeatingClosure on_learn_more_clicked,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
std::unique_ptr<message_center::Notification> discovery_notification =
CreateNotification(kFastPairDiscoveryUserNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
discovery_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DISCOVERY_NOTIFICATION_TITLE, device_name));
discovery_notification->set_message(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DISCOVERY_NOTIFICATION_EMAIL_MESSAGE, device_name,
email_address));
message_center::ButtonInfo connect_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_CONNECT_BUTTON));
message_center::ButtonInfo learn_more_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_LEARN_MORE_BUTTON));
discovery_notification->set_buttons({connect_button, learn_more_button});
scoped_refptr<NotificationDelegate> notification_delegate =
base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/on_connect_clicked,
/*on_close=*/std::move(on_close),
/*on_secondary_click=*/on_learn_more_clicked,
/*expire_notification_timer=*/&expire_notification_timer_);
discovery_notification->set_delegate(notification_delegate);
discovery_notification->SetImage(device_image);
// Start timer for how long to show the notification before removing the
// notification. After the timeout period, we will remove the notification
// from the Message Center.
expire_notification_timer_.Start(
FROM_HERE, kNotificationTimeout,
base::BindOnce(
&FastPairNotificationController::RemoveNotificationsByTimeout,
weak_ptr_factory_.GetWeakPtr(), notification_delegate));
message_center_->AddNotification(std::move(discovery_notification));
}
void FastPairNotificationController::ShowGuestDiscoveryNotification(
const std::u16string& device_name,
const gfx::Image device_image,
base::RepeatingClosure on_connect_clicked,
base::RepeatingClosure on_learn_more_clicked,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
std::unique_ptr<message_center::Notification> discovery_notification =
CreateNotification(kFastPairDiscoveryGuestNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
discovery_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DISCOVERY_NOTIFICATION_TITLE, device_name));
discovery_notification->set_message(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DISCOVERY_NOTIFICATION_MESSAGE, device_name));
message_center::ButtonInfo connect_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_CONNECT_BUTTON));
message_center::ButtonInfo learn_more_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_LEARN_MORE_BUTTON));
discovery_notification->set_buttons({connect_button, learn_more_button});
scoped_refptr<NotificationDelegate> notification_delegate =
base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/on_connect_clicked,
/*on_close=*/std::move(on_close),
/*on_secondary_click=*/on_learn_more_clicked,
/*expire_notification_timer=*/&expire_notification_timer_);
discovery_notification->set_delegate(notification_delegate);
discovery_notification->SetImage(device_image);
// Start timer for how long to show the notification before removing the
// notification. After the timeout period, we will remove the notification
// from the Message Center.
expire_notification_timer_.Start(
FROM_HERE, kNotificationTimeout,
base::BindOnce(
&FastPairNotificationController::RemoveNotificationsByTimeout,
weak_ptr_factory_.GetWeakPtr(), notification_delegate));
message_center_->AddNotification(std::move(discovery_notification));
}
void FastPairNotificationController::ShowSubsequentDiscoveryNotification(
const std::u16string& device_name,
const std::u16string& email_address,
gfx::Image device_image,
base::RepeatingClosure on_connect_clicked,
base::RepeatingClosure on_learn_more_clicked,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
std::unique_ptr<message_center::Notification> discovery_notification =
CreateNotification(kFastPairDiscoverySubsequentNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
discovery_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DISCOVERY_NOTIFICATION_TITLE, device_name));
discovery_notification->set_message(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DISCOVERY_NOTIFICATION_SUBSEQUENT, device_name,
email_address));
message_center::ButtonInfo connect_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_CONNECT_BUTTON));
message_center::ButtonInfo learn_more_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_LEARN_MORE_BUTTON));
discovery_notification->set_buttons({connect_button, learn_more_button});
scoped_refptr<NotificationDelegate> notification_delegate =
base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/on_connect_clicked,
/*on_close=*/std::move(on_close),
/*on_secondary_click=*/on_learn_more_clicked,
/*expire_notification_timer=*/&expire_notification_timer_);
discovery_notification->set_delegate(notification_delegate);
discovery_notification->SetImage(device_image);
// Start timer for how long to show the notification before removing the
// notification. After the timeout period, we will remove the notification
// from the Message Center.
expire_notification_timer_.Start(
FROM_HERE, kNotificationTimeout,
base::BindOnce(
&FastPairNotificationController::RemoveNotificationsByTimeout,
weak_ptr_factory_.GetWeakPtr(), notification_delegate));
message_center_->AddNotification(std::move(discovery_notification));
}
void FastPairNotificationController::ShowApplicationAvailableNotification(
const std::u16string& device_name,
gfx::Image device_image,
base::RepeatingClosure download_app_callback,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
std::unique_ptr<message_center::Notification>
application_available_notification = CreateNotification(
kFastPairApplicationAvailableNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
application_available_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_DOWNLOAD_NOTIFICATION_APP_TITLE, device_name));
application_available_notification->set_message(l10n_util::GetStringUTF16(
IDS_FAST_PAIR_DOWNLOAD_NOTIFICATION_APP_MESSAGE));
message_center::ButtonInfo download_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_DOWNLOAD_APP_BUTTON));
application_available_notification->set_buttons({download_button});
application_available_notification->set_delegate(
base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/download_app_callback,
/*on_close=*/std::move(on_close)));
application_available_notification->SetImage(device_image);
message_center_->AddNotification(
std::move(application_available_notification));
}
void FastPairNotificationController::ShowApplicationInstalledNotification(
const std::u16string& device_name,
gfx::Image device_image,
const std::u16string& app_name,
base::RepeatingClosure launch_app_callback,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
std::unique_ptr<message_center::Notification>
application_installed_notification = CreateNotification(
kFastPairApplicationInstalledNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
application_installed_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_SETUP_APP_NOTIFICATION_TITLE, device_name));
application_installed_notification->set_message(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_SETUP_APP_NOTIFICATION_MESSAGE));
message_center::ButtonInfo setup_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_SETUP_APP_BUTTON));
application_installed_notification->set_buttons({setup_button});
application_installed_notification->set_delegate(
base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/launch_app_callback,
/*on_close=*/std::move(on_close)));
application_installed_notification->SetImage(device_image);
message_center_->AddNotification(
std::move(application_installed_notification));
}
void FastPairNotificationController::ShowPairingNotification(
const std::u16string& device_name,
gfx::Image device_image,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
// If we get to this point in the pairing flow where we are showing the
// Pairing notification, then the user has elected to begin pairing and we
// can stop the timer that was waiting for user interaction on the
// Discovery notification. We do not need the timer for the Pairing
// notification since it will be removed when pairing succeeds or fails by
// the system.
expire_notification_timer_.Stop();
std::unique_ptr<message_center::Notification> pairing_notification =
CreateNotification(kFastPairPairingNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
pairing_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_PAIRING_NOTIFICATION_TITLE, device_name));
pairing_notification->set_delegate(base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/base::DoNothing(),
/*on_close=*/std::move(on_close)));
pairing_notification->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
pairing_notification->set_progress(kInfiniteLoadingProgressValue);
pairing_notification->SetImage(device_image);
pairing_notification->set_pinned(true);
message_center_->AddNotification(std::move(pairing_notification));
}
void FastPairNotificationController::ShowAssociateAccount(
const std::u16string& device_name,
const std::u16string& email_address,
gfx::Image device_image,
base::RepeatingClosure on_save_clicked,
base::RepeatingClosure on_learn_more_clicked,
base::OnceCallback<void(FastPairNotificationDismissReason)> on_close) {
std::unique_ptr<message_center::Notification> associate_account_notification =
CreateNotification(kFastPairAssociateAccountNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
associate_account_notification->set_title(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_ASSOCIATE_ACCOUNT_NOTIFICATION_TITLE, device_name));
associate_account_notification->set_message(l10n_util::GetStringFUTF16(
IDS_FAST_PAIR_ASSOCIATE_ACCOUNT_NOTIFICATION_MESSAGE, device_name,
email_address));
message_center::ButtonInfo save_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_SAVE_BUTTON));
message_center::ButtonInfo learn_more_button(
l10n_util::GetStringUTF16(IDS_FAST_PAIR_LEARN_MORE_BUTTON));
associate_account_notification->set_buttons({save_button, learn_more_button});
scoped_refptr<NotificationDelegate> notification_delegate =
base::MakeRefCounted<NotificationDelegate>(
/*on_primary_click=*/on_save_clicked,
/*on_close=*/std::move(on_close),
/*on_secondary_click=*/on_learn_more_clicked,
/*expire_notification_timer=*/&expire_notification_timer_);
associate_account_notification->set_delegate(notification_delegate);
associate_account_notification->SetImage(device_image);
// Start timer for how long to show the notification before removing the
// notification. After the timeout period, we will remove the notification
// from the Message Center.
expire_notification_timer_.Start(
FROM_HERE, kNotificationTimeout,
base::BindOnce(
&FastPairNotificationController::RemoveNotificationsByTimeout,
weak_ptr_factory_.GetWeakPtr(), notification_delegate));
message_center_->AddNotification(std::move(associate_account_notification));
}
void FastPairNotificationController::ShowPasskey(
const std::u16string& device_name,
uint32_t passkey) {
std::unique_ptr<message_center::Notification> passkey_notification =
CreateNotification(kFastPairDisplayPasskeyNotificationId,
message_center::SystemNotificationWarningLevel::NORMAL,
message_center_);
passkey_notification->set_message(l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_BLUETOOTH_DISPLAY_PASSKEY, device_name,
base::UTF8ToUTF16(base::StringPrintf("%06i", passkey))));
message_center_->AddNotification(std::move(passkey_notification));
}
void FastPairNotificationController::RemoveNotifications() {
message_center_->RemoveNotificationsForNotifierId(kNotifierFastPair);
}
void FastPairNotificationController::RemoveNotificationsByTimeout(
scoped_refptr<NotificationDelegate> notification_delegate) {
notification_delegate->DismissedByTimeout();
RemoveNotifications();
}
} // namespace quick_pair
} // namespace ash
|