forked from kurtisnelson/wearscript-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneliner.py
146 lines (124 loc) · 5.25 KB
/
oneliner.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
#!/usr/bin/env python
import shutil
import urllib2
import platform
import tempfile
import urllib
import os
import subprocess
import webbrowser
import stat
URL = 'https://github.com/kurtisnelson/wearscript-android/releases/download/v0.3.0/'
def browser(url):
print('Open this url in a browser: ' + url)
#webbrowser.open(url)
class InstallerLinux(object):
def __init__(self):
self.apks = [URL + 'CaptureActivity.apk', URL + 'OpenCV_2.4.6_Manager_2.9_armv7a-neon.apk', URL + 'WearScript-release.apk', URL + 'picarus.apk']
self.urls = [URL + 'adb.linux']
self.adb_name = 'adb.linux'
self.directory = tempfile.mkdtemp()
self.adb = os.path.join(self.directory, self.adb_name)
print('Working Directory: ' + self.directory)
def _screen_on(self):
if not self._screen_state():
p = subprocess.Popen([self.adb, 'shell', 'input', 'keyevent', '26'])
r = p.wait()
def _screen_state(self):
p = subprocess.Popen([self.adb, 'shell', 'dumpsys', 'input_method'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return p.communicate()[0].find('mScreenOn=true') != -1
def download(self):
for url in self.urls + self.apks:
print('Downloading: ' + url)
urllib.urlretrieve(url, os.path.join(self.directory, os.path.basename(url)))
def chmod_adb(self):
mode = stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR
os.chmod(self.adb, mode)
def connect_glass(self):
print('Make sure Glass is in debug mode (instructions http://goo.gl/vEoyul) or your Android phone is in developer mode (http://goo.gl/E0hXdj) and connect it...waiting')
while 1:
try:
p = subprocess.Popen([self.adb, 'shell', 'ls'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
except OSError:
raise RuntimeError('Cannot execute adb\nYou may have a 64 bit processor and need the 32 bit libs.\nOn Ubuntu try: sudo apt-get install --reinstall libc6-i386 libncurses5:i386 libstdc++6:i386')
r = p.wait()
if r == 0:
print('Glass connected')
break
def push_apks(self):
print('Uninstalling any previous WearScript...')
try:
p = subprocess.Popen([self.adb, 'uninstall', 'com.dappervision.wearscript'])
r = p.wait()
except:
pass
print('Installing APKs...')
for apk_url in self.apks:
apk = os.path.join(self.directory, os.path.basename(apk_url))
p = subprocess.Popen([self.adb, 'install', '-r', apk])
r = p.wait()
def authenticate(self):
url = 'https://api.wearscript.com/'
browser(url)
print('then authenticate with your Google account, click the gears icon on the bottom left. Press "Generate QR Code"')
raw_input('Press enter when you are done...')
print('Starting Setup with WearScript (a QR scanner should be running now). If a menu of options is visible on Glass then downswipe to close it. Scan the code')
self._screen_on()
p = subprocess.Popen([self.adb, 'shell', 'am', 'start', '-n', 'com.dappervision.wearscript/.ui.SetupActivity'])
r = p.wait()
raw_input('Press enter when you are done...')
def playground(self):
print('Starting WearScript on Glass')
self._screen_on()
p = subprocess.Popen([self.adb, 'shell', 'am', 'start', '-n', 'com.dappervision.wearscript/.ui.ScriptActivity'])
r = p.wait()
url = 'https://api.wearscript.com'
print('then press Ctrl+Enter (or Cmd+Enter on OS X) to run a script, you are all done')
browser(url)
def cleanup(self):
print('Removing Working Directory: ' + self.directory)
shutil.rmtree(self.directory)
class InstallerOSX(InstallerLinux):
def __init__(self):
super(InstallerOSX, self).__init__()
self.adb_name = 'adb.osx'
self.adb = os.path.join(self.directory, self.adb_name)
self.urls = [URL + self.adb_name]
class InstallerWidows(InstallerLinux):
def __init__(self):
super(InstallerWindows, self).__init__()
self.urls = [URL + 'AdbWinApi.dll',
URL + 'AdbWinUsbApi.dll',
URL + 'adb.exe']
self.adb_name = 'adb.exe'
self.adb = os.path.join(self.directory, self.adb_name)
def os_detector():
uname = platform.uname()
if uname[0] == 'Darwin' and uname[-2] == 'x86_64':
return 'osx'
elif uname[0] == 'Linux' and uname[-2] == 'x86_64':
return 'linux64'
return 'other'
def download(directory, url):
print('Downloading: ' + url)
urllib.urlretrieve(url, os.path.join(directory, os.path.basename(url)))
def main():
curos = os_detector()
print('Detected os[%s]' % str(curos))
try:
if curos == 'osx':
installer = InstallerOSX()
elif curos.startswith('linux'):
installer = InstallerLinux()
else:
raise RuntimeError('Unknown OS')
installer.download()
installer.chmod_adb()
installer.connect_glass()
installer.push_apks()
installer.authenticate()
installer.playground()
finally:
installer.cleanup()
if __name__ == '__main__':
main()