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

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;
@ -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;
@ -125,6 +128,21 @@ public class RecordingActivity extends Activity implements View.OnClickListener
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();
}
} }
} }
}; };