Replace instances of checking for kEnableExperimentalHotwording flag with
[chromium-blink-merge.git] / ash / virtual_keyboard_controller.cc
blob18050cb1b43e1a48d41dad40ea924eac902193dd
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 #include "ash/virtual_keyboard_controller.h"
7 #include <vector>
9 #include "ash/shell.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "base/command_line.h"
12 #include "base/strings/string_util.h"
13 #include "ui/events/devices/device_data_manager.h"
14 #include "ui/events/devices/input_device.h"
15 #include "ui/events/devices/keyboard_device.h"
16 #include "ui/events/devices/touchscreen_device.h"
17 #include "ui/gfx/x/x11_types.h"
18 #include "ui/keyboard/keyboard_switches.h"
19 #include "ui/keyboard/keyboard_util.h"
21 namespace ash {
23 VirtualKeyboardController::VirtualKeyboardController()
24 : has_external_keyboard_(false),
25 has_internal_keyboard_(false),
26 has_touchscreen_(false),
27 ignore_external_keyboard_(false) {
28 Shell::GetInstance()->AddShellObserver(this);
29 ui::DeviceDataManager::GetInstance()->AddObserver(this);
30 UpdateDevices();
33 VirtualKeyboardController::~VirtualKeyboardController() {
34 Shell::GetInstance()->RemoveShellObserver(this);
35 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
38 void VirtualKeyboardController::OnMaximizeModeStarted() {
39 if (!CommandLine::ForCurrentProcess()->HasSwitch(
40 keyboard::switches::kAutoVirtualKeyboard)) {
41 SetKeyboardEnabled(true);
45 void VirtualKeyboardController::OnMaximizeModeEnded() {
46 if (!CommandLine::ForCurrentProcess()->HasSwitch(
47 keyboard::switches::kAutoVirtualKeyboard)) {
48 SetKeyboardEnabled(false);
52 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
53 UpdateDevices();
56 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
57 UpdateDevices();
60 void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
61 ignore_external_keyboard_ = !ignore_external_keyboard_;
62 UpdateKeyboardEnabled();
65 void VirtualKeyboardController::UpdateDevices() {
66 ui::DeviceDataManager* device_data_manager =
67 ui::DeviceDataManager::GetInstance();
69 // Checks for touchscreens.
70 has_touchscreen_ = device_data_manager->touchscreen_devices().size() > 0;
72 // Checks for keyboards.
73 has_external_keyboard_ = false;
74 has_internal_keyboard_ = false;
75 for (const ui::KeyboardDevice& device :
76 device_data_manager->keyboard_devices()) {
77 if (has_internal_keyboard_ && has_external_keyboard_)
78 break;
79 ui::InputDeviceType type = device.type;
80 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
81 has_internal_keyboard_ = true;
82 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
83 has_external_keyboard_ = true;
85 // Update keyboard state.
86 UpdateKeyboardEnabled();
89 void VirtualKeyboardController::UpdateKeyboardEnabled() {
90 if (!CommandLine::ForCurrentProcess()->HasSwitch(
91 keyboard::switches::kAutoVirtualKeyboard)) {
92 SetKeyboardEnabled(Shell::GetInstance()
93 ->maximize_mode_controller()
94 ->IsMaximizeModeWindowManagerEnabled());
95 return;
97 SetKeyboardEnabled(!has_internal_keyboard_ && has_touchscreen_ &&
98 (!has_external_keyboard_ || ignore_external_keyboard_));
99 ash::Shell::GetInstance()
100 ->system_tray_notifier()
101 ->NotifyVirtualKeyboardSuppressionChanged(!has_internal_keyboard_ &&
102 has_touchscreen_ &&
103 has_external_keyboard_);
106 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
107 keyboard::SetTouchKeyboardEnabled(enabled);
108 if (enabled) {
109 Shell::GetInstance()->CreateKeyboard();
110 } else {
111 if (!keyboard::IsKeyboardEnabled())
112 Shell::GetInstance()->DeactivateKeyboard();
116 } // namespace ash