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.util.Date;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
@ -37,6 +39,8 @@ public class RecordingActivity extends Activity implements View.OnClickListener
private MediaRecorder mRecorder;
private long mStartTime = 0;
private CountDownLatch outputFileWrittenLatch = new CountDownLatch(1);
private Handler mHandler = new Handler();
private Runnable mTickExecutor = new Runnable() {
@Override
@ -47,7 +51,6 @@ public class RecordingActivity extends Activity implements View.OnClickListener
};
private File mOutputFile;
private boolean mShouldFinishAfterWrite = false;
private FileObserver mFileObserver;
@ -55,7 +58,7 @@ public class RecordingActivity extends Activity implements View.OnClickListener
protected void onCreate(Bundle savedInstanceState) {
setTheme(ThemeHelper.findDialog(this));
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.shareButton.setOnClickListener(this);
this.setFinishOnTouchOutside(false);
@ -107,14 +110,14 @@ public class RecordingActivity extends Activity implements View.OnClickListener
}
}
protected void stopRecording(boolean saveFile) {
mShouldFinishAfterWrite = saveFile;
protected void stopRecording(final boolean saveFile) {
try {
mRecorder.stop();
mRecorder.release();
} catch (Exception e) {
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 {
mRecorder = null;
@ -122,9 +125,24 @@ public class RecordingActivity extends Activity implements View.OnClickListener
}
if (!saveFile && mOutputFile != null) {
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) {
@ -157,10 +175,7 @@ public class RecordingActivity extends Activity implements View.OnClickListener
@Override
public void onEvent(int event, String s) {
if (s != null && s.equals(mOutputFile.getName()) && event == FileObserver.CLOSE_WRITE) {
if (mShouldFinishAfterWrite) {
setResult(Activity.RESULT_OK, new Intent().setData(Uri.fromFile(mOutputFile)));
finish();
}
outputFileWrittenLatch.countDown();
}
}
};