Skip to content

Commit

Permalink
Add Android IME management
Browse files Browse the repository at this point in the history
  • Loading branch information
QADavidCalvo committed May 16, 2024
1 parent 8431773 commit cf68c30
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions kebbie/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
KBKITPRO = "kbkitpro"
KBKITOSS = "kbkitoss"
SWIFTKEY = "swiftkey"
KEYBOARD_PACKAGE = {
GBOARD: "com.google.android.inputmethod.latin",
SWIFTKEY: "com.touchtype.swiftkey",
TAPPA: "com.tappa.keyboard",
}
ANDROID_CAPABILITIES = {
"platformName": "android",
"automationName": "UiAutomator2",
Expand Down Expand Up @@ -294,6 +299,10 @@ def __init__(
self.last_char_is_space = False
self.last_char_is_eos = False

# Set the keyboard as default
if self.platform == ANDROID:
self.select_keyboard(keyboard)

# Get the right layout
if self.keyboard == GBOARD:
self.detected = GboardLayoutDetector(self.driver, self._tap)
Expand Down Expand Up @@ -356,6 +365,19 @@ def get_android_devices() -> List[str]:
devices = [d.split()[0] for d in devices if not (d.startswith("List of devices attached") or len(d) == 0)]
return devices

def select_keyboard(self, keyboard):
ime_list = subprocess.check_output(['adb', 'shell', 'ime', 'list', '-s'], universal_newlines=True)
ime_name = None
for ime in ime_list.strip().split('\n'):
if KEYBOARD_PACKAGE[keyboard] in ime:
ime_name = ime
break
if ime_name:
subprocess.run(['adb', 'shell', 'settings', 'put', 'secure', 'show_ime_with_hard_keyboard', '1'],
stdout=subprocess.PIPE)
subprocess.run(['adb', 'shell', 'ime', 'enable', ime_name], stdout=subprocess.PIPE)
subprocess.run(['adb', 'shell', 'ime', 'set', ime_name], stdout=subprocess.PIPE)

def get_ios_devices() -> List[Tuple[str, str]]:
"""Static method that uses the `xcrun simctl` command to retrieve the
list of booted devices.
Expand Down Expand Up @@ -870,7 +892,7 @@ class GboardLayoutDetector(LayoutDetector):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
xpath_root="./*/*[@package='com.google.android.inputmethod.latin']",
xpath_root="./*/*[@package='" + KEYBOARD_PACKAGE[GBOARD] + "']",
xpath_keys=".//*[@resource-id][@content-desc]",
**kwargs,
)
Expand Down Expand Up @@ -1020,7 +1042,7 @@ class SwiftkeyLayoutDetector(LayoutDetector):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
xpath_root="./*/*[@package='com.touchtype.swiftkey']",
xpath_root="./*/*[@package='" + KEYBOARD_PACKAGE[SWIFTKEY] + "']",
xpath_keys=".//*[@class='android.view.View'][@content-desc]",
**kwargs,
)
Expand Down Expand Up @@ -1053,7 +1075,7 @@ class TappaLayoutDetector(LayoutDetector):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
xpath_root="./*/*[@package='com.tappa.keyboard']",
xpath_root="./*/*[@package='" + KEYBOARD_PACKAGE[TAPPA] + "']",
xpath_keys=".//com.mocha.keyboard.inputmethod.keyboard.Key",
**kwargs,
)
Expand All @@ -1067,7 +1089,7 @@ def get_suggestions(self) -> List[str]:
suggestions = []

# Get the raw content as text, weed out useless elements
section = self.driver.page_source.split("com.tappa.keyboard:id/toolbar")[1].split(
section = self.driver.page_source.split(KEYBOARD_PACKAGE[TAPPA] + ":id/toolbar")[1].split(
"</android.widget.FrameLayout>"
)[0]

Expand Down

0 comments on commit cf68c30

Please sign in to comment.