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