Skip to content

Commit

Permalink
#379: Task/activity sound icon and playback (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperJarek authored and JanBan committed Nov 13, 2019
1 parent 68226fd commit c93bd5e
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StepRecyclerViewAdapter.StepRecyclerViewHolder> implements ChildActivityList {
private StepItemClickListener stepItemClickListener;
private List<StepTemplate> steps;
private String imageDirectory;
AppComponent appComponent;

private Integer currentStepPosition = 0;
Integer getCurrentStepPosition() { return currentStepPosition; }
Expand All @@ -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();
Expand All @@ -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() {
Expand All @@ -77,11 +89,16 @@ public void onClick(View v) {
};
}

StepRecyclerViewAdapter(List<StepTemplate> steps, String imageDirectory, StepItemClickListener stepItemClickListener)
StepRecyclerViewAdapter(List<StepTemplate> steps,
String imageDirectory,
StepItemClickListener stepItemClickListener,
AppComponent appComponent
)
{
this.steps = steps;
this.imageDirectory = imageDirectory;
this.stepItemClickListener = stepItemClickListener;
this.appComponent = appComponent;
}

@Override
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,6 +48,7 @@ public class StepSlidesActivity extends AppCompatActivity {
ImageButton nextButton;
MediaPlayer startSound;
MediaPlayer endSound;
ImageView soundImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void setRecyclerView() {
ChildPlan activePlan = childPlanRepository.getActivePlan();
List<TaskTemplate> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaskRecyclerViewAdapter.TaskRecyclerViewHolder> implements ChildActivityList {
private List<TaskTemplate> tasks;
private TaskItemClickListener taskItemClickListener;
private String imageDirectory;
private AppComponent appComponent;

private Integer currentTaskPosition = 0;
Integer getCurrentTaskPosition() { return currentTaskPosition; }
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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<TaskTemplate> tasks,
TaskItemClickListener taskItemClickListener,
final String imageDirectory
final String imageDirectory,
AppComponent appComponent
) {
this.tasks = tasks;
this.taskItemClickListener = taskItemClickListener;
this.imageDirectory = imageDirectory;
this.appComponent = appComponent;
}

@Override
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -48,6 +49,7 @@ public class TaskSlidesActivity extends AppCompatActivity {
ImageButton nextButton;
MediaPlayer startSound;
MediaPlayer endSound;
ImageView soundImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
android:padding="8dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/id_step_sound_icon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="right"
android:layout_weight="0"
android:adjustViewBounds="false"
app:srcCompat="@drawable/soundtube" />

<ImageView
android:id="@+id/id_iv_step_image"
android:layout_width="221dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

<!--sound icon-->

<ImageView
android:id="@+id/id_task_sound_icon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="right"
android:layout_weight="0"
android:adjustViewBounds="false"
app:srcCompat="@drawable/soundtube" />

<ImageView
android:id="@+id/id_iv_task_image"
android:layout_width="221dp"
Expand Down

0 comments on commit c93bd5e

Please sign in to comment.