lizh1 commited on
Commit
0838626
·
verified ·
1 Parent(s): 5fa4751

Upload adb_client.py

Browse files
Files changed (1) hide show
  1. resources/android/adb_client.py +152 -0
resources/android/adb_client.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import time
4
+ from flask import Flask, request, jsonify
5
+
6
+
7
+ def list_all_devices():
8
+ adb_command = "adb devices"
9
+ device_list = []
10
+ result = EmulatorController.execute_adb(adb_command)
11
+ if result != "ERROR":
12
+ devices = result.split("\n")[1:]
13
+ for d in devices:
14
+ device_list.append(d.split()[0])
15
+
16
+ return device_list
17
+
18
+ def get_adb_device_name(avd_name=None):
19
+ device_list = list_all_devices()
20
+ for device in device_list:
21
+ command = f"adb -s {device} emu avd name"
22
+ ret = EmulatorController.execute_adb(command)
23
+ ret = ret.split("\n")[0]
24
+ if ret == avd_name:
25
+ return device
26
+ return None
27
+
28
+
29
+ app = Flask(__name__)
30
+
31
+ class Config:
32
+ avd_log_dir = "/logs" # 请根据实际路径进行修改
33
+
34
+ class EmulatorController:
35
+ def __init__(self):
36
+ self.avd_log_dir = "logs"
37
+ self.emulator_process = None
38
+ self.out_file = None
39
+
40
+ @classmethod
41
+ def execute_adb(self, adb_command):
42
+ print(f"Executing command: {adb_command}")
43
+ assert adb_command.startswith("adb"), "Command must start with 'adb'"
44
+ adb_command = "/root/.android/platform-tools/adb" + adb_command[3:]
45
+
46
+ result = subprocess.run(adb_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
47
+ print(f"Return code: {result}")
48
+ if result.returncode == 0:
49
+ return result.stdout.strip()
50
+ print(f"Command execution failed: {adb_command}")
51
+ print(result.stderr)
52
+ return "ERROR"
53
+
54
+ def start_emulator(self, avd_name):
55
+ print(f"Starting Android Emulator with AVD name: {avd_name}")
56
+
57
+ if not os.path.exists(self.avd_log_dir):
58
+ os.makedirs(self.avd_log_dir, exist_ok=True)
59
+
60
+ self.out_file = open(os.path.join(self.avd_log_dir, 'emulator_output.txt'), 'a')
61
+
62
+ self.emulator_process = subprocess.Popen(
63
+ ["/root/.android/emulator/emulator", "-avd", avd_name, "-no-snapshot-save", "-no-window", "-no-audio"],
64
+ stdout=self.out_file,
65
+ stderr=self.out_file
66
+ )
67
+
68
+ print("Waiting for the emulator to start...")
69
+
70
+ while True:
71
+ time.sleep(1)
72
+ try:
73
+ device = get_adb_device_name(avd_name)
74
+ except:
75
+ import traceback
76
+ traceback.print_exc()
77
+ continue
78
+ if device is not None:
79
+ break
80
+
81
+ print("Device name: ", device)
82
+ print("AVD name: ", avd_name)
83
+
84
+ while True:
85
+ boot_complete = f"adb -s {device} shell getprop init.svc.bootanim"
86
+ boot_complete = self.execute_adb(boot_complete)
87
+ if boot_complete == 'stopped':
88
+ print("Emulator started successfully")
89
+ break
90
+ time.sleep(1)
91
+
92
+ time.sleep(1)
93
+
94
+ device_list = list_all_devices()
95
+ if len(device_list) == 1:
96
+ device = device_list[0]
97
+ print(f"Device selected: {device}")
98
+
99
+ return device
100
+
101
+ def stop_emulator(self, avd_name):
102
+ print("Stopping Android Emulator...")
103
+ if self.emulator_process:
104
+ self.emulator_process.terminate()
105
+
106
+ while True:
107
+ try:
108
+ device = get_adb_device_name(avd_name)
109
+ command = f"adb -s {device} reboot -p"
110
+ ret = self.execute_adb(command)
111
+ self.emulator_process.terminate()
112
+ except:
113
+ device = None
114
+ if device is None:
115
+ print("Emulator stopped successfully")
116
+ break
117
+ time.sleep(1)
118
+
119
+ if self.out_file:
120
+ self.out_file.close()
121
+
122
+ emulator_controller = EmulatorController()
123
+
124
+ @app.route('/start', methods=['POST'])
125
+ def start():
126
+ avd_name = request.json.get('avd_name')
127
+ if not avd_name:
128
+ return jsonify({"error": "No AVD name provided"}), 400
129
+
130
+ device = emulator_controller.start_emulator(avd_name)
131
+ return jsonify({"result": "Emulator started", "device": device})
132
+
133
+ @app.route('/stop', methods=['POST'])
134
+ def stop():
135
+ avd_name = request.json.get('avd_name')
136
+ if not avd_name:
137
+ return jsonify({"error": "No AVD name provided"}), 400
138
+
139
+ emulator_controller.stop_emulator(avd_name)
140
+ return jsonify({"result": "Emulator stopped"})
141
+
142
+ @app.route('/execute', methods=['POST'])
143
+ def execute():
144
+ adb_command = request.json.get('command')
145
+ if not adb_command:
146
+ return jsonify({"error": "No command provided"}), 400
147
+
148
+ result = emulator_controller.execute_adb(adb_command)
149
+ return jsonify({"result": result})
150
+
151
+ if __name__ == '__main__':
152
+ app.run(host='0.0.0.0', port=6060)