Commit 0ccd5a60 authored by CallisteHanriat's avatar CallisteHanriat

Progress bar for elicitation mode works properly + adding some documentation

parent 80bafb6f
......@@ -136,6 +136,33 @@
android:onClick="onValidate"
android:src="@drawable/ok_32" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/elicit_phrase_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<TextView
android:id="@+id/elicit_total_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / " />
<view class="org.lp20.aikuma.ui.InterleavedSeekBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/seek_elicit_progression"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="3dp"
/>
</LinearLayout>
</LinearLayout>
......
......@@ -5,12 +5,12 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
android:paddingRight="@dimen/activity_horizontal_margin">
<!-- Back button -->
<GridLayout
......@@ -45,7 +45,7 @@
android:id="@+id/separator"
android:visibility="visible"
android:background="@android:color/darker_gray"
android:layout_marginBottom="25dp"/>
android:layout_marginBottom="50dp"/>
<TextView
android:id="@+id/maintextView2"
......@@ -99,7 +99,7 @@
android:id="@+id/tv_elicit_phrase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingBottom="10dp"
android:text="TextView"
android:textSize="35sp"
android:textStyle="bold"
......@@ -136,6 +136,33 @@
android:onClick="onValidate"
android:src="@drawable/ok_32" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/elicit_phrase_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<TextView
android:id="@+id/elicit_total_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / " />
<view class="org.lp20.aikuma.ui.InterleavedSeekBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/seek_elicit_progression"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="3dp"
/>
</LinearLayout>
</LinearLayout>
......
package org.lp20.aikuma.ui;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
......@@ -34,6 +36,7 @@ import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
......@@ -45,7 +48,7 @@ public class RecordElicitation extends AikumaActivity {
protected long sampleRate = 16000l;
private boolean recording = false;
private BufferedReader reader;
private int phraseId = 1;
private int phraseId = 0;
private String eliciTextFile;
private ListenFragment fragment;
......@@ -57,6 +60,12 @@ public class RecordElicitation extends AikumaActivity {
private int speakerAge=0;
private int speakerGender=0;
private Date date;
/**
* the progress bar used to display the user's progression.
*/
private InterleavedSeekBar progressBar;
private int numberOfLines;
/** Called when the activity is first created. */
@Override
......@@ -67,6 +76,7 @@ public class RecordElicitation extends AikumaActivity {
eliciTextFile = getIntent().getStringExtra(ElicitationMode.importFileName);
Log.i(TAG, "Selected file: " + eliciTextFile);
initProgressBars();
// retrieving metadata
Bundle bundle = getIntent().getBundleExtra(RecordingMetadataLig.metaBundle);
recordLang = bundle.getParcelable(RecordingMetadataLig.metaRecordLang);
......@@ -79,7 +89,7 @@ public class RecordElicitation extends AikumaActivity {
try {
date = new SimpleDateFormat().parse(bundle.getString(RecordingMetadataLig.metaDate));
// date = dateformat.parse(metaIntent.getStringExtra(RecordingMetadataLig.metaDate));
} catch (ParseException e1) {
} catch (ParseException e1) {
date = new Date();
} catch (Exception e) {
Log.e(TAG, "Exception caught: " + e);
......@@ -94,13 +104,22 @@ public class RecordElicitation extends AikumaActivity {
String phrase;
while ((phrase = reader.readLine()) != null && (phrase.isEmpty() || phrase.split("##").length <= 1)) { continue; }
if (phrase == null) { throw new IOException("Empty text file"); }
numberOfLines = countNumberOfLines();
TextView tv = (TextView) findViewById(R.id.elicit_phrase_number);
tv.setText("0");
TextView tv_total = (TextView) findViewById(R.id.elicit_total_number);
tv_total.setText(" / " + numberOfLines);
// progressBar.setProgress((int) ((float)phraseId/(float)numberOfLines * 100));
String[] phrasePair = phrase.split("##");
TextView tvFrPhrase = (TextView) findViewById(R.id.tv_elicit_phrase);
tvFrPhrase.setText(phrasePair[0]);
tvFrPhrase = (TextView) findViewById(R.id.tv_orig_phrase);
tvFrPhrase.setText(phrasePair[1]);
TextView tv = (TextView) findViewById(R.id.phrase_x);
tv.setText("Phrase" + phraseId);
TextView tv2 = (TextView) findViewById(R.id.phrase_x);
tv2.setText("Phrase" + phraseId);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
Log.e(TAG,"text file could not be found: " + e1);
......@@ -115,6 +134,75 @@ public class RecordElicitation extends AikumaActivity {
fragment = (ListenFragment) getFragmentManager().findFragmentById(R.id.phrase_player);
}
/**
* called when we want to save one record. Used when we touch newt button
*/
private void saveOneRecord() {
recordUUID = UUID.randomUUID();
try {
File f = new File(Recording.getNoSyncRecordingsPath(),
recordUUID.toString() + ".wav");
if (recorder != null) { recorder.release(); }
recorder = new Recorder(0, f, sampleRate);
Log.i(TAG, "recorder filename: " + recorder.getWriter().getFullFileName());
fragment.releasePlayer();
// test on the lines read:
// check that the file is not empty by ensuring that the phrase is not null (end of file reached)
// check that the sentence is correctly formatted (no empty line and '##' delimited found)
String phrase;
while ((phrase = reader.readLine()) != null && (phrase.isEmpty() || phrase.split("##").length <= 1)) { continue; }
if (phrase == null) { throw new IOException("End of text file reached."); }
TextView tv2 = (TextView) findViewById(R.id.elicit_phrase_number);
tv2.setText(phraseId + 1 + "");
TextView tv_total = (TextView) findViewById(R.id.elicit_total_number);
tv_total.setText(" / " + numberOfLines);
progressBar.setProgress((int) (((float)phraseId + 1.0)/(float)numberOfLines * 100));
String[] phrasePair = phrase.split("##");
TextView tvFrPhrase = (TextView) findViewById(R.id.tv_elicit_phrase);
tvFrPhrase.setText(phrasePair[0]);
tvFrPhrase = (TextView) findViewById(R.id.tv_orig_phrase);
tvFrPhrase.setText(phrasePair[1]);
phraseId++;
TextView tv = (TextView) findViewById(R.id.phrase_x);
tv.setText("Phrase" + phraseId);
} catch (MicException e) {
this.finish();
Toast.makeText(getApplicationContext(),
"Error setting up microphone.",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this, "No more sentences to display", Toast.LENGTH_LONG).show();
Log.e(TAG,"No more sentences to display or an error occurred: " + e);
this.finish();
}
}
/**
* called to set progressionBar behavior
* progressionBar is not modifiable by hand
*/
private void initProgressBars() {
progressBar = (InterleavedSeekBar) findViewById(R.id.seek_elicit_progression);
progressBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int originalProgress;
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
if (fromUser) {
seekBar.setProgress(originalProgress);
}
}
public void onStopTrackingTouch(SeekBar _seekBar) {};
public void onStartTrackingTouch(SeekBar _seekBar) {
originalProgress = progressBar.getProgress();
};
});
}
@Override
public void onDestroy() {
super.onDestroy();
......@@ -131,40 +219,13 @@ public class RecordElicitation extends AikumaActivity {
}
}
/**
* called when we click on next button
* @param _view the next button
*/
public void onNextClick(View _view) {
if (saveRecording()) {
recordUUID = UUID.randomUUID();
try {
File f = new File(Recording.getNoSyncRecordingsPath(),
recordUUID.toString() + ".wav");
if (recorder != null) { recorder.release(); }
recorder = new Recorder(0, f, sampleRate);
Log.i(TAG, "recorder filename: " + recorder.getWriter().getFullFileName());
fragment.releasePlayer();
// test on the lines read:
// check that the file is not empty by ensuring that the phrase is not null (end of file reached)
// check that the sentence is correctly formatted (no empty line and '##' delimited found)
String phrase;
while ((phrase = reader.readLine()) != null && (phrase.isEmpty() || phrase.split("##").length <= 1)) { continue; }
if (phrase == null) { throw new IOException("End of text file reached."); }
String[] phrasePair = phrase.split("##");
TextView tvFrPhrase = (TextView) findViewById(R.id.tv_elicit_phrase);
tvFrPhrase.setText(phrasePair[0]);
tvFrPhrase = (TextView) findViewById(R.id.tv_orig_phrase);
tvFrPhrase.setText(phrasePair[1]);
phraseId++;
TextView tv = (TextView) findViewById(R.id.phrase_x);
tv.setText("Phrase" + phraseId);
} catch (MicException e) {
this.finish();
Toast.makeText(getApplicationContext(),
"Error setting up microphone.",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this, "No more sentences to display", Toast.LENGTH_LONG).show();
Log.e(TAG,"No more sentences to display or an error occurred: " + e);
this.finish();
}
saveOneRecord();
} else {
Toast.makeText(this, "Going to next sentence...", Toast.LENGTH_LONG).show();
}
......@@ -175,6 +236,10 @@ public class RecordElicitation extends AikumaActivity {
this.finish();
}
/**
* called when we want to stop the recording and save the file.
* @return
*/
private boolean saveRecording() {
try {
recorder.stop();
......@@ -295,6 +360,34 @@ public class RecordElicitation extends AikumaActivity {
}
}
/**
* called by constructor to set the number of lines in the file
* @return the number of lines in a txt file.
* @throws IOException if the file cannot be read
*/
public int countNumberOfLines() throws IOException {
InputStream is;
is = new BufferedInputStream(new FileInputStream(this.eliciTextFile));
try {
byte[] c = new byte[8192];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars-1; ++i) {
if (c[i] == '#' && c[i+1] == '#') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
public void onBackPressed(View v) {
this.finish();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment