-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
274 lines (236 loc) · 11.2 KB
/
main.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
import sys
import subprocess
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QComboBox, QLineEdit, QTableWidget, QTableWidgetItem, QHeaderView, QMessageBox
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QTimer
from PyQt5.QtGui import QColor
class AdbWorker(QThread):
output = pyqtSignal(str)
def __init__(self, command):
super().__init__()
self.command = command
def run(self):
try:
result = subprocess.check_output(self.command, shell=True, text=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
result = e.output
self.output.emit(result)
class AndroidAppManager(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Android App Manager")
self.setGeometry(100, 100, 800, 600)
self.setStyleSheet("""
QMainWindow {
background-color: #2b2b2b;
color: #ffffff;
}
QLabel, QPushButton, QComboBox, QLineEdit, QTableWidget {
color: #ffffff;
background-color: #3c3f41;
border: 1px solid #555555;
padding: 5px;
border-radius: 3px;
}
QPushButton:hover {
background-color: #4c5052;
}
QTableWidget {
gridline-color: #555555;
}
QHeaderView::section {
background-color: #3c3f41;
color: #ffffff;
padding: 5px;
border: 1px solid #555555;
}
""")
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)
self.all_apps = [] # Store all apps for filtering
self.workers = [] # Keep track of all running workers
self.all_apps_output = "" # Initialize the attribute to store all apps output
self.setup_ui()
def setup_ui(self):
# Device selection
device_layout = QHBoxLayout()
self.device_combo = QComboBox()
self.device_combo.setMinimumWidth(200)
# Add event listener for device selection change
self.device_combo.currentIndexChanged.connect(lambda: (self.all_apps.clear(), self.app_table.setRowCount(0), self.app_table.clearContents(), self.update_device_info(), self.load_all_apps()))
self.refresh_devices_btn = QPushButton("Refresh Devices")
self.refresh_devices_btn.clicked.connect(self.refresh_devices)
device_layout.addWidget(QLabel("Select Device:"))
device_layout.addWidget(self.device_combo)
device_layout.addWidget(self.refresh_devices_btn)
device_layout.addStretch()
self.layout.addLayout(device_layout)
# Device info
self.device_info_label = QLabel("Device Info: Not connected")
self.layout.addWidget(self.device_info_label)
# Search
search_layout = QHBoxLayout()
self.search_input = QLineEdit()
self.search_input.setPlaceholderText("Search apps...")
self.search_btn = QPushButton("Search")
self.search_btn.clicked.connect(self.search_apps)
search_layout.addWidget(self.search_input)
search_layout.addWidget(self.search_btn)
self.layout.addLayout(search_layout)
# App list
self.app_table = QTableWidget()
self.app_table.setColumnCount(4)
self.app_table.setHorizontalHeaderLabels(["App Name", "Package Name", "Status", "Type"])
self.app_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.app_table.setSelectionBehavior(QTableWidget.SelectRows) # Change selection to row
self.layout.addWidget(self.app_table)
# Action buttons
action_layout = QHBoxLayout()
self.enable_btn = QPushButton("Enable")
self.disable_btn = QPushButton("Disable")
self.uninstall_btn = QPushButton("Uninstall")
self.enable_btn.clicked.connect(self.enable_app)
self.disable_btn.clicked.connect(self.disable_app)
self.uninstall_btn.clicked.connect(self.uninstall_app)
action_layout.addWidget(self.enable_btn)
action_layout.addWidget(self.disable_btn)
action_layout.addWidget(self.uninstall_btn)
self.layout.addLayout(action_layout)
self.refresh_devices()
def run_adb_command(self, command, callback):
worker = AdbWorker(command)
worker.output.connect(callback)
worker.finished.connect(lambda: self.workers.remove(worker)) # Remove worker from list when done
self.workers.append(worker)
worker.start()
def refresh_devices(self):
self.device_combo.clear()
print("Refreshing devices...")
self.run_adb_command("adb devices", self.update_device_list)
def update_device_list(self, output):
print(f"ADB output: {output}")
devices = []
for line in output.strip().split('\n')[1:]:
if '\t' in line:
device, status = line.split('\t')
if status == 'device':
devices.append(device)
print(f"Detected devices: {devices}")
self.device_combo.addItems(devices)
if devices:
self.device_combo.setCurrentIndex(0)
# self.update_device_info()
# self.load_all_apps() # Load all apps when a device is selected
else:
self.all_apps = [] # Clear the apps list
self.app_table.setRowCount(0) # Clear the displayed rows in the app table
self.app_table.clearContents() # Remove all items from the app table
print("No devices detected")
def update_device_info(self):
device = self.device_combo.currentText()
if device:
self.run_adb_command(f"adb -s {device} shell getprop", self.display_device_info)
def display_device_info(self, output):
info = {}
for line in output.split('\n'):
if ':' in line:
key, value = line.split(':', 1)
info[key.strip()[1:-1]] = value.strip()[1:-1]
device_info = f"Model: {info.get('ro.product.model', 'Unknown')}\t\t"
device_info += f"Android Version: {info.get('ro.build.version.release', 'Unknown')}\t\t"
device_info += f"API Level: {info.get('ro.build.version.sdk', 'Unknown')}"
self.device_info_label.setText(f"Device Info:\n\n{device_info}")
def load_all_apps(self):
device = self.device_combo.currentText()
if device:
self.all_apps = [] # Clear the apps list
self.app_table.setRowCount(0) # Clear the displayed rows in the app table
self.app_table.clearContents() # Remove all items from the app table
self.run_adb_command(f"adb -s {device} shell pm list packages -f -u", self.store_all_apps_output)
def store_all_apps_output(self, output):
self.all_apps_output = output # Store the output for later use
self.store_all_apps()
def store_all_apps(self):
self.all_apps = []
device = self.device_combo.currentText()
if device:
# Run the command to get disabled packages in a separate thread
self.run_adb_command(f"adb -s {device} shell pm list packages -d", self.process_disabled_packages)
def process_disabled_packages(self, disabled_output):
disabled_packages = set(line.split(':')[1] for line in disabled_output.strip().split('\n') if line)
for line in self.all_apps_output.strip().split('\n'):
if '=' in line:
path, package = line.rsplit('=', 1)
app_name = package.split('.')[-1]
status = "Disabled" if package in disabled_packages else "Enabled"
app_type = "System" if "/system/" in path else "User"
self.all_apps.append((app_name, package, status, app_type))
self.display_apps(self.all_apps)
def display_apps(self, apps):
self.app_table.setRowCount(0)
for app_name, package, status, app_type in apps:
row = self.app_table.rowCount()
self.app_table.insertRow(row)
self.app_table.setItem(row, 0, QTableWidgetItem(app_name))
self.app_table.setItem(row, 1, QTableWidgetItem(package))
self.app_table.setItem(row, 2, QTableWidgetItem(status))
self.app_table.setItem(row, 3, QTableWidgetItem(app_type))
if status == "Disabled":
for col in range(4):
self.app_table.item(row, col).setBackground(QColor(244, 67, 54)) # Material Red
def search_apps(self):
query = self.search_input.text().lower()
filtered_apps = [app for app in self.all_apps if query in app[1].lower()]
self.display_apps(filtered_apps)
def get_app_status(self, package):
device = self.device_combo.currentText()
result = subprocess.check_output(f"adb -s {device} shell pm list packages -d", shell=True, text=True)
return "Disabled" if package in result else "Enabled"
def enable_app(self):
self.change_app_state("enable")
def disable_app(self):
self.change_app_state("disable")
def change_app_state(self, action):
device = self.device_combo.currentText()
if not device:
QMessageBox.warning(self, "Error", "No device selected")
return
selected_items = self.app_table.selectedItems()
if not selected_items:
QMessageBox.warning(self, "Error", "No app selected")
return
package = self.app_table.item(selected_items[0].row(), 1).text()
command = f"adb -s {device} shell pm {action}{'-user' if action == 'disable' else ''} {package}"
self.run_adb_command(command, lambda _: QTimer.singleShot(1000, self.load_all_apps)) # Delay before refreshing
def uninstall_app(self):
device = self.device_combo.currentText()
if not device:
QMessageBox.warning(self, "Error", "No device selected")
return
selected_items = self.app_table.selectedItems()
if not selected_items:
QMessageBox.warning(self, "Error", "No app selected")
return
row = selected_items[0].row()
package = self.app_table.item(row, 1).text()
app_type = self.app_table.item(row, 3).text()
if app_type == "System":
reply = QMessageBox.warning(self, "Warning",
"You are about to uninstall a system app. This may cause system instability. "
"Are you sure you want to proceed?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.No:
return
command = f"adb -s {device} shell pm uninstall --user 0 {package}"
self.run_adb_command(command, lambda _: QTimer.singleShot(1000, self.load_all_apps))
def closeEvent(self, event):
for worker in self.workers:
if worker.isRunning():
worker.quit()
worker.wait()
super().closeEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = AndroidAppManager()
window.show()
sys.exit(app.exec_())