-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathandroid_controller.py
168 lines (144 loc) · 5 KB
/
android_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
## read plugin api:
## https://kupferlauncher.github.io/Documentation/PluginAPI.html
__kupfer_name__ = _('Android Controller')
__version__ = '0.2.0'
__author__ = 'Hugo Sena Ribeiro <[email protected]>'
__description__ = '''Control your android device with ADB'''
__kupfer_sources__ = ("DeviceSource",)
__kupfer_actions__ = ("SendToMobile",)
from kupfer.plugin_support import PluginSettings
__kupfer_settings__ = PluginSettings(
{
"key" : "device_dir",
"label": "Device Dir",
"type": str,
"value": "/storage/sdcard0/Download"
}
)
from kupfer.objects import Leaf, TextLeaf, FileLeaf, UrlLeaf
class DeviceLeaf(Leaf):
def __init__(self, obj):
Leaf.__init__(self, obj, "Android " + obj[0])
def get_actions(self):
for key in KEY_NAMES.keys():
yield KeyAction(key)
from kupfer.objects import Source
class DeviceSource(Source):
def __init__(self):
Source.__init__(self, _("Connected Devices"))
def get_items(self):
out = subprocess.check_output(['adb', 'devices', '-l']).decode("utf-8")
for line in out.split('\n')[1:]:
if line:
yield DeviceLeaf(
[ v for v in line.split(' ') if v]
)
KEY_MUTE = 91
KEY_VOLUME_UP = 24
KEY_VOLUME_DOWN = 25
KEY_MEDIA_PLAY_PAUSE = 85
KEY_MEDIA_NEXT = 87
KEY_MEDIA_PREVIOUS = 88
KEY_MEDIA_STOP = 86
KEY_NAMES = {
KEY_MUTE: "Mute",
KEY_VOLUME_UP: "Volume Up",
KEY_VOLUME_DOWN: "Volume Down",
KEY_MEDIA_PLAY_PAUSE: "Play/Pause",
KEY_MEDIA_NEXT: "Next Media",
KEY_MEDIA_PREVIOUS: "Prev Media",
KEY_MEDIA_STOP: "Stop Media"
}
import subprocess
from kupfer.objects import Action
class KeyAction(Action):
def __init__(self, key):
Action.__init__(self, name=KEY_NAMES[key])
self.key = key
def activate(self, leaf):
device = leaf.object[0]
subprocess.Popen([
'adb',
'-s',
device,
'shell',
'input',
'keyevent',
str(self.key)
])
def item_types(self):
yield DeviceLeaf
from kupfer.obj.contacts import ContactLeaf
class SendToMobile(Action):
def __init__(self):
Action.__init__(self, "Send to Device")
def activate(self, leaf, device_leaf):
adb_cmd = ([], [])
if hasattr(leaf, 'to_android_cmd'):
adb_cmd = leaf.to_android_cmd(device_leaf)
else:
adb_cmd = self._to_android_cmd(leaf, device_leaf)
if adb_cmd:
subprocess.Popen(adb_cmd)
def _contact_to_android(self, obj):
suffix = ['-a', 'android.intent.action.INSERT']
suffix += ['-t', 'vnd.android.cursor.dir/person']
for k, v in obj.items():
if v and not k.startswith('_'):
if isinstance(v, str):
suffix += [
'-e', k.lower(),
"'{}'".format(
str(v).replace('"', '\\"').replace("'", "\\'")
)
]
elif hasattr(v, 'keys'):
for kk, vv in v.items():
suffix += [
'-e', kk.lower(),
"'{}'".format(
str(vv).replace('"', '\\"').replace("'", "\\'")
)
]
elif hasattr(v, 'index'):
for vv in v:
suffix += [
'-e', k.lower(),
"'{}'".format(
str(vv).replace('"', '\\"').replace("'", "\\'")
)
]
return suffix
def _file_to_android(self, device, leaf):
device_dir = __kupfer_settings__['device_dir']
adb_cmd = ['adb', '-s', device, 'push',
leaf.canonical_path(), device_dir]
prefix, suffix = ([], [])
return adb_cmd, prefix, suffix
def _to_android_cmd(self, leaf, device_leaf):
device = device_leaf.object[0]
adb_cmd = ['adb', '-s', device, 'shell']
prefix = ['am', 'start']
suffix = ['-a', 'android.intent.action.VIEW', '-d']
if hasattr(leaf, 'to_android_itent'):
suffix = leaf.to_android_itent(device_leaf)
elif isinstance(leaf, ContactLeaf):
suffix = self._contact_to_android(leaf.object)
elif isinstance(leaf, FileLeaf):
adb_cmd, prefix, suffix = self._file_to_android(device, leaf)
else:
suffix += [
"'{}'".format(
str(leaf.object).replace('"', '\\"').replace("'", "\\'")
)
]
return adb_cmd + prefix + suffix
def item_types(self,):
yield UrlLeaf
yield TextLeaf
yield FileLeaf
yield ContactLeaf
def requires_object(self):
return True
def object_types(self):
yield DeviceLeaf