Skip to content

Commit b9dbd88

Browse files
committed
Move file chooser dialog for recording from record start to record stop
Now user will be asked to choose file where to save video when he'll hit Stop recording button. Video will be saved in some temporary file under OS tmp folder before it'll be moved to user-picker file
1 parent 46a9d7b commit b9dbd88

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

src/main/java/com/github/xsavikx/androidscreencast/api/AndroidDeviceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class AndroidDeviceImpl implements AndroidDevice {
2121
private static final Logger logger = Logger.getLogger(AndroidDeviceImpl.class);
2222
private final IDevice device;
2323

24-
@Autowired(required = false)
24+
@Autowired
2525
public AndroidDeviceImpl(IDevice device) {
2626
this.device = device;
2727
}

src/main/java/com/github/xsavikx/androidscreencast/api/command/executor/ShellCommandExecutor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@
1919
public class ShellCommandExecutor implements CommandExecutor {
2020
private static final Logger LOGGER = Logger.getLogger(ShellCommandExecutor.class);
2121
private final IDevice device;
22+
private final MultiLineReceiverPrinter multiLineReceiverPrinter;
2223
@Value("${adb.command.timeout:5}")
2324
private long adbCommandTimeout;
2425

2526
@Autowired
26-
public ShellCommandExecutor(IDevice device) {
27+
public ShellCommandExecutor(IDevice device, MultiLineReceiverPrinter multiLineReceiverPrinter) {
2728
this.device = device;
29+
this.multiLineReceiverPrinter = multiLineReceiverPrinter;
2830
}
2931

3032
@Override
3133
public void execute(Command command) {
3234
LOGGER.debug("execute(Command command=" + command + ") - start");
3335

3436
try {
35-
device.executeShellCommand(command.getFormattedCommand(), new MultiLineReceiverPrinter(),
37+
device.executeShellCommand(command.getFormattedCommand(), multiLineReceiverPrinter,
3638
adbCommandTimeout, TimeUnit.SECONDS);
3739
} catch (TimeoutException | AdbCommandRejectedException | ShellCommandUnresponsiveException | IOException e) {
3840
LOGGER.error("execute(Command)", e);

src/main/java/com/github/xsavikx/androidscreencast/api/injector/ScreenCaptureRunnable.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ public void setListener(ScreenCaptureListener listener) {
113113

114114
public void startRecording(File file) {
115115
try {
116-
File outputFile = file;
117-
if (!outputFile.getName().toLowerCase().endsWith(MOV_FILE_TYPE))
118-
outputFile = new File(file.getAbsolutePath() + MOV_FILE_TYPE);
119-
qos = new QuickTimeOutputStream(outputFile, QuickTimeOutputStream.VideoFormat.JPG);
116+
qos = new QuickTimeOutputStream(file, QuickTimeOutputStream.VideoFormat.JPG);
120117
} catch (IOException e) {
121118
throw new IORuntimeException(e);
122119
}

src/main/java/com/github/xsavikx/androidscreencast/api/recording/QuickTimeOutputStream.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public class QuickTimeOutputStream {
8484
* @throws IllegalArgumentException if videoFormat is null or if framerate is <= 0
8585
*/
8686
public QuickTimeOutputStream(File file, VideoFormat format) throws IOException {
87+
checkNotNull(file, "Result file should not be null.");
8788
checkNotNull(format, "Video format must not be null.");
8889
Files.deleteIfExists(file.toPath());
8990
out = new FileImageOutputStream(file);

src/main/java/com/github/xsavikx/androidscreencast/ui/JFrameMain.java

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import com.github.xsavikx.androidscreencast.api.injector.InputKeyEvent;
55
import com.github.xsavikx.androidscreencast.app.AndroidScreencastApplication;
66
import com.github.xsavikx.androidscreencast.constant.Constants;
7+
import com.github.xsavikx.androidscreencast.exception.IORuntimeException;
78
import com.github.xsavikx.androidscreencast.spring.config.ApplicationContextProvider;
89
import com.github.xsavikx.androidscreencast.ui.explorer.JFrameExplorer;
910
import com.github.xsavikx.androidscreencast.ui.interaction.KeyEventDispatcherFactory;
1011
import com.github.xsavikx.androidscreencast.ui.interaction.KeyboardActionListenerFactory;
12+
import com.google.common.io.Files;
1113
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.core.env.Environment;
1315
import org.springframework.stereotype.Component;
@@ -19,6 +21,9 @@
1921
import java.awt.event.ActionEvent;
2022
import java.awt.event.ActionListener;
2123
import java.awt.event.MouseAdapter;
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.nio.file.Path;
2227

2328
@Component
2429
public class JFrameMain extends JFrame {
@@ -92,22 +97,7 @@ public void initialize() {
9297
jbKbSearch.addActionListener(KeyboardActionListenerFactory.getInstance(InputKeyEvent.KEYCODE_SEARCH));
9398
jbKbPhoneOn.addActionListener(KeyboardActionListenerFactory.getInstance(InputKeyEvent.KEYCODE_CALL));
9499
jbKbPhoneOff.addActionListener(KeyboardActionListenerFactory.getInstance(InputKeyEvent.KEYCODE_ENDCALL));
95-
jbRecord.addActionListener(new ActionListener() {
96-
private boolean recordStarted = false;
97-
98-
@Override
99-
public void actionPerformed(ActionEvent e) {
100-
if (!recordStarted) {
101-
recordStarted = true;
102-
jbRecord.setText("Stop record");
103-
startRecording();
104-
} else {
105-
recordStarted = false;
106-
stopRecording();
107-
jbRecord.setText("Start record");
108-
}
109-
}
110-
});
100+
jbRecord.addActionListener(createRecordActionListener());
111101

112102

113103
jtbHardkeys.add(jbKbHome);
@@ -150,6 +140,45 @@ public void actionPerformed(ActionEvent e) {
150140
jtb.add(jbRecord);
151141
}
152142

143+
private ActionListener createRecordActionListener() {
144+
return new ActionListener() {
145+
private final Path tmpDir = Files.createTempDir().toPath();
146+
private boolean recording = false;
147+
private File tmpVideoFile;
148+
149+
@Override
150+
public void actionPerformed(ActionEvent e) {
151+
try {
152+
if (!recording) {
153+
recording = true;
154+
jbRecord.setText("Stop record");
155+
tmpVideoFile = java.nio.file.Files.createTempFile(tmpDir, "androidScreenCast", ".mov.tmp").toFile();
156+
startRecording(tmpVideoFile);
157+
} else {
158+
recording = false;
159+
stopRecording();
160+
jbRecord.setText("Start record");
161+
JFileChooser jFileChooser = new JFileChooser();
162+
FileNameExtensionFilter filter = new FileNameExtensionFilter("Video file", "mov");
163+
jFileChooser.setFileFilter(filter);
164+
int returnVal = jFileChooser.showSaveDialog(JFrameMain.this);
165+
if (returnVal == JFileChooser.APPROVE_OPTION) {
166+
File resultFile = jFileChooser.getSelectedFile();
167+
if (!resultFile.getName().endsWith(".mov")) {
168+
resultFile = new File(resultFile.getAbsolutePath() + ".mov");
169+
}
170+
Files.move(tmpVideoFile, resultFile);
171+
} else {
172+
tmpVideoFile.deleteOnExit();
173+
}
174+
}
175+
} catch (IOException ex) {
176+
throw new IORuntimeException(ex);
177+
}
178+
}
179+
};
180+
}
181+
153182
public void launchInjector() {
154183
injector.setScreenCaptureListener((size, image, landscape) -> {
155184
if (oldImageDimension == null || !size.equals(oldImageDimension)) {
@@ -162,15 +191,8 @@ public void launchInjector() {
162191
injector.start();
163192
}
164193

165-
private void startRecording() {
166-
JFileChooser jFileChooser = new JFileChooser();
167-
FileNameExtensionFilter filter = new FileNameExtensionFilter("Video file", "mov");
168-
jFileChooser.setFileFilter(filter);
169-
int returnVal = jFileChooser.showSaveDialog(this);
170-
jFileChooser.getName();
171-
if (returnVal == JFileChooser.APPROVE_OPTION) {
172-
injector.startRecording(jFileChooser.getSelectedFile());
173-
}
194+
private void startRecording(File file) {
195+
injector.startRecording(file);
174196
}
175197

176198
private void stopRecording() {

0 commit comments

Comments
 (0)