Skip to content

Instantly share code, notes, and snippets.

@xslendix
Last active September 22, 2021 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xslendix/582faab6593e52f784a227b23579499c to your computer and use it in GitHub Desktop.
Save xslendix/582faab6593e52f784a227b23579499c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
import os
import os.path as op
import subprocess
from subprocess import check_output as ceout
from adbutils import adb
device = adb.device()
#MainWindow class
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
self.setWindowTitle("File Explorer")
self.setMinimumSize(160,160)
self.resize(700,600)
self.treeView = QtWidgets.QListWidget()
self.path = '/sdcard'
self.updateList()
self.treeViewPC = QtWidgets.QListWidget()
self.pathPC = op.expanduser('~')
self.updateListPC()
self.treeView.itemDoubleClicked.connect(self.clicked)
self.treeViewPC.itemDoubleClicked.connect(self.clickedPC)
LayoutLeft = QtWidgets.QVBoxLayout()
LayoutLeft.addWidget(self.treeView)
buttonTransferRemote = QtWidgets.QPushButton(">>>>")
buttonTransferRemote.clicked.connect(self.transfer)
LayoutLeft.addWidget(buttonTransferRemote)
LayoutRight = QtWidgets.QVBoxLayout()
LayoutRight.addWidget(self.treeViewPC)
buttonTransferLocal = QtWidgets.QPushButton("<<<<")
buttonTransferLocal.clicked.connect(self.transferPC)
LayoutRight.addWidget(buttonTransferLocal)
#Create Layout
Layout = QtWidgets.QHBoxLayout()
Layout.addLayout(LayoutLeft)
Layout.addLayout(LayoutRight)
widget.setLayout(Layout)
self.createActions()
self.createMenus()
self.createStatusBar()
def transfer(self):
filename = op.basename(self.path + '/' + self.treeView.currentItem().text())
self.statusBar().showMessage("Downloading " + filename)
p = subprocess.Popen(('adb', 'pull', self.path + '/' + filename, self.pathPC + '/' + filename))
p.wait()
self.statusBar().showMessage("Ready")
self.updateListPC()
def transferPC(self):
filename = op.basename(self.pathPC + '/' + self.treeViewPC.currentItem().text())
self.statusBar().showMessage("Uploading " + filename)
p = subprocess.Popen(('adb', 'push', self.pathPC + '/' + filename, self.path + '/' + filename))
p.wait()
self.statusBar().showMessage("Ready")
self.updateList()
def clicked(self):
if self.isFile(self.path + '/' + self.treeView.currentItem().text()):
self.path += '/' + self.treeView.currentItem().text()
print('remote:', self.path)
self.updateList()
def updateList(self, num=0):
self.treeView.clear()
index = 0
output = ceout(('adb', 'shell', 'ls', '-a', '-1a', '/' + self.path)).decode()
files = output.split('\n')
files.sort()
for i in files:
if not i.startswith('ls: '):
self.treeView.insertItem(index, i)
index += 1
def isFile(self, path='/sdcard'):
print(device.shell('test -f ' + path + ' && echo 1 || echo 0'))
return device.shell('test -f ' + path + ' && echo 1 || echo 0')[0] == '0'
def clickedPC(self):
if not op.isfile(self.pathPC + '/' + self.treeViewPC.currentItem().text()):
self.pathPC += op.expanduser('/' + self.treeViewPC.currentItem().text())
print('pc:', self.pathPC)
self.updateListPC()
else:
subprocess.call(('xdg-open', self.pathPC + '/' + self.treeViewPC.currentItem().text()))
def updateListPC(self, num=0):
self.treeViewPC.clear()
index = 0
files = os.listdir(self.pathPC)
files.append('.')
files.append('..')
files.sort()
for i in files:
self.treeViewPC.insertItem(index, i)
index += 1
def createStatusBar(self):
self.statusBar().showMessage("Ready")
def about(self):
QtWidgets.QMessageBox.about(self, "About File Explorer",
"Version 1.0\n"
"Copyright 2014 Korben Carreno\n"
"Example of a File Explorer")
#Actions for menu buttons
def createActions(self):
self.newAct = QtWidgets.QAction("&New", self, shortcut=QtGui.QKeySequence.New, statusTip="Create a new file")
self.exitAct = QtWidgets.QAction("E&xit", self, shortcut="Ctrl+Q", statusTip="Exit the application", triggered=self.close)
self.aboutAct = QtWidgets.QAction("&About", self,statusTip="About File Explorer", triggered=self.about)
#Menus
def createMenus(self):
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.newAct)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.exitAct)
self.helpMenu = self.menuBar().addMenu("&Help")
self.helpMenu.addAction(self.aboutAct)
#main
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment