Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
add prepare_ime api
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 8, 2017
1 parent 51efeaa commit 6bb3276
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
24 changes: 20 additions & 4 deletions atx/drivers/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,21 @@ def enable_ime(self, ime):
Args:
- ime(string): for example "android.unicode.ime/.Utf7ImeService"
Raises:
RuntimeError
"""
self.adb_shell(['ime', 'enable', ime])
self.adb_shell(['ime', 'set', ime])

from_time = time.time()
while time.time() - from_time < 10.0:
if ime == self.current_ime(): # and self._adb_device.is_keyboard_shown():
return
time.sleep(0.2)
else:
raise RuntimeError("Error switch to input-method (%s)." % ime)

def _is_utf7ime(self, ime=None):
if ime is None:
ime = self.current_ime()
Expand All @@ -522,14 +533,19 @@ def _is_utf7ime(self, ime=None):
'com.netease.atx.assistant/.ime.Utf7ImeService',
'com.netease.nie.yosemite/.ime.ImeService']

def _prepare_ime(self):
def prepare_ime(self):
"""
Change current method to adb-keyboard
Raises:
RuntimeError
"""
if self._is_utf7ime():
return True

for ime in self.input_methods():
if self._is_utf7ime(ime):
self.enable_ime(ime)
return True
return False
# raise RuntimeError("Input method for programers not detected.\n" +
# "\tInstall with: python -m atx install atx-assistant")
Expand Down Expand Up @@ -564,7 +580,7 @@ def type(self, text, enter=False, next=False):
app source see here: https://github.com/openatx/android-unicode
"""
utext = strutils.decode(text)
if self._prepare_ime():
if self.prepare_ime():
estext = base64.b64encode(utext.encode('utf-7'))
self.adb_shell(['am', 'broadcast', '-a', 'ADB_INPUT_TEXT', '--es', 'format', 'base64', '--es', 'msg', estext])
else:
Expand All @@ -581,7 +597,7 @@ def clear_text(self, count=100):
Args:
- count (int): send KEY_DEL count
"""
self._prepare_ime()
self.prepare_ime()
self.keyevent('KEYCODE_MOVE_END')
self.adb_shell(['am', 'broadcast', '-a', 'ADB_INPUT_CODE', '--ei', 'code', '67', '--ei', 'repeat', str(count)])

Expand Down
11 changes: 9 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,22 @@ d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 steps
d.swipe(sx, sy, ex, ey, steps=10)

## 文本的输入 (only Android)

## 文本的输入
```py
d.type("hello world")
d.type("atx", enter=True) # perform enter after input
d.type("atx", next=True) # jump to next after input
d.clear_text() # clear input
```

安卓手机因为输入法的众多,接口不统一,所以为了方便我们的自动化,就专门做了一个输入法。下载安装ATX助手即可

```
python -matx install atx-assistant
```

通常直接调用 `d.type`是不会出问题的,但也不是绝对的。最好在测试之前调用 `d.prepare_ime()` 将输入法切换到我们定制的**ATX助手输入法**

## Common settings

```py
Expand Down

0 comments on commit 6bb3276

Please sign in to comment.