[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name: wsa_tools
# Description: adb一键控制wsa和手机设备
# Author: looking4freedom
# Date: 2021/10/25
# -------------------------------------------------------------------------------
import json
import os
import tkinter as tk
from tkinter import *
import threading
from tkinter import ttk
from tkinter import filedialog
# tkinter制作界面
class APKTk:
def __init__(self):
self.root = Tk()
self.root.geometry('800x300')
self.root.title("ADB控制器")
self.path = StringVar()
self.path.set("请输入需要安装的apk完整路径")
self.package_name = StringVar()
self.package_name.set("请输入app的包名")
self.select_device_list = []
self.device_list = self.get_device_list()
self.cb_list = []
self.cmb_list = []
self.package_3 = self.show_package()
self.flag = True
try:
cmd = 'adb connect 127.0.0.1:58526'
os.system(cmd)
except:
print("请检查WSA设置,是否开启开发人员模式")
@staticmethod
def install_and_open(device_name, file_path):
# 安装app
os.system("adb -s " + device_name + " install -r " + file_path)
@staticmethod
def clear_data(device_name, package_name):
# 清除全部数据
os.system("adb -s " + device_name + " shell pm clear " + package_name)
@staticmethod
def kill_process(device_name, package_name):
# 强制杀死app
os.system("adb -s " + device_name + " shell am force-stop " + package_name)
def get_activity(self):
# 获取设备当前的activity
for j in self.devices_list():
print(f"设备{j}当前页面信息:")
phone_page = os.popen("adb -s " + j + " shell dumpsys activity activities | findstr mResumedActivity")
phone_page_str = phone_page.read()
phone_page.close()
print(phone_page_str)
try:
for i in phone_page_str.split('\n'):
if "com.microsoft.windows.userapp" not in i:
app_name = re.findall(r'(com.*?)/', i)[0]
self.package_name.set(app_name)
except IndexError:
pass
def check_process(self):
# 查看进程
for i in self.devices_list():
print(f"设备{i}的应用{self.get_package_name()}当前进程信息:")
os.system("adb -s " + i + " shell ps -ef | findstr " + str(self.get_package_name()))
def show_package(self):
# 列出第三方包
for i in self.device_list:
if i == '127.0.0.1:58526':
print(f"设备{i}的第三方应用包:")
res = os.popen("adb -s " + i + " shell pm list package -3")
res_str = res.readlines()
res.close()
packages = [i[8:-1] for i in res_str]
print('packages', packages)
return packages
def outline_or_online(self):
if self.flag:
os.system("adb disconnect 127.0.0.1:58526")
self.flag = False
else:
os.system("adb connect 127.0.0.1:58526")
self.flag = True
@staticmethod
def clear_log_cache(device_name):
# 清除手机日志缓存
os.system("adb -s " + device_name + " logcat -c")
print(f"设备{device_name}清除日志缓存成功")
@staticmethod
def uninstall_all(device_name, package_name):
# 卸载app
os.system("adb -s " + device_name + " uninstall " + package_name)
@staticmethod
def get_device_list():
res = os.popen("adb devices")
res_str = res.readlines()
res.close()
device_list = [sub.split('\t')[0] for sub in res_str[1:-1]]
return device_list
def mul_check_box(self):
print("制作多选框", self.device_list)
for index, item in enumerate(self.device_list):
with os.popen("adb -s " + item + " -d shell getprop ro.product.brand") as f:
android_brand = f.read().strip()
with os.popen("adb -s " + item + " -d shell getprop ro.product.model") as f:
android_model = f.read().strip()
with os.popen("adb -s " + item + " -d shell getprop ro.build.version.release") as f:
android_version = f.read().strip()
device_mes = {
"手机品牌": android_brand,
"手机型号": android_model,
"安卓版本": android_version
}
device_mes = json.dumps(device_mes, ensure_ascii=False)
device_info = item + '\n' + device_mes
self.select_device_list.append(tk.StringVar())
cb = Checkbutton(self.root, text=device_info, variable=self.select_device_list[-1],
onvalue=item, offvalue='')
self.cb_list.append(cb)
cb.grid(row=index, column=0, sticky='w')
cb.select()
def combobox_list(self):
theLabel = tk.Label(self.root, text='请选择WSA中你要启动的APP:')
self.cmb_list.append(theLabel)
theLabel.grid(row=len(self.device_list) + 1, sticky='w')
comvalue = tk.StringVar()
comboxlist = ttk.Combobox(self.root, textvariable=comvalue, state='readonly', values=self.package_3) # 初始化
self.cmb_list.append(comboxlist)
comboxlist.bind("<<ComboboxSelected>>", lambda _: os.system("adb -s 127.0.0.1 shell monkey -p " + str(comboxlist.get()) + ' 1')) # 绑定事件,(下拉列表框被选中时,绑定go()函数)
comboxlist.grid(row=len(self.device_list) + 1)
def select_path(self):
# 选择文件
file_path = filedialog.askopenfilename()
self.path.set(file_path)
return file_path
# 刷新设备列表
def refresh_data(self):
print("重新获取设备信息")
for item in self.cb_list:
item.destroy()
self.cb_list = []
self.device_list = self.get_device_list()
self.select_device_list = []
self.mul_check_box()
for item in self.cmb_list:
item.destroy()
self.cmb_list = []
self.package_3 = self.show_package()
self.combobox_list()
def path_button(self):
Entry(self.root, textvariable=self.path, width=65).grid(row=len(self.device_list) + 2, column=0)
Button(self.root, text="选择文件", command=self.select_path).grid(row=len(self.device_list) + 2, column=1)
def install_button(self):
button_install = Button(self.root, text="安装APP", command=self.install)
button_install.grid(row=len(self.device_list) + 2, column=2)
def refresh_button(self):
button_install = Button(self.root, text="刷新设备", command=self.refresh_data)
button_install.grid(row=len(self.device_list) + 2, column=3)
def activity_button(self):
Button(self.root, text="获取页面", command=self.get_activity).grid(row=len(self.device_list) + 3, column=1)
def clear_button(self):
button_clear = Button(self.root, text="清除数据", command=self.clear)
button_clear.grid(row=len(self.device_list) + 3, column=2)
def kill_button(self):
button_kill = Button(self.root, text="杀掉应用", command=self.kill)
button_kill.grid(row=len(self.device_list) + 3, column=3)
def check_button(self):
button_check = Button(self.root, text="查看进程", command=self.check_process)
button_check.grid(row=len(self.device_list) + 3, column=4)
def package_button(self):
button_check = Button(self.root, text="断开/重连", command=self.outline_or_online)
button_check.grid(row=len(self.device_list) + 2, column=5)
def cache_button(self):
button_gdt = Button(self.root, text="清除日志", command=self.cache)
button_gdt.grid(row=len(self.device_list) + 2, column=4)
def uninstall_button(self):
button_gdt = Button(self.root, text="一键卸载", command=self.uninstall)
button_gdt.grid(row=len(self.device_list) + 3, column=5)
def input_text(self):
entry_log = Entry(self.root, width=65, textvariable=self.path)
entry_log.grid(row=len(self.device_list) + 2, column=0, sticky='w')
def input_package(self):
package_log = Entry(self.root, width=65, textvariable=self.package_name)
package_log.grid(row=len(self.device_list) + 3, column=0, sticky='w')
def get_apk_name(self):
return self.path.get()
def get_package_name(self):
return self.package_name.get()
def devices_list(self):
selected_device_list = [i.get() for i in self.select_device_list if i.get()]
print(selected_device_list)
return selected_device_list
def mainloop(self):
self.root.mainloop()
def install(self):
apk_name = self.get_apk_name()
print(apk_name)
for device in self.devices_list():
threading.Thread(target=self.install_and_open, args=(device, apk_name)).start()
def clear(self):
package_name = self.get_package_name()
for device in self.devices_list():
threading.Thread(target=self.clear_data, args=(device, package_name)).start()
def kill(self):
package_name = self.get_package_name()
for device in self.devices_list():
threading.Thread(target=self.kill_process, args=(device, package_name)).start()
def cache(self):
for device in self.devices_list():
threading.Thread(target=self.clear_log_cache, args=(device,)).start()
def uninstall(self):
package_name = self.get_package_name()
for device in self.devices_list():
threading.Thread(target=self.uninstall_all, args=(device, package_name)).start()
if __name__ == '__main__':
apkTk = APKTk()
apkTk.mul_check_box()
apkTk.combobox_list()
apkTk.path_button()
apkTk.input_package()
apkTk.install_button()
apkTk.refresh_button()
apkTk.activity_button()
apkTk.clear_button()
apkTk.kill_button()
apkTk.check_button()
apkTk.package_button()
apkTk.cache_button()
apkTk.uninstall_button()
apkTk.root.mainloop()