blob: ed57757b099a03156ff11e9b73a578819ad7a1fb [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
dcheng22724952015-12-31 03:17:547#include <utility>
8
msw7f1ec922016-06-22 23:58:569#include "ash/common/display/display_info.h"
msw6a94e0492016-06-17 17:21:3810#include "ash/common/system/system_notifier.h"
[email protected]6ef71d72013-08-10 18:13:4411#include "ash/display/display_manager.h"
12#include "ash/shell.h"
13#include "base/strings/utf_string_conversions.h"
14#include "grit/ash_resources.h"
15#include "grit/ash_strings.h"
16#include "ui/base/l10n/l10n_util.h"
17#include "ui/base/l10n/time_format.h"
18#include "ui/base/resource/resource_bundle.h"
oshimaf84b0da722016-04-27 19:47:1919#include "ui/display/display.h"
20#include "ui/display/screen.h"
[email protected]6ef71d72013-08-10 18:13:4421#include "ui/message_center/message_center.h"
22#include "ui/message_center/notification.h"
23#include "ui/message_center/notification_delegate.h"
24
25using message_center::Notification;
26
27namespace ash {
[email protected]6ef71d72013-08-10 18:13:4428namespace {
29
30bool g_use_timer = true;
31
32class ResolutionChangeNotificationDelegate
33 : public message_center::NotificationDelegate {
34 public:
35 ResolutionChangeNotificationDelegate(
36 ResolutionNotificationController* controller,
37 bool has_timeout);
38
39 protected:
dcheng222b9c72015-01-16 00:48:0140 ~ResolutionChangeNotificationDelegate() override;
[email protected]6ef71d72013-08-10 18:13:4441
42 private:
43 // message_center::NotificationDelegate overrides:
dcheng222b9c72015-01-16 00:48:0144 void Close(bool by_user) override;
45 void Click() override;
46 bool HasClickedListener() override;
47 void ButtonClick(int button_index) override;
[email protected]6ef71d72013-08-10 18:13:4448
49 ResolutionNotificationController* controller_;
50 bool has_timeout_;
51
52 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
53};
54
55ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
56 ResolutionNotificationController* controller,
57 bool has_timeout)
jamescookb8dcef522016-06-25 14:42:5558 : controller_(controller), has_timeout_(has_timeout) {
[email protected]6ef71d72013-08-10 18:13:4459 DCHECK(controller_);
60}
61
jamescookb8dcef522016-06-25 14:42:5562ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {}
[email protected]6ef71d72013-08-10 18:13:4463
[email protected]6ef71d72013-08-10 18:13:4464void ResolutionChangeNotificationDelegate::Close(bool by_user) {
65 if (by_user)
[email protected]99e487e2013-08-13 20:58:4166 controller_->AcceptResolutionChange(false);
[email protected]6ef71d72013-08-10 18:13:4467}
68
69void ResolutionChangeNotificationDelegate::Click() {
[email protected]99e487e2013-08-13 20:58:4170 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4471}
72
73bool ResolutionChangeNotificationDelegate::HasClickedListener() {
74 return true;
75}
76
77void ResolutionChangeNotificationDelegate::ButtonClick(int button_index) {
78 // If there's the timeout, the first button is "Accept". Otherwise the
79 // button click should be "Revert".
80 if (has_timeout_ && button_index == 0)
[email protected]99e487e2013-08-13 20:58:4181 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4482 else
83 controller_->RevertResolutionChange();
84}
85
86} // namespace
87
88// static
89const int ResolutionNotificationController::kTimeoutInSec = 15;
90
91// static
92const char ResolutionNotificationController::kNotificationId[] =
93 "chrome://settings/display/resolution";
94
95struct ResolutionNotificationController::ResolutionChangeInfo {
avidb567a8a2015-12-20 17:07:2496 ResolutionChangeInfo(int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:2997 const DisplayMode& old_resolution,
98 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:4499 const base::Closure& accept_callback);
100 ~ResolutionChangeInfo();
101
102 // The id of the display where the resolution change happens.
avidb567a8a2015-12-20 17:07:24103 int64_t display_id;
[email protected]6ef71d72013-08-10 18:13:44104
105 // The resolution before the change.
[email protected]7badf7f2014-08-06 04:38:29106 DisplayMode old_resolution;
[email protected]6ef71d72013-08-10 18:13:44107
[email protected]c91bd6c2014-01-22 11:51:10108 // The requested resolution. Note that this may be different from
109 // |current_resolution| which is the actual resolution set.
[email protected]7badf7f2014-08-06 04:38:29110 DisplayMode new_resolution;
[email protected]6ef71d72013-08-10 18:13:44111
[email protected]c91bd6c2014-01-22 11:51:10112 // The actual resolution after the change.
[email protected]7badf7f2014-08-06 04:38:29113 DisplayMode current_resolution;
[email protected]c91bd6c2014-01-22 11:51:10114
[email protected]6ef71d72013-08-10 18:13:44115 // The callback when accept is chosen.
116 base::Closure accept_callback;
117
118 // The remaining timeout in seconds. 0 if the change does not time out.
avidb567a8a2015-12-20 17:07:24119 uint8_t timeout_count;
[email protected]6ef71d72013-08-10 18:13:44120
121 // The timer to invoke OnTimerTick() every second. This cannot be
122 // OneShotTimer since the message contains text "automatically closed in xx
123 // seconds..." which has to be updated every second.
danakj8c3eb802015-09-24 07:53:00124 base::RepeatingTimer timer;
[email protected]6ef71d72013-08-10 18:13:44125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
128};
129
130ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
avidb567a8a2015-12-20 17:07:24131 int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:29132 const DisplayMode& old_resolution,
133 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44134 const base::Closure& accept_callback)
135 : display_id(display_id),
136 old_resolution(old_resolution),
137 new_resolution(new_resolution),
138 accept_callback(accept_callback),
139 timeout_count(0) {
140 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
oshimaf84b0da722016-04-27 19:47:19141 if (!display::Display::HasInternalDisplay() &&
[email protected]6ef71d72013-08-10 18:13:44142 display_manager->num_connected_displays() == 1u) {
143 timeout_count = kTimeoutInSec;
144 }
145}
146
147ResolutionNotificationController::ResolutionChangeInfo::
jamescookb8dcef522016-06-25 14:42:55148 ~ResolutionChangeInfo() {}
[email protected]6ef71d72013-08-10 18:13:44149
150ResolutionNotificationController::ResolutionNotificationController() {
oshimae2818922015-07-28 01:18:52151 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
oshimaf84b0da722016-04-27 19:47:19152 display::Screen::GetScreen()->AddObserver(this);
[email protected]6ef71d72013-08-10 18:13:44153}
154
155ResolutionNotificationController::~ResolutionNotificationController() {
oshimae2818922015-07-28 01:18:52156 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
oshimaf84b0da722016-04-27 19:47:19157 display::Screen::GetScreen()->RemoveObserver(this);
[email protected]6ef71d72013-08-10 18:13:44158}
159
[email protected]7badf7f2014-08-06 04:38:29160void ResolutionNotificationController::PrepareNotification(
avidb567a8a2015-12-20 17:07:24161 int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:29162 const DisplayMode& old_resolution,
163 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44164 const base::Closure& accept_callback) {
oshimaf84b0da722016-04-27 19:47:19165 DCHECK(!display::Display::IsInternalDisplayId(display_id));
[email protected]6ef71d72013-08-10 18:13:44166 // If multiple resolution changes are invoked for the same display,
167 // the original resolution for the first resolution change has to be used
168 // instead of the specified |old_resolution|.
[email protected]7badf7f2014-08-06 04:38:29169 DisplayMode original_resolution;
[email protected]6ef71d72013-08-10 18:13:44170 if (change_info_ && change_info_->display_id == display_id) {
[email protected]7badf7f2014-08-06 04:38:29171 DCHECK(change_info_->new_resolution.size == old_resolution.size);
[email protected]6ef71d72013-08-10 18:13:44172 original_resolution = change_info_->old_resolution;
173 }
174
jamescookb8dcef522016-06-25 14:42:55175 change_info_.reset(new ResolutionChangeInfo(display_id, old_resolution,
176 new_resolution, accept_callback));
[email protected]7badf7f2014-08-06 04:38:29177 if (!original_resolution.size.IsEmpty())
[email protected]6ef71d72013-08-10 18:13:44178 change_info_->old_resolution = original_resolution;
[email protected]6ef71d72013-08-10 18:13:44179}
180
181bool ResolutionNotificationController::DoesNotificationTimeout() {
182 return change_info_ && change_info_->timeout_count > 0;
183}
184
[email protected]86eee6e2013-08-27 01:27:15185void ResolutionNotificationController::CreateOrUpdateNotification(
186 bool enable_spoken_feedback) {
[email protected]6ef71d72013-08-10 18:13:44187 message_center::MessageCenter* message_center =
188 message_center::MessageCenter::Get();
189 if (!change_info_) {
190 message_center->RemoveNotification(kNotificationId, false /* by_user */);
191 return;
192 }
193
194 base::string16 timeout_message;
195 message_center::RichNotificationData data;
196 if (change_info_->timeout_count > 0) {
197 data.buttons.push_back(message_center::ButtonInfo(
198 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT)));
199 timeout_message = l10n_util::GetStringFUTF16(
200 IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT,
[email protected]b6cc963b2014-02-27 15:32:23201 ui::TimeFormat::Simple(
202 ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
[email protected]6ef71d72013-08-10 18:13:44203 base::TimeDelta::FromSeconds(change_info_->timeout_count)));
204 }
205 data.buttons.push_back(message_center::ButtonInfo(
jamescookb8dcef522016-06-25 14:42:55206 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT)));
[email protected]6ef71d72013-08-10 18:13:44207
[email protected]86eee6e2013-08-27 01:27:15208 data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
209
[email protected]c91bd6c2014-01-22 11:51:10210 const base::string16 display_name = base::UTF8ToUTF16(
211 Shell::GetInstance()->display_manager()->GetDisplayNameForId(
212 change_info_->display_id));
213 const base::string16 message =
[email protected]7badf7f2014-08-06 04:38:29214 (change_info_->new_resolution.size ==
jamescookb8dcef522016-06-25 14:42:55215 change_info_->current_resolution.size)
216 ? l10n_util::GetStringFUTF16(
217 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED, display_name,
218 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()))
219 : l10n_util::GetStringFUTF16(
220 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
221 display_name,
222 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()),
223 base::UTF8ToUTF16(
224 change_info_->current_resolution.size.ToString()));
[email protected]c91bd6c2014-01-22 11:51:10225
[email protected]6ef71d72013-08-10 18:13:44226 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
dchenga94547472016-04-08 08:41:11227 std::unique_ptr<Notification> notification(new Notification(
miguelg53a6dde52015-08-20 09:00:30228 message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, message,
229 timeout_message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
230 base::string16() /* display_source */, GURL(),
[email protected]b78d79a52013-09-12 01:52:21231 message_center::NotifierId(
[email protected]de22ffea2013-12-07 03:34:29232 message_center::NotifierId::SYSTEM_COMPONENT,
233 system_notifier::kNotifierDisplayResolutionChange),
miguelg53a6dde52015-08-20 09:00:30234 data, new ResolutionChangeNotificationDelegate(
235 this, change_info_->timeout_count > 0)));
[email protected]6ef71d72013-08-10 18:13:44236 notification->SetSystemPriority();
dcheng22724952015-12-31 03:17:54237 message_center->AddNotification(std::move(notification));
[email protected]6ef71d72013-08-10 18:13:44238}
239
240void ResolutionNotificationController::OnTimerTick() {
241 if (!change_info_)
242 return;
243
244 --change_info_->timeout_count;
245 if (change_info_->timeout_count == 0)
246 RevertResolutionChange();
247 else
[email protected]86eee6e2013-08-27 01:27:15248 CreateOrUpdateNotification(false);
[email protected]6ef71d72013-08-10 18:13:44249}
250
[email protected]99e487e2013-08-13 20:58:41251void ResolutionNotificationController::AcceptResolutionChange(
252 bool close_notification) {
253 if (close_notification) {
254 message_center::MessageCenter::Get()->RemoveNotification(
255 kNotificationId, false /* by_user */);
256 }
oshima8f0f45e42015-04-03 19:55:22257 if (!change_info_)
258 return;
[email protected]6ef71d72013-08-10 18:13:44259 base::Closure callback = change_info_->accept_callback;
260 change_info_.reset();
261 callback.Run();
262}
263
264void ResolutionNotificationController::RevertResolutionChange() {
jamescookb8dcef522016-06-25 14:42:55265 message_center::MessageCenter::Get()->RemoveNotification(kNotificationId,
266 false /* by_user */);
oshima8f0f45e42015-04-03 19:55:22267 if (!change_info_)
268 return;
avidb567a8a2015-12-20 17:07:24269 int64_t display_id = change_info_->display_id;
[email protected]7badf7f2014-08-06 04:38:29270 DisplayMode old_resolution = change_info_->old_resolution;
[email protected]6ef71d72013-08-10 18:13:44271 change_info_.reset();
jamescookb8dcef522016-06-25 14:42:55272 Shell::GetInstance()->display_manager()->SetDisplayMode(display_id,
273 old_resolution);
[email protected]6ef71d72013-08-10 18:13:44274}
275
[email protected]6ef71d72013-08-10 18:13:44276void ResolutionNotificationController::OnDisplayAdded(
oshimaf84b0da722016-04-27 19:47:19277 const display::Display& new_display) {}
[email protected]6ef71d72013-08-10 18:13:44278
279void ResolutionNotificationController::OnDisplayRemoved(
oshimaf84b0da722016-04-27 19:47:19280 const display::Display& old_display) {
[email protected]6ef71d72013-08-10 18:13:44281 if (change_info_ && change_info_->display_id == old_display.id())
282 RevertResolutionChange();
283}
284
[email protected]0c5703d2014-05-22 01:26:01285void ResolutionNotificationController::OnDisplayMetricsChanged(
oshimaf84b0da722016-04-27 19:47:19286 const display::Display&,
287 uint32_t) {}
[email protected]0c5703d2014-05-22 01:26:01288
[email protected]6ef71d72013-08-10 18:13:44289void ResolutionNotificationController::OnDisplayConfigurationChanged() {
290 if (!change_info_)
291 return;
292
jamescookb8dcef522016-06-25 14:42:55293 change_info_->current_resolution =
294 Shell::GetInstance()->display_manager()->GetActiveModeForDisplayId(
295 change_info_->display_id);
[email protected]86eee6e2013-08-27 01:27:15296 CreateOrUpdateNotification(true);
[email protected]6ef71d72013-08-10 18:13:44297 if (g_use_timer && change_info_->timeout_count > 0) {
jamescookb8dcef522016-06-25 14:42:55298 change_info_->timer.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this,
[email protected]6ef71d72013-08-10 18:13:44299 &ResolutionNotificationController::OnTimerTick);
300 }
301}
302
303void ResolutionNotificationController::SuppressTimerForTest() {
304 g_use_timer = false;
305}
306
[email protected]6ef71d72013-08-10 18:13:44307} // namespace ash