• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 
5 #include "ash/system/session_length_limit/tray_session_length_limit.h"
6 
7 #include <algorithm>
8 
9 #include "ash/shelf/shelf_types.h"
10 #include "ash/shell.h"
11 #include "ash/system/system_notifier.h"
12 #include "ash/system/tray/system_tray.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/system/tray/system_tray_notifier.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_utils.h"
17 #include "base/location.h"
18 #include "base/logging.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "grit/ash_resources.h"
23 #include "grit/ash_strings.h"
24 #include "third_party/skia/include/core/SkColor.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/l10n/time_format.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/font.h"
29 #include "ui/message_center/message_center.h"
30 #include "ui/message_center/notification.h"
31 #include "ui/views/border.h"
32 #include "ui/views/controls/label.h"
33 #include "ui/views/layout/box_layout.h"
34 #include "ui/views/layout/grid_layout.h"
35 #include "ui/views/view.h"
36 
37 using message_center::Notification;
38 
39 namespace ash {
40 namespace internal {
41 
42 namespace {
43 
44 // If the remaining session time falls below this threshold, the user should be
45 // informed that the session is about to expire.
46 const int kExpiringSoonThresholdInSeconds = 5 * 60;  // 5 minutes.
47 
48 // Color in which the remaining session time is normally shown.
49 const SkColor kRemainingTimeColor = SK_ColorWHITE;
50 // Color in which the remaining session time is shown when it is expiring soon.
51 const SkColor kRemainingTimeExpiringSoonColor = SK_ColorRED;
52 
CreateAndSetupLabel()53 views::Label* CreateAndSetupLabel() {
54   views::Label* label = new views::Label;
55   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
56   SetupLabelForTray(label);
57   gfx::Font font = label->font();
58   label->SetFont(font.DeriveFont(0, font.GetStyle() & ~gfx::Font::BOLD));
59   return label;
60 }
61 
IntToTwoDigitString(int value)62 base::string16 IntToTwoDigitString(int value) {
63   DCHECK_GE(value, 0);
64   DCHECK_LE(value, 99);
65   if (value < 10)
66     return ASCIIToUTF16("0") + base::IntToString16(value);
67   return base::IntToString16(value);
68 }
69 
FormatRemainingSessionTimeNotification(const base::TimeDelta & remaining_session_time)70 base::string16 FormatRemainingSessionTimeNotification(
71     const base::TimeDelta& remaining_session_time) {
72   return l10n_util::GetStringFUTF16(
73       IDS_ASH_STATUS_TRAY_REMAINING_SESSION_TIME_NOTIFICATION,
74       ui::TimeFormat::TimeDurationLong(remaining_session_time));
75 }
76 
77 // Creates, or updates the notification for session length timeout with
78 // |remaining_time|.  |state_changed| is true when its internal state has been
79 // changed from another.
CreateOrUpdateNotification(const std::string & notification_id,const base::TimeDelta & remaining_time,bool state_changed)80 void CreateOrUpdateNotification(const std::string& notification_id,
81                                 const base::TimeDelta& remaining_time,
82                                 bool state_changed) {
83   message_center::MessageCenter* message_center =
84       message_center::MessageCenter::Get();
85 
86   // Do not create a new notification if no state has changed. It may happen
87   // when the notification is already closed by the user, see crbug.com/285941.
88   if (!state_changed && !message_center->HasNotification(notification_id))
89     return;
90 
91   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
92   message_center::RichNotificationData data;
93   // Makes the spoken feedback only when the state has been changed.
94   data.should_make_spoken_feedback_for_popup_updates = state_changed;
95   scoped_ptr<Notification> notification(new Notification(
96       message_center::NOTIFICATION_TYPE_SIMPLE,
97       notification_id,
98       FormatRemainingSessionTimeNotification(remaining_time),
99       base::string16() /* message */,
100       bundle.GetImageNamed(IDR_AURA_UBER_TRAY_SESSION_LENGTH_LIMIT_TIMER),
101       base::string16() /* display_source */,
102       message_center::NotifierId(
103           message_center::NotifierId::SYSTEM_COMPONENT,
104           system_notifier::kNotifierSessionLengthTimeout),
105       data,
106       NULL /* delegate */));
107   notification->SetSystemPriority();
108   message_center::MessageCenter::Get()->AddNotification(notification.Pass());
109 }
110 
111 }  // namespace
112 
113 namespace tray {
114 
115 class RemainingSessionTimeTrayView : public views::View {
116  public:
117   RemainingSessionTimeTrayView(const TraySessionLengthLimit* owner,
118                                ShelfAlignment shelf_alignment);
119   virtual ~RemainingSessionTimeTrayView();
120 
121   void UpdateClockLayout(ShelfAlignment shelf_alignment);
122   void Update();
123 
124  private:
125   void SetBorder(ShelfAlignment shelf_alignment);
126 
127   const TraySessionLengthLimit* owner_;
128 
129   views::Label* horizontal_layout_label_;
130   views::Label* vertical_layout_label_hours_left_;
131   views::Label* vertical_layout_label_hours_right_;
132   views::Label* vertical_layout_label_minutes_left_;
133   views::Label* vertical_layout_label_minutes_right_;
134   views::Label* vertical_layout_label_seconds_left_;
135   views::Label* vertical_layout_label_seconds_right_;
136 
137   DISALLOW_COPY_AND_ASSIGN(RemainingSessionTimeTrayView);
138 };
139 
RemainingSessionTimeTrayView(const TraySessionLengthLimit * owner,ShelfAlignment shelf_alignment)140 RemainingSessionTimeTrayView::RemainingSessionTimeTrayView(
141     const TraySessionLengthLimit* owner,
142     ShelfAlignment shelf_alignment)
143     : owner_(owner),
144       horizontal_layout_label_(NULL),
145       vertical_layout_label_hours_left_(NULL),
146       vertical_layout_label_hours_right_(NULL),
147       vertical_layout_label_minutes_left_(NULL),
148       vertical_layout_label_minutes_right_(NULL),
149       vertical_layout_label_seconds_left_(NULL),
150       vertical_layout_label_seconds_right_(NULL) {
151   UpdateClockLayout(shelf_alignment);
152 }
153 
~RemainingSessionTimeTrayView()154 RemainingSessionTimeTrayView::~RemainingSessionTimeTrayView() {
155 }
156 
UpdateClockLayout(ShelfAlignment shelf_alignment)157 void RemainingSessionTimeTrayView::UpdateClockLayout(
158     ShelfAlignment shelf_alignment) {
159   SetBorder(shelf_alignment);
160   const bool horizontal_layout = (shelf_alignment == SHELF_ALIGNMENT_BOTTOM ||
161       shelf_alignment == SHELF_ALIGNMENT_TOP);
162   if (horizontal_layout && !horizontal_layout_label_) {
163     // Remove labels used for vertical layout.
164     RemoveAllChildViews(true);
165     vertical_layout_label_hours_left_ = NULL;
166     vertical_layout_label_hours_right_ = NULL;
167     vertical_layout_label_minutes_left_ = NULL;
168     vertical_layout_label_minutes_right_ = NULL;
169     vertical_layout_label_seconds_left_ = NULL;
170     vertical_layout_label_seconds_right_ = NULL;
171 
172     // Create label used for horizontal layout.
173     horizontal_layout_label_ = CreateAndSetupLabel();
174 
175     // Construct layout.
176     SetLayoutManager(
177         new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
178     AddChildView(horizontal_layout_label_);
179 
180   } else if (!horizontal_layout && horizontal_layout_label_) {
181     // Remove label used for horizontal layout.
182     RemoveAllChildViews(true);
183     horizontal_layout_label_ = NULL;
184 
185     // Create labels used for vertical layout.
186     vertical_layout_label_hours_left_ = CreateAndSetupLabel();
187     vertical_layout_label_hours_right_ = CreateAndSetupLabel();
188     vertical_layout_label_minutes_left_ = CreateAndSetupLabel();
189     vertical_layout_label_minutes_right_ = CreateAndSetupLabel();
190     vertical_layout_label_seconds_left_ = CreateAndSetupLabel();
191     vertical_layout_label_seconds_right_ = CreateAndSetupLabel();
192 
193     // Construct layout.
194     views::GridLayout* layout = new views::GridLayout(this);
195     SetLayoutManager(layout);
196     views::ColumnSet* columns = layout->AddColumnSet(0);
197     columns->AddPaddingColumn(0, 6);
198     columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
199                        0, views::GridLayout::USE_PREF, 0, 0);
200     columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
201                        0, views::GridLayout::USE_PREF, 0, 0);
202     layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVerticalAlignment);
203     layout->StartRow(0, 0);
204     layout->AddView(vertical_layout_label_hours_left_);
205     layout->AddView(vertical_layout_label_hours_right_);
206     layout->StartRow(0, 0);
207     layout->AddView(vertical_layout_label_minutes_left_);
208     layout->AddView(vertical_layout_label_minutes_right_);
209     layout->StartRow(0, 0);
210     layout->AddView(vertical_layout_label_seconds_left_);
211     layout->AddView(vertical_layout_label_seconds_right_);
212     layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVerticalAlignment);
213   }
214   Update();
215 }
216 
Update()217 void RemainingSessionTimeTrayView::Update() {
218   const TraySessionLengthLimit::LimitState limit_state =
219       owner_->GetLimitState();
220 
221   if (limit_state == TraySessionLengthLimit::LIMIT_NONE) {
222     SetVisible(false);
223     return;
224   }
225 
226   int seconds = owner_->GetRemainingSessionTime().InSeconds();
227   // If the remaining session time is 100 hours or more, show 99:59:59 instead.
228   seconds = std::min(seconds, 100 * 60 * 60 - 1);  // 100 hours - 1 second.
229   int minutes = seconds / 60;
230   seconds %= 60;
231   const int hours = minutes / 60;
232   minutes %= 60;
233 
234   const base::string16 hours_str = IntToTwoDigitString(hours);
235   const base::string16 minutes_str = IntToTwoDigitString(minutes);
236   const base::string16 seconds_str = IntToTwoDigitString(seconds);
237   const SkColor color =
238       limit_state == TraySessionLengthLimit::LIMIT_EXPIRING_SOON ?
239           kRemainingTimeExpiringSoonColor : kRemainingTimeColor;
240 
241   if (horizontal_layout_label_) {
242     horizontal_layout_label_->SetText(l10n_util::GetStringFUTF16(
243         IDS_ASH_STATUS_TRAY_REMAINING_SESSION_TIME,
244         hours_str, minutes_str, seconds_str));
245     horizontal_layout_label_->SetEnabledColor(color);
246   } else if (vertical_layout_label_hours_left_) {
247     vertical_layout_label_hours_left_->SetText(hours_str.substr(0, 1));
248     vertical_layout_label_hours_right_->SetText(hours_str.substr(1, 1));
249     vertical_layout_label_minutes_left_->SetText(minutes_str.substr(0, 1));
250     vertical_layout_label_minutes_right_->SetText(minutes_str.substr(1, 1));
251     vertical_layout_label_seconds_left_->SetText(seconds_str.substr(0, 1));
252     vertical_layout_label_seconds_right_->SetText(seconds_str.substr(1, 1));
253     vertical_layout_label_hours_left_->SetEnabledColor(color);
254     vertical_layout_label_hours_right_->SetEnabledColor(color);
255     vertical_layout_label_minutes_left_->SetEnabledColor(color);
256     vertical_layout_label_minutes_right_->SetEnabledColor(color);
257     vertical_layout_label_seconds_left_->SetEnabledColor(color);
258     vertical_layout_label_seconds_right_->SetEnabledColor(color);
259   }
260 
261   Layout();
262   SetVisible(true);
263 }
264 
SetBorder(ShelfAlignment shelf_alignment)265 void RemainingSessionTimeTrayView::SetBorder(ShelfAlignment shelf_alignment) {
266   if (shelf_alignment == SHELF_ALIGNMENT_BOTTOM ||
267       shelf_alignment == SHELF_ALIGNMENT_TOP) {
268     set_border(views::Border::CreateEmptyBorder(
269         0, kTrayLabelItemHorizontalPaddingBottomAlignment,
270         0, kTrayLabelItemHorizontalPaddingBottomAlignment));
271   } else {
272     set_border(NULL);
273   }
274 }
275 
276 }  // namespace tray
277 
278 // static
279 const char TraySessionLengthLimit::kNotificationId[] =
280     "chrome://session/timeout";
281 
TraySessionLengthLimit(SystemTray * system_tray)282 TraySessionLengthLimit::TraySessionLengthLimit(SystemTray* system_tray)
283     : SystemTrayItem(system_tray),
284       tray_view_(NULL),
285       limit_state_(LIMIT_NONE) {
286   Shell::GetInstance()->system_tray_notifier()->
287       AddSessionLengthLimitObserver(this);
288   Update();
289 }
290 
~TraySessionLengthLimit()291 TraySessionLengthLimit::~TraySessionLengthLimit() {
292   Shell::GetInstance()->system_tray_notifier()->
293       RemoveSessionLengthLimitObserver(this);
294 }
295 
CreateTrayView(user::LoginStatus status)296 views::View* TraySessionLengthLimit::CreateTrayView(user::LoginStatus status) {
297   CHECK(tray_view_ == NULL);
298   tray_view_ = new tray::RemainingSessionTimeTrayView(
299       this, system_tray()->shelf_alignment());
300   return tray_view_;
301 }
302 
DestroyTrayView()303 void TraySessionLengthLimit::DestroyTrayView() {
304   tray_view_ = NULL;
305 }
306 
UpdateAfterShelfAlignmentChange(ShelfAlignment alignment)307 void TraySessionLengthLimit::UpdateAfterShelfAlignmentChange(
308     ShelfAlignment alignment) {
309   if (tray_view_)
310     tray_view_->UpdateClockLayout(alignment);
311 }
312 
OnSessionStartTimeChanged()313 void TraySessionLengthLimit::OnSessionStartTimeChanged() {
314   Update();
315 }
316 
OnSessionLengthLimitChanged()317 void TraySessionLengthLimit::OnSessionLengthLimitChanged() {
318   Update();
319 }
320 
321 TraySessionLengthLimit::LimitState
GetLimitState() const322     TraySessionLengthLimit::GetLimitState() const {
323   return limit_state_;
324 }
325 
GetRemainingSessionTime() const326 base::TimeDelta TraySessionLengthLimit::GetRemainingSessionTime() const {
327   return remaining_session_time_;
328 }
329 
Update()330 void TraySessionLengthLimit::Update() {
331   SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
332   const LimitState previous_limit_state = limit_state_;
333   if (!delegate->GetSessionStartTime(&session_start_time_) ||
334       !delegate->GetSessionLengthLimit(&limit_)) {
335     remaining_session_time_ = base::TimeDelta();
336     limit_state_ = LIMIT_NONE;
337     timer_.reset();
338   } else {
339     remaining_session_time_ = std::max(
340         limit_ - (base::TimeTicks::Now() - session_start_time_),
341         base::TimeDelta());
342     limit_state_ = remaining_session_time_.InSeconds() <=
343         kExpiringSoonThresholdInSeconds ? LIMIT_EXPIRING_SOON : LIMIT_SET;
344     if (!timer_)
345       timer_.reset(new base::RepeatingTimer<TraySessionLengthLimit>);
346     if (!timer_->IsRunning()) {
347       // Start a timer that will update the remaining session time every second.
348       timer_->Start(FROM_HERE,
349                     base::TimeDelta::FromSeconds(1),
350                     this,
351                     &TraySessionLengthLimit::Update);
352     }
353   }
354 
355   switch (limit_state_) {
356     case LIMIT_NONE:
357       message_center::MessageCenter::Get()->RemoveNotification(
358           kNotificationId, false /* by_user */);
359       break;
360     case LIMIT_SET:
361       CreateOrUpdateNotification(
362           kNotificationId,
363           remaining_session_time_,
364           previous_limit_state == LIMIT_NONE);
365       break;
366     case LIMIT_EXPIRING_SOON:
367       CreateOrUpdateNotification(
368           kNotificationId,
369           remaining_session_time_,
370           previous_limit_state == LIMIT_NONE ||
371           previous_limit_state == LIMIT_SET);
372       break;
373   }
374 
375   // Update the tray view last so that it can check whether the notification
376   // view is currently visible or not.
377   if (tray_view_)
378     tray_view_->Update();
379 }
380 
IsTrayViewVisibleForTest()381 bool TraySessionLengthLimit::IsTrayViewVisibleForTest() {
382   return tray_view_ && tray_view_->visible();
383 }
384 
385 }  // namespace internal
386 }  // namespace ash
387