implement time out for waiting on voice recording

This commit is contained in:
Daniel Gultsch 2019-07-23 17:31:56 +02:00
parent 1af52a7a30
commit f597fc46da
1 changed files with 162 additions and 147 deletions

View File

@ -21,6 +21,8 @@ import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
@ -37,6 +39,8 @@ public class RecordingActivity extends Activity implements View.OnClickListener
private MediaRecorder mRecorder; private MediaRecorder mRecorder;
private long mStartTime = 0; private long mStartTime = 0;
private CountDownLatch outputFileWrittenLatch = new CountDownLatch(1);
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private Runnable mTickExecutor = new Runnable() { private Runnable mTickExecutor = new Runnable() {
@Override @Override
@ -47,7 +51,6 @@ public class RecordingActivity extends Activity implements View.OnClickListener
}; };
private File mOutputFile; private File mOutputFile;
private boolean mShouldFinishAfterWrite = false;
private FileObserver mFileObserver; private FileObserver mFileObserver;
@ -55,7 +58,7 @@ public class RecordingActivity extends Activity implements View.OnClickListener
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setTheme(ThemeHelper.findDialog(this)); setTheme(ThemeHelper.findDialog(this));
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
this.binding = DataBindingUtil.setContentView(this,R.layout.activity_recording); this.binding = DataBindingUtil.setContentView(this, R.layout.activity_recording);
this.binding.cancelButton.setOnClickListener(this); this.binding.cancelButton.setOnClickListener(this);
this.binding.shareButton.setOnClickListener(this); this.binding.shareButton.setOnClickListener(this);
this.setFinishOnTouchOutside(false); this.setFinishOnTouchOutside(false);
@ -107,14 +110,14 @@ public class RecordingActivity extends Activity implements View.OnClickListener
} }
} }
protected void stopRecording(boolean saveFile) { protected void stopRecording(final boolean saveFile) {
mShouldFinishAfterWrite = saveFile;
try { try {
mRecorder.stop(); mRecorder.stop();
mRecorder.release(); mRecorder.release();
} catch (Exception e) { } catch (Exception e) {
if (saveFile) { if (saveFile) {
Toast.makeText(this,R.string.unable_to_save_recording, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.unable_to_save_recording, Toast.LENGTH_SHORT).show();
return;
} }
} finally { } finally {
mRecorder = null; mRecorder = null;
@ -122,9 +125,24 @@ public class RecordingActivity extends Activity implements View.OnClickListener
} }
if (!saveFile && mOutputFile != null) { if (!saveFile && mOutputFile != null) {
if (mOutputFile.delete()) { if (mOutputFile.delete()) {
Log.d(Config.LOGTAG,"deleted canceled recording"); Log.d(Config.LOGTAG, "deleted canceled recording");
} }
} }
if (saveFile) {
new Thread(() -> {
try {
if (!outputFileWrittenLatch.await(2, TimeUnit.SECONDS)) {
Log.d(Config.LOGTAG, "time out waiting for output file to be written");
}
} catch (InterruptedException e) {
Log.d(Config.LOGTAG, "interrupted while waiting for output file to be written" ,e);
}
runOnUiThread(() -> {
setResult(Activity.RESULT_OK, new Intent().setData(Uri.fromFile(mOutputFile)));
finish();
});
}).start();
}
} }
private static File generateOutputFilename(Context context) { private static File generateOutputFilename(Context context) {
@ -157,10 +175,7 @@ public class RecordingActivity extends Activity implements View.OnClickListener
@Override @Override
public void onEvent(int event, String s) { public void onEvent(int event, String s) {
if (s != null && s.equals(mOutputFile.getName()) && event == FileObserver.CLOSE_WRITE) { if (s != null && s.equals(mOutputFile.getName()) && event == FileObserver.CLOSE_WRITE) {
if (mShouldFinishAfterWrite) { outputFileWrittenLatch.countDown();
setResult(Activity.RESULT_OK, new Intent().setData(Uri.fromFile(mOutputFile)));
finish();
}
} }
} }
}; };