7
7
import java .util .regex .*;
8
8
import java .util .stream .Stream ;
9
9
import java .util .concurrent .TimeUnit ;
10
+ import java .util .concurrent .atomic .AtomicInteger ;
11
+ import java .util .logging .Level ;
12
+ import java .util .logging .Logger ;
10
13
//</editor-fold>
11
14
12
15
/**
15
18
*/
16
19
public class GUI_functions {
17
20
21
+ private static String adb = "adb" ;
22
+ private static String scrcpy = "scrcpy" ;
23
+
24
+ /**
25
+ * Method to check if there is adb and scrcpy in folder where .jar at.
26
+ * Execute FileInputStream and catch error if there is no adb/scrcpy exe file.
27
+ *
28
+ * @param errorHandler interface PopupHandler
29
+ * @return {@code List<String> output}
30
+ * @author opelooo
31
+ */
32
+ public static int checkAdb_Scrcpy_InFolder (PopupHandler errorHandler ) {
33
+ // check adb and scrcpy in environment variable
34
+ if (checkAdb_Scrcpy_InEnvironment (errorHandler ) == 1 ) {
35
+ return 1 ;
36
+ }
37
+
38
+ StringBuilder error = new StringBuilder ();
39
+ // check adb exe in folder
40
+ try {
41
+ new FileInputStream ("/adb.exe" );
42
+ } catch (FileNotFoundException ex ) {
43
+ error .append ("\n " ).append (ex .getMessage ());
44
+ }
45
+
46
+ // check scrcpy exe in folder
47
+ try {
48
+ new FileInputStream ("/scrcpy.exe" );
49
+ } catch (FileNotFoundException ex ) {
50
+ error .append ("\n " ).append (ex .getMessage ());
51
+ errorHandler .showError ("Program not found in folder, can not run program: " + error );
52
+ return -1 ;
53
+ }
54
+
55
+ GUI_functions .adb = "adb.exe" ;
56
+ GUI_functions .scrcpy = "scrcpy.exe" ;
57
+ return 1 ;
58
+ }
59
+
60
+ /**
61
+ * Method to check if there is adb and scrcpy in environment variable.
62
+ * Execute {@code adb} and {@code scrcpy} command using ProcessBuilder.
63
+ *
64
+ * @param errorHandler interface PopupHandler
65
+ * @return {@code List<String> output}
66
+ * @author opelooo
67
+ */
68
+ private static int checkAdb_Scrcpy_InEnvironment (PopupHandler errorHandler ) {
69
+ StringBuilder error = new StringBuilder ();
70
+ AtomicInteger status = new AtomicInteger (1 );
71
+
72
+ // Create a new thread to check for adb and scrcpy
73
+ Thread checkThread = new Thread (() -> {
74
+ try {
75
+ ProcessBuilder pb = new ProcessBuilder (adb );
76
+ pb .start ();
77
+ } catch (Exception e ) {
78
+ status .set (-1 );
79
+ error .append (e .getMessage ());
80
+ }
81
+
82
+ try {
83
+ ProcessBuilder pb = new ProcessBuilder (scrcpy );
84
+ pb .start ();
85
+ } catch (Exception e ) {
86
+ status .set (-1 );
87
+ error .append ("\n " ).append (e .getMessage ());
88
+ }
89
+ });
90
+ // Start the thread
91
+ checkThread .start ();
92
+ // wait to finish
93
+ try {
94
+ checkThread .join (); // This will wait for the thread to finish
95
+ } catch (InterruptedException e ) {
96
+ // Handle interruption
97
+ errorHandler .showError ("Thread was interrupted." );
98
+ checkThread .interrupt ();
99
+ return -1 ;
100
+ }
101
+ if (status .get () == -1 ) {
102
+ errorHandler .showError ("Can not found adb and scrcpy in environment!\n " + error );
103
+ return -1 ;
104
+ }
105
+ return 1 ;
106
+ }
107
+
18
108
/**
19
109
* Method to list devices connected to the computer, this method execute
20
110
* {@code adb devices} command using ProcessBuilder.
@@ -27,7 +117,7 @@ public static List<String> adb_devices(PopupHandler errorHandler) {
27
117
List <String > output = new ArrayList <>();
28
118
try {
29
119
// Create a process to execute 'adb devices'
30
- ProcessBuilder pb = new ProcessBuilder (" adb" , "devices" );
120
+ ProcessBuilder pb = new ProcessBuilder (adb , "devices" );
31
121
Process process = pb .start ();
32
122
33
123
BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
@@ -70,7 +160,7 @@ public static void run_scrcpy(PopupHandler errorHandler,
70
160
try {
71
161
// creating list of process
72
162
List <String > list = new ArrayList <>(Arrays .asList (
73
- " scrcpy" , "-s" , device_code , String .format ("-m %s" , maxSize )
163
+ scrcpy , "-s" , device_code , String .format ("-m %s" , maxSize )
74
164
));
75
165
76
166
Stream .of (
@@ -79,7 +169,7 @@ public static void run_scrcpy(PopupHandler errorHandler,
79
169
stayAwake ? "--stay-awake" : null ,
80
170
!bitRate .isEmpty () ? String .format ("-b %s" , bitRate ) : null
81
171
).filter (Objects ::nonNull ).forEach (list ::add );
82
-
172
+
83
173
// Create a process
84
174
ProcessBuilder pb = new ProcessBuilder (list );
85
175
pb .start ();
@@ -114,7 +204,7 @@ public static String adb_device_info(PopupHandler errorHandler, String device_co
114
204
// Create a process
115
205
ProcessBuilder pb
116
206
= new ProcessBuilder (
117
- " adb" , "-s" , device_code , "shell" ,
207
+ adb , "-s" , device_code , "shell" ,
118
208
"getprop" , "ro.product.manufacturer"
119
209
);
120
210
Process process = pb .start ();
@@ -153,7 +243,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
153
243
// Create a process to execute 'adb devices'
154
244
ProcessBuilder pb
155
245
= new ProcessBuilder (
156
- " adb" , "-s" , device_code , "shell" ,
246
+ adb , "-s" , device_code , "shell" ,
157
247
"ip" , "route"
158
248
);
159
249
Process process = pb .start ();
@@ -177,7 +267,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
177
267
// Wait for the process to complete
178
268
process .waitFor ();
179
269
} catch (IOException | InterruptedException | NullPointerException e ) {
180
-
270
+
181
271
}
182
272
return device_ip_addr ;
183
273
}
@@ -189,13 +279,13 @@ public static void adb_connect_tcpip(PopupHandler errorHandler, String device_co
189
279
new Thread (() -> {
190
280
try {
191
281
List <String > adbTcpIpMode = new ArrayList <>(Arrays .asList (
192
- " adb" , "-s" , device_code , "tcpip" , "5555"
282
+ adb , "-s" , device_code , "tcpip" , "5555"
193
283
));
194
284
List <String > adbConnectDevice = new ArrayList <>(Arrays .asList (
195
- " adb" , "connect" , String .format ("%s:5555" , deviceIP )
285
+ adb , "connect" , String .format ("%s:5555" , deviceIP )
196
286
));
197
287
List <String > scrcpyTcpIp = new ArrayList <>(Arrays .asList (
198
- " scrcpy" , String .format ("--tcpip=%s:5555" , deviceIP )
288
+ scrcpy , String .format ("--tcpip=%s:5555" , deviceIP )
199
289
));
200
290
201
291
// Create a process
@@ -221,5 +311,4 @@ public static void adb_connect_tcpip(PopupHandler errorHandler, String device_co
221
311
}).start ();
222
312
}
223
313
224
-
225
314
}
0 commit comments