blob: f615a94553d2db791e146bff8b2dd99109a24a6f [file] [log] [blame]
[email protected]6ef71d72013-08-10 18:13:441// 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.
4
5#include "ash/display/resolution_notification_controller.h"
6
7#include "ash/display/display_controller.h"
[email protected]7badf7f2014-08-06 04:38:298#include "ash/display/display_info.h"
[email protected]6ef71d72013-08-10 18:13:449#include "ash/display/display_manager.h"
10#include "ash/shell.h"
[email protected]a69b6f872013-08-24 14:38:3311#include "ash/system/system_notifier.h"
[email protected]6ef71d72013-08-10 18:13:4412#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"
23
24using message_center::Notification;
25
26namespace ash {
[email protected]6ef71d72013-08-10 18:13:4427namespace {
28
29bool g_use_timer = true;
30
31class ResolutionChangeNotificationDelegate
32 : public message_center::NotificationDelegate {
33 public:
34 ResolutionChangeNotificationDelegate(
35 ResolutionNotificationController* controller,
36 bool has_timeout);
37
38 protected:
dcheng222b9c72015-01-16 00:48:0139 ~ResolutionChangeNotificationDelegate() override;
[email protected]6ef71d72013-08-10 18:13:4440
41 private:
42 // message_center::NotificationDelegate overrides:
dcheng222b9c72015-01-16 00:48:0143 void Close(bool by_user) override;
44 void Click() override;
45 bool HasClickedListener() override;
46 void ButtonClick(int button_index) override;
[email protected]6ef71d72013-08-10 18:13:4447
48 ResolutionNotificationController* controller_;
49 bool has_timeout_;
50
51 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
52};
53
54ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
55 ResolutionNotificationController* controller,
56 bool has_timeout)
57 : controller_(controller),
58 has_timeout_(has_timeout) {
59 DCHECK(controller_);
60}
61
62ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
63}
64
[email protected]6ef71d72013-08-10 18:13:4465void ResolutionChangeNotificationDelegate::Close(bool by_user) {
66 if (by_user)
[email protected]99e487e2013-08-13 20:58:4167 controller_->AcceptResolutionChange(false);
[email protected]6ef71d72013-08-10 18:13:4468}
69
70void ResolutionChangeNotificationDelegate::Click() {
[email protected]99e487e2013-08-13 20:58:4171 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4472}
73
74bool ResolutionChangeNotificationDelegate::HasClickedListener() {
75 return true;
76}
77
78void 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)
[email protected]99e487e2013-08-13 20:58:4182 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4483 else
84 controller_->RevertResolutionChange();
85}
86
87} // namespace
88
89// static
90const int ResolutionNotificationController::kTimeoutInSec = 15;
91
92// static
93const char ResolutionNotificationController::kNotificationId[] =
94 "chrome://settings/display/resolution";
95
96struct ResolutionNotificationController::ResolutionChangeInfo {
97 ResolutionChangeInfo(int64 display_id,
[email protected]7badf7f2014-08-06 04:38:2998 const DisplayMode& old_resolution,
99 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44100 const base::Closure& accept_callback);
101 ~ResolutionChangeInfo();
102
103 // The id of the display where the resolution change happens.
104 int64 display_id;
105
106 // The resolution before the change.
[email protected]7badf7f2014-08-06 04:38:29107 DisplayMode old_resolution;
[email protected]6ef71d72013-08-10 18:13:44108
[email protected]c91bd6c2014-01-22 11:51:10109 // The requested resolution. Note that this may be different from
110 // |current_resolution| which is the actual resolution set.
[email protected]7badf7f2014-08-06 04:38:29111 DisplayMode new_resolution;
[email protected]6ef71d72013-08-10 18:13:44112
[email protected]c91bd6c2014-01-22 11:51:10113 // The actual resolution after the change.
[email protected]7badf7f2014-08-06 04:38:29114 DisplayMode current_resolution;
[email protected]c91bd6c2014-01-22 11:51:10115
[email protected]6ef71d72013-08-10 18:13:44116 // The callback when accept is chosen.
117 base::Closure accept_callback;
118
119 // The remaining timeout in seconds. 0 if the change does not time out.
120 uint8 timeout_count;
121
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;
126
127 private:
128 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
129};
130
131ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
132 int64 display_id,
[email protected]7badf7f2014-08-06 04:38:29133 const DisplayMode& old_resolution,
134 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44135 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),
140 timeout_count(0) {
141 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
142 if (!display_manager->HasInternalDisplay() &&
143 display_manager->num_connected_displays() == 1u) {
144 timeout_count = kTimeoutInSec;
145 }
146}
147
148ResolutionNotificationController::ResolutionChangeInfo::
149 ~ResolutionChangeInfo() {
150}
151
152ResolutionNotificationController::ResolutionNotificationController() {
153 Shell::GetInstance()->display_controller()->AddObserver(this);
154 Shell::GetScreen()->AddObserver(this);
155}
156
157ResolutionNotificationController::~ResolutionNotificationController() {
158 Shell::GetInstance()->display_controller()->RemoveObserver(this);
159 Shell::GetScreen()->RemoveObserver(this);
160}
161
[email protected]7badf7f2014-08-06 04:38:29162void ResolutionNotificationController::PrepareNotification(
[email protected]6ef71d72013-08-10 18:13:44163 int64 display_id,
[email protected]7badf7f2014-08-06 04:38:29164 const DisplayMode& old_resolution,
165 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44166 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|.
[email protected]7badf7f2014-08-06 04:38:29170 DisplayMode original_resolution;
[email protected]6ef71d72013-08-10 18:13:44171 if (change_info_ && change_info_->display_id == display_id) {
[email protected]7badf7f2014-08-06 04:38:29172 DCHECK(change_info_->new_resolution.size == old_resolution.size);
[email protected]6ef71d72013-08-10 18:13:44173 original_resolution = change_info_->old_resolution;
174 }
175
176 change_info_.reset(new ResolutionChangeInfo(
177 display_id, old_resolution, new_resolution, accept_callback));
[email protected]7badf7f2014-08-06 04:38:29178 if (!original_resolution.size.IsEmpty())
[email protected]6ef71d72013-08-10 18:13:44179 change_info_->old_resolution = original_resolution;
[email protected]6ef71d72013-08-10 18:13:44180}
181
182bool ResolutionNotificationController::DoesNotificationTimeout() {
183 return change_info_ && change_info_->timeout_count > 0;
184}
185
[email protected]86eee6e2013-08-27 01:27:15186void ResolutionNotificationController::CreateOrUpdateNotification(
187 bool enable_spoken_feedback) {
[email protected]6ef71d72013-08-10 18:13:44188 message_center::MessageCenter* message_center =
189 message_center::MessageCenter::Get();
190 if (!change_info_) {
191 message_center->RemoveNotification(kNotificationId, false /* by_user */);
192 return;
193 }
194
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,
[email protected]b6cc963b2014-02-27 15:32:23202 ui::TimeFormat::Simple(
203 ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
[email protected]6ef71d72013-08-10 18:13:44204 base::TimeDelta::FromSeconds(change_info_->timeout_count)));
205 }
206 data.buttons.push_back(message_center::ButtonInfo(
207 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT)));
208
[email protected]86eee6e2013-08-27 01:27:15209 data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
210
[email protected]c91bd6c2014-01-22 11:51:10211 const base::string16 display_name = base::UTF8ToUTF16(
212 Shell::GetInstance()->display_manager()->GetDisplayNameForId(
213 change_info_->display_id));
214 const base::string16 message =
[email protected]7badf7f2014-08-06 04:38:29215 (change_info_->new_resolution.size ==
216 change_info_->current_resolution.size) ?
[email protected]c91bd6c2014-01-22 11:51:10217 l10n_util::GetStringFUTF16(
218 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
219 display_name,
[email protected]7badf7f2014-08-06 04:38:29220 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString())) :
[email protected]c91bd6c2014-01-22 11:51:10221 l10n_util::GetStringFUTF16(
222 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
223 display_name,
[email protected]7badf7f2014-08-06 04:38:29224 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()),
225 base::UTF8ToUTF16(change_info_->current_resolution.size.ToString()));
[email protected]c91bd6c2014-01-22 11:51:10226
[email protected]6ef71d72013-08-10 18:13:44227 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
228 scoped_ptr<Notification> notification(new Notification(
229 message_center::NOTIFICATION_TYPE_SIMPLE,
230 kNotificationId,
[email protected]c91bd6c2014-01-22 11:51:10231 message,
[email protected]6ef71d72013-08-10 18:13:44232 timeout_message,
[email protected]9bc85622014-01-22 22:29:14233 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
[email protected]6ef71d72013-08-10 18:13:44234 base::string16() /* display_source */,
[email protected]b78d79a52013-09-12 01:52:21235 message_center::NotifierId(
[email protected]de22ffea2013-12-07 03:34:29236 message_center::NotifierId::SYSTEM_COMPONENT,
237 system_notifier::kNotifierDisplayResolutionChange),
[email protected]6ef71d72013-08-10 18:13:44238 data,
239 new ResolutionChangeNotificationDelegate(
240 this, change_info_->timeout_count > 0)));
241 notification->SetSystemPriority();
242 message_center->AddNotification(notification.Pass());
243}
244
245void ResolutionNotificationController::OnTimerTick() {
246 if (!change_info_)
247 return;
248
249 --change_info_->timeout_count;
250 if (change_info_->timeout_count == 0)
251 RevertResolutionChange();
252 else
[email protected]86eee6e2013-08-27 01:27:15253 CreateOrUpdateNotification(false);
[email protected]6ef71d72013-08-10 18:13:44254}
255
[email protected]99e487e2013-08-13 20:58:41256void ResolutionNotificationController::AcceptResolutionChange(
257 bool close_notification) {
258 if (close_notification) {
259 message_center::MessageCenter::Get()->RemoveNotification(
260 kNotificationId, false /* by_user */);
261 }
[email protected]6ef71d72013-08-10 18:13:44262 base::Closure callback = change_info_->accept_callback;
263 change_info_.reset();
264 callback.Run();
265}
266
267void ResolutionNotificationController::RevertResolutionChange() {
268 message_center::MessageCenter::Get()->RemoveNotification(
269 kNotificationId, false /* by_user */);
270 int64 display_id = change_info_->display_id;
[email protected]7badf7f2014-08-06 04:38:29271 DisplayMode old_resolution = change_info_->old_resolution;
[email protected]6ef71d72013-08-10 18:13:44272 change_info_.reset();
[email protected]7badf7f2014-08-06 04:38:29273 Shell::GetInstance()->display_manager()->SetDisplayMode(
[email protected]6ef71d72013-08-10 18:13:44274 display_id, old_resolution);
275}
276
[email protected]6ef71d72013-08-10 18:13:44277void ResolutionNotificationController::OnDisplayAdded(
278 const gfx::Display& new_display) {
279}
280
281void ResolutionNotificationController::OnDisplayRemoved(
282 const gfx::Display& old_display) {
283 if (change_info_ && change_info_->display_id == old_display.id())
284 RevertResolutionChange();
285}
286
[email protected]0c5703d2014-05-22 01:26:01287void ResolutionNotificationController::OnDisplayMetricsChanged(
288 const gfx::Display&, uint32_t) {
289}
290
[email protected]6ef71d72013-08-10 18:13:44291void ResolutionNotificationController::OnDisplayConfigurationChanged() {
292 if (!change_info_)
293 return;
294
[email protected]7badf7f2014-08-06 04:38:29295 change_info_->current_resolution = Shell::GetInstance()->display_manager()->
296 GetActiveModeForDisplayId(change_info_->display_id);
[email protected]86eee6e2013-08-27 01:27:15297 CreateOrUpdateNotification(true);
[email protected]6ef71d72013-08-10 18:13:44298 if (g_use_timer && change_info_->timeout_count > 0) {
299 change_info_->timer.Start(FROM_HERE,
300 base::TimeDelta::FromSeconds(1),
301 this,
302 &ResolutionNotificationController::OnTimerTick);
303 }
304}
305
306void ResolutionNotificationController::SuppressTimerForTest() {
307 g_use_timer = false;
308}
309
[email protected]6ef71d72013-08-10 18:13:44310} // namespace ash