2
2
3
3
//<editor-fold defaultstate="collapsed" desc=" imports ">
4
4
import java .io .*;
5
+ import java .nio .file .Paths ;
5
6
import java .util .*;
6
7
import javax .swing .*;
7
8
import java .util .regex .*;
@@ -20,10 +21,11 @@ public class GUI_functions {
20
21
21
22
private static String adb = "adb" ;
22
23
private static String scrcpy = "scrcpy" ;
23
-
24
+
24
25
/**
25
26
* 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
+ * Execute FileInputStream and catch error if there is no adb/scrcpy exe
28
+ * file.
27
29
*
28
30
* @param errorHandler interface PopupHandler
29
31
* @return {@code List<String> output}
@@ -35,20 +37,20 @@ public static int checkAdb_Scrcpy_InFolder(PopupHandler errorHandler) {
35
37
return 1 ;
36
38
}
37
39
40
+ // Check for adb and scrcpy executables in the folder
41
+ String [] programs = {"adb.exe" , "scrcpy.exe" };
38
42
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 ());
43
+
44
+ for (String program : programs ) {
45
+ File programFile = Paths .get (program ).toFile ();
46
+ if (!programFile .exists ()) {
47
+ error .append ("\n " ).append (program ).append (" not found in folder." );
48
+ }
44
49
}
45
50
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 );
51
+ // If error occurred, show the error and return -1
52
+ if (error .length () > 0 ) {
53
+ errorHandler .showError ("Programs not found in folder, cannot run program: " + error );
52
54
return -1 ;
53
55
}
54
56
@@ -59,52 +61,49 @@ public static int checkAdb_Scrcpy_InFolder(PopupHandler errorHandler) {
59
61
60
62
/**
61
63
* Method to check if there is adb and scrcpy in environment variable.
62
- * Execute {@code adb} and {@code scrcpy} command using ProcessBuilder.
63
64
*
64
65
* @param errorHandler interface PopupHandler
65
66
* @return {@code List<String> output}
66
67
* @author opelooo
67
68
*/
68
69
private static int checkAdb_Scrcpy_InEnvironment (PopupHandler errorHandler ) {
69
70
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
71
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 );
72
+ // Check adb and scrcpy in the environment
73
+ if (!checkProgramInEnvironment (adb , error ) || !checkProgramInEnvironment (scrcpy , error )) {
74
+ errorHandler .showError ("Cannot find adb or scrcpy in environment!\n " + error );
103
75
return -1 ;
104
76
}
77
+
105
78
return 1 ;
106
79
}
107
80
81
+ /**
82
+ * Helper method to check if a program is available in the environment.
83
+ * Execute {@code adb --version} and {@code scrcpy --version} command using ProcessBuilder.
84
+ *
85
+ * @param errorHandler interface PopupHandler
86
+ * @return {@code List<String> output}
87
+ * @author opelooo
88
+ */
89
+ private static boolean checkProgramInEnvironment (String program , StringBuilder error ) {
90
+ try {
91
+ ProcessBuilder pb = new ProcessBuilder (program , "--version" ); // Run the version command to check
92
+ Process process = pb .start ();
93
+ int exitCode = process .waitFor (); // Wait for the process to finish and get the exit code
94
+
95
+ if (exitCode != 0 ) {
96
+ error .append (program ).append (" returned non-zero exit code: " ).append (exitCode ).append ("\n " );
97
+ return false ;
98
+ }
99
+
100
+ } catch (IOException | InterruptedException e ) {
101
+ error .append ("Error checking " ).append (program ).append (": " ).append (e .getMessage ()).append ("\n " );
102
+ return false ;
103
+ }
104
+ return true ;
105
+ }
106
+
108
107
/**
109
108
* Method to list devices connected to the computer, this method execute
110
109
* {@code adb devices} command using ProcessBuilder.
@@ -202,8 +201,7 @@ public static String adb_device_info(PopupHandler errorHandler, String device_co
202
201
String output = new String ();
203
202
try {
204
203
// Create a process
205
- ProcessBuilder pb
206
- = new ProcessBuilder (
204
+ ProcessBuilder pb = new ProcessBuilder (
207
205
adb , "-s" , device_code , "shell" ,
208
206
"getprop" , "ro.product.manufacturer"
209
207
);
@@ -241,8 +239,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
241
239
String device_ip_addr = new String ();
242
240
try {
243
241
// Create a process to execute 'adb devices'
244
- ProcessBuilder pb
245
- = new ProcessBuilder (
242
+ ProcessBuilder pb = new ProcessBuilder (
246
243
adb , "-s" , device_code , "shell" ,
247
244
"ip" , "route"
248
245
);
@@ -267,7 +264,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
267
264
// Wait for the process to complete
268
265
process .waitFor ();
269
266
} catch (IOException | InterruptedException | NullPointerException e ) {
270
-
267
+ errorHandler . showError ( "Exception occurred: " + e . getMessage ());
271
268
}
272
269
return device_ip_addr ;
273
270
}
0 commit comments