From cc957b5451385758b5a7972f313848b6c5b8f177 Mon Sep 17 00:00:00 2001 From: ma1co Date: Fri, 4 Nov 2016 23:44:56 +0000 Subject: [PATCH] GUI tab for firmware update --- pmca-gui.py | 121 +++++++++++++++++++++++++++++++------------ pmca/commands/usb.py | 6 ++- pmca/ui/__init__.py | 9 ++++ 3 files changed, 103 insertions(+), 33 deletions(-) diff --git a/pmca-gui.py b/pmca-gui.py index acfbb53..6f4f728 100644 --- a/pmca-gui.py +++ b/pmca-gui.py @@ -81,10 +81,26 @@ def doAfter(self, result): self.ui.installButton.config(state=NORMAL) -class InstallerUi(UiRoot): - MODE_APP = 0 - MODE_APK = 1 +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 InstallerUi(UiRoot): """Main window""" def __init__(self, title): UiRoot.__init__(self) @@ -92,13 +108,49 @@ def __init__(self, title): self.title(title) self.geometry('450x500') - mainFrame = Frame(self, padding=10) - mainFrame.pack(fill=X) + 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(FirmwareFrame(self, padding=10), text='Update firmware') + + 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() self.modeVar.set(self.MODE_APP) - appFrame = Labelframe(mainFrame, padding=5) + 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.pack(fill=X) @@ -110,7 +162,7 @@ def __init__(self, title): self.appLoadButton = Button(appFrame, text='Refresh', command=AppLoadTask(self).run) self.appLoadButton.pack() - apkFrame = Labelframe(mainFrame, padding=5) + apkFrame = Labelframe(self, padding=5) apkFrame['labelwidget'] = Radiobutton(apkFrame, text='Select an apk', variable=self.modeVar, value=self.MODE_APK) apkFrame.pack(fill=X) @@ -120,22 +172,10 @@ def __init__(self, title): self.apkSelectButton = Button(apkFrame, text='Open apk...', command=self.openApk) self.apkSelectButton.pack() - buttonFrame = Labelframe(mainFrame, padding=5) - buttonFrame['labelwidget'] = Label(buttonFrame, text='Actions') - buttonFrame.pack(fill=X) - - self.infoButton = Button(buttonFrame, text='Get camera info', command=InfoTask(self).run) - self.infoButton.pack(fill=X) - - self.installButton = Button(buttonFrame, text='Install selected app', command=InstallTask(self).run) - self.installButton.pack(fill=X) + self.installButton = Button(self, text='Install selected app', command=InstallTask(self).run, padding=5) + self.installButton.pack(fill=X, pady=(5, 0)) - self.logText = ScrollingText(self) - self.logText.text.configure(state=DISABLED) - self.logText.pack(fill=BOTH, expand=True) - - self.redirectStreams() - AppLoadTask(self).run() + self.run(AppLoadTask(self).run) def getMode(self): return self.modeVar.get() @@ -150,16 +190,6 @@ def openApk(self): def getSelectedApk(self): return self.apkFile.get() - 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))) - def setAppList(self, apps): self.appList = apps self.appCombo['values'] = [''] + [app.name for app in apps] @@ -170,6 +200,33 @@ def getSelectedApp(self): 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() + + def main(): """Gui main""" ui = InstallerUi('pmca-gui' + (' ' + version if version else '')) diff --git a/pmca/commands/usb.py b/pmca/commands/usb.py index 9013b0f..a9a3d66 100644 --- a/pmca/commands/usb.py +++ b/pmca/commands/usb.py @@ -204,7 +204,11 @@ def firmwareUpdateCommand(file, driverName=None): with importDriver(driverName) as driver: device = getDevice(driver) - if device and not isinstance(device, SonyMtpAppInstaller): + if device: + if isinstance(device, SonyMtpAppInstaller): + print 'Error: Cannot use camera in app install mode. Please restart the device.' + return + dev = SonyUpdaterCamera(device) print 'Initializing firmware update' diff --git a/pmca/ui/__init__.py b/pmca/ui/__init__.py index 2283177..46ce806 100644 --- a/pmca/ui/__init__.py +++ b/pmca/ui/__init__.py @@ -23,6 +23,15 @@ def _processQueue(self): self.after(100, self._processQueue) +class UiFrame(Frame): + def __init__(self, parent, **kwargs): + Frame.__init__(self, parent, **kwargs) + self._parent = parent + + def run(self, func): + self._parent.run(func) + + class BackgroundTask: """Similar to Android's AsyncTask""" def __init__(self, ui):