Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / ui / idle_app_name_notification_view.cc
1 // Copyright 2014 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 "chrome/browser/chromeos/ui/idle_app_name_notification_view.h"
6
7 #include <string>
8
9 #include "ash/shell.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/wm/window_animations.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "extensions/common/extension.h"
18 #include "grit/generated_resources.h"
19 #include "ui/accessibility/ax_view_state.h"
20 #include "ui/aura/window.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/compositor/layer_animation_observer.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/gfx/font_list.h"
27 #include "ui/gfx/text_utils.h"
28 #include "ui/views/controls/label.h"
29 #include "ui/views/layout/box_layout.h"
30 #include "ui/views/layout/fill_layout.h"
31 #include "ui/views/view.h"
32 #include "ui/views/widget/widget.h"
33 #include "ui/views/widget/widget_delegate.h"
34
35 namespace ui {
36 class LayerAnimationSequence;
37 }
38
39 namespace chromeos {
40 namespace {
41
42 // Color of the text of the warning message.
43 const SkColor kTextColor = SK_ColorBLACK;
44
45 // Color of the text of the warning message.
46 const SkColor kErrorTextColor = SK_ColorRED;
47
48 // Color of the window background.
49 const SkColor kWindowBackgroundColor = SK_ColorWHITE;
50
51 // Radius of the rounded corners of the window.
52 const int kWindowCornerRadius = 4;
53
54 // Creates and shows the message widget for |view| with |animation_time_ms|.
55 void CreateAndShowWidgetWithContent(views::WidgetDelegate* delegate,
56                                     views::View* view,
57                                     int animation_time_ms) {
58   aura::Window* root_window = ash::Shell::GetTargetRootWindow();
59   gfx::Size rs = root_window->bounds().size();
60   gfx::Size ps = view->GetPreferredSize();
61   gfx::Rect bounds((rs.width() - ps.width()) / 2,
62                    -ps.height(),
63                    ps.width(),
64                    ps.height());
65   views::Widget::InitParams params;
66   params.type = views::Widget::InitParams::TYPE_POPUP;
67   params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
68   params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET;
69   params.accept_events = false;
70   params.can_activate = false;
71   params.keep_on_top = true;
72   params.remove_standard_frame = true;
73   params.delegate = delegate;
74   params.bounds = bounds;
75   params.parent = ash::Shell::GetContainer(
76       root_window, ash::kShellWindowId_SettingBubbleContainer);
77   views::Widget* widget = new views::Widget;
78   widget->Init(params);
79   widget->SetContentsView(view);
80   gfx::NativeView native_view = widget->GetNativeView();
81   native_view->SetName("KioskIdleAppNameNotification");
82
83   // Note: We cannot use the Window show/hide animations since they are disabled
84   // for kiosk by command line.
85   ui::LayerAnimator* animator = new ui::LayerAnimator(
86           base::TimeDelta::FromMilliseconds(animation_time_ms));
87   native_view->layer()->SetAnimator(animator);
88   widget->Show();
89
90   // We don't care about the show animation since it is off screen, so stop the
91   // started animation and move the message into view.
92   animator->StopAnimating();
93   bounds.set_y((rs.height() - ps.height()) / 20);
94   widget->SetBounds(bounds);
95
96   // Allow to use the message for spoken feedback.
97   view->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
98 }
99
100 }  // namespace
101
102 // The class which implements the content view for the message.
103 class IdleAppNameNotificationDelegateView
104     : public views::WidgetDelegateView,
105       public ui::ImplicitAnimationObserver {
106  public:
107   // An idle message which will get shown from the caller and hides itself after
108   // a time, calling |owner->CloseMessage| to inform the owner that it got
109   // destroyed. The |app_name| is a string which gets used as message and
110   // |error| is true if something is not correct.
111   // |message_visibility_time_in_ms| ms's after creation the message will start
112   // to remove itself from the screen.
113   IdleAppNameNotificationDelegateView(IdleAppNameNotificationView *owner,
114                                       const base::string16& app_name,
115                                       bool error,
116                                       int message_visibility_time_in_ms)
117       : owner_(owner),
118         widget_closed_(false) {
119     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
120     // Add the application name label to the message.
121     AddLabel(app_name,
122              rb.GetFontList(ui::ResourceBundle::BoldFont),
123              error ? kErrorTextColor : kTextColor);
124     spoken_text_ = app_name;
125     SetLayoutManager(new views::FillLayout);
126
127     // Set a timer which will trigger to remove the message after the given
128     // time.
129     hide_timer_.Start(
130         FROM_HERE,
131         base::TimeDelta::FromMilliseconds(message_visibility_time_in_ms),
132         this,
133         &IdleAppNameNotificationDelegateView::RemoveMessage);
134   }
135
136   virtual ~IdleAppNameNotificationDelegateView() {
137     // The widget is already closing, but the other cleanup items need to be
138     // performed.
139     widget_closed_ = true;
140     Close();
141   }
142
143   // Close the widget immediately. This can be called from the owner or from
144   // this class.
145   void Close() {
146     // Stop the timer (if it was running).
147     hide_timer_.Stop();
148     // Inform our owner that we are going away.
149     if (owner_) {
150       IdleAppNameNotificationView* owner = owner_;
151       owner_ = NULL;
152       owner->CloseMessage();
153     }
154     // Close the owning widget - if required.
155     if (!widget_closed_) {
156       widget_closed_ = true;
157       GetWidget()->Close();
158     }
159   }
160
161   // Animate the window away (and close once done).
162   void RemoveMessage() {
163     aura::Window* widget_view = GetWidget()->GetNativeView();
164     ui::Layer* layer = widget_view->layer();
165     ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
166     settings.AddObserver(this);
167     gfx::Rect rect = widget_view->bounds();
168     rect.set_y(-GetPreferredSize().height());
169     layer->SetBounds(rect);
170   }
171
172   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
173     SkPaint paint;
174     paint.setStyle(SkPaint::kFill_Style);
175     paint.setColor(kWindowBackgroundColor);
176     canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, paint);
177     views::WidgetDelegateView::OnPaint(canvas);
178   }
179
180   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE {
181     state->name = spoken_text_;
182     state->role = ui::AX_ROLE_ALERT;
183   }
184
185   // ImplicitAnimationObserver overrides
186   virtual void OnImplicitAnimationsCompleted() OVERRIDE {
187     Close();
188   }
189
190  private:
191   // Adds the label to the view, using |text| with a |font| and a |text_color|.
192   void AddLabel(const base::string16& text,
193                 const gfx::FontList& font,
194                 SkColor text_color) {
195     views::Label* label = new views::Label;
196     label->SetText(text);
197     label->SetHorizontalAlignment(gfx::ALIGN_CENTER);
198     label->SetFontList(font);
199     label->SetEnabledColor(text_color);
200     label->SetDisabledColor(text_color);
201     label->SetAutoColorReadabilityEnabled(false);
202     AddChildView(label);
203   }
204
205   // A timer which calls us to remove the message from the screen.
206   base::OneShotTimer<IdleAppNameNotificationDelegateView> hide_timer_;
207
208   // The owner of this message which needs to get notified when the message
209   // closes.
210   IdleAppNameNotificationView* owner_;
211
212   // The spoken text.
213   base::string16 spoken_text_;
214
215   // True if the widget got already closed.
216   bool widget_closed_;
217
218   DISALLOW_COPY_AND_ASSIGN(IdleAppNameNotificationDelegateView);
219 };
220
221 IdleAppNameNotificationView::IdleAppNameNotificationView(
222     int message_visibility_time_in_ms,
223     int animation_time_ms,
224     const extensions::Extension* extension)
225     : view_(NULL) {
226   ShowMessage(message_visibility_time_in_ms, animation_time_ms, extension);
227 }
228
229 IdleAppNameNotificationView::~IdleAppNameNotificationView() {
230   CloseMessage();
231 }
232
233 void IdleAppNameNotificationView::CloseMessage() {
234   if (view_) {
235     IdleAppNameNotificationDelegateView* view = view_;
236     view_ = NULL;
237     view->Close();
238   }
239 }
240
241 bool IdleAppNameNotificationView::IsVisible() {
242   return view_ != NULL;
243 }
244
245 base::string16 IdleAppNameNotificationView::GetShownTextForTest() {
246   ui::AXViewState state;
247   DCHECK(view_);
248   view_->GetAccessibleState(&state);
249   return state.name;
250 }
251
252 void IdleAppNameNotificationView::ShowMessage(
253     int message_visibility_time_in_ms,
254     int animation_time_ms,
255     const extensions::Extension* extension) {
256   DCHECK(!view_);
257
258   base::string16 app_name;
259   bool error = false;
260   if (extension &&
261       !base::ContainsOnlyChars(extension->name(), base::kWhitespaceASCII)) {
262     app_name = base::UTF8ToUTF16(extension->name());
263   } else {
264     error = true;
265     app_name = l10n_util::GetStringUTF16(
266         IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION);
267   }
268
269   view_ = new IdleAppNameNotificationDelegateView(
270       this,
271       app_name,
272       error,
273       message_visibility_time_in_ms + animation_time_ms);
274   CreateAndShowWidgetWithContent(view_, view_, animation_time_ms);
275 }
276
277 }  // namespace chromeos