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
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580

ash / system / phonehub / phone_hub_tray.cc [blame]

// Copyright 2020 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/phone_hub_tray.h"

#include <string>
#include <utility>

#include "ash/accessibility/accessibility_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/focus/focus_cycler.h"
#include "ash/multi_device_setup/multi_device_notification_presenter.h"
#include "ash/public/cpp/system/anchored_nudge_manager.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/eche/eche_icon_loading_indicator_view.h"
#include "ash/system/eche/eche_tray.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/phonehub/onboarding_nudge_controller.h"
#include "ash/system/phonehub/phone_hub_content_view.h"
#include "ash/system/phonehub/phone_hub_metrics.h"
#include "ash/system/phonehub/quick_actions_view.h"
#include "ash/system/phonehub/task_continuation_view.h"
#include "ash/system/phonehub/ui_constants.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/system_menu_button.h"
#include "ash/system/tray/tray_bubble_wrapper.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tray_utils.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "base/power_monitor/power_monitor.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/default_clock.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/phonehub/icon_decoder.h"
#include "chromeos/ash/components/phonehub/phone_hub_manager.h"
#include "chromeos/ash/components/phonehub/phone_model.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/display/manager/display_manager.h"
#include "ui/events/event.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/controls/image_view.h"

namespace ash {

namespace {

// Command ID for Phone Hub context menu
constexpr int kHidePhoneHubIconCommandId = 1;

// Padding for tray icons (dp; the button that shows the phone_hub menu).
constexpr int kTrayIconMainAxisInset = 6;
constexpr int kTrayIconCrossAxisInset = 0;
constexpr int kEcheIconMinSize = 24;
constexpr int kIconSpacing = 12;

constexpr int kHidePhoneHubContexMenuIconSize = 20;

constexpr auto kBubblePadding =
    gfx::Insets::TLBR(0, 0, kBubbleBottomPaddingDip, 0);

bool IsInUserSession() {
  SessionControllerImpl* session_controller =
      Shell::Get()->session_controller();
  return session_controller->GetSessionState() ==
             session_manager::SessionState::ACTIVE &&
         !session_controller->IsRunningInAppMode();
}

}  // namespace

PhoneHubTray::PhoneHubTray(Shelf* shelf)
    : TrayBackgroundView(shelf, TrayBackgroundViewCatalogName::kPhoneHub),
      ui_controller_(new PhoneHubUiController()),
      last_unlocked_timestamp_(base::Time::NowFromSystemTime()) {
  // By default, if the individual buttons did not handle the event consider it
  // as a phone hub icon event.
  SetCallback(base::BindRepeating(&PhoneHubTray::PhoneHubIconActivated,
                                  base::Unretained(this)));

  observed_phone_hub_ui_controller_.Observe(ui_controller_.get());
  observed_session_.Observe(Shell::Get()->session_controller());

  tray_container()->SetMargin(kTrayIconMainAxisInset, kTrayIconCrossAxisInset);
  // TODO(nayebi): Think about constructing the eche_icon outside of this class,
  // either as an input argument or being set through a setter.
  if (features::IsEcheSWAEnabled()) {
    auto eche_icon = std::make_unique<views::ImageButton>(base::BindRepeating(
        &PhoneHubTray::EcheIconActivated, weak_factory_.GetWeakPtr()));
    eche_icon->SetButtonController(std::make_unique<views::ButtonController>(
        /*views::Button*=*/eche_icon.get(),
        std::make_unique<TrayBackgroundView::TrayButtonControllerDelegate>(
            /*views::Button*=*/eche_icon.get(),
            TrayBackgroundViewCatalogName::kPhoneHub)));
    eche_icon->SetImageVerticalAlignment(
        views::ImageButton::VerticalAlignment::ALIGN_MIDDLE);
    eche_icon->SetImageHorizontalAlignment(
        views::ImageButton::HorizontalAlignment::ALIGN_CENTER);
    eche_icon->SetMinimumImageSize(
        gfx::Size(kEcheIconMinSize, kEcheIconMinSize));
    eche_icon->SetVisible(false);
    eche_loading_indicator_ = eche_icon->AddChildView(
        std::make_unique<EcheIconLoadingIndicatorView>(eche_icon.get()));
    eche_loading_indicator_->SetVisible(false);
    eche_icon_ = tray_container()->AddChildView(std::move(eche_icon));
    tray_container()->SetSpacingBetweenChildren(kIconSpacing);
  }
  auto icon = std::make_unique<views::ImageButton>(base::BindRepeating(
      &PhoneHubTray::PhoneHubIconActivated, weak_factory_.GetWeakPtr()));
  icon->SetButtonController(std::make_unique<views::ButtonController>(
      /*views::Button*=*/icon.get(),
      std::make_unique<TrayBackgroundView::TrayButtonControllerDelegate>(
          /*views::Button*=*/icon.get(),
          TrayBackgroundViewCatalogName::kPhoneHub)));
  icon->SetFocusBehavior(FocusBehavior::NEVER);
  icon->SetTooltipText(
      l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
  icon->SetImageVerticalAlignment(
      views::ImageButton::VerticalAlignment::ALIGN_MIDDLE);
  icon->SetImageHorizontalAlignment(
      views::ImageButton::HorizontalAlignment::ALIGN_CENTER);
  icon_ = tray_container()->AddChildView(std::move(icon));

  UpdateTrayItemColor(is_active());

  onboarding_nudge_controller_ =
      features::IsPhoneHubOnboardingNotifierRevampEnabled()
          ? std::make_unique<OnboardingNudgeController>(
                /*phone_hub_tray=*/this,
                /*animation_stop_callback=*/
                base::BindRepeating(&PhoneHubTray::StopPulseAnimation,
                                    weak_factory_.GetWeakPtr()),
                /*start_animation_callback=*/
                base::BindRepeating(&PhoneHubTray::StartPulseAnimation,
                                    weak_factory_.GetWeakPtr()),
                base::DefaultClock::GetInstance())
          : nullptr;

  Shell::Get()->display_manager()->AddDisplayManagerObserver(this);

  GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
}

PhoneHubTray::~PhoneHubTray() {
  if (bubble_)
    bubble_->bubble_view()->ResetDelegate();
  if (phone_hub_manager_) {
    phone_hub_manager_->GetAppStreamManager()->RemoveObserver(this);
  }
  if (phone_hub_manager_ && IsInPhoneHubNudgeExperimentGroup() &&
      onboarding_nudge_controller_) {
    phone_hub_manager_->GetFeatureStatusProvider()->RemoveObserver(
        onboarding_nudge_controller_.get());
  }
  Shell::Get()->display_manager()->RemoveDisplayManagerObserver(this);
}

void PhoneHubTray::SetPhoneHubManager(
    phonehub::PhoneHubManager* phone_hub_manager) {
  ui_controller_->SetPhoneHubManager(phone_hub_manager);
  if (phone_hub_manager_) {
    phone_hub_manager_->GetAppStreamManager()->RemoveObserver(this);
  }
  if (phone_hub_manager) {
    phone_hub_manager->GetAppStreamManager()->AddObserver(this);
  }
  phone_hub_manager_ = phone_hub_manager;
  if (phone_hub_manager_ && IsInPhoneHubNudgeExperimentGroup() &&
      onboarding_nudge_controller_) {
    phone_hub_manager_->GetFeatureStatusProvider()->AddObserver(
        onboarding_nudge_controller_.get());
  }
}

void PhoneHubTray::ClickedOutsideBubble(const ui::LocatedEvent& event) {
  CloseBubble();
}

void PhoneHubTray::UpdateTrayItemColor(bool is_active) {
  icon_->SetImageModel(
      views::ImageButton::STATE_NORMAL,
      ui::ImageModel::FromVectorIcon(
          kPhoneHubPhoneIcon,
          is_active ? cros_tokens::kCrosSysSystemOnPrimaryContainer
                    : cros_tokens::kCrosSysOnSurface));
}

void PhoneHubTray::HandleLocaleChange() {
  icon_->SetTooltipText(
      l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
}

void PhoneHubTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {
  if (bubble_->bubble_view() == bubble_view)
    CloseBubble();
}

std::u16string PhoneHubTray::GetAccessibleNameForBubble() {
  return l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME);
}

bool PhoneHubTray::ShouldEnableExtraKeyboardAccessibility() {
  return Shell::Get()->accessibility_controller()->spoken_feedback().enabled();
}

void PhoneHubTray::HideBubble(const TrayBubbleView* bubble_view) {
  HideBubbleWithView(bubble_view);
}

void PhoneHubTray::OnPhoneHubUiStateChanged() {
  UpdateVisibility();
  UpdateHeaderVisibility();

  if (!bubble_)
    return;
  TrayBubbleView* bubble_view = bubble_->bubble_view();

  DCHECK(ui_controller_.get());
  std::unique_ptr<PhoneHubContentView> content_view =
      ui_controller_->CreateContentView(this);
  if (!content_view.get()) {
    CloseBubble();
    return;
  }

  if (content_view_) {
    // If we are already showing the same content_view, no need to remove and
    // update the tray.
    // TODO(crbug.com/1185316) : Find way to update views without work around
    // when same view is removed and added.
    if (content_view->GetID() == content_view_->GetID())
      return;

    bubble_view->RemoveChildView(content_view_);
    delete content_view_;
  }
  content_view_ = bubble_view->AddChildView(std::move(content_view));

  // Updates bubble to handle possible size change with a different child view.
  bubble_view->UpdateBubble();
}

void PhoneHubTray::OnSessionStateChanged(session_manager::SessionState state) {
  TemporarilyDisableAnimation();
  if (state == session_manager::SessionState::ACTIVE) {
    last_unlocked_timestamp_ = base::Time::NowFromSystemTime();
    UpdateVisibility();
  }
}

void PhoneHubTray::OnActiveUserSessionChanged(const AccountId& account_id) {
  TemporarilyDisableAnimation();
}

void PhoneHubTray::AnchorUpdated() {
  if (bubble_)
    bubble_->bubble_view()->UpdateBubble();
}

void PhoneHubTray::OnVisibilityAnimationFinished(
    bool should_log_visible_pod_count,
    bool aborted) {
  TrayBackgroundView::OnVisibilityAnimationFinished(
      should_log_visible_pod_count, aborted);
  if (IsInPhoneHubNudgeExperimentGroup() &&
      ui_controller_->ui_state() ==
          PhoneHubUiController::UiState::kOnboardingWithoutPhone) {
    onboarding_nudge_controller_->ShowNudgeIfNeeded();
  }
}

void PhoneHubTray::OnDidApplyDisplayChanges() {
  if (!bubble_ || !bubble_->GetBubbleView())
    return;
  bubble_->GetBubbleView()->ChangeAnchorRect(
      shelf()->GetSystemTrayAnchorRect());
}

void PhoneHubTray::Initialize() {
  TrayBackgroundView::Initialize();
  // For secondary displays to have Phone Hub visible, manager must
  // be set.
  phonehub::PhoneHubManager* phone_hub_tray_manager =
      Shell::Get()->system_tray_model()->phone_hub_manager();
  if (phone_hub_tray_manager) {
    SetPhoneHubManager(phone_hub_tray_manager);
  }
  UpdateVisibility();
}

void PhoneHubTray::ShowBubble() {
  if (bubble_)
    return;

  ui_controller_->HandleBubbleOpened();

  auto bubble_view =
      std::make_unique<TrayBubbleView>(CreateInitParamsForTrayBubble(
          /*tray=*/this, /*anchor_to_shelf_corner=*/true));
  bubble_view->SetBorder(views::CreateEmptyBorder(kBubblePadding));
  bubble_view->box_layout()->SetDefaultFlex(0);

  // Creates header view on top for displaying phone status and settings icon.
  auto phone_status = ui_controller_->CreateStatusHeaderView(this);
  phone_status_view_dont_use_ = phone_status.get();
  DCHECK(phone_status_view_dont_use_);
  bubble_view->AddChildView(std::move(phone_status));

  // Other contents, i.e. the connected view and the interstitial views,
  // will be positioned underneath the phone status view and updated based
  // on the current mode.
  auto content_view = ui_controller_->CreateContentView(this);
  content_view_ = content_view.get();

  if (!content_view_) {
    CloseBubble();
    return;
  }

  bubble_view->AddChildView(std::move(content_view));

  bubble_ = std::make_unique<TrayBubbleWrapper>(this);
  bubble_->ShowBubble(std::move(bubble_view));
  UpdateHeaderVisibility();

  SetIsActive(true);

  phone_hub_metrics::LogScreenOnBubbleOpen(
      content_view_->GetScreenForMetrics());
}

TrayBubbleView* PhoneHubTray::GetBubbleView() {
  return bubble_ ? bubble_->bubble_view() : nullptr;
}

views::Widget* PhoneHubTray::GetBubbleWidget() const {
  return bubble_ ? bubble_->GetBubbleWidget() : nullptr;
}

bool PhoneHubTray::CanOpenConnectedDeviceSettings() {
  return TrayPopupUtils::CanOpenWebUISettings();
}

void PhoneHubTray::OpenConnectedDevicesSettings() {
  DCHECK(content_view_);
  phone_hub_metrics::LogScreenOnSettingsButtonClicked(
      content_view_->GetScreenForMetrics());

  DCHECK(CanOpenConnectedDeviceSettings());
  Shell::Get()->system_tray_model()->client()->ShowConnectedDevicesSettings();
}

void PhoneHubTray::HideStatusHeaderView() {
  if (!GetPhoneStatusView())
    return;

  GetPhoneStatusView()->SetVisible(false);
  bubble_->bubble_view()->UpdateBubble();
}

bool PhoneHubTray::IsPhoneHubIconClickedWhenNudgeVisible() {
  return is_icon_clicked_when_nudge_visible_;
}

void PhoneHubTray::OnAppStreamUpdate(
    const phonehub::proto::AppStreamUpdate app_stream_update) {
  auto* app = &app_stream_update.foreground_app();
  // TODO(nayebi): Try to extract this decoding process into a shared code
  // inside the icon decoder.
  // Decode the icon
  std::unique_ptr<std::vector<phonehub::IconDecoder::DecodingData>>
      decoding_data_list =
          std::make_unique<std::vector<phonehub::IconDecoder::DecodingData>>();
  std::hash<std::string> str_hash;
  std::string key = app->package_name() + base::NumberToString(app->user_id());
  phonehub::IconDecoder::DecodingData decoding_data =
      phonehub::IconDecoder::DecodingData(str_hash(key), app->icon());
  // load with default image
  decoding_data.result =
      gfx::Image(CreateVectorIcon(kPhoneHubPhoneIcon, gfx::kGoogleGrey700));
  decoding_data_list->push_back(decoding_data);
  phone_hub_manager_->GetIconDecoder()->BatchDecode(
      std::move(decoding_data_list),
      base::BindOnce(&PhoneHubTray::OnIconsDecoded, weak_factory_.GetWeakPtr(),
                     app->visible_name()));
}

void PhoneHubTray::OnIconsDecoded(
    std::string visible_name,
    std::unique_ptr<std::vector<phonehub::IconDecoder::DecodingData>>
        decoding_data_list) {
  if (decoding_data_list->empty())
    return;
  EcheTray* eche_tray = Shell::GetPrimaryRootWindowController()
                            ->GetStatusAreaWidget()
                            ->eche_tray();
  if (!eche_tray)
    return;

  eche_tray->SetIcon(decoding_data_list->front().result,
                     base::UTF8ToUTF16(visible_name));
}

void PhoneHubTray::SetEcheIconActivationCallback(
    base::RepeatingCallback<void()> callback) {
  eche_icon_callback_ = std::move(callback);
}

void PhoneHubTray::CloseBubbleInternal() {
  if (!bubble_)
    return;

  auto* bubble_view = bubble_->GetBubbleView();
  if (bubble_view)
    bubble_view->ResetDelegate();

  if (content_view_) {
    phone_hub_metrics::LogScreenOnBubbleClose(
        content_view_->GetScreenForMetrics());

    content_view_->OnBubbleClose();
    content_view_ = nullptr;
  }

  if (phone_status_view_dont_use_) {
    phone_status_view_dont_use_ = nullptr;
  }

  if (features::IsEcheSWAEnabled() && features::IsEcheLauncherEnabled() &&
      phone_hub_manager_->GetAppStreamLauncherDataModel()) {
    phone_hub_manager_->GetAppStreamLauncherDataModel()
        ->SetShouldShowMiniLauncher(false);
  }

  bubble_.reset();
  // Reset the value when bubble is closed so that next time when setup dialog
  // is opened from Phone Hub bubble it will not be logged to wrong bucket.
  is_icon_clicked_when_nudge_visible_ = false;
  SetIsActive(false);
  shelf()->UpdateAutoHideState();
}

void PhoneHubTray::UpdateVisibility() {
  DCHECK(ui_controller_.get());
  auto ui_state = ui_controller_->ui_state();
  // If the icon becomes visible for onboarding after 5 minutes of log in, we do
  // not show the icon until next log in/unlock.
  if (features::IsPhoneHubMonochromeNotificationIconsEnabled() &&
      ui_state == PhoneHubUiController::UiState::kOnboardingWithoutPhone &&
      !IsInsideUnlockWindow()) {
    return;
  }
  icon_->set_context_menu_controller(
      ui_state == PhoneHubUiController::UiState::kOnboardingWithPhone ||
              ui_state == PhoneHubUiController::UiState::kOnboardingWithoutPhone
          ? this
          : nullptr);

  SetVisiblePreferred(ui_state != PhoneHubUiController::UiState::kHidden &&
                      IsInUserSession());
}

std::unique_ptr<ui::SimpleMenuModel> PhoneHubTray::CreateContextMenuModel() {
  auto context_menu_model = std::make_unique<ui::SimpleMenuModel>(this);

  context_menu_model->AddItemWithIcon(
      kHidePhoneHubIconCommandId,
      l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ICON_DISMISS_TEXT),
      ui::ImageModel::FromVectorIcon(vector_icons::kVisibilityOffIcon,
                                     ui::kColorAshSystemUIMenuIcon,
                                     kHidePhoneHubContexMenuIconSize));

  return context_menu_model;
}

void PhoneHubTray::ExecuteCommand(int command_id, int event_flags) {
  if (command_id == kHidePhoneHubIconCommandId) {
    phone_hub_manager_->GetOnboardingUiTracker()->DismissSetupUi();
    return;
  }
  NOTREACHED();
}

void PhoneHubTray::UpdateHeaderVisibility() {
  if (!features::IsEcheLauncherEnabled())
    return;
  if (!GetPhoneStatusView())
    return;

  DCHECK(ui_controller_.get());
  auto ui_state = ui_controller_->ui_state();
  GetPhoneStatusView()->SetVisible(
      ui_state != PhoneHubUiController::UiState::kMiniLauncher);
}

void PhoneHubTray::TemporarilyDisableAnimation() {
  base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, DisableShowAnimation().Release(), base::Seconds(5));
}

void PhoneHubTray::EcheIconActivated(const ui::Event& event) {
  eche_icon_callback_.Run();
}

void PhoneHubTray::PhoneHubIconActivated(const ui::Event& event) {
  // Simply toggle between visible/invisibvle
  if (bubble_ && bubble_->bubble_view()->GetVisible()) {
    CloseBubble();
    return;
  }

  if (features::IsPhoneHubOnboardingNotifierRevampEnabled() &&
      AnchoredNudgeManager::Get()->IsNudgeShown(
          OnboardingNudgeController::kPhoneHubNudgeId)) {
    is_icon_clicked_when_nudge_visible_ = true;
    onboarding_nudge_controller_->HideNudge();
    onboarding_nudge_controller_->MaybeRecordNudgeAction();
  }

  ShowBubble();

  if (message_center::MessageCenter::Get()->FindPopupNotificationById(
          MultiDeviceNotificationPresenter::kSetupNotificationId) &&
      ui_controller_->ui_state() ==
          PhoneHubUiController::UiState::kOnboardingWithoutPhone &&
      !is_icon_clicked_when_setup_notification_visible_) {
    Shell::Get()
        ->multidevice_notification_presenter()
        ->UpdateIsSetupNotificationInteracted(true);
    phone_hub_metrics::LogMultiDeviceSetupNotificationInteraction();
    // Set to true to prevent duplicate logging data if the icon is clicked
    // multiple times when notification is visible.
    is_icon_clicked_when_setup_notification_visible_ = true;
  }
}

views::View* PhoneHubTray::GetPhoneStatusView() {
  if (!bubble_ || !bubble_->GetBubbleView()) {
    phone_status_view_dont_use_ = nullptr;
  }
  return phone_status_view_dont_use_;
}

bool PhoneHubTray::IsInsideUnlockWindow() {
  return (base::Time::NowFromSystemTime() - last_unlocked_timestamp_) <=
         features::kMultiDeviceSetupNotificationTimeLimit.Get();
}

bool PhoneHubTray::IsInPhoneHubNudgeExperimentGroup() {
  return features::IsPhoneHubOnboardingNotifierRevampEnabled() &&
         features::kPhoneHubOnboardingNotifierUseNudge.Get();
}

BEGIN_METADATA(PhoneHubTray)
END_METADATA

}  // namespace ash