1
1
package com .opelooo .scrcpyGUI ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .IOException ;
5
- import java .io .InputStreamReader ;
6
- import java .util .ArrayList ;
7
- import java .util .List ;
8
- import java .util .regex .Matcher ;
9
- import java .util .regex .Pattern ;
3
+ //<editor-fold defaultstate="collapsed" desc=" imports ">
4
+ import java .awt .Component ;
5
+ import java .io .*;
6
+ import java .util .*;
7
+ import javax .swing .*;
8
+ import java .util .regex .*;
9
+ import java .util .stream .Stream ;
10
+ import java .util .concurrent .TimeUnit ;
11
+ //</editor-fold>
10
12
11
13
/**
12
14
*
@@ -18,10 +20,11 @@ public class GUI_functions {
18
20
* Method to list devices connected to the computer, this method execute
19
21
* {@code adb devices} command using ProcessBuilder.
20
22
*
23
+ * @param errorHandler
21
24
* @return {@code List<String> output}
22
25
* @author opelooo
23
26
*/
24
- public static List <String > adb_devices () {
27
+ public static List <String > adb_devices (PopupHandler errorHandler ) {
25
28
List <String > output = new ArrayList <>();
26
29
try {
27
30
// Create a process to execute 'adb devices'
@@ -40,17 +43,18 @@ public static List<String> adb_devices() {
40
43
41
44
// Wait for the process to complete
42
45
process .waitFor ();
43
- } catch (IOException | InterruptedException e ) {
44
- e . printStackTrace (); // Handle exceptions properly
46
+ } catch (IOException | InterruptedException | NullPointerException e ) {
47
+ errorHandler . showError ( "Exception occurred: " + e . getMessage ());
45
48
}
46
-
47
49
return output ;
48
50
}
49
51
50
52
/**
51
53
* Method to customize run scrcpy, this method execute
52
54
* {@code scrcpy -s device_code -m 1024 -b 2M} command using ProcessBuilder.
53
55
*
56
+ * @param ParentFrame
57
+ * @param errorHandler
54
58
* @param device_code device ID from adb devices list
55
59
* @param maxSize maximum size of mirroring panel
56
60
* @param bitRate bit rate transfer for mirroring
@@ -60,36 +64,38 @@ public static List<String> adb_devices() {
60
64
*
61
65
* @author opelooo
62
66
*/
63
- public static void run_scrcpy (
67
+ public static void run_scrcpy (Component ParentFrame , PopupHandler errorHandler ,
64
68
String device_code , String maxSize , String bitRate ,
65
69
boolean videoOn , boolean screenOn , boolean stayAwake ) {
70
+ JDialog dialog = errorHandler .progressBarDialog ();
66
71
new Thread (() -> {
67
72
try {
68
73
// creating list of process
69
- List <String > list = new ArrayList <>();
70
- list .add ("scrcpy" );
71
- list .add ("-s" );
72
- list .add (device_code );
73
- list .add (String .format ("-m %s" , maxSize ));
74
- list .add (String .format ("-b %s" , bitRate ));
75
-
76
- if (!videoOn ) {
77
- list .add ("--no-video" );
78
- }
79
- if (!screenOn ) {
80
- list .add ("--turn-screen-off" );
81
- }
82
- if (stayAwake ) {
83
- list .add ("--stay-awake" );
84
- }
74
+ List <String > list = new ArrayList <>(Arrays .asList (
75
+ "scrcpy" , "-s" , device_code , String .format ("-m %s" , maxSize )
76
+ ));
77
+
78
+ Stream .of (
79
+ videoOn ? null : "--no-video" ,
80
+ screenOn ? null : "--turn-screen-off" ,
81
+ stayAwake ? "--stay-awake" : null ,
82
+ !bitRate .isEmpty () ? String .format ("-b %s" , bitRate ) : null
83
+ ).filter (Objects ::nonNull ).forEach (list ::add );
85
84
86
85
// Create a process
87
86
ProcessBuilder pb = new ProcessBuilder (list );
88
-
89
87
pb .start ();
90
- // Optionally, monitor the process if needed
91
- } catch (IOException e ) {
92
- e .printStackTrace (); // Handle exceptions properly
88
+
89
+ TimeUnit .MILLISECONDS .sleep (1500 );
90
+ SwingUtilities .invokeLater (() -> {
91
+ dialog .dispose (); // Close the dialog
92
+ });
93
+
94
+ } catch (IOException | NullPointerException | InterruptedException e ) {
95
+ SwingUtilities .invokeLater (() -> {
96
+ dialog .dispose (); // Close the dialog
97
+ errorHandler .showError ("Exception occurred: " + e .getMessage ());
98
+ });
93
99
}
94
100
}).start (); // Start the new thread
95
101
}
@@ -99,11 +105,12 @@ public static void run_scrcpy(
99
105
* {@code adb -s device_code shell getprop ro.product.manufacturer} command
100
106
* using ProcessBuilder.
101
107
*
108
+ * @param errorHandler
102
109
* @param device_code device ID from adb devices list
103
110
* @return {@code String output}
104
111
* @author opelooo
105
112
*/
106
- public static String adb_device_info (String device_code ) {
113
+ public static String adb_device_info (PopupHandler errorHandler , String device_code ) {
107
114
String output = new String ();
108
115
try {
109
116
// Create a process
@@ -126,23 +133,23 @@ public static String adb_device_info(String device_code) {
126
133
127
134
// Wait for the process to complete
128
135
process .waitFor ();
129
- } catch (IOException | InterruptedException e ) {
130
- e . printStackTrace (); // Handle exceptions properly
136
+ } catch (IOException | InterruptedException | NullPointerException e ) {
137
+ errorHandler . showError ( "Exception occurred: " + e . getMessage ());
131
138
}
132
-
133
139
return output ;
134
140
}
135
-
141
+
136
142
/**
137
143
* Method to get device info IP Address, this method execute
138
- * {@code adb -s device_code shell ip route} command
139
- * using ProcessBuilder. After that, the output filtered using regex.
144
+ * {@code adb -s device_code shell ip route} command using ProcessBuilder.
145
+ * After that, the output filtered using regex.
140
146
*
147
+ * @param errorHandler
141
148
* @param device_code device ID from adb devices list
142
149
* @return {@code String device_ip_addr}
143
150
* @author opelooo
144
151
*/
145
- public static String adb_get_device_ip (String device_code ) {
152
+ public static String adb_get_device_ip (PopupHandler errorHandler , String device_code ) {
146
153
String device_ip_addr = new String ();
147
154
try {
148
155
// Create a process to execute 'adb devices'
@@ -157,7 +164,7 @@ public static String adb_get_device_ip(String device_code) {
157
164
158
165
BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
159
166
String line ;
160
-
167
+
161
168
while ((line = reader .readLine ()) != null ) {
162
169
// Compile the pattern and create a matcher
163
170
Pattern pattern = Pattern .compile (regex );
@@ -171,15 +178,51 @@ public static String adb_get_device_ip(String device_code) {
171
178
172
179
// Wait for the process to complete
173
180
process .waitFor ();
174
- } catch (IOException | InterruptedException e ) {
175
- e . printStackTrace (); // Handle exceptions properly
181
+ } catch (IOException | InterruptedException | NullPointerException e ) {
182
+ errorHandler . showError ( "Exception occurred: " + e . getMessage ());
176
183
}
177
-
178
184
return device_ip_addr ;
179
185
}
180
186
181
- public static void adb_connect_tcpip (String device_code ) {
182
-
187
+ public static void adb_connect_tcpip (Component ParentFrame , PopupHandler errorHandler , String device_code ) {
188
+ JDialog dialog = errorHandler .progressBarDialog ();
189
+ String deviceIP = adb_get_device_ip (errorHandler , device_code );
190
+
191
+ new Thread (() -> {
192
+ try {
193
+ List <String > adbTcpIpMode = new ArrayList <>(Arrays .asList (
194
+ "adb" , "-s" , device_code , "tcpip" , "5555"
195
+ ));
196
+ List <String > adbConnectDevice = new ArrayList <>(Arrays .asList (
197
+ "adb" , "connect" , String .format ("%s:5555" , deviceIP )
198
+ ));
199
+ List <String > scrcpyTcpIp = new ArrayList <>(Arrays .asList (
200
+ "scrcpy" , String .format ("--tcpip=%s:5555" , deviceIP )
201
+ ));
202
+
203
+ // Create a process
204
+ ProcessBuilder pbAdbTcpIpMode = new ProcessBuilder (adbTcpIpMode );
205
+ pbAdbTcpIpMode .start ();
206
+
207
+ ProcessBuilder pbAdbConnectDevice = new ProcessBuilder (adbConnectDevice );
208
+ pbAdbConnectDevice .start ();
209
+
210
+ ProcessBuilder pbScrcpyTcpIp = new ProcessBuilder (scrcpyTcpIp );
211
+ pbScrcpyTcpIp .start ();
212
+
213
+ TimeUnit .MILLISECONDS .sleep (1500 );
214
+ SwingUtilities .invokeLater (() -> {
215
+ dialog .dispose (); // Close the dialog
216
+ });
217
+ } catch (IOException | InterruptedException | NullPointerException e ) {
218
+ SwingUtilities .invokeLater (() -> {
219
+ errorHandler .showError ("Exception occurred: " + e .getMessage ());
220
+ dialog .dispose (); // Close the dialog
221
+ });
222
+ }
223
+
224
+ }).start ();
183
225
}
184
226
227
+
185
228
}
0 commit comments