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_manager.h"
10 #include "ash/system/system_notifier.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "grit/ash_resources.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/l10n/time_format.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/display.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/message_center/message_center.h"
20 #include "ui/message_center/notification.h"
21 #include "ui/message_center/notification_delegate.h"
23 using message_center::Notification
;
29 bool g_use_timer
= true;
31 class ResolutionChangeNotificationDelegate
32 : public message_center::NotificationDelegate
{
34 ResolutionChangeNotificationDelegate(
35 ResolutionNotificationController
* controller
,
39 virtual ~ResolutionChangeNotificationDelegate();
42 // message_center::NotificationDelegate overrides:
43 virtual void Display() OVERRIDE
;
44 virtual void Error() OVERRIDE
;
45 virtual void Close(bool by_user
) OVERRIDE
;
46 virtual void Click() OVERRIDE
;
47 virtual bool HasClickedListener() OVERRIDE
;
48 virtual void ButtonClick(int button_index
) OVERRIDE
;
50 ResolutionNotificationController
* controller_
;
53 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate
);
56 ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
57 ResolutionNotificationController
* controller
,
59 : controller_(controller
),
60 has_timeout_(has_timeout
) {
64 ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
67 void ResolutionChangeNotificationDelegate::Display() {
70 void ResolutionChangeNotificationDelegate::Error() {
73 void ResolutionChangeNotificationDelegate::Close(bool by_user
) {
75 controller_
->AcceptResolutionChange(false);
78 void ResolutionChangeNotificationDelegate::Click() {
79 controller_
->AcceptResolutionChange(true);
82 bool ResolutionChangeNotificationDelegate::HasClickedListener() {
86 void ResolutionChangeNotificationDelegate::ButtonClick(int button_index
) {
87 // If there's the timeout, the first button is "Accept". Otherwise the
88 // button click should be "Revert".
89 if (has_timeout_
&& button_index
== 0)
90 controller_
->AcceptResolutionChange(true);
92 controller_
->RevertResolutionChange();
98 const int ResolutionNotificationController::kTimeoutInSec
= 15;
101 const char ResolutionNotificationController::kNotificationId
[] =
102 "chrome://settings/display/resolution";
104 struct ResolutionNotificationController::ResolutionChangeInfo
{
105 ResolutionChangeInfo(int64 display_id
,
106 const gfx::Size
& old_resolution
,
107 const gfx::Size
& new_resolution
,
108 const base::Closure
& accept_callback
);
109 ~ResolutionChangeInfo();
111 // The id of the display where the resolution change happens.
114 // The resolution before the change.
115 gfx::Size old_resolution
;
117 // The requested resolution. Note that this may be different from
118 // |current_resolution| which is the actual resolution set.
119 gfx::Size new_resolution
;
121 // The actual resolution after the change.
122 gfx::Size current_resolution
;
124 // The callback when accept is chosen.
125 base::Closure accept_callback
;
127 // The remaining timeout in seconds. 0 if the change does not time out.
130 // The timer to invoke OnTimerTick() every second. This cannot be
131 // OneShotTimer since the message contains text "automatically closed in xx
132 // seconds..." which has to be updated every second.
133 base::RepeatingTimer
<ResolutionNotificationController
> timer
;
136 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo
);
139 ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
141 const gfx::Size
& old_resolution
,
142 const gfx::Size
& new_resolution
,
143 const base::Closure
& accept_callback
)
144 : display_id(display_id
),
145 old_resolution(old_resolution
),
146 new_resolution(new_resolution
),
147 accept_callback(accept_callback
),
149 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
150 if (!display_manager
->HasInternalDisplay() &&
151 display_manager
->num_connected_displays() == 1u) {
152 timeout_count
= kTimeoutInSec
;
156 ResolutionNotificationController::ResolutionChangeInfo::
157 ~ResolutionChangeInfo() {
160 ResolutionNotificationController::ResolutionNotificationController() {
161 Shell::GetInstance()->display_controller()->AddObserver(this);
162 Shell::GetScreen()->AddObserver(this);
165 ResolutionNotificationController::~ResolutionNotificationController() {
166 Shell::GetInstance()->display_controller()->RemoveObserver(this);
167 Shell::GetScreen()->RemoveObserver(this);
170 void ResolutionNotificationController::SetDisplayResolutionAndNotify(
172 const gfx::Size
& old_resolution
,
173 const gfx::Size
& new_resolution
,
174 const base::Closure
& accept_callback
) {
175 // If multiple resolution changes are invoked for the same display,
176 // the original resolution for the first resolution change has to be used
177 // instead of the specified |old_resolution|.
178 gfx::Size original_resolution
;
179 if (change_info_
&& change_info_
->display_id
== display_id
) {
180 DCHECK(change_info_
->new_resolution
== old_resolution
);
181 original_resolution
= change_info_
->old_resolution
;
184 change_info_
.reset(new ResolutionChangeInfo(
185 display_id
, old_resolution
, new_resolution
, accept_callback
));
186 if (!original_resolution
.IsEmpty())
187 change_info_
->old_resolution
= original_resolution
;
189 // SetDisplayResolution() causes OnConfigurationChanged() and the notification
190 // will be shown at that point.
191 Shell::GetInstance()->display_manager()->SetDisplayResolution(
192 display_id
, new_resolution
);
195 bool ResolutionNotificationController::DoesNotificationTimeout() {
196 return change_info_
&& change_info_
->timeout_count
> 0;
199 void ResolutionNotificationController::CreateOrUpdateNotification(
200 bool enable_spoken_feedback
) {
201 message_center::MessageCenter
* message_center
=
202 message_center::MessageCenter::Get();
204 message_center
->RemoveNotification(kNotificationId
, false /* by_user */);
208 base::string16 timeout_message
;
209 message_center::RichNotificationData data
;
210 if (change_info_
->timeout_count
> 0) {
211 data
.buttons
.push_back(message_center::ButtonInfo(
212 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT
)));
213 timeout_message
= l10n_util::GetStringFUTF16(
214 IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT
,
215 ui::TimeFormat::TimeDurationLong(
216 base::TimeDelta::FromSeconds(change_info_
->timeout_count
)));
218 data
.buttons
.push_back(message_center::ButtonInfo(
219 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT
)));
221 data
.should_make_spoken_feedback_for_popup_updates
= enable_spoken_feedback
;
223 const base::string16 display_name
= base::UTF8ToUTF16(
224 Shell::GetInstance()->display_manager()->GetDisplayNameForId(
225 change_info_
->display_id
));
226 const base::string16 message
=
227 (change_info_
->new_resolution
== change_info_
->current_resolution
) ?
228 l10n_util::GetStringFUTF16(
229 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED
,
231 base::UTF8ToUTF16(change_info_
->new_resolution
.ToString())) :
232 l10n_util::GetStringFUTF16(
233 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED
,
235 base::UTF8ToUTF16(change_info_
->new_resolution
.ToString()),
236 base::UTF8ToUTF16(change_info_
->current_resolution
.ToString()));
238 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
239 scoped_ptr
<Notification
> notification(new Notification(
240 message_center::NOTIFICATION_TYPE_SIMPLE
,
244 bundle
.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY
),
245 base::string16() /* display_source */,
246 message_center::NotifierId(
247 message_center::NotifierId::SYSTEM_COMPONENT
,
248 system_notifier::kNotifierDisplayResolutionChange
),
250 new ResolutionChangeNotificationDelegate(
251 this, change_info_
->timeout_count
> 0)));
252 notification
->SetSystemPriority();
253 message_center
->AddNotification(notification
.Pass());
256 void ResolutionNotificationController::OnTimerTick() {
260 --change_info_
->timeout_count
;
261 if (change_info_
->timeout_count
== 0)
262 RevertResolutionChange();
264 CreateOrUpdateNotification(false);
267 void ResolutionNotificationController::AcceptResolutionChange(
268 bool close_notification
) {
269 if (close_notification
) {
270 message_center::MessageCenter::Get()->RemoveNotification(
271 kNotificationId
, false /* by_user */);
273 base::Closure callback
= change_info_
->accept_callback
;
274 change_info_
.reset();
278 void ResolutionNotificationController::RevertResolutionChange() {
279 message_center::MessageCenter::Get()->RemoveNotification(
280 kNotificationId
, false /* by_user */);
281 int64 display_id
= change_info_
->display_id
;
282 gfx::Size old_resolution
= change_info_
->old_resolution
;
283 change_info_
.reset();
284 Shell::GetInstance()->display_manager()->SetDisplayResolution(
285 display_id
, old_resolution
);
288 void ResolutionNotificationController::OnDisplayBoundsChanged(
289 const gfx::Display
& display
) {
292 void ResolutionNotificationController::OnDisplayAdded(
293 const gfx::Display
& new_display
) {
296 void ResolutionNotificationController::OnDisplayRemoved(
297 const gfx::Display
& old_display
) {
298 if (change_info_
&& change_info_
->display_id
== old_display
.id())
299 RevertResolutionChange();
302 void ResolutionNotificationController::OnDisplayConfigurationChanged() {
306 const DisplayInfo
& info
= Shell::GetInstance()->display_manager()->
307 GetDisplayInfo(change_info_
->display_id
);
308 change_info_
->current_resolution
= info
.bounds_in_native().size();
309 CreateOrUpdateNotification(true);
310 if (g_use_timer
&& change_info_
->timeout_count
> 0) {
311 change_info_
->timer
.Start(FROM_HERE
,
312 base::TimeDelta::FromSeconds(1),
314 &ResolutionNotificationController::OnTimerTick
);
318 void ResolutionNotificationController::SuppressTimerForTest() {
322 } // namespace internal