4
4
import com .github .xsavikx .androidscreencast .api .injector .InputKeyEvent ;
5
5
import com .github .xsavikx .androidscreencast .app .AndroidScreencastApplication ;
6
6
import com .github .xsavikx .androidscreencast .constant .Constants ;
7
+ import com .github .xsavikx .androidscreencast .exception .IORuntimeException ;
7
8
import com .github .xsavikx .androidscreencast .spring .config .ApplicationContextProvider ;
8
9
import com .github .xsavikx .androidscreencast .ui .explorer .JFrameExplorer ;
9
10
import com .github .xsavikx .androidscreencast .ui .interaction .KeyEventDispatcherFactory ;
10
11
import com .github .xsavikx .androidscreencast .ui .interaction .KeyboardActionListenerFactory ;
12
+ import com .google .common .io .Files ;
11
13
import org .springframework .beans .factory .annotation .Autowired ;
12
14
import org .springframework .core .env .Environment ;
13
15
import org .springframework .stereotype .Component ;
19
21
import java .awt .event .ActionEvent ;
20
22
import java .awt .event .ActionListener ;
21
23
import java .awt .event .MouseAdapter ;
24
+ import java .io .File ;
25
+ import java .io .IOException ;
26
+ import java .nio .file .Path ;
22
27
23
28
@ Component
24
29
public class JFrameMain extends JFrame {
@@ -92,22 +97,7 @@ public void initialize() {
92
97
jbKbSearch .addActionListener (KeyboardActionListenerFactory .getInstance (InputKeyEvent .KEYCODE_SEARCH ));
93
98
jbKbPhoneOn .addActionListener (KeyboardActionListenerFactory .getInstance (InputKeyEvent .KEYCODE_CALL ));
94
99
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 ());
111
101
112
102
113
103
jtbHardkeys .add (jbKbHome );
@@ -150,6 +140,45 @@ public void actionPerformed(ActionEvent e) {
150
140
jtb .add (jbRecord );
151
141
}
152
142
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
+
153
182
public void launchInjector () {
154
183
injector .setScreenCaptureListener ((size , image , landscape ) -> {
155
184
if (oldImageDimension == null || !size .equals (oldImageDimension )) {
@@ -162,15 +191,8 @@ public void launchInjector() {
162
191
injector .start ();
163
192
}
164
193
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 );
174
196
}
175
197
176
198
private void stopRecording () {
0 commit comments