From 985330be40046829374a7898423c445127e0747c Mon Sep 17 00:00:00 2001 From: Jarecki Date: Wed, 13 Nov 2019 20:34:27 +0100 Subject: [PATCH] #379: Task/activity sound icon and playback --- .../child_app/utility/SoundHelper.java | 16 +++++++++-- .../view/common/SoundIconListener.java | 17 ++++++++++++ .../view/step_list/StepListActivity.java | 2 +- .../step_list/StepRecyclerViewAdapter.java | 27 +++++++++++++++---- .../view/step_slides/StepSlidesActivity.java | 14 +++++++++- .../view/task_list/TaskListActivity.java | 2 +- .../task_list/TaskRecyclerViewAdapter.java | 22 ++++++++++++--- .../view/task_slides/TaskSlidesActivity.java | 13 +++++++++ .../main/res/layout/child_app_item_step.xml | 9 +++++++ .../main/res/layout/child_app_item_task.xml | 9 +++++++ 10 files changed, 118 insertions(+), 13 deletions(-) create mode 100644 Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/common/SoundIconListener.java 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 index 85eb491d..61fe7a3f 100644 --- 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 @@ -30,8 +30,7 @@ public static SoundHelper getSoundHelper(AppComponent appComponent) private void setLoopedSoundPath() { Long soundId = childPlanRepository.getActivePlan().getChild().getTimerSoundId(); - loopedSoundPath = assetsHelper.getFileFullPath(assetRepository.get(soundId)); - + loopedSoundPath = soundId != null ? assetsHelper.getFileFullPath(assetRepository.get(soundId)): null; } public MediaPlayer prepareLoopedSound() { @@ -58,4 +57,17 @@ public void resetLoopedSound(MediaPlayer sound){ e.printStackTrace(); } } + + public MediaPlayer getSound(long soundId){ + MediaPlayer sound = new MediaPlayer(); + String path = assetsHelper.getFileFullPath(assetRepository.get(soundId)); + try { + sound.setDataSource(path); + sound.setAudioStreamType(AudioManager.STREAM_MUSIC); + sound.prepare(); + } catch (Exception e) { + e.printStackTrace(); + } + return sound; + } } diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/common/SoundIconListener.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/common/SoundIconListener.java new file mode 100644 index 00000000..e7a09468 --- /dev/null +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/common/SoundIconListener.java @@ -0,0 +1,17 @@ +package pg.autyzm.friendly_plans.child_app.view.common; + + +import android.media.MediaPlayer; +import android.view.View; + +public class SoundIconListener implements View.OnClickListener { + private MediaPlayer sound; + public SoundIconListener(MediaPlayer sound){ + this.sound = sound; + } + + @Override + public void onClick(View view) { + sound.start(); + } +} 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 094bf9ca..a5d43f8e 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 @@ -48,7 +48,7 @@ void setRecyclerView() { MediaPlayer endSound = SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).prepareLoopedSound(); stepListener.setStartSound(startSound); stepListener.setEndSound(endSound); - stepRecyclerViewAdapter = new StepRecyclerViewAdapter(steps, filesDirectory, stepListener); + stepRecyclerViewAdapter = new StepRecyclerViewAdapter(steps, filesDirectory, stepListener, ((App) getApplication()).getAppComponent()); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_child_app_step_list); recyclerView.setHasFixedSize(true); 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 2bf0d0a9..178356b9 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 @@ -16,15 +16,19 @@ import java.util.List; import database.entities.StepTemplate; +import pg.autyzm.friendly_plans.AppComponent; import pg.autyzm.friendly_plans.R; import pg.autyzm.friendly_plans.child_app.utility.Consts; +import pg.autyzm.friendly_plans.child_app.utility.SoundHelper; import pg.autyzm.friendly_plans.child_app.view.common.ChildActivityList; import pg.autyzm.friendly_plans.child_app.utility.ChildActivityState; +import pg.autyzm.friendly_plans.child_app.view.common.SoundIconListener; public class StepRecyclerViewAdapter extends RecyclerView.Adapter implements ChildActivityList { private StepItemClickListener stepItemClickListener; private List steps; private String imageDirectory; + AppComponent appComponent; private Integer currentStepPosition = 0; Integer getCurrentStepPosition() { return currentStepPosition; } @@ -41,23 +45,23 @@ protected interface StepItemClickListener { static class StepRecyclerViewHolder extends RecyclerView.ViewHolder { StepItemClickListener stepItemClickListener; TextView stepName; - + ImageView soundImage; ImageView stepImage; String imageDirectory; - TextView durationLabel; StepRecyclerViewHolder(View itemView, String imageDirectory, StepItemClickListener stepItemClickListener) { super(itemView); this.stepName = (TextView) itemView.findViewById(R.id.id_tv_step_name); this.stepImage = (ImageView) itemView.findViewById(R.id.id_iv_step_image); + this.soundImage = (ImageView) itemView.findViewById(R.id.id_step_sound_icon); this.durationLabel = (TextView) itemView.findViewById(R.id.id_tv_step_duration_time); this.imageDirectory = imageDirectory; this.stepItemClickListener = stepItemClickListener; itemView.setOnClickListener(stepItemListener); } - void setUpHolder(StepTemplate step) { + void setUpHolder(StepTemplate step, AppComponent appComponent) { stepName.setText(step.getName()); if (step.getPicture() != null) { String imageName = step.getPicture().getFilename(); @@ -67,6 +71,14 @@ void setUpHolder(StepTemplate step) { } else { stepImage.setVisibility(View.INVISIBLE); } + if(step.getSound() == null){ + soundImage.setVisibility(View.INVISIBLE); + } + else{ + MediaPlayer sound = SoundHelper.getSoundHelper(appComponent).getSound(step.getSoundId()); + SoundIconListener soundClickListener = new SoundIconListener(sound); + soundImage.setOnClickListener(soundClickListener); + } } View.OnClickListener stepItemListener = new View.OnClickListener() { @@ -77,11 +89,16 @@ public void onClick(View v) { }; } - StepRecyclerViewAdapter(List steps, String imageDirectory, StepItemClickListener stepItemClickListener) + StepRecyclerViewAdapter(List steps, + String imageDirectory, + StepItemClickListener stepItemClickListener, + AppComponent appComponent + ) { this.steps = steps; this.imageDirectory = imageDirectory; this.stepItemClickListener = stepItemClickListener; + this.appComponent = appComponent; } @Override @@ -96,7 +113,7 @@ public StepRecyclerViewAdapter.StepRecyclerViewHolder onCreateViewHolder(ViewGro public void onBindViewHolder(StepRecyclerViewAdapter.StepRecyclerViewHolder holder, int position) { if (steps != null && !steps.isEmpty()) { StepTemplate step = steps.get(position); - holder.setUpHolder(step); + holder.setUpHolder(step, appComponent); } holder.itemView.setBackgroundColor(Color.TRANSPARENT); diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_slides/StepSlidesActivity.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_slides/StepSlidesActivity.java index b78b75bd..6c535c64 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_slides/StepSlidesActivity.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/step_slides/StepSlidesActivity.java @@ -26,6 +26,7 @@ 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; +import pg.autyzm.friendly_plans.child_app.view.common.SoundIconListener; import static android.view.View.VISIBLE; @@ -47,6 +48,7 @@ public class StepSlidesActivity extends AppCompatActivity { ImageButton nextButton; MediaPlayer startSound; MediaPlayer endSound; + ImageView soundImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -100,6 +102,16 @@ private void setupStepView(StepTemplate step){ .into(stepImage); } else stepImage.setVisibility(View.INVISIBLE); + + if(step.getSound() == null){ + soundImage.setVisibility(View.INVISIBLE); + } + else{ + soundImage.setVisibility(View.VISIBLE); + MediaPlayer sound = SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).getSound(step.getSoundId()); + SoundIconListener soundClickListener = new SoundIconListener(sound); + soundImage.setOnClickListener(soundClickListener); + } } private void displayNavigationControls(boolean shouldDisplay){ @@ -134,7 +146,7 @@ private void setUpView() { stepTimerIcon = (ImageView)findViewById(R.id.id_iv_child_activity_duration_icon); backButton = (ImageButton)findViewById(R.id.id_bv_back_button); nextButton = (ImageButton)findViewById(R.id.id_bv_next_button); - + soundImage = (ImageView)findViewById(R.id.id_iv_child_activity_sound); backButton.setOnClickListener(backButtonListener); nextButton.setOnClickListener(nextButtonListener); stepTimerIcon.setOnClickListener(timerButtonListener); diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskListActivity.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskListActivity.java index 919854f5..59262e85 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskListActivity.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskListActivity.java @@ -137,7 +137,7 @@ void setRecyclerView() { ChildPlan activePlan = childPlanRepository.getActivePlan(); List tasks = activePlan.getPlanTemplate().getTasksWithThisPlan(); String filesDirectory = getApplicationContext().getFilesDir().toString(); - taskRecyclerViewAdapter = new TaskRecyclerViewAdapter(tasks, taskItemClickListener, filesDirectory); + taskRecyclerViewAdapter = new TaskRecyclerViewAdapter(tasks, taskItemClickListener, filesDirectory, appComponent); recyclerView.setAdapter(taskRecyclerViewAdapter); } diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskRecyclerViewAdapter.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskRecyclerViewAdapter.java index 35f2a11a..a8a2f442 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskRecyclerViewAdapter.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_list/TaskRecyclerViewAdapter.java @@ -19,13 +19,16 @@ import pg.autyzm.friendly_plans.AppComponent; import pg.autyzm.friendly_plans.R; import pg.autyzm.friendly_plans.child_app.utility.Consts; +import pg.autyzm.friendly_plans.child_app.utility.SoundHelper; import pg.autyzm.friendly_plans.child_app.view.common.ChildActivityList; import pg.autyzm.friendly_plans.child_app.utility.ChildActivityState; +import pg.autyzm.friendly_plans.child_app.view.common.SoundIconListener; public class TaskRecyclerViewAdapter extends RecyclerView.Adapter implements ChildActivityList { private List tasks; private TaskItemClickListener taskItemClickListener; private String imageDirectory; + private AppComponent appComponent; private Integer currentTaskPosition = 0; Integer getCurrentTaskPosition() { return currentTaskPosition; } @@ -47,6 +50,7 @@ static class TaskRecyclerViewHolder extends RecyclerView.ViewHolder { TextView taskDuration; ImageView taskImage; ImageView actionPicture; + ImageView soundImage; TaskItemClickListener taskItemClickListener; String imageDirectory; boolean hasTimer = false; @@ -62,6 +66,7 @@ static class TaskRecyclerViewHolder extends RecyclerView.ViewHolder { this.actionPicture = (ImageView) itemView.findViewById(R.id.id_task_activity_icon); this.taskDuration = (TextView) itemView.findViewById(R.id.id_tv_task_duration_time); this.taskImage = (ImageView) itemView.findViewById(R.id.id_iv_task_image); + this.soundImage = (ImageView) itemView.findViewById(R.id.id_task_sound_icon); itemView.setOnClickListener(actionIconListener); this.taskItemClickListener = taskItemClickListener; this.imageDirectory = imageDirectory; @@ -80,7 +85,7 @@ else if (hasSteps) } }; - void setUpHolder(TaskTemplate task){ + void setUpHolder(TaskTemplate task, AppComponent appComponent){ taskName.setText(task.getName()); if (task.getPicture() != null) { @@ -110,17 +115,28 @@ else if (task.getDurationTime() != null) { actionPicture.setVisibility(View.INVISIBLE); taskDuration.setVisibility(View.INVISIBLE); } + + if(task.getSound() == null){ + soundImage.setVisibility(View.INVISIBLE); + } + else{ + MediaPlayer sound = SoundHelper.getSoundHelper(appComponent).getSound(task.getSoundId()); + SoundIconListener soundClickListener = new SoundIconListener(sound); + soundImage.setOnClickListener(soundClickListener); + } } } TaskRecyclerViewAdapter( List tasks, TaskItemClickListener taskItemClickListener, - final String imageDirectory + final String imageDirectory, + AppComponent appComponent ) { this.tasks = tasks; this.taskItemClickListener = taskItemClickListener; this.imageDirectory = imageDirectory; + this.appComponent = appComponent; } @Override @@ -135,7 +151,7 @@ public TaskRecyclerViewAdapter.TaskRecyclerViewHolder onCreateViewHolder(ViewGro public void onBindViewHolder(TaskRecyclerViewHolder holder, int position) { if (tasks != null && !tasks.isEmpty()) { TaskTemplate task = tasks.get(position); - holder.setUpHolder(task); + holder.setUpHolder(task, appComponent); } holder.itemView.setBackgroundColor(Color.TRANSPARENT); diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_slides/TaskSlidesActivity.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_slides/TaskSlidesActivity.java index 610f2b23..08e31672 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_slides/TaskSlidesActivity.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/child_app/view/task_slides/TaskSlidesActivity.java @@ -27,6 +27,7 @@ import pg.autyzm.friendly_plans.child_app.utility.Consts; import pg.autyzm.friendly_plans.child_app.utility.SoundHelper; import pg.autyzm.friendly_plans.child_app.utility.StepsDisplayUtils; +import pg.autyzm.friendly_plans.child_app.view.common.SoundIconListener; import static android.view.View.VISIBLE; @@ -48,6 +49,7 @@ public class TaskSlidesActivity extends AppCompatActivity { ImageButton nextButton; MediaPlayer startSound; MediaPlayer endSound; + ImageView soundImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -101,6 +103,16 @@ private void setupTaskView(TaskTemplate task){ .into(taskImage); } else taskImage.setVisibility(View.INVISIBLE); + + if(task.getSound() == null){ + soundImage.setVisibility(View.INVISIBLE); + } + else{ + soundImage.setVisibility(View.VISIBLE); + MediaPlayer sound = SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).getSound(task.getSoundId()); + SoundIconListener soundClickListener = new SoundIconListener(sound); + soundImage.setOnClickListener(soundClickListener); + } } private void displayNavigationControls(boolean shouldDisplay){ @@ -135,6 +147,7 @@ private void setUpView() { taskTimerIcon = (ImageView)findViewById(R.id.id_iv_child_activity_duration_icon); backButton = (ImageButton)findViewById(R.id.id_bv_back_button); nextButton = (ImageButton)findViewById(R.id.id_bv_next_button); + soundImage = (ImageView)findViewById(R.id.id_iv_child_activity_sound); backButton.setOnClickListener(backButtonListener); nextButton.setOnClickListener(nextButtonListener); diff --git a/Friendly-plans/app/src/main/res/layout/child_app_item_step.xml b/Friendly-plans/app/src/main/res/layout/child_app_item_step.xml index e07b64c7..de2b4858 100644 --- a/Friendly-plans/app/src/main/res/layout/child_app_item_step.xml +++ b/Friendly-plans/app/src/main/res/layout/child_app_item_step.xml @@ -6,6 +6,15 @@ android:padding="8dp" android:orientation="horizontal"> + + + +