human_presence_orientation_controller.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2022 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. #ifndef ASH_SYSTEM_HUMAN_PRESENCE_HUMAN_PRESENCE_ORIENTATION_CONTROLLER_H_
  5. #define ASH_SYSTEM_HUMAN_PRESENCE_HUMAN_PRESENCE_ORIENTATION_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/public/cpp/tablet_mode_observer.h"
  8. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/scoped_observation.h"
  11. #include "chromeos/dbus/power/power_manager_client.h"
  12. #include "ui/display/display.h"
  13. #include "ui/display/display_observer.h"
  14. namespace ash {
  15. // HPS features only work when the sensor is in a "standard" configuration: in
  16. // laptop mode on the user's desk / lap in front of them.
  17. //
  18. // This controller tracks the physical state of the device and signals observers
  19. // when it enters or leaves non-standard orientations.
  20. class ASH_EXPORT HumanPresenceOrientationController
  21. : public TabletModeObserver,
  22. public display::DisplayObserver,
  23. public PowerManagerClient::Observer {
  24. public:
  25. class Observer : public base::CheckedObserver {
  26. public:
  27. ~Observer() override = default;
  28. // Called when the suitability of the device orientation for HPS-based
  29. // features changes.
  30. virtual void OnOrientationChanged(bool suitable_for_hps) = 0;
  31. };
  32. HumanPresenceOrientationController();
  33. HumanPresenceOrientationController(
  34. const HumanPresenceOrientationController& other) = delete;
  35. HumanPresenceOrientationController& operator=(
  36. const HumanPresenceOrientationController& other) = delete;
  37. ~HumanPresenceOrientationController() override;
  38. // Start or stop listening for changes to device orientation status.
  39. void AddObserver(Observer* observer);
  40. void RemoveObserver(Observer* observer);
  41. // Whether or not the device orientation is currently suitable for HPS-based
  42. // features.
  43. bool IsOrientationSuitable() const;
  44. private:
  45. // TabletModeObserver:
  46. void OnTabletPhysicalStateChanged() override;
  47. // display::DisplayObserver:
  48. void OnDisplayMetricsChanged(const display::Display& display,
  49. uint32_t changed_metrics) override;
  50. // chromeos::PowerManagerClient::Observer:
  51. void LidEventReceived(chromeos::PowerManagerClient::LidState state,
  52. base::TimeTicks timestamp) override;
  53. void OnReceiveSwitchStates(
  54. absl::optional<chromeos::PowerManagerClient::SwitchStates> switch_states);
  55. // Updates the internal state of the controller, and notifies observers if
  56. // the state has changed.
  57. void UpdateOrientation(bool physical_tablet_state,
  58. bool display_rotated,
  59. bool lid_closed);
  60. // If the device is physically configured like a tablet, we will sense frames
  61. // from atypical angles. Note this is different from tablet mode in general,
  62. // since a device can be in a physical tablet configuration but not be in
  63. // general "tablet mode" because it is still using the non-tablet UI (e.g.
  64. // when an external keyboard is connected).
  65. bool physical_tablet_state_ = false;
  66. // We make the assumption that the user is always oriented to match the screen
  67. // rotation. Hence, if the screen is rotated, we will be sensing rotated
  68. // frames.
  69. bool display_rotated_ = false;
  70. // Device with close lid captures unusable frames.
  71. bool lid_closed_ = false;
  72. // Clients listening for orientation status changes.
  73. base::ObserverList<Observer> observers_;
  74. base::ScopedObservation<TabletModeController, TabletModeObserver>
  75. tablet_mode_observation_{this};
  76. display::ScopedDisplayObserver display_observation_{this};
  77. base::ScopedObservation<chromeos::PowerManagerClient,
  78. chromeos::PowerManagerClient::Observer>
  79. power_manager_client_observation_{this};
  80. base::WeakPtrFactory<HumanPresenceOrientationController> weak_factory_{this};
  81. };
  82. } // namespace ash
  83. #endif // ASH_SYSTEM_HUMAN_PRESENCE_HUMAN_PRESENCE_ORIENTATION_CONTROLLER_H_