-
Notifications
You must be signed in to change notification settings - Fork 227
/
pmca-gui.py
executable file
·369 lines (277 loc) · 10.2 KB
/
pmca-gui.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python3
"""A simple gui interface"""
import sys
import traceback
import webbrowser
import config
from pmca.commands.usb import *
from pmca.platform.backend.senser import *
from pmca.platform.backend.usb import *
from pmca.platform.tweaks import *
from pmca.ui import *
if getattr(sys, 'frozen', False):
from frozenversion import version
else:
version = None
class PrintRedirector(object):
"""Redirect writes to a function"""
def __init__(self, func, parent=None):
self.func = func
self.parent = parent
def write(self, str):
if self.parent:
self.parent.write(str)
self.func(str)
def flush(self):
self.parent.flush()
class AppLoadTask(BackgroundTask):
def doBefore(self):
self.ui.setAppList([])
self.ui.appLoadButton.config(state=DISABLED)
def do(self, arg):
try:
print('')
return list(listApps().values())
except Exception:
traceback.print_exc()
def doAfter(self, result):
if result:
self.ui.setAppList(result)
self.ui.appLoadButton.config(state=NORMAL)
class InfoTask(BackgroundTask):
"""Task to run infoCommand()"""
def doBefore(self):
self.ui.infoButton.config(state=DISABLED)
def do(self, arg):
try:
print('')
infoCommand()
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.infoButton.config(state=NORMAL)
class InstallTask(BackgroundTask):
"""Task to run installCommand()"""
def doBefore(self):
self.ui.installButton.config(state=DISABLED)
return self.ui.getMode(), self.ui.getSelectedApk(), self.ui.getSelectedApp()
def do(self, args):
(mode, apkFilename, app) = args
try:
print('')
if mode == self.ui.MODE_APP and app:
installCommand(appPackage=app.package)
elif mode == self.ui.MODE_APK and apkFilename:
with open(apkFilename, 'rb') as f:
installCommand(apkFile=f)
else:
installCommand()
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.installButton.config(state=NORMAL)
class FirmwareUpdateTask(BackgroundTask):
"""Task to run firmwareUpdateCommand()"""
def doBefore(self):
self.ui.fwUpdateButton.config(state=DISABLED)
return self.ui.getSelectedDat()
def do(self, datFile):
try:
if datFile:
print('')
with open(datFile, 'rb') as f:
firmwareUpdateCommand(f)
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.fwUpdateButton.config(state=NORMAL)
class StartPlatformShellTask(BackgroundTask):
def doBefore(self):
self.ui.startUpdaterShellButton.config(state=DISABLED)
self.ui.startSenserShellButton.config(state=DISABLED)
def do(self, arg):
try:
print('')
self.start()
except Exception:
traceback.print_exc()
def start(self):
pass
def launchShell(self, backend):
backend.start()
tweaks = TweakInterface(backend)
if next(tweaks.getTweaks(), None):
endFlag = threading.Event()
root = self.ui.master
root.run(lambda: root.after(0, lambda: TweakDialog(root, tweaks, endFlag)))
endFlag.wait()
else:
print('No tweaks available')
backend.stop()
def doAfter(self, result):
self.ui.startUpdaterShellButton.config(state=NORMAL)
self.ui.startSenserShellButton.config(state=NORMAL)
class StartUpdaterShellTask(StartPlatformShellTask):
"""Task to run updaterShellCommand() and open TweakDialog"""
def start(self):
updaterShellCommand(complete=lambda dev: self.launchShell(UsbPlatformBackend(dev)))
class StartSenserShellTask(StartPlatformShellTask):
"""Task to run senserShellCommand() and open TweakDialog"""
def start(self):
senserShellCommand(complete=lambda dev: self.launchShell(SenserPlatformBackend(dev)))
class TweakApplyTask(BackgroundTask):
"""Task to run TweakInterface.apply()"""
def doBefore(self):
self.ui.setState(DISABLED)
def do(self, arg):
try:
print('Applying tweaks...')
self.ui.tweakInterface.apply()
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.setState(NORMAL)
self.ui.cancel()
class MainUi(UiRoot):
"""Main window"""
def __init__(self, title):
UiRoot.__init__(self)
self.title(title)
self.geometry('450x500')
self['menu'] = Menu(self)
tabs = Notebook(self, padding=5)
tabs.pack(fill=X)
tabs.add(InfoFrame(self, padding=10), text='Camera info')
tabs.add(InstallerFrame(self, padding=10), text='Install app')
tabs.add(UpdaterShellFrame(self, padding=10), text='Tweaks')
tabs.add(FirmwareFrame(self, padding=10), text='Update firmware')
docsLink = Label(self, text='Camera compatibility', foreground='blue', cursor='hand2')
docsLink.bind('<Button-1>', lambda e: webbrowser.open_new(config.docsUrl + '/devices.html'))
docsLink.pack(pady=(0, 5))
self.logText = ScrollingText(self)
self.logText.text.configure(state=DISABLED)
self.logText.pack(fill=BOTH, expand=True)
self.redirectStreams()
def log(self, msg):
self.logText.text.configure(state=NORMAL)
self.logText.text.insert(END, msg)
self.logText.text.configure(state=DISABLED)
self.logText.text.see(END)
def redirectStreams(self):
for stream in ['stdout', 'stderr']:
setattr(sys, stream, PrintRedirector(lambda str: self.run(lambda: self.log(str)), getattr(sys, stream)))
class InfoFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
self.infoButton = Button(self, text='Get camera info', command=InfoTask(self).run, padding=5)
self.infoButton.pack(fill=X)
class InstallerFrame(UiFrame):
MODE_APP = 0
MODE_APK = 1
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
self.modeVar = IntVar(value=self.MODE_APP)
appFrame = Labelframe(self, padding=5)
appFrame['labelwidget'] = Radiobutton(appFrame, text='Select an app from the app list', variable=self.modeVar, value=self.MODE_APP)
appFrame.columnconfigure(0, weight=1)
appFrame.pack(fill=X)
self.appCombo = Combobox(appFrame, state='readonly')
self.appCombo.bind('<<ComboboxSelected>>', lambda e: self.modeVar.set(self.MODE_APP))
self.appCombo.grid(row=0, column=0, sticky=W+E)
self.setAppList([])
self.appLoadButton = Button(appFrame, text='Refresh', command=AppLoadTask(self).run)
self.appLoadButton.grid(row=0, column=1)
appListLink = Label(appFrame, text='Source', foreground='blue', cursor='hand2')
appListLink.bind('<Button-1>', lambda e: webbrowser.open_new('https://github.com/' + config.githubAppListUser + '/' + config.githubAppListRepo))
appListLink.grid(columnspan=2, sticky=W)
apkFrame = Labelframe(self, padding=5)
apkFrame['labelwidget'] = Radiobutton(apkFrame, text='Select an apk', variable=self.modeVar, value=self.MODE_APK)
apkFrame.columnconfigure(0, weight=1)
apkFrame.pack(fill=X)
self.apkFile = Entry(apkFrame)
self.apkFile.grid(row=0, column=0, sticky=W+E)
self.apkSelectButton = Button(apkFrame, text='Open apk...', command=self.openApk)
self.apkSelectButton.grid(row=0, column=1)
self.installButton = Button(self, text='Install selected app', command=InstallTask(self).run, padding=5)
self.installButton.pack(fill=X, pady=(5, 0))
self.run(AppLoadTask(self).run)
def getMode(self):
return self.modeVar.get()
def openApk(self):
fn = askopenfilename(filetypes=[('Apk files', '.apk'), ('All files', '.*')])
if fn:
self.apkFile.delete(0, END)
self.apkFile.insert(0, fn)
self.modeVar.set(self.MODE_APK)
def getSelectedApk(self):
return self.apkFile.get()
def setAppList(self, apps):
self.appList = apps
self.appCombo['values'] = [''] + [app.name for app in apps]
self.appCombo.current(0)
def getSelectedApp(self):
if self.appCombo.current() > 0:
return self.appList[self.appCombo.current() - 1]
class FirmwareFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
datFrame = Labelframe(self, padding=5)
datFrame['labelwidget'] = Label(datFrame, text='Firmware file')
datFrame.pack(fill=X)
self.datFile = Entry(datFrame)
self.datFile.pack(side=LEFT, fill=X, expand=True)
self.datSelectButton = Button(datFrame, text='Open...', command=self.openDat)
self.datSelectButton.pack()
self.fwUpdateButton = Button(self, text='Update firmware', command=FirmwareUpdateTask(self).run, padding=5)
self.fwUpdateButton.pack(fill=X, pady=(5, 0))
def openDat(self):
fn = askopenfilename(filetypes=[('Firmware files', '.dat'), ('All files', '.*')])
if fn:
self.datFile.delete(0, END)
self.datFile.insert(0, fn)
def getSelectedDat(self):
return self.datFile.get()
class UpdaterShellFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
self.startUpdaterShellButton = Button(self, text='Start tweaking (updater mode)', command=StartUpdaterShellTask(self).run, padding=5)
self.startUpdaterShellButton.pack(fill=X)
self.startSenserShellButton = Button(self, text='Start tweaking (service mode)', command=StartSenserShellTask(self).run, padding=5)
self.startSenserShellButton.pack(fill=X, pady=(5, 0))
class TweakDialog(UiDialog):
def __init__(self, parent, tweakInterface, endFlag=None):
self.tweakInterface = tweakInterface
self.endFlag = endFlag
UiDialog.__init__(self, parent, "Tweaks")
def body(self, top):
tweakFrame = Labelframe(top, padding=5)
tweakFrame['labelwidget'] = Label(tweakFrame, text='Tweaks')
tweakFrame.pack(fill=X)
self.boxFrame = Frame(tweakFrame)
self.boxFrame.pack(fill=BOTH, expand=True)
self.applyButton = Button(top, text='Apply', command=TweakApplyTask(self).run, padding=5)
self.applyButton.pack(fill=X)
self.updateStatus()
def updateStatus(self):
for child in self.boxFrame.winfo_children():
child.destroy()
for id, desc, status, value in self.tweakInterface.getTweaks():
var = IntVar(value=status)
c = Checkbutton(self.boxFrame, text=desc + '\n' + value, variable=var, command=lambda id=id, var=var: self.setTweak(id, var.get()))
c.pack(fill=X)
def setTweak(self, id, enabled):
self.tweakInterface.setEnabled(id, enabled)
self.updateStatus()
def setState(self, state):
for widget in self.boxFrame.winfo_children() + [self.applyButton]:
widget.config(state=state)
def cancel(self, event=None):
UiDialog.cancel(self, event)
if self.endFlag:
self.endFlag.set()
def main():
"""Gui main"""
ui = MainUi('OpenMemories: pmca-gui' + (' ' + version if version else ''))
ui.mainloop()
if __name__ == '__main__':
main()