1 // Copyright 2013 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/display/resolution_notification_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_info.h"
9 #include "ash/display/display_manager.h"
10 #include "ash/shell.h"
11 #include "ash/system/system_notifier.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "grit/ash_resources.h"
14 #include "grit/ash_strings.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/l10n/time_format.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/message_center/message_center.h"
21 #include "ui/message_center/notification.h"
22 #include "ui/message_center/notification_delegate.h"
24 using message_center::Notification
;
29 bool g_use_timer
= true;
31 class ResolutionChangeNotificationDelegate
32 : public message_center::NotificationDelegate
{
34 ResolutionChangeNotificationDelegate(
35 ResolutionNotificationController
* controller
,
39 ~ResolutionChangeNotificationDelegate() override
;
42 // message_center::NotificationDelegate overrides:
43 void Close(bool by_user
) override
;
44 void Click() override
;
45 bool HasClickedListener() override
;
46 void ButtonClick(int button_index
) override
;
48 ResolutionNotificationController
* controller_
;
51 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate
);
54 ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
55 ResolutionNotificationController
* controller
,
57 : controller_(controller
),
58 has_timeout_(has_timeout
) {
62 ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
65 void ResolutionChangeNotificationDelegate::Close(bool by_user
) {
67 controller_
->AcceptResolutionChange(false);
70 void ResolutionChangeNotificationDelegate::Click() {
71 controller_
->AcceptResolutionChange(true);
74 bool ResolutionChangeNotificationDelegate::HasClickedListener() {
78 void ResolutionChangeNotificationDelegate::ButtonClick(int button_index
) {
79 // If there's the timeout, the first button is "Accept". Otherwise the
80 // button click should be "Revert".
81 if (has_timeout_
&& button_index
== 0)
82 controller_
->AcceptResolutionChange(true);
84 controller_
->RevertResolutionChange();
90 const int ResolutionNotificationController::kTimeoutInSec
= 15;
93 const char ResolutionNotificationController::kNotificationId
[] =
94 "chrome://settings/display/resolution";
96 struct ResolutionNotificationController::ResolutionChangeInfo
{
97 ResolutionChangeInfo(int64 display_id
,
98 const DisplayMode
& old_resolution
,
99 const DisplayMode
& new_resolution
,
100 const base::Closure
& accept_callback
);
101 ~ResolutionChangeInfo();
103 // The id of the display where the resolution change happens.
106 // The resolution before the change.
107 DisplayMode old_resolution
;
109 // The requested resolution. Note that this may be different from
110 // |current_resolution| which is the actual resolution set.
111 DisplayMode new_resolution
;
113 // The actual resolution after the change.
114 DisplayMode current_resolution
;
116 // The callback when accept is chosen.
117 base::Closure accept_callback
;
119 // The remaining timeout in seconds. 0 if the change does not time out.
122 // The timer to invoke OnTimerTick() every second. This cannot be
123 // OneShotTimer since the message contains text "automatically closed in xx
124 // seconds..." which has to be updated every second.
125 base::RepeatingTimer
<ResolutionNotificationController
> timer
;
128 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo
);
131 ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
133 const DisplayMode
& old_resolution
,
134 const DisplayMode
& new_resolution
,
135 const base::Closure
& accept_callback
)
136 : display_id(display_id
),
137 old_resolution(old_resolution
),
138 new_resolution(new_resolution
),
139 accept_callback(accept_callback
),
141 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
142 if (!display_manager
->HasInternalDisplay() &&
143 display_manager
->num_connected_displays() == 1u) {
144 timeout_count
= kTimeoutInSec
;
148 ResolutionNotificationController::ResolutionChangeInfo::
149 ~ResolutionChangeInfo() {
152 ResolutionNotificationController::ResolutionNotificationController() {
153 Shell::GetInstance()->display_controller()->AddObserver(this);
154 Shell::GetScreen()->AddObserver(this);
157 ResolutionNotificationController::~ResolutionNotificationController() {
158 Shell::GetInstance()->display_controller()->RemoveObserver(this);
159 Shell::GetScreen()->RemoveObserver(this);
162 void ResolutionNotificationController::PrepareNotification(
164 const DisplayMode
& old_resolution
,
165 const DisplayMode
& new_resolution
,
166 const base::Closure
& accept_callback
) {
167 // If multiple resolution changes are invoked for the same display,
168 // the original resolution for the first resolution change has to be used
169 // instead of the specified |old_resolution|.
170 DisplayMode original_resolution
;
171 if (change_info_
&& change_info_
->display_id
== display_id
) {
172 DCHECK(change_info_
->new_resolution
.size
== old_resolution
.size
);
173 original_resolution
= change_info_
->old_resolution
;
176 change_info_
.reset(new ResolutionChangeInfo(
177 display_id
, old_resolution
, new_resolution
, accept_callback
));
178 if (!original_resolution
.size
.IsEmpty())
179 change_info_
->old_resolution
= original_resolution
;
182 bool ResolutionNotificationController::DoesNotificationTimeout() {
183 return change_info_
&& change_info_
->timeout_count
> 0;
186 void ResolutionNotificationController::CreateOrUpdateNotification(
187 bool enable_spoken_feedback
) {
188 message_center::MessageCenter
* message_center
=
189 message_center::MessageCenter::Get();
191 message_center
->RemoveNotification(kNotificationId
, false /* by_user */);
195 base::string16 timeout_message
;
196 message_center::RichNotificationData data
;
197 if (change_info_
->timeout_count
> 0) {
198 data
.buttons
.push_back(message_center::ButtonInfo(
199 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT
)));
200 timeout_message
= l10n_util::GetStringFUTF16(
201 IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT
,
202 ui::TimeFormat::Simple(
203 ui::TimeFormat::FORMAT_DURATION
, ui::TimeFormat::LENGTH_LONG
,
204 base::TimeDelta::FromSeconds(change_info_
->timeout_count
)));
206 data
.buttons
.push_back(message_center::ButtonInfo(
207 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT
)));
209 data
.should_make_spoken_feedback_for_popup_updates
= enable_spoken_feedback
;
211 const base::string16 display_name
= base::UTF8ToUTF16(
212 Shell::GetInstance()->display_manager()->GetDisplayNameForId(
213 change_info_
->display_id
));
214 const base::string16 message
=
215 (change_info_
->new_resolution
.size
==
216 change_info_
->current_resolution
.size
) ?
217 l10n_util::GetStringFUTF16(
218 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED
,
220 base::UTF8ToUTF16(change_info_
->new_resolution
.size
.ToString())) :
221 l10n_util::GetStringFUTF16(
222 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED
,
224 base::UTF8ToUTF16(change_info_
->new_resolution
.size
.ToString()),
225 base::UTF8ToUTF16(change_info_
->current_resolution
.size
.ToString()));
227 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
228 scoped_ptr
<Notification
> notification(new Notification(
229 message_center::NOTIFICATION_TYPE_SIMPLE
,
233 bundle
.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY
),
234 base::string16() /* display_source */,
235 message_center::NotifierId(
236 message_center::NotifierId::SYSTEM_COMPONENT
,
237 system_notifier::kNotifierDisplayResolutionChange
),
239 new ResolutionChangeNotificationDelegate(
240 this, change_info_
->timeout_count
> 0)));
241 notification
->SetSystemPriority();
242 message_center
->AddNotification(notification
.Pass());
245 void ResolutionNotificationController::OnTimerTick() {
249 --change_info_
->timeout_count
;
250 if (change_info_
->timeout_count
== 0)
251 RevertResolutionChange();
253 CreateOrUpdateNotification(false);
256 void ResolutionNotificationController::AcceptResolutionChange(
257 bool close_notification
) {
258 if (close_notification
) {
259 message_center::MessageCenter::Get()->RemoveNotification(
260 kNotificationId
, false /* by_user */);
264 base::Closure callback
= change_info_
->accept_callback
;
265 change_info_
.reset();
269 void ResolutionNotificationController::RevertResolutionChange() {
270 message_center::MessageCenter::Get()->RemoveNotification(
271 kNotificationId
, false /* by_user */);
274 int64 display_id
= change_info_
->display_id
;
275 DisplayMode old_resolution
= change_info_
->old_resolution
;
276 change_info_
.reset();
277 Shell::GetInstance()->display_manager()->SetDisplayMode(
278 display_id
, old_resolution
);
281 void ResolutionNotificationController::OnDisplayAdded(
282 const gfx::Display
& new_display
) {
285 void ResolutionNotificationController::OnDisplayRemoved(
286 const gfx::Display
& old_display
) {
287 if (change_info_
&& change_info_
->display_id
== old_display
.id())
288 RevertResolutionChange();
291 void ResolutionNotificationController::OnDisplayMetricsChanged(
292 const gfx::Display
&, uint32_t) {
295 void ResolutionNotificationController::OnDisplayConfigurationChanged() {
299 change_info_
->current_resolution
= Shell::GetInstance()->display_manager()->
300 GetActiveModeForDisplayId(change_info_
->display_id
);
301 CreateOrUpdateNotification(true);
302 if (g_use_timer
&& change_info_
->timeout_count
> 0) {
303 change_info_
->timer
.Start(FROM_HERE
,
304 base::TimeDelta::FromSeconds(1),
306 &ResolutionNotificationController::OnTimerTick
);
310 void ResolutionNotificationController::SuppressTimerForTest() {