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 accelerometer 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 kLidRecentlyOpenedDuration = |
| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 151 |
138 #endif // OS_CHROMEOS | 152 #endif // OS_CHROMEOS |
139 | 153 |
140 } // namespace | 154 } // namespace |
141 | 155 |
142 MaximizeModeController::MaximizeModeController() | 156 MaximizeModeController::MaximizeModeController() |
143 : rotation_locked_(false), | 157 : rotation_locked_(false), |
144 have_seen_accelerometer_data_(false), | 158 have_seen_accelerometer_data_(false), |
145 in_set_screen_rotation_(false), | 159 in_set_screen_rotation_(false), |
146 user_rotation_(gfx::Display::ROTATE_0), | 160 user_rotation_(gfx::Display::ROTATE_0), |
147 last_touchview_transition_time_(base::Time::Now()) { | 161 last_touchview_transition_time_(base::Time::Now()), |
| 162 tick_clock_(new base::DefaultTickClock()), |
| 163 lid_is_closed_(false) { |
148 Shell::GetInstance()->accelerometer_controller()->AddObserver(this); | 164 Shell::GetInstance()->accelerometer_controller()->AddObserver(this); |
149 Shell::GetInstance()->AddShellObserver(this); | 165 Shell::GetInstance()->AddShellObserver(this); |
| 166 #if defined(OS_CHROMEOS) |
| 167 chromeos::DBusThreadManager::Get()-> |
| 168 GetPowerManagerClient()->AddObserver(this); |
| 169 #endif // OS_CHROMEOS |
150 } | 170 } |
151 | 171 |
152 MaximizeModeController::~MaximizeModeController() { | 172 MaximizeModeController::~MaximizeModeController() { |
153 Shell::GetInstance()->RemoveShellObserver(this); | 173 Shell::GetInstance()->RemoveShellObserver(this); |
154 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); | 174 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); |
| 175 #if defined(OS_CHROMEOS) |
| 176 chromeos::DBusThreadManager::Get()-> |
| 177 GetPowerManagerClient()->RemoveObserver(this); |
| 178 #endif // OS_CHROMEOS |
155 } | 179 } |
156 | 180 |
157 void MaximizeModeController::SetRotationLocked(bool rotation_locked) { | 181 void MaximizeModeController::SetRotationLocked(bool rotation_locked) { |
158 if (rotation_locked_ == rotation_locked) | 182 if (rotation_locked_ == rotation_locked) |
159 return; | 183 return; |
160 rotation_locked_ = rotation_locked; | 184 rotation_locked_ = rotation_locked; |
161 FOR_EACH_OBSERVER(Observer, observers_, | 185 FOR_EACH_OBSERVER(Observer, observers_, |
162 OnRotationLockChanged(rotation_locked_)); | 186 OnRotationLockChanged(rotation_locked_)); |
163 } | 187 } |
164 | 188 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 if (user_rotation != current_rotation_) { | 256 if (user_rotation != current_rotation_) { |
233 // A user may change other display configuration settings. When the user | 257 // A user may change other display configuration settings. When the user |
234 // does change the rotation setting, then lock rotation to prevent the | 258 // does change the rotation setting, then lock rotation to prevent the |
235 // accelerometer from erasing their change. | 259 // accelerometer from erasing their change. |
236 SetRotationLocked(true); | 260 SetRotationLocked(true); |
237 user_rotation_ = user_rotation; | 261 user_rotation_ = user_rotation; |
238 current_rotation_ = user_rotation; | 262 current_rotation_ = user_rotation; |
239 } | 263 } |
240 } | 264 } |
241 | 265 |
| 266 #if defined(OS_CHROMEOS) |
| 267 void MaximizeModeController::LidEventReceived(bool open, |
| 268 const base::TimeTicks& time) { |
| 269 if (open) |
| 270 last_lid_open_time_ = time; |
| 271 lid_is_closed_ = !open; |
| 272 LeaveMaximizeMode(); |
| 273 } |
| 274 |
| 275 void MaximizeModeController::SuspendImminent() { |
| 276 RecordTouchViewStateTransition(); |
| 277 } |
| 278 |
| 279 void MaximizeModeController::SuspendDone( |
| 280 const base::TimeDelta& sleep_duration) { |
| 281 last_touchview_transition_time_ = base::Time::Now(); |
| 282 } |
| 283 #endif // OS_CHROMEOS |
| 284 |
242 void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base, | 285 void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base, |
243 const gfx::Vector3dF& lid) { | 286 const gfx::Vector3dF& lid) { |
244 static const gfx::Vector3dF hinge_vector(0.0f, 1.0f, 0.0f); | 287 static const gfx::Vector3dF hinge_vector(0.0f, 1.0f, 0.0f); |
245 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); | 288 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); |
246 // Ignore the component of acceleration parallel to the hinge for the purposes | 289 // Ignore the component of acceleration parallel to the hinge for the purposes |
247 // of hinge angle calculation. | 290 // of hinge angle calculation. |
248 gfx::Vector3dF base_flattened(base); | 291 gfx::Vector3dF base_flattened(base); |
249 gfx::Vector3dF lid_flattened(lid); | 292 gfx::Vector3dF lid_flattened(lid); |
250 base_flattened.set_y(0.0f); | 293 base_flattened.set_y(0.0f); |
251 lid_flattened.set_y(0.0f); | 294 lid_flattened.set_y(0.0f); |
252 | 295 |
253 // As the hinge approaches a vertical angle, the base and lid accelerometers | 296 // As the hinge approaches a vertical angle, the base and lid accelerometers |
254 // approach the same values making any angle calculations highly inaccurate. | 297 // approach the same values making any angle calculations highly inaccurate. |
255 // Bail out early when it is too close. | 298 // Bail out early when it is too close. |
256 if (base_flattened.Length() < kHingeAngleDetectionThreshold || | 299 if (base_flattened.Length() < kHingeAngleDetectionThreshold || |
257 lid_flattened.Length() < kHingeAngleDetectionThreshold) { | 300 lid_flattened.Length() < kHingeAngleDetectionThreshold) { |
258 return; | 301 return; |
259 } | 302 } |
260 | 303 |
261 // Compute the angle between the base and the lid. | 304 // Compute the angle between the base and the lid. |
262 float angle = ClockwiseAngleBetweenVectorsInDegrees(base_flattened, | 305 float angle = ClockwiseAngleBetweenVectorsInDegrees(base_flattened, |
263 lid_flattened, hinge_vector); | 306 lid_flattened, hinge_vector); |
264 | 307 |
| 308 bool is_angle_stable = angle > kMinStableAngle && angle < kMaxStableAngle; |
| 309 |
| 310 // Clear the last_lid_open_time_ for a stable reading so that there is less |
| 311 // chance of a delay if the lid is moved from the close state to the fully |
| 312 // open state very quickly. |
| 313 if (is_angle_stable) |
| 314 last_lid_open_time_ = base::TimeTicks(); |
| 315 |
265 // Toggle maximize mode on or off when corresponding thresholds are passed. | 316 // Toggle maximize mode on or off when corresponding thresholds are passed. |
266 // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager | 317 // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager |
267 // such that observations of state changes occur after the change and shell | 318 // such that observations of state changes occur after the change and shell |
268 // has fewer states to track. | 319 // has fewer states to track. |
269 if (maximize_mode_engaged && | 320 if (maximize_mode_engaged && is_angle_stable && |
270 angle > kFullyOpenAngleErrorTolerance && | |
271 angle < kExitMaximizeModeAngle) { | 321 angle < kExitMaximizeModeAngle) { |
272 LeaveMaximizeMode(); | 322 LeaveMaximizeMode(); |
273 } else if (!maximize_mode_engaged && | 323 } else if (!lid_is_closed_ && !maximize_mode_engaged && |
274 angle > kEnterMaximizeModeAngle) { | 324 angle > kEnterMaximizeModeAngle && |
| 325 (is_angle_stable || !WasLidOpenedRecently())) { |
275 EnterMaximizeMode(); | 326 EnterMaximizeMode(); |
276 } | 327 } |
277 } | 328 } |
278 | 329 |
279 void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) { | 330 void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) { |
280 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); | 331 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); |
281 | 332 |
282 // TODO(jonross): track the updated rotation angle even when locked. So that | 333 // TODO(jonross): track the updated rotation angle even when locked. So that |
283 // when rotation lock is removed the accelerometer rotation can be applied | 334 // when rotation lock is removed the accelerometer rotation can be applied |
284 // without waiting for the next update. | 335 // without waiting for the next update. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 DisplayManager* display_manager, | 394 DisplayManager* display_manager, |
344 gfx::Display::Rotation rotation) { | 395 gfx::Display::Rotation rotation) { |
345 base::AutoReset<bool> auto_in_set_screen_rotation( | 396 base::AutoReset<bool> auto_in_set_screen_rotation( |
346 &in_set_screen_rotation_, true); | 397 &in_set_screen_rotation_, true); |
347 current_rotation_ = rotation; | 398 current_rotation_ = rotation; |
348 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), | 399 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), |
349 rotation); | 400 rotation); |
350 } | 401 } |
351 | 402 |
352 void MaximizeModeController::EnterMaximizeMode() { | 403 void MaximizeModeController::EnterMaximizeMode() { |
| 404 if (IsMaximizeModeWindowManagerEnabled()) |
| 405 return; |
353 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 406 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
354 current_rotation_ = user_rotation_ = display_manager-> | 407 current_rotation_ = user_rotation_ = display_manager-> |
355 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); | 408 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); |
356 EnableMaximizeModeWindowManager(true); | 409 EnableMaximizeModeWindowManager(true); |
357 #if defined(USE_X11) | 410 #if defined(USE_X11) |
358 event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11); | 411 event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11); |
359 #endif | 412 #endif |
360 #if defined(OS_CHROMEOS) | 413 #if defined(OS_CHROMEOS) |
361 event_handler_.reset(new ScreenshotActionHandler); | 414 event_handler_.reset(new ScreenshotActionHandler); |
362 #endif | 415 #endif |
363 Shell::GetInstance()->display_controller()->AddObserver(this); | 416 Shell::GetInstance()->display_controller()->AddObserver(this); |
364 } | 417 } |
365 | 418 |
366 void MaximizeModeController::LeaveMaximizeMode() { | 419 void MaximizeModeController::LeaveMaximizeMode() { |
| 420 if (!IsMaximizeModeWindowManagerEnabled()) |
| 421 return; |
367 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 422 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
368 gfx::Display::Rotation current_rotation = display_manager-> | 423 gfx::Display::Rotation current_rotation = display_manager-> |
369 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); | 424 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); |
370 if (current_rotation != user_rotation_) | 425 if (current_rotation != user_rotation_) |
371 SetDisplayRotation(display_manager, user_rotation_); | 426 SetDisplayRotation(display_manager, user_rotation_); |
372 rotation_locked_ = false; | 427 rotation_locked_ = false; |
373 EnableMaximizeModeWindowManager(false); | 428 EnableMaximizeModeWindowManager(false); |
374 event_blocker_.reset(); | 429 event_blocker_.reset(); |
375 event_handler_.reset(); | 430 event_handler_.reset(); |
376 } | 431 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 } | 432 } |
385 | 433 |
386 // Called after maximize mode has started, windows might still animate though. | 434 // Called after maximize mode has started, windows might still animate though. |
387 void MaximizeModeController::OnMaximizeModeStarted() { | 435 void MaximizeModeController::OnMaximizeModeStarted() { |
388 RecordTouchViewStateTransition(); | 436 RecordTouchViewStateTransition(); |
389 } | 437 } |
390 | 438 |
391 // Called after maximize mode has ended, windows might still be returning to | 439 // Called after maximize mode has ended, windows might still be returning to |
392 // their original position. | 440 // their original position. |
393 void MaximizeModeController::OnMaximizeModeEnded() { | 441 void MaximizeModeController::OnMaximizeModeEnded() { |
(...skipping 27 matching lines...) Expand all Loading... |
421 base::TimeDelta total_runtime = total_touchview_time_ + | 469 base::TimeDelta total_runtime = total_touchview_time_ + |
422 total_non_touchview_time_; | 470 total_non_touchview_time_; |
423 if (total_runtime.InSeconds() > 0) { | 471 if (total_runtime.InSeconds() > 0) { |
424 UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage", | 472 UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage", |
425 100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds()); | 473 100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds()); |
426 } | 474 } |
427 } | 475 } |
428 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 476 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
429 } | 477 } |
430 | 478 |
| 479 bool MaximizeModeController::WasLidOpenedRecently() const { |
| 480 if (last_lid_open_time_.is_null()) |
| 481 return false; |
| 482 |
| 483 base::TimeTicks now = tick_clock_->NowTicks(); |
| 484 DCHECK(now >= last_lid_open_time_); |
| 485 base::TimeDelta elapsed_time = now - last_lid_open_time_; |
| 486 return elapsed_time <= kLidRecentlyOpenedDuration; |
| 487 } |
| 488 |
| 489 void MaximizeModeController::SetTickClockForTest( |
| 490 scoped_ptr<base::TickClock> tick_clock) { |
| 491 DCHECK(tick_clock_); |
| 492 tick_clock_ = tick_clock.Pass(); |
| 493 } |
| 494 |
431 } // namespace ash | 495 } // namespace ash |
OLD | NEW |