Skip to content

Commit 54bd60f

Browse files
committed
Update v2.5.1 fix bug
change log: - fix bug when only one device connected, scrcpy will open automaticaly - new way to looking for adb and scrcpy in folder and in environment - won't open when there is no adb and scrcpy found in both environment nor in folder - merge showDeviceInfo and showInfo
1 parent a72b607 commit 54bd60f

File tree

3 files changed

+81
-79
lines changed

3 files changed

+81
-79
lines changed

src/com/opelooo/scrcpyGUI/GUI_functions.java

+48-51
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//<editor-fold defaultstate="collapsed" desc=" imports ">
44
import java.io.*;
5+
import java.nio.file.Paths;
56
import java.util.*;
67
import javax.swing.*;
78
import java.util.regex.*;
@@ -20,10 +21,11 @@ public class GUI_functions {
2021

2122
private static String adb = "adb";
2223
private static String scrcpy = "scrcpy";
23-
24+
2425
/**
2526
* 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.
2729
*
2830
* @param errorHandler interface PopupHandler
2931
* @return {@code List<String> output}
@@ -35,20 +37,20 @@ public static int checkAdb_Scrcpy_InFolder(PopupHandler errorHandler) {
3537
return 1;
3638
}
3739

40+
// Check for adb and scrcpy executables in the folder
41+
String[] programs = {"adb.exe", "scrcpy.exe"};
3842
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+
}
4449
}
4550

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);
5254
return -1;
5355
}
5456

@@ -59,52 +61,49 @@ public static int checkAdb_Scrcpy_InFolder(PopupHandler errorHandler) {
5961

6062
/**
6163
* Method to check if there is adb and scrcpy in environment variable.
62-
* Execute {@code adb} and {@code scrcpy} command using ProcessBuilder.
6364
*
6465
* @param errorHandler interface PopupHandler
6566
* @return {@code List<String> output}
6667
* @author opelooo
6768
*/
6869
private static int checkAdb_Scrcpy_InEnvironment(PopupHandler errorHandler) {
6970
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-
}
8171

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);
10375
return -1;
10476
}
77+
10578
return 1;
10679
}
10780

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+
108107
/**
109108
* Method to list devices connected to the computer, this method execute
110109
* {@code adb devices} command using ProcessBuilder.
@@ -202,8 +201,7 @@ public static String adb_device_info(PopupHandler errorHandler, String device_co
202201
String output = new String();
203202
try {
204203
// Create a process
205-
ProcessBuilder pb
206-
= new ProcessBuilder(
204+
ProcessBuilder pb = new ProcessBuilder(
207205
adb, "-s", device_code, "shell",
208206
"getprop", "ro.product.manufacturer"
209207
);
@@ -241,8 +239,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
241239
String device_ip_addr = new String();
242240
try {
243241
// Create a process to execute 'adb devices'
244-
ProcessBuilder pb
245-
= new ProcessBuilder(
242+
ProcessBuilder pb = new ProcessBuilder(
246243
adb, "-s", device_code, "shell",
247244
"ip", "route"
248245
);
@@ -267,7 +264,7 @@ public static String adb_get_device_ip(PopupHandler errorHandler, String device_
267264
// Wait for the process to complete
268265
process.waitFor();
269266
} catch (IOException | InterruptedException | NullPointerException e) {
270-
267+
errorHandler.showError("Exception occurred: " + e.getMessage());
271268
}
272269
return device_ip_addr;
273270
}

src/com/opelooo/scrcpyGUI/PopupHandler.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ public interface PopupHandler {
1919
* Method to show popup message containing error happening
2020
*
2121
* @param infoMessage String containing information message
22+
* @param typeOfInfo contain title showing type of information
2223
*/
23-
void showInfo(String infoMessage);
24-
25-
/**
26-
* Method to show popup message containing device info
27-
*
28-
* @param message String containing device info message
29-
*/
30-
void showDeviceInfo(String message);
24+
void showInfo(String infoMessage, String typeOfInfo);
3125

3226
/**
3327
* Method to create new JDialog for progress bar

src/com/opelooo/scrcpyGUI/scrcpy_main_panel.java

+31-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public class scrcpy_main_panel extends javax.swing.JFrame implements PopupHandler {
1919

2020
private final DefaultListModel<String> listModel_JD; // Declare the list model
21+
private final int ADB_SCRCPY_STATUS = GUI_functions.checkAdb_Scrcpy_InFolder(this);
2122
private JPopupMenu popupMenu; // The popup menu
2223

2324
/**
@@ -29,11 +30,13 @@ public scrcpy_main_panel() {
2930

3031
listModel_JD = new DefaultListModel<>(); // Initialize the list model
3132
list_devices.setModel(listModel_JD); // Set the list model to JList
32-
if (GUI_functions.checkAdb_Scrcpy_InFolder(this) == 1) {
33-
updateDeviceList();
34-
}
33+
3534
poupMenuDeviceInfo();
3635

36+
if (ADB_SCRCPY_STATUS != 1) {
37+
return;
38+
}
39+
updateDeviceList();
3740
}
3841

3942
/**
@@ -305,8 +308,9 @@ private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN
305308
private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
306309
// TODO add your handling code here:
307310
String selectedDevice = list_devices.getSelectedValue();
308-
showDeviceInfo("Device Info for: " + selectedDevice
309-
+ "\nDevice Manufacturer: " + GUI_functions.adb_device_info(this, selectedDevice));
311+
showInfo("Device Info for: " + selectedDevice
312+
+ "\nDevice Manufacturer: "
313+
+ GUI_functions.adb_device_info(this, selectedDevice), "Device Info");
310314
}//GEN-LAST:event_selectButtonActionPerformed
311315

312316
private void bitRateSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bitRateSliderMouseDragged
@@ -331,7 +335,12 @@ public static void main(String args[]) {
331335
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
332336
/* Create and display the form */
333337
java.awt.EventQueue.invokeLater(() -> {
334-
javax.swing.JFrame mainFrame = new scrcpy_main_panel();
338+
scrcpy_main_panel mainFrame = new scrcpy_main_panel();
339+
// Check the adb status using the instance method
340+
if (getAdbStatus(mainFrame) != 1) {
341+
mainFrame.dispose();
342+
return;
343+
}
335344
// Calculate the frame location
336345
int x1 = (screenSize.width - mainFrame.getWidth()) / 2;
337346
int y1 = (screenSize.height - mainFrame.getHeight()) / 2;
@@ -341,6 +350,11 @@ public static void main(String args[]) {
341350
});
342351
}
343352

353+
// Access the instance variable using the passed scrcpyPanel
354+
public static int getAdbStatus(scrcpy_main_panel scrcpyPanel) {
355+
return scrcpyPanel.ADB_SCRCPY_STATUS;
356+
}
357+
344358
//<editor-fold defaultstate="collapsed" desc=" Variables declaration - do not modify ">
345359
// Variables declaration - do not modify//GEN-BEGIN:variables
346360
private javax.swing.JComboBox<String> audioFormats;
@@ -429,11 +443,13 @@ private void showPopupMenu(MouseEvent e) {
429443
return;
430444
}
431445
if (e.getSource() == infoMenuItem) {
432-
showDeviceInfo("Device Info for: " + selectedDevice
433-
+ "\nDevice Manufacturer: " + GUI_functions.adb_device_info(this, selectedDevice));
446+
showInfo("Device Info for: " + selectedDevice
447+
+ "\nDevice Manufacturer: "
448+
+ GUI_functions.adb_device_info(this, selectedDevice), "Device Info");
434449
} else if (e.getSource() == ipInfo) {
435-
showDeviceInfo("Device Info for: " + selectedDevice
436-
+ "\nDevice IP Address: " + GUI_functions.adb_get_device_ip(this, selectedDevice));
450+
showInfo("Device Info for: " + selectedDevice
451+
+ "\nDevice IP Address: "
452+
+ GUI_functions.adb_get_device_ip(this, selectedDevice), "Device Info");
437453
}
438454
};
439455

@@ -456,14 +472,6 @@ private void setIcon(String iconURL) {
456472
}
457473
}
458474

459-
@Override
460-
public void showDeviceInfo(String message) {
461-
if (message.contains("null")) {
462-
return;
463-
}
464-
JOptionPane.showMessageDialog(this, message, "Device Info", JOptionPane.INFORMATION_MESSAGE);
465-
}
466-
467475
@Override
468476
public void showError(String errorMessage) {
469477
errorMessage = (errorMessage.contains("null")) ? "Please select device" : errorMessage;
@@ -491,7 +499,10 @@ public JDialog progressBarDialog() {
491499
}
492500

493501
@Override
494-
public void showInfo(String infoMessage) {
495-
JOptionPane.showMessageDialog(this, infoMessage, "Info", JOptionPane.INFORMATION_MESSAGE);
502+
public void showInfo(String infoMessage, String title) {
503+
if (infoMessage.contains("null")) {
504+
return;
505+
}
506+
JOptionPane.showMessageDialog(this, infoMessage, title, JOptionPane.INFORMATION_MESSAGE);
496507
}
497508
}

0 commit comments

Comments
 (0)