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/accelerometer/accelerometer_observer.h"
9 #include "ash/ash_export.h"
10 #include "ash/display/display_controller.h"
11 #include "ash/display/display_manager.h"
12 #include "ash/shell_observer.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/power_monitor/power_observer.h"
17 #include "ui/gfx/display.h"
25 class MaximizeModeControllerTest;
26 class MaximizeModeEventBlocker;
27 class MaximizeModeWindowManager;
28 class MaximizeModeWindowManagerTest;
30 // MaximizeModeController listens to accelerometer events and automatically
31 // enters and exits maximize mode when the lid is opened beyond the triggering
32 // angle and rotates the display to match the device when in maximize mode.
33 class ASH_EXPORT MaximizeModeController : public AccelerometerObserver,
34 public base::PowerObserver,
36 public DisplayController::Observer {
38 // Observer that reports changes to the state of MaximizeModeController's
42 // Invoked whenever |rotation_locked_| is changed.
43 virtual void OnRotationLockChanged(bool rotation_locked) {}
46 virtual ~Observer() {}
49 MaximizeModeController();
50 virtual ~MaximizeModeController();
52 bool in_set_screen_rotation() const {
53 return in_set_screen_rotation_;
56 // True if |rotation_lock_| has been set, and OnAccelerometerUpdated will not
57 // change the display rotation.
58 bool rotation_locked() {
59 return rotation_locked_;
62 // If |rotation_locked| future calls to OnAccelerometerUpdated will not
63 // change the display rotation.
64 void SetRotationLocked(bool rotation_locked);
66 // Add/Remove observers.
67 void AddObserver(Observer* observer);
68 void RemoveObserver(Observer* observer);
70 // True if it is possible to enter maximize mode in the current
71 // configuration. If this returns false, it should never be the case that
72 // maximize mode becomes enabled.
73 bool CanEnterMaximizeMode();
75 // TODO(jonross): Merge this with EnterMaximizeMode. Currently these are
76 // separate for several reasons: there is no internal display when running
77 // unittests; the event blocker prevents keyboard input when running ChromeOS
78 // on linux. http://crbug.com/362881
79 // Turn the always maximize mode window manager on or off.
80 void EnableMaximizeModeWindowManager(bool enable);
82 // Test if the MaximizeModeWindowManager is enabled or not.
83 bool IsMaximizeModeWindowManagerEnabled() const;
85 // TODO(jonross): move this into the destructor. Currently separated as
86 // ShellOberver notifies of maximize mode ending, and the observers end up
87 // attempting to access MaximizeModeController via the Shell. If done in
88 // destructor the controller is null, and the observers segfault.
89 // Shuts down down the MaximizeModeWindowManager and notifies all observers.
92 // AccelerometerObserver:
93 virtual void OnAccelerometerUpdated(const gfx::Vector3dF& base,
94 const gfx::Vector3dF& lid) OVERRIDE;
97 virtual void OnAppTerminating() OVERRIDE;
98 virtual void OnMaximizeModeStarted() OVERRIDE;
99 virtual void OnMaximizeModeEnded() OVERRIDE;
101 // base::PowerObserver:
102 virtual void OnSuspend() OVERRIDE;
103 virtual void OnResume() OVERRIDE;
105 // DisplayController::Observer:
106 virtual void OnDisplayConfigurationChanged() OVERRIDE;
109 friend class MaximizeModeControllerTest;
110 friend class MaximizeModeWindowManagerTest;
112 // Detect hinge rotation from |base| and |lid| accelerometers and
113 // automatically start / stop maximize mode.
114 void HandleHingeRotation(const gfx::Vector3dF& base,
115 const gfx::Vector3dF& lid);
117 // Detect screen rotation from |lid| accelerometer and automatically rotate
119 void HandleScreenRotation(const gfx::Vector3dF& lid);
121 // Sets the display rotation and suppresses display notifications.
122 void SetDisplayRotation(DisplayManager* display_manager,
123 gfx::Display::Rotation rotation);
125 // Enables MaximizeModeWindowManager, and determines the current state of
127 void EnterMaximizeMode();
129 // Removes MaximizeModeWindowManager and resets the display rotation if there
130 // is no rotation lock.
131 void LeaveMaximizeMode();
133 // Record UMA stats tracking touchview usage.
134 void RecordTouchViewStateTransition();
136 // The maximized window manager (if enabled).
137 scoped_ptr<MaximizeModeWindowManager> maximize_mode_window_manager_;
139 // An event targeter controller which traps mouse and keyboard events while
140 // maximize mode is engaged.
141 scoped_ptr<MaximizeModeEventBlocker> event_blocker_;
143 // An event handler used to detect screenshot actions while in maximize mode.
144 scoped_ptr<ui::EventHandler> event_handler_;
146 // When true calls to OnAccelerometerUpdated will not rotate the display.
147 bool rotation_locked_;
149 // Whether we have ever seen accelerometer data.
150 bool have_seen_accelerometer_data_;
152 // True when the screen's orientation is being changed.
153 bool in_set_screen_rotation_;
155 // The rotation of the display set by the user. This rotation will be
156 // restored upon exiting maximize mode.
157 gfx::Display::Rotation user_rotation_;
159 // The current rotation set by MaximizeModeController for the internal
160 // display. Compared in OnDisplayConfigurationChanged to determine user
161 // display setting changes.
162 gfx::Display::Rotation current_rotation_;
164 // Rotation Lock observers.
165 ObserverList<Observer> observers_;
167 // Tracks time spent in (and out of) touchview mode.
168 base::Time last_touchview_transition_time_;
169 base::TimeDelta total_touchview_time_;
170 base::TimeDelta total_non_touchview_time_;
172 DISALLOW_COPY_AND_ASSIGN(MaximizeModeController);
177 #endif // ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_