summaryrefslogtreecommitdiffstats
path: root/ash/wm/maximize_mode/maximize_mode_controller.cc
blob: 663dcfc1a90bb4b47d051cab1506b16d93d695f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/maximize_mode/maximize_mode_controller.h"

#include "ash/accelerators/accelerator_controller.h"
#include "ash/accelerators/accelerator_table.h"
#include "ash/accelerometer/accelerometer_controller.h"
#include "ash/ash_switches.h"
#include "ash/display/display_manager.h"
#include "ash/shell.h"
#include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
#include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/vector3d_f.h"

#if defined(USE_X11)
#include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard_x11.h"
#endif

#if defined(OS_CHROMEOS)
#include "chromeos/dbus/dbus_thread_manager.h"
#endif  // OS_CHROMEOS

namespace ash {

namespace {

// The hinge angle at which to enter maximize mode.
const float kEnterMaximizeModeAngle = 200.0f;

// The angle at which to exit maximize mode, this is specifically less than the
// angle to enter maximize mode to prevent rapid toggling when near the angle.
const float kExitMaximizeModeAngle = 160.0f;

// Defines a range for which accelerometer readings are considered accurate.
// When the lid is near open (or near closed) the accelerometer readings may be
// inaccurate and a lid that is fully open may appear to be near closed (and
// vice versa).
const float kMinStableAngle = 20.0f;
const float kMaxStableAngle = 340.0f;

// The time duration to consider the lid to be recently opened.
// This is used to prevent entering maximize mode if an erroneous accelerometer
// reading makes the lid appear to be fully open when the user is opening the
// lid from a closed position.
const base::TimeDelta kLidRecentlyOpenedDuration =
    base::TimeDelta::FromSeconds(2);

// The mean acceleration due to gravity on Earth in m/s^2.
const float kMeanGravity = 9.80665f;

// When the device approaches vertical orientation (i.e. portrait orientation)
// the accelerometers for the base and lid approach the same values (i.e.
// gravity pointing in the direction of the hinge). When this happens we cannot
// compute the hinge angle reliably and must turn ignore accelerometer readings.
// This is the minimum acceleration perpendicular to the hinge under which to
// detect hinge angle in m/s^2.
const float kHingeAngleDetectionThreshold = 2.5f;

// The maximum deviation from the acceleration expected due to gravity under
// which to detect hinge angle and screen rotation in m/s^2
const float kDeviationFromGravityThreshold = 1.0f;

// The maximum deviation between the magnitude of the two accelerometers under
// which to detect hinge angle and screen rotation in m/s^2. These
// accelerometers are attached to the same physical device and so should be
// under the same acceleration.
const float kNoisyMagnitudeDeviation = 1.0f;

// The angle which the screen has to be rotated past before the display will
// rotate to match it (i.e. 45.0f is no stickiness).
const float kDisplayRotationStickyAngleDegrees = 60.0f;

// The minimum acceleration in m/s^2 in a direction required to trigger screen
// rotation. This prevents rapid toggling of rotation when the device is near
// flat and there is very little screen aligned force on it. The value is
// effectively the sine of the rise angle required times the acceleration due
// to gravity, with the current value requiring at least a 25 degree rise.
const float kMinimumAccelerationScreenRotation = 4.2f;

const float kRadiansToDegrees = 180.0f / 3.14159265f;

// Returns the angle between |base| and |other| in degrees.
float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
                                 const gfx::Vector3dF& other) {
  return acos(gfx::DotProduct(base, other) /
              base.Length() / other.Length()) * kRadiansToDegrees;
}

// Returns the clockwise angle between |base| and |other| where |normal| is the
// normal of the virtual surface to measure clockwise according to.
float ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
                                            const gfx::Vector3dF& other,
                                            const gfx::Vector3dF& normal) {
  float angle = AngleBetweenVectorsInDegrees(base, other);
  gfx::Vector3dF cross(base);
  cross.Cross(other);
  // If the dot product of this cross product is normal, it means that the
  // shortest angle between |base| and |other| was counterclockwise with respect
  // to the surface represented by |normal| and this angle must be reversed.
  if (gfx::DotProduct(cross, normal) > 0.0f)
    angle = 360.0f - angle;
  return angle;
}

#if defined(OS_CHROMEOS)

// An event handler which listens for a volume down + power keypress and
// triggers a screenshot when this is seen.
class ScreenshotActionHandler : public ui::EventHandler {
 public:
  ScreenshotActionHandler();
  virtual ~ScreenshotActionHandler();

  // ui::EventHandler:
  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;

 private:
  bool volume_down_pressed_;

  DISALLOW_COPY_AND_ASSIGN(ScreenshotActionHandler);
};

ScreenshotActionHandler::ScreenshotActionHandler()
    : volume_down_pressed_(false) {
  Shell::GetInstance()->PrependPreTargetHandler(this);
}

ScreenshotActionHandler::~ScreenshotActionHandler() {
  Shell::GetInstance()->RemovePreTargetHandler(this);
}

void ScreenshotActionHandler::OnKeyEvent(ui::KeyEvent* event) {
  if (event->key_code() == ui::VKEY_VOLUME_DOWN) {
    volume_down_pressed_ = event->type() == ui::ET_KEY_PRESSED ||
                           event->type() == ui::ET_TRANSLATED_KEY_PRESS;
  } else if (volume_down_pressed_ &&
             event->key_code() == ui::VKEY_POWER &&
             event->type() == ui::ET_KEY_PRESSED) {
    Shell::GetInstance()->accelerator_controller()->PerformAction(
        ash::TAKE_SCREENSHOT, ui::Accelerator());
  }
}

#endif  // OS_CHROMEOS

}  // namespace

MaximizeModeController::MaximizeModeController()
    : rotation_locked_(false),
      have_seen_accelerometer_data_(false),
      ignore_display_configuration_updates_(false),
      shutting_down_(false),
      user_rotation_(gfx::Display::ROTATE_0),
      last_touchview_transition_time_(base::Time::Now()),
      tick_clock_(new base::DefaultTickClock()),
      lid_is_closed_(false) {
  Shell::GetInstance()->accelerometer_controller()->AddObserver(this);
  Shell::GetInstance()->AddShellObserver(this);
#if defined(OS_CHROMEOS)
  chromeos::DBusThreadManager::Get()->
      GetPowerManagerClient()->AddObserver(this);
#endif  // OS_CHROMEOS
}

MaximizeModeController::~MaximizeModeController() {
  Shell::GetInstance()->RemoveShellObserver(this);
  Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this);
#if defined(OS_CHROMEOS)
  chromeos::DBusThreadManager::Get()->
      GetPowerManagerClient()->RemoveObserver(this);
#endif  // OS_CHROMEOS
}

void MaximizeModeController::SetRotationLocked(bool rotation_locked) {
  if (rotation_locked_ == rotation_locked)
    return;
  base::AutoReset<bool> auto_ignore_display_configuration_updates(
      &ignore_display_configuration_updates_, true);
  rotation_locked_ = rotation_locked;
  Shell::GetInstance()->display_manager()->
      RegisterDisplayRotationProperties(rotation_locked_, current_rotation_);
  FOR_EACH_OBSERVER(Observer, observers_,
                    OnRotationLockChanged(rotation_locked_));
}

void MaximizeModeController::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void MaximizeModeController::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

bool MaximizeModeController::CanEnterMaximizeMode() {
  // If we have ever seen accelerometer data, then HandleHingeRotation may
  // trigger maximize mode at some point in the future.
  // The --enable-touch-view-testing switch can also mean that we may enter
  // maximize mode.
  return have_seen_accelerometer_data_ ||
         CommandLine::ForCurrentProcess()->HasSwitch(
             switches::kAshEnableTouchViewTesting);
}

void MaximizeModeController::EnableMaximizeModeWindowManager(bool enable) {
  if (enable && !maximize_mode_window_manager_.get()) {
    maximize_mode_window_manager_.reset(new MaximizeModeWindowManager());
    // TODO(jonross): Move the maximize mode notifications from ShellObserver
    // to MaximizeModeController::Observer
    Shell::GetInstance()->OnMaximizeModeStarted();
  } else if (!enable && maximize_mode_window_manager_.get()) {
    maximize_mode_window_manager_.reset();
    Shell::GetInstance()->OnMaximizeModeEnded();
  }
}

bool MaximizeModeController::IsMaximizeModeWindowManagerEnabled() const {
  return maximize_mode_window_manager_.get() != NULL;
}

void MaximizeModeController::AddWindow(aura::Window* window) {
  if (IsMaximizeModeWindowManagerEnabled())
    maximize_mode_window_manager_->AddWindow(window);
}

void MaximizeModeController::Shutdown() {
  shutting_down_ = true;
  LeaveMaximizeMode();
}

void MaximizeModeController::OnAccelerometerUpdated(
    const ui::AccelerometerUpdate& update) {
  bool first_accelerometer_update = !have_seen_accelerometer_data_;
  have_seen_accelerometer_data_ = true;

  // Ignore the reading if it appears unstable. The reading is considered
  // unstable if it deviates too much from gravity and/or the magnitude of the
  // reading from the lid differs too much from the reading from the base.
  float base_magnitude =
      update.has(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD) ?
      update.get(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD).Length() :
      0.0f;
  float lid_magnitude = update.has(ui::ACCELEROMETER_SOURCE_SCREEN) ?
      update.get(ui::ACCELEROMETER_SOURCE_SCREEN).Length() : 0.0f;
  bool lid_stable = update.has(ui::ACCELEROMETER_SOURCE_SCREEN) &&
      std::abs(lid_magnitude - kMeanGravity) <= kDeviationFromGravityThreshold;
  bool base_angle_stable = lid_stable &&
      update.has(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD) &&
      std::abs(base_magnitude - lid_magnitude) <= kNoisyMagnitudeDeviation &&
      std::abs(base_magnitude - kMeanGravity) <= kDeviationFromGravityThreshold;

  if (base_angle_stable) {
    // Responding to the hinge rotation can change the maximize mode state which
    // affects screen rotation, so we handle hinge rotation first.
    HandleHingeRotation(
        update.get(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD),
        update.get(ui::ACCELEROMETER_SOURCE_SCREEN));
  }
  if (lid_stable)
    HandleScreenRotation(update.get(ui::ACCELEROMETER_SOURCE_SCREEN));

  if (first_accelerometer_update) {
    // On the first accelerometer update we will know if we have entered
    // maximize mode or not. Update the preferences to reflect the current
    // state.
    Shell::GetInstance()->display_manager()->
        RegisterDisplayRotationProperties(rotation_locked_, current_rotation_);
  }
}

void MaximizeModeController::OnDisplayConfigurationChanged() {
  if (ignore_display_configuration_updates_)
    return;
  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
  gfx::Display::Rotation user_rotation = display_manager->
      GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation();
  if (user_rotation != current_rotation_) {
    // A user may change other display configuration settings. When the user
    // does change the rotation setting, then lock rotation to prevent the
    // accelerometer from erasing their change.
    SetRotationLocked(true);
    user_rotation_ = user_rotation;
    current_rotation_ = user_rotation;
  }
}

#if defined(OS_CHROMEOS)
void MaximizeModeController::LidEventReceived(bool open,
                                              const base::TimeTicks& time) {
  if (open)
    last_lid_open_time_ = time;
  lid_is_closed_ = !open;
  LeaveMaximizeMode();
}

void MaximizeModeController::SuspendImminent() {
  RecordTouchViewStateTransition();
}

void MaximizeModeController::SuspendDone(
    const base::TimeDelta& sleep_duration) {
  last_touchview_transition_time_ = base::Time::Now();
}
#endif  // OS_CHROMEOS

void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base,
                                                 const gfx::Vector3dF& lid) {
  static const gfx::Vector3dF hinge_vector(1.0f, 0.0f, 0.0f);
  bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled();
  // Ignore the component of acceleration parallel to the hinge for the purposes
  // of hinge angle calculation.
  gfx::Vector3dF base_flattened(base);
  gfx::Vector3dF lid_flattened(lid);
  base_flattened.set_x(0.0f);
  lid_flattened.set_x(0.0f);

  // As the hinge approaches a vertical angle, the base and lid accelerometers
  // approach the same values making any angle calculations highly inaccurate.
  // Bail out early when it is too close.
  if (base_flattened.Length() < kHingeAngleDetectionThreshold ||
      lid_flattened.Length() < kHingeAngleDetectionThreshold) {
    return;
  }

  // Compute the angle between the base and the lid.
  float lid_angle = 180.0f - ClockwiseAngleBetweenVectorsInDegrees(
      base_flattened, lid_flattened, hinge_vector);
  if (lid_angle < 0.0f)
    lid_angle += 360.0f;

  bool is_angle_stable = lid_angle >= kMinStableAngle &&
                         lid_angle <= kMaxStableAngle;

  // Clear the last_lid_open_time_ for a stable reading so that there is less
  // chance of a delay if the lid is moved from the close state to the fully
  // open state very quickly.
  if (is_angle_stable)
    last_lid_open_time_ = base::TimeTicks();

  // Toggle maximize mode on or off when corresponding thresholds are passed.
  // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager
  // such that observations of state changes occur after the change and shell
  // has fewer states to track.
  if (maximize_mode_engaged && is_angle_stable &&
      lid_angle <= kExitMaximizeModeAngle) {
    LeaveMaximizeMode();
  } else if (!lid_is_closed_ && !maximize_mode_engaged &&
             lid_angle >= kEnterMaximizeModeAngle &&
             (is_angle_stable || !WasLidOpenedRecently())) {
    EnterMaximizeMode();
  }
}

void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) {
  bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled();

  // TODO(jonross): track the updated rotation angle even when locked. So that
  // when rotation lock is removed the accelerometer rotation can be applied
  // without waiting for the next update.
  if (!maximize_mode_engaged || rotation_locked_)
    return;

  DisplayManager* display_manager =
      Shell::GetInstance()->display_manager();
  gfx::Display::Rotation current_rotation = display_manager->GetDisplayInfo(
      gfx::Display::InternalDisplayId()).rotation();

  // After determining maximize mode state, determine if the screen should
  // be rotated.
  gfx::Vector3dF lid_flattened(lid.x(), lid.y(), 0.0f);
  float lid_flattened_length = lid_flattened.Length();
  // When the lid is close to being flat, don't change rotation as it is too
  // sensitive to slight movements.
  if (lid_flattened_length < kMinimumAccelerationScreenRotation)
    return;

  // The reference vector is the angle of gravity when the device is rotated
  // clockwise by 45 degrees. Computing the angle between this vector and
  // gravity we can easily determine the expected display rotation.
  static const gfx::Vector3dF rotation_reference(-1.0f, -1.0f, 0.0f);

  // Set the down vector to match the expected direction of gravity given the
  // last configured rotation. This is used to enforce a stickiness that the
  // user must overcome to rotate the display and prevents frequent rotations
  // when holding the device near 45 degrees.
  gfx::Vector3dF down(0.0f, 0.0f, 0.0f);
  if (current_rotation == gfx::Display::ROTATE_0)
    down.set_y(-1.0f);
  else if (current_rotation == gfx::Display::ROTATE_90)
    down.set_x(-1.0f);
  else if (current_rotation == gfx::Display::ROTATE_180)
    down.set_y(1.0f);
  else
    down.set_x(1.0f);

  // Don't rotate if the screen has not passed the threshold.
  if (AngleBetweenVectorsInDegrees(down, lid_flattened) <
      kDisplayRotationStickyAngleDegrees) {
    return;
  }

  float angle = ClockwiseAngleBetweenVectorsInDegrees(rotation_reference,
      lid_flattened, gfx::Vector3dF(0.0f, 0.0f, -1.0f));

  gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_90;
  if (angle < 90.0f)
    new_rotation = gfx::Display::ROTATE_0;
  else if (angle < 180.0f)
    new_rotation = gfx::Display::ROTATE_270;
  else if (angle < 270.0f)
    new_rotation = gfx::Display::ROTATE_180;

  if (new_rotation != current_rotation)
    SetDisplayRotation(display_manager, new_rotation);
}

void MaximizeModeController::SetDisplayRotation(
    DisplayManager* display_manager,
    gfx::Display::Rotation rotation) {
  base::AutoReset<bool> auto_ignore_display_configuration_updates(
      &ignore_display_configuration_updates_, true);
  current_rotation_ = rotation;
  display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(),
                                      rotation);
}

void MaximizeModeController::EnterMaximizeMode() {
  if (IsMaximizeModeWindowManagerEnabled())
    return;
  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
  if (display_manager->HasInternalDisplay()) {
    current_rotation_ = user_rotation_ = display_manager->
        GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation();
    LoadDisplayRotationProperties();
  }
  EnableMaximizeModeWindowManager(true);
#if defined(USE_X11)
  event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11);
#endif
#if defined(OS_CHROMEOS)
  event_handler_.reset(new ScreenshotActionHandler);
#endif
  Shell::GetInstance()->display_controller()->AddObserver(this);
}

void MaximizeModeController::LeaveMaximizeMode() {
  if (!IsMaximizeModeWindowManagerEnabled())
    return;
  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
  if (display_manager->HasInternalDisplay()) {
    gfx::Display::Rotation current_rotation = display_manager->
        GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation();
    if (current_rotation != user_rotation_)
      SetDisplayRotation(display_manager, user_rotation_);
  }
  if (!shutting_down_)
    SetRotationLocked(false);
  EnableMaximizeModeWindowManager(false);
  event_blocker_.reset();
  event_handler_.reset();
  Shell::GetInstance()->display_controller()->RemoveObserver(this);
}

// Called after maximize mode has started, windows might still animate though.
void MaximizeModeController::OnMaximizeModeStarted() {
  RecordTouchViewStateTransition();
}

// Called after maximize mode has ended, windows might still be returning to
// their original position.
void MaximizeModeController::OnMaximizeModeEnded() {
  RecordTouchViewStateTransition();
}

void MaximizeModeController::RecordTouchViewStateTransition() {
  if (CanEnterMaximizeMode()) {
    base::Time current_time = base::Time::Now();
    base::TimeDelta delta = current_time - last_touchview_transition_time_;
    if (IsMaximizeModeWindowManagerEnabled()) {
      UMA_HISTOGRAM_LONG_TIMES("Ash.TouchView.TouchViewInactive", delta);
      total_non_touchview_time_ += delta;
    } else {
      UMA_HISTOGRAM_LONG_TIMES("Ash.TouchView.TouchViewActive", delta);
      total_touchview_time_ += delta;
    }
    last_touchview_transition_time_ = current_time;
  }
}

void MaximizeModeController::LoadDisplayRotationProperties() {
  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
  if (!display_manager->registered_internal_display_rotation_lock())
    return;

  SetDisplayRotation(display_manager,
                     display_manager->registered_internal_display_rotation());
  SetRotationLocked(true);
}

void MaximizeModeController::OnAppTerminating() {
  if (CanEnterMaximizeMode()) {
    RecordTouchViewStateTransition();
    UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchView.TouchViewActiveTotal",
        total_touchview_time_.InMinutes(),
        1, base::TimeDelta::FromDays(7).InMinutes(), 50);
    UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchView.TouchViewInactiveTotal",
        total_non_touchview_time_.InMinutes(),
        1, base::TimeDelta::FromDays(7).InMinutes(), 50);
    base::TimeDelta total_runtime = total_touchview_time_ +
        total_non_touchview_time_;
    if (total_runtime.InSeconds() > 0) {
      UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage",
          100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds());
    }
  }
  Shell::GetInstance()->display_controller()->RemoveObserver(this);
}

bool MaximizeModeController::WasLidOpenedRecently() const {
  if (last_lid_open_time_.is_null())
    return false;

  base::TimeTicks now = tick_clock_->NowTicks();
  DCHECK(now >= last_lid_open_time_);
  base::TimeDelta elapsed_time = now - last_lid_open_time_;
  return elapsed_time <= kLidRecentlyOpenedDuration;
}

void MaximizeModeController::SetTickClockForTest(
    scoped_ptr<base::TickClock> tick_clock) {
  DCHECK(tick_clock_);
  tick_clock_ = tick_clock.Pass();
}

}  // namespace ash