OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/maximize_mode/maximize_mode_controller.h" | 5 #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
6 | 6 |
7 #include "ash/accelerators/accelerator_controller.h" | 7 #include "ash/accelerators/accelerator_controller.h" |
8 #include "ash/accelerators/accelerator_table.h" | 8 #include "ash/accelerators/accelerator_table.h" |
9 #include "ash/accelerometer/accelerometer_controller.h" | 9 #include "ash/accelerometer/accelerometer_controller.h" |
10 #include "ash/ash_switches.h" | 10 #include "ash/ash_switches.h" |
11 #include "ash/display/display_manager.h" | 11 #include "ash/display/display_manager.h" |
12 #include "ash/shell.h" | 12 #include "ash/shell.h" |
13 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h" | 13 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h" |
14 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard.h" | 14 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard.h" |
15 #include "base/auto_reset.h" | 15 #include "base/auto_reset.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/time/default_tick_clock.h" | |
19 #include "base/time/tick_clock.h" | |
18 #include "ui/base/accelerators/accelerator.h" | 20 #include "ui/base/accelerators/accelerator.h" |
19 #include "ui/events/event.h" | 21 #include "ui/events/event.h" |
20 #include "ui/events/event_handler.h" | 22 #include "ui/events/event_handler.h" |
21 #include "ui/events/keycodes/keyboard_codes.h" | 23 #include "ui/events/keycodes/keyboard_codes.h" |
22 #include "ui/gfx/vector3d_f.h" | 24 #include "ui/gfx/vector3d_f.h" |
23 | 25 |
24 #if defined(USE_X11) | 26 #if defined(USE_X11) |
25 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard_x11.h" | 27 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard_x11.h" |
26 #endif | 28 #endif |
27 | 29 |
30 #if defined(OS_CHROMEOS) | |
31 #include "chromeos/dbus/dbus_thread_manager.h" | |
32 #endif // OS_CHROMEOS | |
33 | |
28 namespace ash { | 34 namespace ash { |
29 | 35 |
30 namespace { | 36 namespace { |
31 | 37 |
32 // The hinge angle at which to enter maximize mode. | 38 // The hinge angle at which to enter maximize mode. |
33 const float kEnterMaximizeModeAngle = 200.0f; | 39 const float kEnterMaximizeModeAngle = 200.0f; |
34 | 40 |
35 // The angle at which to exit maximize mode, this is specifically less than the | 41 // The angle at which to exit maximize mode, this is specifically less than the |
36 // angle to enter maximize mode to prevent rapid toggling when near the angle. | 42 // angle to enter maximize mode to prevent rapid toggling when near the angle. |
37 const float kExitMaximizeModeAngle = 160.0f; | 43 const float kExitMaximizeModeAngle = 160.0f; |
38 | 44 |
39 // When the lid is fully open 360 degrees, the accelerometer readings can | 45 // Defines a range for which accelerometer readings are considered accurate. |
40 // occasionally appear as though the lid is almost closed. If the lid appears | 46 // When the lid is near open (or near closed) the acceleromter readings may be |
41 // near closed but the device is on we assume it is an erroneous reading from | 47 // inaccurate and a lid that is fully open may appear to be near closed (and |
42 // it being open 360 degrees. | 48 // vice versa). |
43 const float kFullyOpenAngleErrorTolerance = 20.0f; | 49 const float kMinStableAngle = 20.0f; |
50 const float kMaxStableAngle = 340.0f; | |
51 | |
52 // The time duration to consider the lid to be recently opened. | |
53 // This is used to prevent entering maximize mode if an erroneous accelerometer | |
54 // reading makes the lid appear to be fully open when the user is opening the | |
55 // lid from a closed position. | |
56 const base::TimeDelta kLidRecentlyOpenedTolerance = | |
57 base::TimeDelta::FromSeconds(2); | |
44 | 58 |
45 // When the device approaches vertical orientation (i.e. portrait orientation) | 59 // When the device approaches vertical orientation (i.e. portrait orientation) |
46 // the accelerometers for the base and lid approach the same values (i.e. | 60 // the accelerometers for the base and lid approach the same values (i.e. |
47 // gravity pointing in the direction of the hinge). When this happens we cannot | 61 // gravity pointing in the direction of the hinge). When this happens we cannot |
48 // compute the hinge angle reliably and must turn ignore accelerometer readings. | 62 // compute the hinge angle reliably and must turn ignore accelerometer readings. |
49 // This is the minimum acceleration perpendicular to the hinge under which to | 63 // This is the minimum acceleration perpendicular to the hinge under which to |
50 // detect hinge angle. | 64 // detect hinge angle. |
51 const float kHingeAngleDetectionThreshold = 0.25f; | 65 const float kHingeAngleDetectionThreshold = 0.25f; |
52 | 66 |
53 // The maximum deviation from the acceleration expected due to gravity under | 67 // The maximum deviation from the acceleration expected due to gravity under |
(...skipping 12 matching lines...) Expand all Loading... | |
66 | 80 |
67 // The minimum acceleration in a direction required to trigger screen rotation. | 81 // The minimum acceleration in a direction required to trigger screen rotation. |
68 // This prevents rapid toggling of rotation when the device is near flat and | 82 // This prevents rapid toggling of rotation when the device is near flat and |
69 // there is very little screen aligned force on it. The value is effectively the | 83 // there is very little screen aligned force on it. The value is effectively the |
70 // sine of the rise angle required, with the current value requiring at least a | 84 // sine of the rise angle required, with the current value requiring at least a |
71 // 25 degree rise. | 85 // 25 degree rise. |
72 const float kMinimumAccelerationScreenRotation = 0.42f; | 86 const float kMinimumAccelerationScreenRotation = 0.42f; |
73 | 87 |
74 const float kRadiansToDegrees = 180.0f / 3.14159265f; | 88 const float kRadiansToDegrees = 180.0f / 3.14159265f; |
75 | 89 |
90 // Returns true if the angle is considered a stable accurate reading. | |
91 bool IsAngleStable(float angle) { | |
92 return angle > kMinStableAngle && angle < kMaxStableAngle; | |
93 } | |
94 | |
76 // Returns the angle between |base| and |other| in degrees. | 95 // Returns the angle between |base| and |other| in degrees. |
77 float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base, | 96 float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base, |
78 const gfx::Vector3dF& other) { | 97 const gfx::Vector3dF& other) { |
79 return acos(gfx::DotProduct(base, other) / | 98 return acos(gfx::DotProduct(base, other) / |
80 base.Length() / other.Length()) * kRadiansToDegrees; | 99 base.Length() / other.Length()) * kRadiansToDegrees; |
81 } | 100 } |
82 | 101 |
83 // Returns the clockwise angle between |base| and |other| where |normal| is the | 102 // Returns the clockwise angle between |base| and |other| where |normal| is the |
84 // normal of the virtual surface to measure clockwise according to. | 103 // normal of the virtual surface to measure clockwise according to. |
85 float ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF& base, | 104 float ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF& base, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 | 156 |
138 #endif // OS_CHROMEOS | 157 #endif // OS_CHROMEOS |
139 | 158 |
140 } // namespace | 159 } // namespace |
141 | 160 |
142 MaximizeModeController::MaximizeModeController() | 161 MaximizeModeController::MaximizeModeController() |
143 : rotation_locked_(false), | 162 : rotation_locked_(false), |
144 have_seen_accelerometer_data_(false), | 163 have_seen_accelerometer_data_(false), |
145 in_set_screen_rotation_(false), | 164 in_set_screen_rotation_(false), |
146 user_rotation_(gfx::Display::ROTATE_0), | 165 user_rotation_(gfx::Display::ROTATE_0), |
147 last_touchview_transition_time_(base::Time::Now()) { | 166 last_touchview_transition_time_(base::Time::Now()), |
167 last_lid_open_time_(), | |
168 tick_clock_(new base::DefaultTickClock()), | |
169 lid_is_closed_(false) { | |
148 Shell::GetInstance()->accelerometer_controller()->AddObserver(this); | 170 Shell::GetInstance()->accelerometer_controller()->AddObserver(this); |
149 Shell::GetInstance()->AddShellObserver(this); | 171 Shell::GetInstance()->AddShellObserver(this); |
172 #if defined(OS_CHROMEOS) | |
173 chromeos::DBusThreadManager::Get()-> | |
174 GetPowerManagerClient()->AddObserver(this); | |
175 #endif // OS_CHROMEOS | |
150 } | 176 } |
151 | 177 |
152 MaximizeModeController::~MaximizeModeController() { | 178 MaximizeModeController::~MaximizeModeController() { |
153 Shell::GetInstance()->RemoveShellObserver(this); | 179 Shell::GetInstance()->RemoveShellObserver(this); |
154 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); | 180 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); |
181 #if defined(OS_CHROMEOS) | |
182 chromeos::DBusThreadManager::Get()-> | |
183 GetPowerManagerClient()->RemoveObserver(this); | |
184 #endif // OS_CHROMEOS | |
155 } | 185 } |
156 | 186 |
157 void MaximizeModeController::SetRotationLocked(bool rotation_locked) { | 187 void MaximizeModeController::SetRotationLocked(bool rotation_locked) { |
158 if (rotation_locked_ == rotation_locked) | 188 if (rotation_locked_ == rotation_locked) |
159 return; | 189 return; |
160 rotation_locked_ = rotation_locked; | 190 rotation_locked_ = rotation_locked; |
161 FOR_EACH_OBSERVER(Observer, observers_, | 191 FOR_EACH_OBSERVER(Observer, observers_, |
162 OnRotationLockChanged(rotation_locked_)); | 192 OnRotationLockChanged(rotation_locked_)); |
163 } | 193 } |
164 | 194 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 if (user_rotation != current_rotation_) { | 262 if (user_rotation != current_rotation_) { |
233 // A user may change other display configuration settings. When the user | 263 // A user may change other display configuration settings. When the user |
234 // does change the rotation setting, then lock rotation to prevent the | 264 // does change the rotation setting, then lock rotation to prevent the |
235 // accelerometer from erasing their change. | 265 // accelerometer from erasing their change. |
236 SetRotationLocked(true); | 266 SetRotationLocked(true); |
237 user_rotation_ = user_rotation; | 267 user_rotation_ = user_rotation; |
238 current_rotation_ = user_rotation; | 268 current_rotation_ = user_rotation; |
239 } | 269 } |
240 } | 270 } |
241 | 271 |
272 #if defined(OS_CHROMEOS) | |
273 void MaximizeModeController::LidEventReceived(bool open, | |
274 const base::TimeTicks& time) { | |
275 if (open) | |
276 last_lid_open_time_ = time; | |
277 lid_is_closed_ = !open; | |
278 LeaveMaximizeMode(); | |
279 } | |
280 | |
281 void MaximizeModeController::SuspendImminent() { | |
282 RecordTouchViewStateTransition(); | |
283 } | |
284 | |
285 void MaximizeModeController::SuspendDone( | |
286 const base::TimeDelta& sleep_duration) { | |
287 last_touchview_transition_time_ = base::Time::Now(); | |
288 // A lid open event won't always occur when coming out of a suspend state. | |
289 lid_is_closed_ = false; | |
290 } | |
291 #endif // OS_CHROMEOS | |
292 | |
242 void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base, | 293 void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base, |
243 const gfx::Vector3dF& lid) { | 294 const gfx::Vector3dF& lid) { |
244 static const gfx::Vector3dF hinge_vector(0.0f, 1.0f, 0.0f); | 295 static const gfx::Vector3dF hinge_vector(0.0f, 1.0f, 0.0f); |
245 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); | 296 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); |
246 // Ignore the component of acceleration parallel to the hinge for the purposes | 297 // Ignore the component of acceleration parallel to the hinge for the purposes |
247 // of hinge angle calculation. | 298 // of hinge angle calculation. |
248 gfx::Vector3dF base_flattened(base); | 299 gfx::Vector3dF base_flattened(base); |
249 gfx::Vector3dF lid_flattened(lid); | 300 gfx::Vector3dF lid_flattened(lid); |
250 base_flattened.set_y(0.0f); | 301 base_flattened.set_y(0.0f); |
251 lid_flattened.set_y(0.0f); | 302 lid_flattened.set_y(0.0f); |
252 | 303 |
253 // As the hinge approaches a vertical angle, the base and lid accelerometers | 304 // As the hinge approaches a vertical angle, the base and lid accelerometers |
254 // approach the same values making any angle calculations highly inaccurate. | 305 // approach the same values making any angle calculations highly inaccurate. |
255 // Bail out early when it is too close. | 306 // Bail out early when it is too close. |
256 if (base_flattened.Length() < kHingeAngleDetectionThreshold || | 307 if (base_flattened.Length() < kHingeAngleDetectionThreshold || |
257 lid_flattened.Length() < kHingeAngleDetectionThreshold) { | 308 lid_flattened.Length() < kHingeAngleDetectionThreshold) { |
258 return; | 309 return; |
259 } | 310 } |
260 | 311 |
261 // Compute the angle between the base and the lid. | 312 // Compute the angle between the base and the lid. |
262 float angle = ClockwiseAngleBetweenVectorsInDegrees(base_flattened, | 313 float angle = ClockwiseAngleBetweenVectorsInDegrees(base_flattened, |
263 lid_flattened, hinge_vector); | 314 lid_flattened, hinge_vector); |
264 | 315 |
316 // Clear the last_lid_open_time_ for a stable reading so that there is less | |
317 // chance of a delay if the lid is moved from the close state to the fully | |
318 // open state very quickly. | |
319 if (IsAngleStable(angle)) { | |
jonross
2014/07/25 15:56:35
nit: no '{' for single line if
bruthig
2014/07/25 16:42:56
Done.
| |
320 last_lid_open_time_ = base::TimeTicks(); | |
321 } | |
322 | |
265 // Toggle maximize mode on or off when corresponding thresholds are passed. | 323 // Toggle maximize mode on or off when corresponding thresholds are passed. |
266 // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager | 324 // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager |
267 // such that observations of state changes occur after the change and shell | 325 // such that observations of state changes occur after the change and shell |
268 // has fewer states to track. | 326 // has fewer states to track. |
269 if (maximize_mode_engaged && | 327 if (lid_is_closed_ || |
270 angle > kFullyOpenAngleErrorTolerance && | 328 (maximize_mode_engaged && |
271 angle < kExitMaximizeModeAngle) { | 329 ((IsAngleStable(angle) && angle < kExitMaximizeModeAngle) || |
330 (angle > kMaxStableAngle && WasLidOpenedRecently())))) { | |
jonross
2014/07/25 15:56:35
nit: for multi-line if-statements only indent wrap
bruthig
2014/07/25 16:42:56
Done.
| |
272 LeaveMaximizeMode(); | 331 LeaveMaximizeMode(); |
273 } else if (!maximize_mode_engaged && | 332 } else if (!maximize_mode_engaged && |
274 angle > kEnterMaximizeModeAngle) { | 333 ((IsAngleStable(angle) && angle > kEnterMaximizeModeAngle) || |
334 (angle > kMaxStableAngle && !WasLidOpenedRecently()))) { | |
275 EnterMaximizeMode(); | 335 EnterMaximizeMode(); |
276 } | 336 } |
277 } | 337 } |
278 | 338 |
279 void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) { | 339 void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) { |
280 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); | 340 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); |
281 | 341 |
282 // TODO(jonross): track the updated rotation angle even when locked. So that | 342 // TODO(jonross): track the updated rotation angle even when locked. So that |
283 // when rotation lock is removed the accelerometer rotation can be applied | 343 // when rotation lock is removed the accelerometer rotation can be applied |
284 // without waiting for the next update. | 344 // without waiting for the next update. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 DisplayManager* display_manager, | 403 DisplayManager* display_manager, |
344 gfx::Display::Rotation rotation) { | 404 gfx::Display::Rotation rotation) { |
345 base::AutoReset<bool> auto_in_set_screen_rotation( | 405 base::AutoReset<bool> auto_in_set_screen_rotation( |
346 &in_set_screen_rotation_, true); | 406 &in_set_screen_rotation_, true); |
347 current_rotation_ = rotation; | 407 current_rotation_ = rotation; |
348 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), | 408 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), |
349 rotation); | 409 rotation); |
350 } | 410 } |
351 | 411 |
352 void MaximizeModeController::EnterMaximizeMode() { | 412 void MaximizeModeController::EnterMaximizeMode() { |
413 if (IsMaximizeModeWindowManagerEnabled()) | |
414 return; | |
353 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 415 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
354 current_rotation_ = user_rotation_ = display_manager-> | 416 current_rotation_ = user_rotation_ = display_manager-> |
355 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); | 417 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); |
356 EnableMaximizeModeWindowManager(true); | 418 EnableMaximizeModeWindowManager(true); |
357 #if defined(USE_X11) | 419 #if defined(USE_X11) |
358 event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11); | 420 event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11); |
359 #endif | 421 #endif |
360 #if defined(OS_CHROMEOS) | 422 #if defined(OS_CHROMEOS) |
361 event_handler_.reset(new ScreenshotActionHandler); | 423 event_handler_.reset(new ScreenshotActionHandler); |
362 #endif | 424 #endif |
363 Shell::GetInstance()->display_controller()->AddObserver(this); | 425 Shell::GetInstance()->display_controller()->AddObserver(this); |
364 } | 426 } |
365 | 427 |
366 void MaximizeModeController::LeaveMaximizeMode() { | 428 void MaximizeModeController::LeaveMaximizeMode() { |
429 if (!IsMaximizeModeWindowManagerEnabled()) | |
430 return; | |
367 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 431 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
368 gfx::Display::Rotation current_rotation = display_manager-> | 432 gfx::Display::Rotation current_rotation = display_manager-> |
369 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); | 433 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); |
370 if (current_rotation != user_rotation_) | 434 if (current_rotation != user_rotation_) |
371 SetDisplayRotation(display_manager, user_rotation_); | 435 SetDisplayRotation(display_manager, user_rotation_); |
372 rotation_locked_ = false; | 436 rotation_locked_ = false; |
373 EnableMaximizeModeWindowManager(false); | 437 EnableMaximizeModeWindowManager(false); |
374 event_blocker_.reset(); | 438 event_blocker_.reset(); |
375 event_handler_.reset(); | 439 event_handler_.reset(); |
376 } | 440 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
377 | |
378 void MaximizeModeController::OnSuspend() { | |
379 RecordTouchViewStateTransition(); | |
380 } | |
381 | |
382 void MaximizeModeController::OnResume() { | |
383 last_touchview_transition_time_ = base::Time::Now(); | |
384 } | 441 } |
385 | 442 |
386 // Called after maximize mode has started, windows might still animate though. | 443 // Called after maximize mode has started, windows might still animate though. |
387 void MaximizeModeController::OnMaximizeModeStarted() { | 444 void MaximizeModeController::OnMaximizeModeStarted() { |
388 RecordTouchViewStateTransition(); | 445 RecordTouchViewStateTransition(); |
389 } | 446 } |
390 | 447 |
391 // Called after maximize mode has ended, windows might still be returning to | 448 // Called after maximize mode has ended, windows might still be returning to |
392 // their original position. | 449 // their original position. |
393 void MaximizeModeController::OnMaximizeModeEnded() { | 450 void MaximizeModeController::OnMaximizeModeEnded() { |
(...skipping 27 matching lines...) Expand all Loading... | |
421 base::TimeDelta total_runtime = total_touchview_time_ + | 478 base::TimeDelta total_runtime = total_touchview_time_ + |
422 total_non_touchview_time_; | 479 total_non_touchview_time_; |
423 if (total_runtime.InSeconds() > 0) { | 480 if (total_runtime.InSeconds() > 0) { |
424 UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage", | 481 UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage", |
425 100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds()); | 482 100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds()); |
426 } | 483 } |
427 } | 484 } |
428 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 485 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
429 } | 486 } |
430 | 487 |
488 bool MaximizeModeController::WasLidOpenedRecently() const { | |
489 if (last_lid_open_time_.is_null()) | |
490 return false; | |
491 | |
492 base::TimeTicks now = tick_clock_->NowTicks(); | |
493 DCHECK(now >= last_lid_open_time_); | |
494 base::TimeDelta elapsed_time = now - last_lid_open_time_; | |
495 return elapsed_time <= kLidRecentlyOpenedTolerance; | |
496 } | |
497 | |
498 void MaximizeModeController::SetTickClockForTest(base::TickClock* tick_clock) { | |
499 DCHECK(tick_clock_); | |
500 tick_clock_.reset(tick_clock); | |
501 } | |
502 | |
431 } // namespace ash | 503 } // namespace ash |
OLD | NEW |