Home | History | Annotate | Download | only in maximize_mode
      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.
      4 
      5 #ifndef ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
      6 #define ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
      7 
      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"
     18 
     19 namespace ui {
     20 class EventHandler;
     21 }
     22 
     23 namespace ash {
     24 
     25 class MaximizeModeControllerTest;
     26 class MaximizeModeEventBlocker;
     27 class MaximizeModeWindowManager;
     28 class MaximizeModeWindowManagerTest;
     29 namespace test {
     30 class MultiUserWindowManagerChromeOSTest;
     31 }
     32 
     33 // MaximizeModeController listens to accelerometer events and automatically
     34 // enters and exits maximize mode when the lid is opened beyond the triggering
     35 // angle and rotates the display to match the device when in maximize mode.
     36 class ASH_EXPORT MaximizeModeController : public AccelerometerObserver,
     37     public base::PowerObserver,
     38     public ShellObserver,
     39     public DisplayController::Observer {
     40  public:
     41   // Observer that reports changes to the state of MaximizeModeController's
     42   // rotation lock.
     43   class Observer {
     44    public:
     45     // Invoked whenever |rotation_locked_| is changed.
     46     virtual void OnRotationLockChanged(bool rotation_locked) {}
     47 
     48    protected:
     49     virtual ~Observer() {}
     50   };
     51 
     52   MaximizeModeController();
     53   virtual ~MaximizeModeController();
     54 
     55   bool in_set_screen_rotation() const {
     56     return in_set_screen_rotation_;
     57   }
     58 
     59   // True if |rotation_lock_| has been set, and OnAccelerometerUpdated will not
     60   // change the display rotation.
     61   bool rotation_locked() {
     62     return rotation_locked_;
     63   }
     64 
     65   // If |rotation_locked| future calls to OnAccelerometerUpdated will not
     66   // change the display rotation.
     67   void SetRotationLocked(bool rotation_locked);
     68 
     69   // Add/Remove observers.
     70   void AddObserver(Observer* observer);
     71   void RemoveObserver(Observer* observer);
     72 
     73   // True if it is possible to enter maximize mode in the current
     74   // configuration. If this returns false, it should never be the case that
     75   // maximize mode becomes enabled.
     76   bool CanEnterMaximizeMode();
     77 
     78   // TODO(jonross): Merge this with EnterMaximizeMode. Currently these are
     79   // separate for several reasons: there is no internal display when running
     80   // unittests; the event blocker prevents keyboard input when running ChromeOS
     81   // on linux. http://crbug.com/362881
     82   // Turn the always maximize mode window manager on or off.
     83   void EnableMaximizeModeWindowManager(bool enable);
     84 
     85   // Test if the MaximizeModeWindowManager is enabled or not.
     86   bool IsMaximizeModeWindowManagerEnabled() const;
     87 
     88   // Add a special window to the MaximizeModeWindowManager for tracking. This is
     89   // only required for special windows which are handled by other window
     90   // managers like the |MultiUserWindowManager|.
     91   // If the maximize mode is not enabled no action will be performed.
     92   void AddWindow(aura::Window* window);
     93 
     94   // TODO(jonross): move this into the destructor. Currently separated as
     95   // ShellOberver notifies of maximize mode ending, and the observers end up
     96   // attempting to access MaximizeModeController via the Shell. If done in
     97   // destructor the controller is null, and the observers segfault.
     98   // Shuts down down the MaximizeModeWindowManager and notifies all observers.
     99   void Shutdown();
    100 
    101   // AccelerometerObserver:
    102   virtual void OnAccelerometerUpdated(const gfx::Vector3dF& base,
    103                                       const gfx::Vector3dF& lid) OVERRIDE;
    104 
    105   // ShellObserver:
    106   virtual void OnAppTerminating() OVERRIDE;
    107   virtual void OnMaximizeModeStarted() OVERRIDE;
    108   virtual void OnMaximizeModeEnded() OVERRIDE;
    109 
    110   // base::PowerObserver:
    111   virtual void OnSuspend() OVERRIDE;
    112   virtual void OnResume() OVERRIDE;
    113 
    114   // DisplayController::Observer:
    115   virtual void OnDisplayConfigurationChanged() OVERRIDE;
    116 
    117  private:
    118   friend class MaximizeModeControllerTest;
    119   friend class MaximizeModeWindowManagerTest;
    120   friend class test::MultiUserWindowManagerChromeOSTest;
    121 
    122   // Detect hinge rotation from |base| and |lid| accelerometers and
    123   // automatically start / stop maximize mode.
    124   void HandleHingeRotation(const gfx::Vector3dF& base,
    125                            const gfx::Vector3dF& lid);
    126 
    127   // Detect screen rotation from |lid| accelerometer and automatically rotate
    128   // screen.
    129   void HandleScreenRotation(const gfx::Vector3dF& lid);
    130 
    131   // Sets the display rotation and suppresses display notifications.
    132   void SetDisplayRotation(DisplayManager* display_manager,
    133                           gfx::Display::Rotation rotation);
    134 
    135   // Enables MaximizeModeWindowManager, and determines the current state of
    136   // rotation lock.
    137   void EnterMaximizeMode();
    138 
    139   // Removes MaximizeModeWindowManager and resets the display rotation if there
    140   // is no rotation lock.
    141   void LeaveMaximizeMode();
    142 
    143   // Record UMA stats tracking touchview usage.
    144   void RecordTouchViewStateTransition();
    145 
    146   // The maximized window manager (if enabled).
    147   scoped_ptr<MaximizeModeWindowManager> maximize_mode_window_manager_;
    148 
    149   // An event targeter controller which traps mouse and keyboard events while
    150   // maximize mode is engaged.
    151   scoped_ptr<MaximizeModeEventBlocker> event_blocker_;
    152 
    153   // An event handler used to detect screenshot actions while in maximize mode.
    154   scoped_ptr<ui::EventHandler> event_handler_;
    155 
    156   // When true calls to OnAccelerometerUpdated will not rotate the display.
    157   bool rotation_locked_;
    158 
    159   // Whether we have ever seen accelerometer data.
    160   bool have_seen_accelerometer_data_;
    161 
    162   // True when the screen's orientation is being changed.
    163   bool in_set_screen_rotation_;
    164 
    165   // The rotation of the display set by the user. This rotation will be
    166   // restored upon exiting maximize mode.
    167   gfx::Display::Rotation user_rotation_;
    168 
    169   // The current rotation set by MaximizeModeController for the internal
    170   // display. Compared in OnDisplayConfigurationChanged to determine user
    171   // display setting changes.
    172   gfx::Display::Rotation current_rotation_;
    173 
    174   // Rotation Lock observers.
    175   ObserverList<Observer> observers_;
    176 
    177   // Tracks time spent in (and out of) touchview mode.
    178   base::Time last_touchview_transition_time_;
    179   base::TimeDelta total_touchview_time_;
    180   base::TimeDelta total_non_touchview_time_;
    181 
    182   DISALLOW_COPY_AND_ASSIGN(MaximizeModeController);
    183 };
    184 
    185 }  // namespace ash
    186 
    187 #endif  // ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
    188