eche_notification_click_handler.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/webui/eche_app_ui/eche_notification_click_handler.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. #include "ash/components/phonehub/phone_hub_manager.h"
  7. #include "ash/root_window_controller.h"
  8. #include "ash/shell.h"
  9. #include "ash/webui/eche_app_ui/launch_app_helper.h"
  10. namespace ash {
  11. namespace eche_app {
  12. EcheNotificationClickHandler::EcheNotificationClickHandler(
  13. phonehub::PhoneHubManager* phone_hub_manager,
  14. FeatureStatusProvider* feature_status_provider,
  15. LaunchAppHelper* launch_app_helper)
  16. : feature_status_provider_(feature_status_provider),
  17. launch_app_helper_(launch_app_helper) {
  18. handler_ = phone_hub_manager->GetNotificationInteractionHandler();
  19. feature_status_provider_->AddObserver(this);
  20. if (handler_ && IsClickable(feature_status_provider_->GetStatus())) {
  21. handler_->AddNotificationClickHandler(this);
  22. is_click_handler_set_ = true;
  23. } else {
  24. PA_LOG(INFO)
  25. << "No Phone Hub interaction handler to set Eche click handler";
  26. }
  27. }
  28. EcheNotificationClickHandler::~EcheNotificationClickHandler() {
  29. feature_status_provider_->RemoveObserver(this);
  30. if (is_click_handler_set_ && handler_)
  31. handler_->RemoveNotificationClickHandler(this);
  32. }
  33. void EcheNotificationClickHandler::HandleNotificationClick(
  34. int64_t notification_id,
  35. const phonehub::Notification::AppMetadata& app_metadata) {
  36. const LaunchAppHelper::AppLaunchProhibitedReason prohibited_reason =
  37. launch_app_helper_->CheckAppLaunchProhibitedReason(
  38. feature_status_provider_->GetStatus());
  39. switch (prohibited_reason) {
  40. case LaunchAppHelper::AppLaunchProhibitedReason::kNotProhibited:
  41. launch_app_helper_->LaunchEcheApp(
  42. notification_id, app_metadata.package_name,
  43. app_metadata.visible_app_name, app_metadata.user_id,
  44. app_metadata.icon);
  45. break;
  46. case LaunchAppHelper::AppLaunchProhibitedReason::kDisabledByScreenLock:
  47. launch_app_helper_->ShowNotification(
  48. app_metadata.visible_app_name, /* message= */ absl::nullopt,
  49. std::make_unique<LaunchAppHelper::NotificationInfo>(
  50. LaunchAppHelper::NotificationInfo::Category::kNative,
  51. LaunchAppHelper::NotificationInfo::NotificationType::
  52. kScreenLock));
  53. break;
  54. }
  55. }
  56. void EcheNotificationClickHandler::OnFeatureStatusChanged() {
  57. if (!handler_) {
  58. PA_LOG(INFO)
  59. << "No Phone Hub interaction handler to set Eche click handler";
  60. return;
  61. }
  62. bool clickable = IsClickable(feature_status_provider_->GetStatus());
  63. if (!is_click_handler_set_ && clickable) {
  64. handler_->AddNotificationClickHandler(this);
  65. is_click_handler_set_ = true;
  66. } else if (is_click_handler_set_ && !clickable) {
  67. handler_->RemoveNotificationClickHandler(this);
  68. is_click_handler_set_ = false;
  69. }
  70. }
  71. bool EcheNotificationClickHandler::IsClickable(FeatureStatus status) {
  72. return status == FeatureStatus::kDisconnected ||
  73. status == FeatureStatus::kConnecting ||
  74. status == FeatureStatus::kConnected;
  75. }
  76. } // namespace eche_app
  77. } // namespace ash