Skip to content

Commit 5cf47db

Browse files
committed
Update new feature, error dialog, loading dialog
1 parent a976879 commit 5cf47db

File tree

4 files changed

+178
-85
lines changed

4 files changed

+178
-85
lines changed

src/com/opelooo/scrcpyGUI/GUI_functions.java

+89-46
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.opelooo.scrcpyGUI;
22

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>
1012

1113
/**
1214
*
@@ -18,10 +20,11 @@ public class GUI_functions {
1820
* Method to list devices connected to the computer, this method execute
1921
* {@code adb devices} command using ProcessBuilder.
2022
*
23+
* @param errorHandler
2124
* @return {@code List<String> output}
2225
* @author opelooo
2326
*/
24-
public static List<String> adb_devices() {
27+
public static List<String> adb_devices(PopupHandler errorHandler) {
2528
List<String> output = new ArrayList<>();
2629
try {
2730
// Create a process to execute 'adb devices'
@@ -40,17 +43,18 @@ public static List<String> adb_devices() {
4043

4144
// Wait for the process to complete
4245
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());
4548
}
46-
4749
return output;
4850
}
4951

5052
/**
5153
* Method to customize run scrcpy, this method execute
5254
* {@code scrcpy -s device_code -m 1024 -b 2M} command using ProcessBuilder.
5355
*
56+
* @param ParentFrame
57+
* @param errorHandler
5458
* @param device_code device ID from adb devices list
5559
* @param maxSize maximum size of mirroring panel
5660
* @param bitRate bit rate transfer for mirroring
@@ -60,36 +64,38 @@ public static List<String> adb_devices() {
6064
*
6165
* @author opelooo
6266
*/
63-
public static void run_scrcpy(
67+
public static void run_scrcpy(Component ParentFrame, PopupHandler errorHandler,
6468
String device_code, String maxSize, String bitRate,
6569
boolean videoOn, boolean screenOn, boolean stayAwake) {
70+
JDialog dialog = errorHandler.progressBarDialog();
6671
new Thread(() -> {
6772
try {
6873
// 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);
8584

8685
// Create a process
8786
ProcessBuilder pb = new ProcessBuilder(list);
88-
8987
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+
});
9399
}
94100
}).start(); // Start the new thread
95101
}
@@ -99,11 +105,12 @@ public static void run_scrcpy(
99105
* {@code adb -s device_code shell getprop ro.product.manufacturer} command
100106
* using ProcessBuilder.
101107
*
108+
* @param errorHandler
102109
* @param device_code device ID from adb devices list
103110
* @return {@code String output}
104111
* @author opelooo
105112
*/
106-
public static String adb_device_info(String device_code) {
113+
public static String adb_device_info(PopupHandler errorHandler, String device_code) {
107114
String output = new String();
108115
try {
109116
// Create a process
@@ -126,23 +133,23 @@ public static String adb_device_info(String device_code) {
126133

127134
// Wait for the process to complete
128135
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());
131138
}
132-
133139
return output;
134140
}
135-
141+
136142
/**
137143
* 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.
140146
*
147+
* @param errorHandler
141148
* @param device_code device ID from adb devices list
142149
* @return {@code String device_ip_addr}
143150
* @author opelooo
144151
*/
145-
public static String adb_get_device_ip(String device_code) {
152+
public static String adb_get_device_ip(PopupHandler errorHandler, String device_code) {
146153
String device_ip_addr = new String();
147154
try {
148155
// Create a process to execute 'adb devices'
@@ -157,7 +164,7 @@ public static String adb_get_device_ip(String device_code) {
157164

158165
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
159166
String line;
160-
167+
161168
while ((line = reader.readLine()) != null) {
162169
// Compile the pattern and create a matcher
163170
Pattern pattern = Pattern.compile(regex);
@@ -171,15 +178,51 @@ public static String adb_get_device_ip(String device_code) {
171178

172179
// Wait for the process to complete
173180
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());
176183
}
177-
178184
return device_ip_addr;
179185
}
180186

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();
183225
}
184226

227+
185228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.opelooo.scrcpyGUI;
2+
3+
import javax.swing.JDialog;
4+
5+
/**
6+
*
7+
* @author Administrator
8+
*/
9+
public interface PopupHandler {
10+
11+
void showError(String errorMessage);
12+
void showDeviceInfo(String message);
13+
JDialog progressBarDialog();
14+
}

src/com/opelooo/scrcpyGUI/scrcpy_main_panel.form

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<Property name="iconImages" type="java.util.List" editor="org.netbeans.modules.form.ComponentChooserEditor">
88
<ComponentRef name="null"/>
99
</Property>
10+
<Property name="name" type="java.lang.String" value="MainFrame" noResource="true"/>
1011
<Property name="resizable" type="boolean" value="false"/>
1112
</Properties>
1213
<SyntheticProperties>

0 commit comments

Comments
 (0)