private void loadExercises(Integer lessonId) { // retrieve sentences to practice from db for each exercise try { InputStream is = getAssets().open(DBHandler.DATABASE_NAME); db = new DBHandler(PracticeActivity.this, is); } catch (IOException e) { e.printStackTrace(); } if (db != null) { exercises = db.findExercises(lessonId); for (Exercise e : exercises) { ArrayList<ScriptEntry> scripts = db.findScripts(e.get_id()); Collections.sort(scripts); int i = 0; for (ScriptEntry s : scripts) { s.setScriptIndex(i); i++; } e.setScriptEntries(scripts); } } }
private void runScriptEntry() { if (current.getShouldRunScript()) { ((Button) findViewById(R.id.btn_try_again)).setVisibility(View.GONE); current.setShouldRunScript( false); // prove to me again that I can execute everything ->go to the next exercise. if (current.getCurrentScriptIndex() < current.getCurrentExercise().getScriptEntries().size()) { final ScriptEntry s = current.getCurrentScriptEntry(); if (s != null) { runOnUiThread( new Runnable() { @Override public void run() { TextView child = new TextView(PracticeActivity.this); child.setTextSize(20f); LinearLayout parent = (LinearLayout) findViewById(R.id.contentFrame); child.setText(s.getTextToShow()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); child.setLayoutParams(params); ArrayList<TextView> items = new ArrayList<>(); items.add(child); for (int i = parent.getChildCount() - 1; i >= 0; i--) { TextView t = (TextView) parent.getChildAt(i); parent.removeViewAt(i); } parent.addView(child); } }); // speak Bundle b = new Bundle(); b.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, s.get_id().toString()); switch (s.getFunctionId()) { case 1: // The device is to speak (tts) the text_to_read (used to give instructions // about the exercises) TTS.setOnUtteranceProgressListener( new UtteranceProgressListener() { @Override public void onStart(String utteranceId) {} @Override public void onDone(String utteranceId) { if (current.getCurrentScriptEntry().getFunctionId() == 1) { current.setShouldRunScript(true); current.selectNextScript(); runScriptEntry(); } } @Override public void onError(String utteranceId) {} }); TTS.speak(s.getTextToRead(), TextToSpeech.QUEUE_FLUSH, null, s.get_id().toString()); break; case 2: // The device is to Read text(tts), Show sentence- tts, Listen to speech, Check // against database info= stt. Listen and compare. TTS.setOnUtteranceProgressListener( new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(10); } @Override public void onDone(String utteranceId) { promptSpeechInput(); // shows mic screen // if voice recognition fails, ask again. no touching button } @Override public void onError(String utteranceId) {} }); TTS.speak(s.getTextToRead(), TextToSpeech.QUEUE_FLUSH, null, s.get_id().toString()); break; case 3: // only checks the speech -> do not provide any kind of model // (neither spoken by the device nor on video) // this method needs a little more time, for the sake of // uability, to ask for the nest input. Users were getting confused about the sounds // built in the Voice Recognition new Handler() .postDelayed( new Runnable() { @Override public void run() { promptSpeechInput(); } }, TRANSITION_PAUSE); break; case 4: // shows video and asks for audio input then checks audio runOnUiThread( new Runnable() { @Override public void run() { final VideoView v = new VideoView(PracticeActivity.this); final LinearLayout r = (LinearLayout) findViewById(R.id.videoFrame); r.setVisibility(View.VISIBLE); r.addView(v); int videoResource = getResources() .getIdentifier("raw/" + s.getTextToRead(), null, getPackageName()); String path = "android.resource://" + getPackageName() + "/" + videoResource; v.setVideoURI(Uri.parse(path)); v.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { r.removeView(v); promptSpeechInput(); } }); v.setOnPreparedListener( new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { v.start(); } }); } }); break; case 5: // only shows a video containing instructions. do not ask for audio back runOnUiThread( new Runnable() { @Override public void run() { final VideoView v = new VideoView(PracticeActivity.this); final LinearLayout r = (LinearLayout) findViewById(R.id.videoFrame); r.setVisibility(View.VISIBLE); r.addView(v); int videoResource = getResources() .getIdentifier("raw/" + s.getTextToRead(), null, getPackageName()); String path = "android.resource://" + getPackageName() + "/" + videoResource; v.setVideoURI(Uri.parse(path)); v.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { r.removeView(v); current.setShouldRunScript(true); current.selectNextScript(); runScriptEntry(); } }); v.setOnPreparedListener( new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { v.start(); } }); } }); break; } } } else { // exercise completed SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); if (exercises.size() > sharedPreferences.getInt("exercise_count", 0)) { Intent i = new Intent(PracticeActivity.this, TransitionActivity.class); startActivity(i); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); current.setShouldRunScript(true); } else { Intent i = new Intent(PracticeActivity.this, LessonCompletedActivity.class); startActivity(i); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); } finish(); } } }