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
3 // found in the LICENSE file.
5 #ifndef ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
6 #define ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
8 #include "ash/ash_export.h"
9 #include "ash/shell_observer.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "ui/gfx/display.h"
15 #if defined(OS_CHROMEOS)
16 #include "chromeos/accelerometer/accelerometer_reader.h"
17 #include "chromeos/accelerometer/accelerometer_types.h"
18 #include "chromeos/dbus/power_manager_client.h"
35 class MaximizeModeControllerTest
;
36 class ScopedDisableInternalMouseAndKeyboard
;
37 class MaximizeModeWindowManager
;
38 class MaximizeModeWindowManagerTest
;
40 class MultiUserWindowManagerChromeOSTest
;
41 class VirtualKeyboardControllerTest
;
44 // MaximizeModeController listens to accelerometer events and automatically
45 // enters and exits maximize mode when the lid is opened beyond the triggering
46 // angle and rotates the display to match the device when in maximize mode.
47 class ASH_EXPORT MaximizeModeController
:
48 #if defined(OS_CHROMEOS)
49 public chromeos::AccelerometerReader::Observer
,
50 public chromeos::PowerManagerClient::Observer
,
52 public ShellObserver
{
54 MaximizeModeController();
55 ~MaximizeModeController() override
;
57 // True if it is possible to enter maximize mode in the current
58 // configuration. If this returns false, it should never be the case that
59 // maximize mode becomes enabled.
60 bool CanEnterMaximizeMode();
62 // TODO(jonross): Merge this with EnterMaximizeMode. Currently these are
63 // separate for several reasons: there is no internal display when running
64 // unittests; the event blocker prevents keyboard input when running ChromeOS
65 // on linux. http://crbug.com/362881
66 // Turn the always maximize mode window manager on or off.
67 void EnableMaximizeModeWindowManager(bool should_enable
);
69 // Test if the MaximizeModeWindowManager is enabled or not.
70 bool IsMaximizeModeWindowManagerEnabled() const;
72 // Add a special window to the MaximizeModeWindowManager for tracking. This is
73 // only required for special windows which are handled by other window
74 // managers like the |MultiUserWindowManager|.
75 // If the maximize mode is not enabled no action will be performed.
76 void AddWindow(aura::Window
* window
);
79 void OnAppTerminating() override
;
80 void OnMaximizeModeStarted() override
;
81 void OnMaximizeModeEnded() override
;
83 #if defined(OS_CHROMEOS)
84 // chromeos::AccelerometerReader::Observer:
85 void OnAccelerometerUpdated(
86 scoped_refptr
<const chromeos::AccelerometerUpdate
> update
) override
;
88 // PowerManagerClient::Observer:
89 void LidEventReceived(bool open
, const base::TimeTicks
& time
) override
;
90 void SuspendImminent() override
;
91 void SuspendDone(const base::TimeDelta
& sleep_duration
) override
;
95 friend class MaximizeModeControllerTest
;
96 friend class MaximizeModeWindowManagerTest
;
97 friend class test::MultiUserWindowManagerChromeOSTest
;
98 friend class test::VirtualKeyboardControllerTest
;
100 // Used for recording metrics for intervals of time spent in
101 // and out of TouchView.
102 enum TouchViewIntervalType
{
103 TOUCH_VIEW_INTERVAL_INACTIVE
,
104 TOUCH_VIEW_INTERVAL_ACTIVE
107 // Set the TickClock. This is only to be used by tests that need to
108 // artificially and deterministically control the current time.
109 void SetTickClockForTest(scoped_ptr
<base::TickClock
> tick_clock
);
111 #if defined(OS_CHROMEOS)
112 // Detect hinge rotation from base and lid accelerometers and automatically
113 // start / stop maximize mode.
114 void HandleHingeRotation(
115 scoped_refptr
<const chromeos::AccelerometerUpdate
> update
);
118 // Returns true if the lid was recently opened.
119 bool WasLidOpenedRecently() const;
121 // Enables MaximizeModeWindowManager, and determines the current state of
123 void EnterMaximizeMode();
125 // Removes MaximizeModeWindowManager and resets the display rotation if there
126 // is no rotation lock.
127 void LeaveMaximizeMode();
129 // Record UMA stats tracking TouchView usage. If |type| is
130 // TOUCH_VIEW_INTERVAL_INACTIVE, then record that TouchView has been
131 // inactive from |touchview_usage_interval_start_time_| until now.
132 // Similarly, record that TouchView has been active if |type| is
133 // TOUCH_VIEW_INTERVAL_ACTIVE.
134 void RecordTouchViewUsageInterval(TouchViewIntervalType type
);
136 // Returns TOUCH_VIEW_INTERVAL_ACTIVE if TouchView is currently active,
137 // otherwise returns TOUCH_VIEW_INTERNAL_INACTIVE.
138 TouchViewIntervalType
CurrentTouchViewIntervalType();
140 // The maximized window manager (if enabled).
141 scoped_ptr
<MaximizeModeWindowManager
> maximize_mode_window_manager_
;
143 // A helper class which when instantiated will block native events from the
144 // internal keyboard and touchpad.
145 scoped_ptr
<ScopedDisableInternalMouseAndKeyboard
> event_blocker_
;
147 // Whether we have ever seen accelerometer data.
148 bool have_seen_accelerometer_data_
;
150 // True when the hinge angle has been detected past 180 degrees.
151 bool lid_open_past_180_
;
153 // Tracks time spent in (and out of) touchview mode.
154 base::Time touchview_usage_interval_start_time_
;
155 base::TimeDelta total_touchview_time_
;
156 base::TimeDelta total_non_touchview_time_
;
158 // Tracks the last time we received a lid open event. This is used to suppress
159 // erroneous accelerometer readings as the lid is opened but the accelerometer
160 // reports readings that make the lid to appear near fully open.
161 base::TimeTicks last_lid_open_time_
;
163 // Source for the current time in base::TimeTicks.
164 scoped_ptr
<base::TickClock
> tick_clock_
;
166 // Tracks when the lid is closed. Used to prevent entering maximize mode.
169 DISALLOW_COPY_AND_ASSIGN(MaximizeModeController
);
174 #endif // ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_