diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/AppComponent.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/AppComponent.java index 08da64bf..2af540d3 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/AppComponent.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/AppComponent.java @@ -13,6 +13,7 @@ import javax.inject.Singleton; import pg.autyzm.friendly_plans.asset.AssetsHelper; import pg.autyzm.friendly_plans.asset.AssetsHelperModule; +import pg.autyzm.friendly_plans.child_app.utility.SoundHelper; import pg.autyzm.friendly_plans.child_app.view.MainActivity; import pg.autyzm.friendly_plans.file_picker.FilePickerModule; import pg.autyzm.friendly_plans.file_picker.FilePickerProxy; @@ -118,4 +119,6 @@ public interface AppComponent { void inject(pg.autyzm.friendly_plans.child_app.view.step_slides.StepSlidesActivity stepSlidesActivity); void inject(pg.autyzm.friendly_plans.child_app.view.task_slides.TaskSlidesActivity taskSlidesActivity); + + void inject(SoundHelper soundHelper); } diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/utility/SoundHelper.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/utility/SoundHelper.java new file mode 100644 index 00000000..5f1c1294 --- /dev/null +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/utility/SoundHelper.java @@ -0,0 +1,61 @@ +package pg.autyzm.friendly_plans.child_app.utility; + +import android.media.AudioManager; +import android.media.MediaPlayer; + +import java.io.IOException; + +import javax.inject.Inject; + +import database.repository.AssetRepository; +import database.repository.ChildPlanRepository; +import pg.autyzm.friendly_plans.AppComponent; +import pg.autyzm.friendly_plans.asset.AssetsHelper; + +public class SoundHelper { + @Inject + ChildPlanRepository childPlanRepository; + @Inject + AssetsHelper assetsHelper; + @Inject + AssetRepository assetRepository; + String loopSoundPath; + public static SoundHelper getSoundHelper(AppComponent appComponent) + { + SoundHelper soundHelper = new SoundHelper(); + appComponent.inject(soundHelper); + soundHelper.setLoopSoundPath(); + return soundHelper; + } + + void setLoopSoundPath() { + Long soundId = childPlanRepository.getActivePlan().getChild().getTimerSoundId(); + loopSoundPath = assetsHelper.getFileFullPath(assetRepository.get(soundId)); + + } + + public MediaPlayer prepareLoopSound() { + MediaPlayer sound = new MediaPlayer(); + try { + sound.setDataSource(loopSoundPath); + sound.setAudioStreamType(AudioManager.STREAM_MUSIC); + sound.setLooping(true); + sound.prepare(); + } catch (Exception e) { + e.printStackTrace(); + } + return sound; + } + + public void resetLoopSound(MediaPlayer sound){ + sound.reset(); + try { + sound.setDataSource(loopSoundPath); + sound.setAudioStreamType(AudioManager.STREAM_MUSIC); + sound.setLooping(true); + sound.prepare(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepListActivity.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepListActivity.java index 18719c42..32d79b1f 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepListActivity.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepListActivity.java @@ -1,6 +1,7 @@ package pg.autyzm.friendly_plans.child_app.view.step_list; import android.content.Intent; +import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; @@ -20,6 +21,7 @@ import pg.autyzm.friendly_plans.child_app.utility.ChildActivityExecutor; import pg.autyzm.friendly_plans.child_app.utility.Consts; import pg.autyzm.friendly_plans.child_app.utility.ChildActivityState; +import pg.autyzm.friendly_plans.child_app.utility.SoundHelper; public class StepListActivity extends AppCompatActivity { @@ -42,6 +44,10 @@ void setRecyclerView() { getIntent().getExtras().getLong(ActivityProperties.TASK_ID) ); String filesDirectory = getApplicationContext().getFilesDir().toString(); + MediaPlayer startSound = MediaPlayer.create(this, R.raw.beep); + MediaPlayer endSound = SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).prepareLoopSound(); + stepListener.setStartSound(startSound); + stepListener.setEndSound(endSound); stepRecyclerViewAdapter = new StepRecyclerViewAdapter(steps, filesDirectory, stepListener); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_child_app_step_list); @@ -53,6 +59,9 @@ void setRecyclerView() { StepRecyclerViewAdapter.StepItemClickListener stepListener = new StepRecyclerViewAdapter.StepItemClickListener() { + MediaPlayer startSound; + MediaPlayer endSound; + @Override public void selectStepListener(int clickPosition, final TextView durationLabel) { if (clickPosition != stepRecyclerViewAdapter.getCurrentStepPosition() @@ -60,6 +69,8 @@ public void selectStepListener(int clickPosition, final TextView durationLabel) return; if (stepRecyclerViewAdapter.getCurrentStepState() == ChildActivityState.FINISHED){ + endSound.stop(); + SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).resetLoopSound(endSound); if (clickPosition < stepRecyclerViewAdapter.getItemCount() - 1) { stepRecyclerViewAdapter.setCurrentStep(clickPosition + 1); return; @@ -73,17 +84,26 @@ public void selectStepListener(int clickPosition, final TextView durationLabel) startChildActivityExecution(durationLabel); } + public void setStartSound(MediaPlayer sound) { + startSound = sound; + } + + public void setEndSound(MediaPlayer sound) { + endSound = sound; + } + private void startChildActivityExecution(final TextView durationLabel){ stepRecyclerViewAdapter.setCurrentStepState(ChildActivityState.IN_PROGRESS); final Handler timerHandler = new Handler(); String durationStr = durationLabel.getText().toString(); Integer duration = Integer.parseInt(durationStr.replaceAll("[^0-9]", "")); - + startSound.start(); Runnable updater = new ChildActivityExecutor(duration, durationLabel, timerHandler, new ChildActivityExecutor.ActivityCompletedListener(){ @Override public void onActivityCompleted() { stepRecyclerViewAdapter.activityCompleted(); + endSound.start(); } }); diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepRecyclerViewAdapter.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepRecyclerViewAdapter.java index 4aaf7d47..2bf0d0a9 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepRecyclerViewAdapter.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_list/StepRecyclerViewAdapter.java @@ -2,6 +2,7 @@ import android.graphics.Color; import android.graphics.Paint; +import android.media.MediaPlayer; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -33,6 +34,8 @@ public class StepRecyclerViewAdapter extends RecyclerView.Adapter