touch_exploration_manager.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #include "ash/accessibility/chromevox/touch_exploration_manager.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "ash/accessibility/accessibility_controller_impl.h"
  8. #include "ash/accessibility/chromevox/touch_exploration_controller.h"
  9. #include "ash/accessibility/ui/accessibility_focus_ring_controller_impl.h"
  10. #include "ash/constants/ash_switches.h"
  11. #include "ash/keyboard/ui/keyboard_ui_controller.h"
  12. #include "ash/public/cpp/accessibility_focus_ring_info.h"
  13. #include "ash/root_window_controller.h"
  14. #include "ash/shell.h"
  15. #include "ash/wm/window_util.h"
  16. #include "base/command_line.h"
  17. #include "base/metrics/histogram_functions.h"
  18. #include "chromeos/ash/components/audio/cras_audio_handler.h"
  19. #include "chromeos/ash/components/audio/sounds.h"
  20. #include "extensions/common/constants.h"
  21. #include "ui/accessibility/ax_enums.mojom.h"
  22. #include "ui/aura/client/aura_constants.h"
  23. #include "ui/gfx/geometry/rect.h"
  24. #include "ui/wm/public/activation_client.h"
  25. namespace ash {
  26. namespace {
  27. AccessibilityControllerImpl* GetA11yController() {
  28. return Shell::Get()->accessibility_controller();
  29. }
  30. } // namespace
  31. TouchExplorationManager::TouchExplorationManager(
  32. RootWindowController* root_window_controller)
  33. : root_window_controller_(root_window_controller),
  34. audio_handler_(CrasAudioHandler::Get()),
  35. observing_window_(nullptr) {
  36. Shell::Get()->AddShellObserver(this);
  37. Shell::Get()->accessibility_controller()->AddObserver(this);
  38. Shell::Get()->activation_client()->AddObserver(this);
  39. keyboard::KeyboardUIController::Get()->AddObserver(this);
  40. UpdateTouchExplorationState();
  41. }
  42. TouchExplorationManager::~TouchExplorationManager() {
  43. // TODO(jamescook): Clean up shutdown order so this check isn't needed. See
  44. // also the TODO in |OnAccessibilityControllerShutdown|.
  45. if (Shell::Get()->accessibility_controller())
  46. Shell::Get()->accessibility_controller()->RemoveObserver(this);
  47. Shell::Get()->activation_client()->RemoveObserver(this);
  48. keyboard::KeyboardUIController::Get()->RemoveObserver(this);
  49. Shell::Get()->RemoveShellObserver(this);
  50. if (observing_window_)
  51. observing_window_->RemoveObserver(this);
  52. }
  53. void TouchExplorationManager::OnAccessibilityStatusChanged() {
  54. UpdateTouchExplorationState();
  55. }
  56. void TouchExplorationManager::OnAccessibilityControllerShutdown() {
  57. // This code helps with shutdown, but it does not obviate the need for similar
  58. // code in |TouchExplorationManager::~TouchExplorationManager|. That is
  59. // because there is a |TouchExplorationManager| per display, but only one
  60. // |AccessibilityController|. If you disconnect an external display, then the
  61. // corresponding |TouchExplorationManager| will be destroyed, but
  62. // |OnAccessibilityControllerShutdown| will not be called thereon.
  63. // TODO(jamescook): Clean up shutdown order so this code is not reached (and
  64. // then remove it). See also the TODO in |~TouchExplorationManager|.
  65. Shell::Get()->accessibility_controller()->RemoveObserver(this);
  66. }
  67. void TouchExplorationManager::OnWindowPropertyChanged(aura::Window* window,
  68. const void* key,
  69. intptr_t old) {
  70. if (key != aura::client::kAccessibilityTouchExplorationPassThrough)
  71. return;
  72. UpdateTouchExplorationState();
  73. }
  74. void TouchExplorationManager::OnWindowDestroying(aura::Window* window) {
  75. DCHECK(observing_window_ == window);
  76. observing_window_->RemoveObserver(this);
  77. observing_window_ = nullptr;
  78. }
  79. void TouchExplorationManager::SetOutputLevel(int volume) {
  80. if (volume > 0) {
  81. if (audio_handler_->IsOutputMuted()) {
  82. audio_handler_->SetOutputMute(false);
  83. }
  84. }
  85. audio_handler_->SetOutputVolumePercent(volume);
  86. // Avoid negative volume.
  87. if (audio_handler_->IsOutputVolumeBelowDefaultMuteLevel())
  88. audio_handler_->SetOutputMute(true);
  89. }
  90. void TouchExplorationManager::SilenceSpokenFeedback() {
  91. if (GetA11yController()->spoken_feedback().enabled())
  92. GetA11yController()->SilenceSpokenFeedback();
  93. }
  94. void TouchExplorationManager::PlayVolumeAdjustEarcon() {
  95. if (!VolumeAdjustSoundEnabled())
  96. return;
  97. if (!audio_handler_->IsOutputMuted() &&
  98. audio_handler_->GetOutputVolumePercent() != 100) {
  99. GetA11yController()->PlayEarcon(Sound::kVolumeAdjust);
  100. }
  101. }
  102. void TouchExplorationManager::PlayPassthroughEarcon() {
  103. GetA11yController()->PlayEarcon(Sound::kPassthrough);
  104. }
  105. void TouchExplorationManager::PlayLongPressRightClickEarcon() {
  106. // TODO: Rename this sound to SOUND_LONG_PRESS_RIGHT_CLICK.
  107. GetA11yController()->PlayEarcon(Sound::kExitScreen);
  108. }
  109. void TouchExplorationManager::PlayEnterScreenEarcon() {
  110. GetA11yController()->PlayEarcon(Sound::kEnterScreen);
  111. }
  112. void TouchExplorationManager::HandleAccessibilityGesture(
  113. ax::mojom::Gesture gesture,
  114. gfx::PointF location) {
  115. base::UmaHistogramEnumeration("Accessibility.ChromeVox.PerformGestureType",
  116. gesture);
  117. GetA11yController()->HandleAccessibilityGesture(gesture, location);
  118. }
  119. void TouchExplorationManager::OnDisplayMetricsChanged(
  120. const display::Display& display,
  121. uint32_t changed_metrics) {
  122. const display::Display this_display =
  123. display::Screen::GetScreen()->GetDisplayNearestWindow(
  124. root_window_controller_->GetRootWindow());
  125. if (this_display.id() == display.id())
  126. UpdateTouchExplorationState();
  127. }
  128. void TouchExplorationManager::OnTwoFingerTouchStart() {
  129. GetA11yController()->OnTwoFingerTouchStart();
  130. }
  131. void TouchExplorationManager::OnTwoFingerTouchStop() {
  132. // Can be null during shutdown.
  133. if (AccessibilityControllerImpl* controller = GetA11yController())
  134. controller->OnTwoFingerTouchStop();
  135. }
  136. void TouchExplorationManager::PlaySpokenFeedbackToggleCountdown(
  137. int tick_count) {
  138. if (GetA11yController()->ShouldToggleSpokenFeedbackViaTouch())
  139. GetA11yController()->PlaySpokenFeedbackToggleCountdown(tick_count);
  140. }
  141. void TouchExplorationManager::PlayTouchTypeEarcon() {
  142. GetA11yController()->PlayEarcon(Sound::kTouchType);
  143. }
  144. void TouchExplorationManager::ToggleSpokenFeedback() {
  145. if (GetA11yController()->ShouldToggleSpokenFeedbackViaTouch()) {
  146. GetA11yController()->SetSpokenFeedbackEnabled(
  147. !GetA11yController()->spoken_feedback().enabled(),
  148. A11Y_NOTIFICATION_SHOW);
  149. }
  150. }
  151. void TouchExplorationManager::OnWindowActivated(
  152. ::wm::ActivationChangeObserver::ActivationReason reason,
  153. aura::Window* gained_active,
  154. aura::Window* lost_active) {
  155. if (lost_active && lost_active->HasObserver(this)) {
  156. lost_active->RemoveObserver(this);
  157. observing_window_ = nullptr;
  158. }
  159. if (gained_active && !gained_active->HasObserver(this)) {
  160. gained_active->AddObserver(this);
  161. observing_window_ = gained_active;
  162. }
  163. UpdateTouchExplorationState();
  164. }
  165. void TouchExplorationManager::SetTouchAccessibilityAnchorPoint(
  166. const gfx::Point& anchor_point) {
  167. if (touch_exploration_controller_) {
  168. touch_exploration_controller_->SetTouchAccessibilityAnchorPoint(
  169. anchor_point);
  170. }
  171. }
  172. void TouchExplorationManager::OnKeyboardVisibleBoundsChanged(
  173. const gfx::Rect& new_bounds) {
  174. UpdateTouchExplorationState();
  175. }
  176. void TouchExplorationManager::OnKeyboardEnabledChanged(bool is_enabled) {
  177. UpdateTouchExplorationState();
  178. }
  179. void TouchExplorationManager::UpdateTouchExplorationState() {
  180. // See crbug.com/603745 for more details.
  181. const bool pass_through_surface =
  182. window_util::GetActiveWindow() &&
  183. window_util::GetActiveWindow()->GetProperty(
  184. aura::client::kAccessibilityTouchExplorationPassThrough);
  185. const bool spoken_feedback_enabled =
  186. GetA11yController()->spoken_feedback().enabled();
  187. if (!touch_accessibility_enabler_) {
  188. // Always enable gesture to toggle spoken feedback.
  189. touch_accessibility_enabler_ = std::make_unique<TouchAccessibilityEnabler>(
  190. root_window_controller_->GetRootWindow(), this);
  191. }
  192. if (spoken_feedback_enabled) {
  193. if (!touch_exploration_controller_.get()) {
  194. touch_exploration_controller_ =
  195. std::make_unique<TouchExplorationController>(
  196. root_window_controller_->GetRootWindow(), this,
  197. touch_accessibility_enabler_->GetWeakPtr());
  198. }
  199. if (pass_through_surface) {
  200. const display::Display display =
  201. display::Screen::GetScreen()->GetDisplayNearestWindow(
  202. root_window_controller_->GetRootWindow());
  203. const gfx::Rect work_area = display.work_area();
  204. touch_exploration_controller_->SetExcludeBounds(work_area);
  205. SilenceSpokenFeedback();
  206. // Clear the focus highlight.
  207. Shell::Get()->accessibility_focus_ring_controller()->SetFocusRing(
  208. extension_misc::kChromeVoxExtensionId,
  209. std::make_unique<AccessibilityFocusRingInfo>());
  210. } else {
  211. touch_exploration_controller_->SetExcludeBounds(gfx::Rect());
  212. }
  213. // Virtual keyboard.
  214. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  215. if (keyboard_controller->IsEnabled()) {
  216. touch_exploration_controller_->SetLiftActivationBounds(
  217. keyboard_controller->GetVisualBoundsInScreen());
  218. }
  219. } else {
  220. touch_exploration_controller_.reset();
  221. }
  222. }
  223. bool TouchExplorationManager::VolumeAdjustSoundEnabled() {
  224. return !base::CommandLine::ForCurrentProcess()->HasSwitch(
  225. switches::kDisableVolumeAdjustSound);
  226. }
  227. } // namespace ash