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.
5 #include "ash/system/tray_update.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shelf/shelf_widget.h"
10 #include "ash/shell.h"
11 #include "ash/system/status_area_widget.h"
12 #include "ash/system/tray/fixed_sized_image_view.h"
13 #include "ash/system/tray/system_tray.h"
14 #include "ash/system/tray/system_tray_delegate.h"
15 #include "ash/system/tray/system_tray_notifier.h"
16 #include "ash/system/tray/tray_constants.h"
17 #include "ash/system/tray/tray_views.h"
18 #include "base/time.h"
19 #include "base/timer.h"
20 #include "grit/ash_resources.h"
21 #include "grit/ash_strings.h"
22 #include "ui/aura/window.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/compositor/layer.h"
25 #include "ui/compositor/layer_animation_observer.h"
26 #include "ui/compositor/layer_animation_sequence.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/views/controls/image_view.h"
29 #include "ui/views/controls/label.h"
30 #include "ui/views/layout/box_layout.h"
31 #include "ui/views/widget/widget.h"
35 // How many seconds should we wait before showing the nag reminder?
36 const int kUpdateNaggingTimeSeconds
= 24 * 60 * 60;
38 // How long should the nag reminder be displayed?
39 const int kShowUpdateNaggerForSeconds
= 15;
41 int DecideResource(ash::UpdateObserver::UpdateSeverity severity
, bool dark
) {
43 case ash::UpdateObserver::UPDATE_NORMAL
:
44 return dark
? IDR_AURA_UBER_TRAY_UPDATE_DARK
:
45 IDR_AURA_UBER_TRAY_UPDATE
;
47 case ash::UpdateObserver::UPDATE_LOW_GREEN
:
48 return dark
? IDR_AURA_UBER_TRAY_UPDATE_DARK_GREEN
:
49 IDR_AURA_UBER_TRAY_UPDATE_GREEN
;
51 case ash::UpdateObserver::UPDATE_HIGH_ORANGE
:
52 return dark
? IDR_AURA_UBER_TRAY_UPDATE_DARK_ORANGE
:
53 IDR_AURA_UBER_TRAY_UPDATE_ORANGE
;
55 case ash::UpdateObserver::UPDATE_SEVERE_RED
:
56 return dark
? IDR_AURA_UBER_TRAY_UPDATE_DARK_RED
:
57 IDR_AURA_UBER_TRAY_UPDATE_RED
;
60 NOTREACHED() << "Unknown update severity level.";
64 class UpdateView
: public ash::internal::ActionableView
{
66 explicit UpdateView(ash::UpdateObserver::UpdateSeverity severity
) {
68 views::BoxLayout(views::BoxLayout::kHorizontal
,
69 ash::kTrayPopupPaddingHorizontal
, 0,
70 ash::kTrayPopupPaddingBetweenItems
));
72 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
73 views::ImageView
* image
=
74 new ash::internal::FixedSizedImageView(0, ash::kTrayPopupItemHeight
);
75 image
->SetImage(bundle
.GetImageNamed(DecideResource(severity
, true)).
79 AddChildView(new views::Label(
80 bundle
.GetLocalizedString(IDS_ASH_STATUS_TRAY_UPDATE
)));
81 SetAccessibleName(bundle
.GetLocalizedString(IDS_ASH_STATUS_TRAY_UPDATE
));
84 virtual ~UpdateView() {}
87 // Overridden from ActionableView.
88 virtual bool PerformAction(const ui::Event
& event
) OVERRIDE
{
89 ash::Shell::GetInstance()->system_tray_delegate()->RequestRestart();
93 DISALLOW_COPY_AND_ASSIGN(UpdateView
);
103 class UpdateNagger
: public ui::LayerAnimationObserver
{
105 explicit UpdateNagger(SystemTrayItem
* owner
)
108 owner_
->system_tray()->GetWidget()->GetNativeView()->layer()->
109 GetAnimator()->AddObserver(this);
112 virtual ~UpdateNagger() {
113 internal::StatusAreaWidget
* status_area
=
114 Shell::GetPrimaryRootWindowController()->shelf()->status_area_widget();
116 status_area
->system_tray()->GetWidget()->GetNativeView()->layer()->
117 GetAnimator()->RemoveObserver(this);
121 void RestartTimer() {
123 timer_
.Start(FROM_HERE
,
124 base::TimeDelta::FromSeconds(kUpdateNaggingTimeSeconds
),
131 owner_
->PopupDetailedView(kShowUpdateNaggerForSeconds
, false);
134 // Overridden from ui::LayerAnimationObserver.
135 virtual void OnLayerAnimationEnded(
136 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{
137 // TODO(oshima): Find out if the updator will be shown on non
139 if (Shell::GetPrimaryRootWindowController()->shelf()->IsVisible())
141 else if (!timer_
.IsRunning())
145 virtual void OnLayerAnimationAborted(
146 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{}
148 virtual void OnLayerAnimationScheduled(
149 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{}
151 SystemTrayItem
* owner_
;
152 base::OneShotTimer
<UpdateNagger
> timer_
;
154 DISALLOW_COPY_AND_ASSIGN(UpdateNagger
);
159 TrayUpdate::TrayUpdate(SystemTray
* system_tray
)
160 : TrayImageItem(system_tray
, IDR_AURA_UBER_TRAY_UPDATE
),
161 severity_(UpdateObserver::UPDATE_NORMAL
) {
162 Shell::GetInstance()->system_tray_notifier()->AddUpdateObserver(this);
165 TrayUpdate::~TrayUpdate() {
166 Shell::GetInstance()->system_tray_notifier()->RemoveUpdateObserver(this);
169 bool TrayUpdate::GetInitialVisibility() {
170 return Shell::GetInstance()->system_tray_delegate()->SystemShouldUpgrade();
173 views::View
* TrayUpdate::CreateDefaultView(user::LoginStatus status
) {
174 if (!Shell::GetInstance()->system_tray_delegate()->SystemShouldUpgrade())
176 return new UpdateView(severity_
);
179 views::View
* TrayUpdate::CreateDetailedView(user::LoginStatus status
) {
180 return CreateDefaultView(status
);
183 void TrayUpdate::DestroyDetailedView() {
185 // The nagger was being displayed. Now that the detailed view is being
186 // closed, that means either the user clicks on it to restart, or the user
187 // didn't click on it to restart. In either case, start the timer to show
188 // the nag reminder again after the specified time.
189 nagger_
->RestartTimer();
193 void TrayUpdate::OnUpdateRecommended(UpdateObserver::UpdateSeverity severity
) {
194 severity_
= severity
;
195 SetImageFromResourceId(DecideResource(severity_
, false));
196 tray_view()->SetVisible(true);
197 if (!Shell::GetPrimaryRootWindowController()->shelf()->IsVisible() &&
199 // The shelf is not visible, and there is no nagger scheduled.
200 nagger_
.reset(new tray::UpdateNagger(this));
204 } // namespace internal