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 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 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(const chromeos::AccelerometerUpdate
& update
);
117 // Returns true if the lid was recently opened.
118 bool WasLidOpenedRecently() const;
120 // Enables MaximizeModeWindowManager, and determines the current state of
122 void EnterMaximizeMode();
124 // Removes MaximizeModeWindowManager and resets the display rotation if there
125 // is no rotation lock.
126 void LeaveMaximizeMode();
128 // Record UMA stats tracking TouchView usage. If |type| is
129 // TOUCH_VIEW_INTERVAL_INACTIVE, then record that TouchView has been
130 // inactive from |touchview_usage_interval_start_time_| until now.
131 // Similarly, record that TouchView has been active if |type| is
132 // TOUCH_VIEW_INTERVAL_ACTIVE.
133 void RecordTouchViewUsageInterval(TouchViewIntervalType type
);
135 // Returns TOUCH_VIEW_INTERVAL_ACTIVE if TouchView is currently active,
136 // otherwise returns TOUCH_VIEW_INTERNAL_INACTIVE.
137 TouchViewIntervalType
CurrentTouchViewIntervalType();
139 // The maximized window manager (if enabled).
140 scoped_ptr
<MaximizeModeWindowManager
> maximize_mode_window_manager_
;
142 // A helper class which when instantiated will block native events from the
143 // internal keyboard and touchpad.
144 scoped_ptr
<ScopedDisableInternalMouseAndKeyboard
> event_blocker_
;
146 // Whether we have ever seen accelerometer data.
147 bool have_seen_accelerometer_data_
;
149 // True when the hinge angle has been detected past 180 degrees.
150 bool lid_open_past_180_
;
152 // Tracks time spent in (and out of) touchview mode.
153 base::Time touchview_usage_interval_start_time_
;
154 base::TimeDelta total_touchview_time_
;
155 base::TimeDelta total_non_touchview_time_
;
157 // Tracks the last time we received a lid open event. This is used to suppress
158 // erroneous accelerometer readings as the lid is opened but the accelerometer
159 // reports readings that make the lid to appear near fully open.
160 base::TimeTicks last_lid_open_time_
;
162 // Source for the current time in base::TimeTicks.
163 scoped_ptr
<base::TickClock
> tick_clock_
;
165 // Tracks when the lid is closed. Used to prevent entering maximize mode.
168 DISALLOW_COPY_AND_ASSIGN(MaximizeModeController
);
173 #endif // ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_