Skip to content

Commit c64933c

Browse files
committed
Update v2.5.0 search adb and scrcpy
search for adb and scrcpy in environment and in folder where the .jar/.exe in
1 parent 957959e commit c64933c

File tree

4 files changed

+236
-139
lines changed

4 files changed

+236
-139
lines changed

src/com/opelooo/scrcpyGUI/GUI_functions.java

+99-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.util.regex.*;
88
import java.util.stream.Stream;
99
import java.util.concurrent.TimeUnit;
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
import java.util.logging.Level;
12+
import java.util.logging.Logger;
1013
//</editor-fold>
1114

1215
/**
@@ -15,6 +18,93 @@
1518
*/
1619
public class GUI_functions {
1720

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+
18108
/**
19109
* Method to list devices connected to the computer, this method execute
20110
* {@code adb devices} command using ProcessBuilder.
@@ -27,7 +117,7 @@ public static List<String> adb_devices(PopupHandler errorHandler) {
27117
List<String> output = new ArrayList<>();
28118
try {
29119
// Create a process to execute 'adb devices'
30-
ProcessBuilder pb = new ProcessBuilder("adb", "devices");
120+
ProcessBuilder pb = new ProcessBuilder(adb, "devices");
31121
Process process = pb.start();
32122

33123
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
@@ -70,7 +160,7 @@ public static void run_scrcpy(PopupHandler errorHandler,
70160
try {
71161
// creating list of process
72162
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)
74164
));
75165

76166
Stream.of(
@@ -79,7 +169,7 @@ public static void run_scrcpy(PopupHandler errorHandler,
79169
stayAwake ? "--stay-awake" : null,
80170
!bitRate.isEmpty() ? String.format("-b %s", bitRate) : null
81171
).filter(Objects::nonNull).forEach(list::add);
82-
172+
83173
// Create a process
84174
ProcessBuilder pb = new ProcessBuilder(list);
85175
pb.start();
@@ -114,7 +204,7 @@ public static String adb_device_info(PopupHandler errorHandler, String device_co
114204
// Create a process
115205
ProcessBuilder pb
116206
= new ProcessBuilder(
117-
"adb", "-s", device_code, "shell",
207+
adb, "-s", device_code, "shell",
118208
"getprop", "ro.product.manufacturer"
119209
);
120210
Process process = pb.start();
@@ -153,7 +243,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
153243
// Create a process to execute 'adb devices'
154244
ProcessBuilder pb
155245
= new ProcessBuilder(
156-
"adb", "-s", device_code, "shell",
246+
adb, "-s", device_code, "shell",
157247
"ip", "route"
158248
);
159249
Process process = pb.start();
@@ -177,7 +267,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
177267
// Wait for the process to complete
178268
process.waitFor();
179269
} catch (IOException | InterruptedException | NullPointerException e) {
180-
270+
181271
}
182272
return device_ip_addr;
183273
}
@@ -189,13 +279,13 @@ public static void adb_connect_tcpip(PopupHandler errorHandler, String device_co
189279
new Thread(() -> {
190280
try {
191281
List<String> adbTcpIpMode = new ArrayList<>(Arrays.asList(
192-
"adb", "-s", device_code, "tcpip", "5555"
282+
adb, "-s", device_code, "tcpip", "5555"
193283
));
194284
List<String> adbConnectDevice = new ArrayList<>(Arrays.asList(
195-
"adb", "connect", String.format("%s:5555", deviceIP)
285+
adb, "connect", String.format("%s:5555", deviceIP)
196286
));
197287
List<String> scrcpyTcpIp = new ArrayList<>(Arrays.asList(
198-
"scrcpy", String.format("--tcpip=%s:5555", deviceIP)
288+
scrcpy, String.format("--tcpip=%s:5555", deviceIP)
199289
));
200290

201291
// Create a process
@@ -221,5 +311,4 @@ public static void adb_connect_tcpip(PopupHandler errorHandler, String device_co
221311
}).start();
222312
}
223313

224-
225314
}

src/com/opelooo/scrcpyGUI/PopupHandler.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
*
7-
* @author Administrator
7+
* @author opelooo
88
*/
99
public interface PopupHandler {
1010

@@ -15,6 +15,13 @@ public interface PopupHandler {
1515
*/
1616
void showError(String errorMessage);
1717

18+
/**
19+
* Method to show popup message containing error happening
20+
*
21+
* @param infoMessage String containing information message
22+
*/
23+
void showInfo(String infoMessage);
24+
1825
/**
1926
* Method to show popup message containing device info
2027
*

src/com/opelooo/scrcpyGUI/scrcpy_main_panel.form

+40-43
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@
2929
<Layout>
3030
<DimensionLayout dim="0">
3131
<Group type="103" groupAlignment="0" attributes="0">
32-
<Component id="jPanel2" alignment="1" max="32767" attributes="0"/>
32+
<Component id="mainPanel" alignment="1" max="32767" attributes="0"/>
3333
</Group>
3434
</DimensionLayout>
3535
<DimensionLayout dim="1">
3636
<Group type="103" groupAlignment="0" attributes="0">
37-
<Component id="jPanel2" alignment="0" max="32767" attributes="0"/>
37+
<Component id="mainPanel" alignment="0" max="32767" attributes="0"/>
3838
</Group>
3939
</DimensionLayout>
4040
</Layout>
4141
<SubComponents>
42-
<Container class="javax.swing.JPanel" name="jPanel2">
42+
<Container class="javax.swing.JPanel" name="mainPanel">
4343

4444
<Layout>
4545
<DimensionLayout dim="0">
4646
<Group type="103" groupAlignment="0" attributes="0">
4747
<Group type="102" alignment="0" attributes="0">
4848
<EmptySpace max="-2" attributes="0"/>
49-
<Component id="jScrollPane1" min="-2" pref="262" max="-2" attributes="0"/>
49+
<Component id="deviceCodeList" min="-2" pref="262" max="-2" attributes="0"/>
5050
<EmptySpace max="-2" attributes="0"/>
51-
<Component id="jPanel4" min="-2" max="-2" attributes="0"/>
51+
<Component id="buttonMenuPanel" min="-2" max="-2" attributes="0"/>
52+
<EmptySpace max="-2" attributes="0"/>
53+
<Component id="optionMenuPanel" max="32767" attributes="0"/>
5254
<EmptySpace max="-2" attributes="0"/>
53-
<Component id="jPanel3" min="-2" max="-2" attributes="0"/>
54-
<EmptySpace max="32767" attributes="0"/>
5555
</Group>
5656
</Group>
5757
</DimensionLayout>
@@ -60,17 +60,17 @@
6060
<Group type="102" alignment="0" attributes="0">
6161
<EmptySpace max="-2" attributes="0"/>
6262
<Group type="103" groupAlignment="0" attributes="0">
63-
<Component id="jPanel3" alignment="1" max="32767" attributes="0"/>
64-
<Component id="jPanel4" alignment="0" max="32767" attributes="0"/>
65-
<Component id="jScrollPane1" alignment="0" pref="296" max="32767" attributes="0"/>
63+
<Component id="optionMenuPanel" alignment="1" max="32767" attributes="0"/>
64+
<Component id="buttonMenuPanel" alignment="0" max="32767" attributes="0"/>
65+
<Component id="deviceCodeList" alignment="0" max="32767" attributes="0"/>
6666
</Group>
6767
<EmptySpace max="-2" attributes="0"/>
6868
</Group>
6969
</Group>
7070
</DimensionLayout>
7171
</Layout>
7272
<SubComponents>
73-
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
73+
<Container class="javax.swing.JScrollPane" name="deviceCodeList">
7474
<AuxValues>
7575
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
7676
</AuxValues>
@@ -95,50 +95,47 @@
9595
</Component>
9696
</SubComponents>
9797
</Container>
98-
<Container class="javax.swing.JPanel" name="jPanel3">
98+
<Container class="javax.swing.JPanel" name="optionMenuPanel">
9999

100100
<Layout>
101101
<DimensionLayout dim="0">
102102
<Group type="103" groupAlignment="0" attributes="0">
103-
<Group type="102" alignment="0" attributes="0">
103+
<Group type="102" attributes="0">
104104
<EmptySpace max="-2" attributes="0"/>
105-
<Group type="103" groupAlignment="0" attributes="0">
106-
<Component id="toggleVideoButton" max="32767" attributes="0"/>
105+
<Group type="103" groupAlignment="0" max="-2" attributes="0">
107106
<Group type="102" alignment="0" attributes="0">
108-
<Component id="jLabel1" min="-2" pref="100" max="-2" attributes="0"/>
107+
<Component id="recordAudioButton" min="-2" max="-2" attributes="0"/>
109108
<EmptySpace max="-2" attributes="0"/>
110-
<Group type="103" groupAlignment="0" attributes="0">
111-
<Component id="bitRateSlider" pref="0" max="32767" attributes="0"/>
112-
<Component id="videoBitRate" max="32767" attributes="0"/>
113-
</Group>
109+
<Component id="audioFormats" max="32767" attributes="0"/>
114110
</Group>
115-
<Group type="102" alignment="1" attributes="0">
111+
<Group type="102" alignment="0" attributes="0">
116112
<Component id="recordVideoButton" min="-2" pref="121" max="-2" attributes="0"/>
117113
<EmptySpace max="-2" attributes="0"/>
118114
<Component id="videoFormats" max="32767" attributes="0"/>
119115
</Group>
120116
<Group type="102" alignment="0" attributes="0">
121-
<Component id="recordAudioButton" min="-2" max="-2" attributes="0"/>
122-
<EmptySpace max="-2" attributes="0"/>
123-
<Component id="audioFormats" max="32767" attributes="0"/>
117+
<Component id="jLabel1" min="-2" pref="100" max="-2" attributes="0"/>
118+
<EmptySpace type="unrelated" max="-2" attributes="0"/>
119+
<Group type="103" groupAlignment="1" attributes="0">
120+
<Component id="videoBitRate" min="-2" pref="90" max="-2" attributes="0"/>
121+
<Component id="bitRateSlider" min="-2" pref="90" max="-2" attributes="0"/>
122+
</Group>
124123
</Group>
125-
<Group type="102" attributes="0">
126-
<Group type="103" groupAlignment="0" attributes="0">
127-
<Group type="103" groupAlignment="0" max="-2" attributes="0">
128-
<Component id="currDeviceCode" alignment="0" min="-2" max="-2" attributes="0"/>
129-
<Component id="toggleScreenButton" alignment="0" pref="202" max="32767" attributes="0"/>
130-
<Component id="toggleStayAwake" alignment="0" max="32767" attributes="0"/>
131-
</Group>
132-
<Group type="102" alignment="0" attributes="0">
133-
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
134-
<EmptySpace min="-2" pref="7" max="-2" attributes="0"/>
135-
<Component id="maxSize" min="-2" pref="95" max="-2" attributes="0"/>
136-
</Group>
124+
<Component id="toggleVideoButton" alignment="0" max="32767" attributes="0"/>
125+
<Group type="103" alignment="1" groupAlignment="0" attributes="0">
126+
<Component id="currDeviceCode" min="-2" max="-2" attributes="0"/>
127+
<Group type="103" alignment="0" groupAlignment="0" max="-2" attributes="0">
128+
<Component id="toggleScreenButton" alignment="0" pref="202" max="32767" attributes="0"/>
129+
<Component id="toggleStayAwake" alignment="0" max="32767" attributes="0"/>
130+
</Group>
131+
<Group type="102" alignment="0" attributes="0">
132+
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
133+
<EmptySpace type="unrelated" max="-2" attributes="0"/>
134+
<Component id="maxSize" min="-2" pref="90" max="-2" attributes="0"/>
137135
</Group>
138-
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
139136
</Group>
140137
</Group>
141-
<EmptySpace max="-2" attributes="0"/>
138+
<EmptySpace pref="10" max="32767" attributes="0"/>
142139
</Group>
143140
</Group>
144141
</DimensionLayout>
@@ -149,14 +146,14 @@
149146
<Component id="currDeviceCode" min="-2" max="-2" attributes="0"/>
150147
<EmptySpace min="-2" pref="17" max="-2" attributes="0"/>
151148
<Group type="103" groupAlignment="0" attributes="0">
152-
<Group type="102" alignment="0" attributes="0">
149+
<Group type="102" attributes="0">
153150
<Component id="bitRateSlider" min="-2" max="-2" attributes="0"/>
154151
<EmptySpace max="-2" attributes="0"/>
155152
<Component id="videoBitRate" min="-2" max="-2" attributes="0"/>
156153
</Group>
157-
<Group type="102" alignment="1" attributes="0">
154+
<Group type="102" alignment="0" attributes="0">
155+
<EmptySpace min="-2" pref="15" max="-2" attributes="0"/>
158156
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
159-
<EmptySpace min="-2" pref="16" max="-2" attributes="0"/>
160157
</Group>
161158
</Group>
162159
<EmptySpace type="unrelated" max="-2" attributes="0"/>
@@ -180,7 +177,7 @@
180177
<Component id="recordAudioButton" alignment="3" min="-2" max="-2" attributes="0"/>
181178
<Component id="audioFormats" alignment="3" min="-2" max="-2" attributes="0"/>
182179
</Group>
183-
<EmptySpace max="32767" attributes="0"/>
180+
<EmptySpace pref="40" max="32767" attributes="0"/>
184181
</Group>
185182
</Group>
186183
</DimensionLayout>
@@ -287,7 +284,7 @@
287284
</Component>
288285
</SubComponents>
289286
</Container>
290-
<Container class="javax.swing.JPanel" name="jPanel4">
287+
<Container class="javax.swing.JPanel" name="buttonMenuPanel">
291288

292289
<Layout>
293290
<DimensionLayout dim="0">

0 commit comments

Comments
 (0)