File: notification_icons_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (396 lines) | stat: -rw-r--r-- 13,971 bytes parent folder | download | duplicates (3)
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
// 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/unified/notification_icons_controller.h"

#include <optional>
#include <string>

#include "ash/constants/ash_constants.h"
#include "ash/public/cpp/vm_camera_mic_constants.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.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/system/notification_center/ash_message_center_lock_screen_controller.h"
#include "ash/system/notification_center/message_center_utils.h"
#include "ash/system/notification_center/notification_center_tray.h"
#include "ash/system/status_area_animation_controller.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/tray/tray_utils.h"
#include "ash/system/unified/notification_counter_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "base/strings/string_util.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/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/vector_icons.h"
#include "ui/views/controls/image_view.h"

namespace ash {

namespace {

constexpr int kIconsViewDisplaySizeThreshold = 768;

// Maximum number of notification icons shown in the system tray button.
constexpr int kMaxNotificationIconsShown = 2;

const char kCapsLockNotifierId[] = "ash.caps-lock";
const char kBatteryNotificationNotifierId[] = "ash.battery";
const char kUsbNotificationNotifierId[] = "ash.power";

bool ShouldShowNotification(message_center::Notification* notification) {
  SessionControllerImpl* session_controller =
      Shell::Get()->session_controller();
  if (!session_controller->ShouldShowNotificationTray() ||
      (session_controller->IsScreenLocked() &&
       !AshMessageCenterLockScreenController::IsEnabled())) {
    return false;
  }

  std::string notifier_id = notification->notifier_id().id;

  if (message_center::MessageCenter::Get()->IsQuietMode() &&
      notifier_id != kCapsLockNotifierId) {
    return false;
  }

  // We don't want to show these notifications since the information is
  // already indicated by another item in tray.
  if (notifier_id == kVmCameraMicNotifierId ||
      notifier_id == kBatteryNotificationNotifierId ||
      notifier_id == kUsbNotificationNotifierId ||
      notifier_id == kPrivacyIndicatorsNotifierId) {
    return false;
  }

  // We only show notification icon in the tray if it is either:
  // *   Pinned (generally used for background process such as sharing your
  //     screen, capslock, etc.).
  // *   Critical warning (display failure, disk space critically low, etc.).
  return notification->pinned() ||
         notification->system_notification_warning_level() ==
             message_center::SystemNotificationWarningLevel::CRITICAL_WARNING;
}

}  // namespace

NotificationIconTrayItemView::NotificationIconTrayItemView(
    Shelf* shelf,
    NotificationIconsController* controller)
    : TrayItemView(shelf), controller_(controller) {
  CreateImageView();
}

NotificationIconTrayItemView::~NotificationIconTrayItemView() = default;

void NotificationIconTrayItemView::SetNotification(
    message_center::Notification* notification) {
  notification_id_ = notification->id();
  notification_ = notification->DeepCopy(
      *notification, GetColorProvider(), /*include_body_image=*/true,
      /*include_small_image=*/true, /*include_icon_images=*/true);

  UpdateImageViewColor();
  image_view()->SetTooltipText(notification->title());
}

void NotificationIconTrayItemView::MaybeReset() {
  if ((!target_visible() && IsAnimating()) ||
      shelf()
          ->status_area_widget()
          ->animation_controller()
          ->is_hide_animation_scheduled()) {
    return;
  }
  Reset();
}

void NotificationIconTrayItemView::Reset() {
  notification_id_ = std::string();
  notification_.reset();
  image_view()->SetImage(ui::ImageModel());
  image_view()->SetTooltipText(std::u16string());
}

void NotificationIconTrayItemView::ImmediatelyUpdateVisibility() {
  TrayItemView::ImmediatelyUpdateVisibility();
  if (!target_visible()) {
    Reset();
  }
}

void NotificationIconTrayItemView::AnimationEnded(
    const gfx::Animation* animation) {
  TrayItemView::AnimationEnded(animation);
  if (!target_visible()) {
    Reset();
  }
}

const std::u16string& NotificationIconTrayItemView::GetAccessibleNameString()
    const {
  if (notification_id_.empty())
    return base::EmptyString16();
  return image_view()->GetTooltipText();
}

const std::string& NotificationIconTrayItemView::GetNotificationId() const {
  return notification_id_;
}

void NotificationIconTrayItemView::HandleLocaleChange() {}

void NotificationIconTrayItemView::OnThemeChanged() {
  TrayItemView::OnThemeChanged();
  controller_->UpdateNotificationIcons();
}

void NotificationIconTrayItemView::UpdateLabelOrImageViewColor(bool active) {
  TrayItemView::UpdateLabelOrImageViewColor(active);

  UpdateImageViewColor();
}

void NotificationIconTrayItemView::UpdateImageViewColor() {
  if (!GetWidget() || !notification_) {
    return;
  }

  const auto* color_provider = GetColorProvider();
  ui::ColorId color_id = kColorAshIconColorPrimary;
  color_id = is_active() ? cros_tokens::kCrosSysSystemOnPrimaryContainer
                         : cros_tokens::kCrosSysOnSurface;
  gfx::Image masked_small_icon = notification_->GenerateMaskedSmallIcon(
      kUnifiedTrayIconSize, color_provider->GetColor(color_id),
      color_provider->GetColor(ui::kColorNotificationIconBackground),
      color_provider->GetColor(ui::kColorNotificationIconForeground));
  if (!masked_small_icon.IsEmpty()) {
    image_view()->SetImage(ui::ImageModel::FromImage(masked_small_icon));
  } else {
    image_view()->SetImage(ui::ImageModel::FromVectorIcon(
        message_center::kProductIcon, color_id, kUnifiedTrayIconSize));
  }
}

BEGIN_METADATA(NotificationIconTrayItemView)
END_METADATA

NotificationIconsController::NotificationIconsController(
    Shelf* shelf,
    NotificationCenterTray* notification_center_tray)
    : shelf_(shelf),
      notification_center_tray_(notification_center_tray) {
  // `notification_center_tray` should not be null.
  DCHECK(notification_center_tray);

  // Initialize `icons_view_visible_` according to display size.
  UpdateIconsViewVisibleForDisplaySize();

  message_center::MessageCenter::Get()->AddObserver(this);
  Shell::Get()->session_controller()->AddObserver(this);
}

NotificationIconsController::~NotificationIconsController() {
  message_center::MessageCenter::Get()->RemoveObserver(this);
  Shell::Get()->session_controller()->RemoveObserver(this);
}

void NotificationIconsController::AddNotificationTrayItems(
    TrayContainer* tray_container) {
  for (int i = 0; i < kMaxNotificationIconsShown; ++i) {
    NotificationIconTrayItemView* tray_item = tray_container->AddChildView(
        std::make_unique<NotificationIconTrayItemView>(shelf_,
                                                       /*controller=*/this));
    tray_items_.push_back(tray_item);
    notification_center_tray_->AddTooltipChangedCallbackToNotificationIcon(tray_item);
  }

  notification_counter_view_ = tray_container->AddChildView(
      std::make_unique<NotificationCounterView>(shelf_, /*controller=*/this));

  quiet_mode_view_ =
      tray_container->AddChildView(std::make_unique<QuietModeView>(shelf_));
}

bool NotificationIconsController::TrayItemHasNotification() const {
  return first_unused_item_index_ != 0;
}

size_t NotificationIconsController::TrayNotificationIconsCount() const {
  // `first_unused_item_index_` is also the total number of notification icons
  // shown in the tray.
  return first_unused_item_index_;
}

std::optional<std::u16string>
NotificationIconsController::GetAccessibleNameString() const {
  if (quiet_mode_view_ && quiet_mode_view_->GetVisible()) {
    return quiet_mode_view_->GetAccessibleNameString();
  }

  if (!notification_counter_view_) {
    return std::nullopt;
  }

  if (!TrayItemHasNotification()) {
    return notification_counter_view_->GetAccessibleNameString();
  }

  std::vector<std::u16string> status;
  status.push_back(l10n_util::GetPluralStringFUTF16(
      IDS_ASH_STATUS_TRAY_NOTIFICATIONS_IMPORTANT_COUNT_ACCESSIBLE_NAME,
      TrayNotificationIconsCount()));
  for (NotificationIconTrayItemView* tray_item : tray_items_) {
    status.push_back(tray_item->GetAccessibleNameString());
  }
  status.push_back(
      notification_counter_view_->GetAccessibleNameString().value_or(
          std::u16string()));
  return l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_NOTIFICATIONS_ICONS_ACCESSIBLE_NAME, status, nullptr);
}

void NotificationIconsController::UpdateNotificationIcons() {
  // Iterates `tray_items_` and notifications in reverse order so new pinned
  // notifications get shown on the left side.
  auto notifications =
      message_center_utils::GetSortedNotificationsWithOwnView();

  auto tray_it = tray_items_.rbegin();
  for (auto notification_it = notifications.rbegin();
       notification_it != notifications.rend(); ++notification_it) {
    if (tray_it == tray_items_.rend()) {
      break;
    }

    if (ShouldShowNotification(*notification_it)) {
      (*tray_it)->SetNotification(*notification_it);
      (*tray_it)->SetVisible(icons_view_visible_);
      ++tray_it;
    }
  }

  first_unused_item_index_ = std::distance(tray_items_.rbegin(), tray_it);
  notification_center_tray_->UpdateAccessibleName();

  for (; tray_it != tray_items_.rend(); ++tray_it) {
    // Note: It is important to set the visibility before resetting so that the
    // icon image does not disappear while the tray item is still visible.
    (*tray_it)->SetVisible(false);
    (*tray_it)->MaybeReset();
  }
}

void NotificationIconsController::UpdateNotificationIndicators() {
  notification_counter_view_->Update();
  quiet_mode_view_->Update();
}

void NotificationIconsController::UpdateIconsViewVisibleForDisplaySize() {
  aura::Window* window = shelf_->status_area_widget()->GetNativeWindow();
  auto display = display::Screen::GetScreen()->GetDisplayNearestWindow(window);
  const int display_size =
      std::max(display.size().width(), display.size().height());
  icons_view_visible_ = display_size >= kIconsViewDisplaySizeThreshold;
}

void NotificationIconsController::OnDisplayMetricsChanged(
    const display::Display& display,
    uint32_t changed_metrics) {
  aura::Window* window = shelf_->status_area_widget()->GetNativeWindow();
  if (display::Screen::GetScreen()->GetDisplayNearestWindow(window).id() !=
      display.id()) {
    return;
  }
  auto old_icons_view_visible = icons_view_visible_;
  UpdateIconsViewVisibleForDisplaySize();
  if (old_icons_view_visible == icons_view_visible_)
    return;

  UpdateNotificationIcons();
  UpdateNotificationIndicators();
}

void NotificationIconsController::OnNotificationAdded(const std::string& id) {
  message_center::Notification* notification =
      message_center::MessageCenter::Get()->FindVisibleNotificationById(id);
  // `notification` is null if it is not visible.
  if (notification && ShouldShowNotification(notification)) {
    // Reset the notification icons if a notification is added since we don't
    // know the position where its icon should be added.
    UpdateNotificationIcons();
  }

  UpdateNotificationIndicators();

  // There are certain sequences of notification updates that can require
  // explicitly updating the notification center tray even after its tray items
  // have been updated - for instance, adding an initial notification on the
  // lock screen. See http://b/297579552.
  notification_center_tray_->UpdateVisibility();
}

void NotificationIconsController::OnNotificationRemoved(const std::string& id,
                                                        bool by_user) {
  // If the notification removed is displayed in an icon, call update to show
  // another notification if needed.
  if (GetNotificationIconShownInTray(id))
    UpdateNotificationIcons();

  UpdateNotificationIndicators();

  // There are certain sequences of notification updates that can require
  // explicitly updating the notification center tray even after its tray items
  // have been updated - for instance, removing a notification group's parent
  // notification when the only remaining notifications belong to that group.
  // See http://b/296918234.
  notification_center_tray_->UpdateVisibility();
}

void NotificationIconsController::OnNotificationUpdated(const std::string& id) {
  // A notification update may impact certain notification icon(s) visibility
  // in the tray, so update all notification icons.
  UpdateNotificationIcons();
  UpdateNotificationIndicators();
}

void NotificationIconsController::OnNotificationDisplayed(
    const std::string& notification_id,
    const message_center::DisplaySource source) {
    UpdateNotificationIcons();
    UpdateNotificationIndicators();
}

void NotificationIconsController::OnQuietModeChanged(bool in_quiet_mode) {
  UpdateNotificationIcons();
  UpdateNotificationIndicators();
}

void NotificationIconsController::OnSessionStateChanged(
    session_manager::SessionState state) {
  UpdateNotificationIcons();
  UpdateNotificationIndicators();
}

NotificationIconTrayItemView*
NotificationIconsController::GetNotificationIconShownInTray(
    const std::string& id) {
  for (NotificationIconTrayItemView* tray_item : tray_items_) {
    if (tray_item->GetNotificationId() == id)
      return tray_item;
  }
  return nullptr;
}

}  // namespace ash