Skip to content

Commit

Permalink
#378: sound on timer start and finish
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperJarek committed Nov 13, 2019
1 parent 1461ffa commit e176b01
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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);
Expand All @@ -53,13 +59,18 @@ 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()
|| stepRecyclerViewAdapter.getCurrentStepState() == ChildActivityState.IN_PROGRESS)
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;
Expand All @@ -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();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +34,8 @@ public class StepRecyclerViewAdapter extends RecyclerView.Adapter<StepRecyclerVi

protected interface StepItemClickListener {
void selectStepListener(int position, TextView stepName);
void setStartSound(MediaPlayer sound);
void setEndSound(MediaPlayer sound);
}

static class StepRecyclerViewHolder extends RecyclerView.ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pg.autyzm.friendly_plans.child_app.view.step_slides;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -24,6 +25,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;

import static android.view.View.VISIBLE;

Expand All @@ -43,6 +45,8 @@ public class StepSlidesActivity extends AppCompatActivity {
ImageView stepTimerIcon;
ImageButton backButton;
ImageButton nextButton;
MediaPlayer startSound;
MediaPlayer endSound;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -55,10 +59,16 @@ protected void onCreate(Bundle savedInstanceState) {
if (steps.isEmpty())
finish();

setUpSounds();
setUpView();
setCurrentStep(0);
}

private void setUpSounds(){
startSound = MediaPlayer.create(this, R.raw.beep);
endSound = SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).prepareLoopSound();
}

private void setCurrentStep(int index){
currentStep = index;
StepTemplate step = steps.get(index);
Expand All @@ -69,7 +79,6 @@ private void setCurrentStep(int index){

private void setupStepView(StepTemplate step){
stepName.setText(step.getName());

Integer duration = step.getDuration();
if(duration != null) {
stepDuration.setText(String.format("%d %s", duration, Consts.DURATION_UNIT_SECONDS));
Expand Down Expand Up @@ -151,8 +160,11 @@ public void onClick(View v) {
public void onClick(View v) {
if (currentStepStatus == ChildActivityState.PENDING_START)
startChildActivityExecution(stepDuration);
else if(currentStepStatus == ChildActivityState.FINISHED)
else if(currentStepStatus == ChildActivityState.FINISHED) {
endSound.stop();
SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).resetLoopSound(endSound);
displayNavigationControls(true);
}
}
};

Expand All @@ -179,12 +191,13 @@ private void startChildActivityExecution(final TextView durationLabel){
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() {
currentStepStatus = ChildActivityState.FINISHED;
endSound.start();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pg.autyzm.friendly_plans.child_app.view.task_list;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -14,27 +15,36 @@

import database.entities.ChildPlan;
import database.entities.TaskTemplate;
import database.repository.AssetRepository;
import database.repository.ChildPlanRepository;
import pg.autyzm.friendly_plans.App;
import pg.autyzm.friendly_plans.AppComponent;
import pg.autyzm.friendly_plans.R;
import pg.autyzm.friendly_plans.asset.AssetsHelper;
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;
import pg.autyzm.friendly_plans.child_app.utility.StepsDisplayUtils;

public class TaskListActivity extends AppCompatActivity {
@Inject
ChildPlanRepository childPlanRepository;
@Inject
AssetsHelper assetsHelper;
@Inject
AssetRepository assetRepository;
private TaskRecyclerViewAdapter taskRecyclerViewAdapter;

TaskRecyclerViewAdapter.TaskItemClickListener taskItemClickListener =
new TaskRecyclerViewAdapter.TaskItemClickListener() {

MediaPlayer startSound;
MediaPlayer endSound;
AppComponent appComponent;
@Override
public void stepsIconListener(int position) {
if (position == taskRecyclerViewAdapter.getCurrentTaskPosition()
&& taskRecyclerViewAdapter.getCurrentTaskState() != ChildActivityState.IN_PROGRESS)
{
&& taskRecyclerViewAdapter.getCurrentTaskState() != ChildActivityState.IN_PROGRESS) {
Intent intent = StepsDisplayUtils.getStepsDisplayIntent(
TaskListActivity.this,
taskRecyclerViewAdapter.getTaskItem(position).getId(),
Expand All @@ -50,7 +60,10 @@ public void timerIconClickListener(int clickPosition, final TextView durationLab
|| taskRecyclerViewAdapter.getCurrentTaskState() == ChildActivityState.IN_PROGRESS)
return;

if (taskRecyclerViewAdapter.getCurrentTaskState() == ChildActivityState.FINISHED){
if (taskRecyclerViewAdapter.getCurrentTaskState() == ChildActivityState.FINISHED) {
SoundHelper.getSoundHelper(((App) getApplication()).getAppComponent()).resetLoopSound(endSound);
endSound.stop();
SoundHelper.getSoundHelper(appComponent).resetLoopSound(endSound);
if (clickPosition < taskRecyclerViewAdapter.getItemCount() - 1) {
taskRecyclerViewAdapter.setCurrentTask(clickPosition + 1);
return;
Expand All @@ -71,17 +84,29 @@ public void blankChildActivityListener(int clickPosition) {
goToPlanFinishedScreen();
}

public void setStartSound(MediaPlayer sound) {
startSound = sound;
}

public void setEndSound(MediaPlayer sound) {
endSound = sound;
}

public void setAppComponent(AppComponent appComponent) {
this.appComponent = appComponent;
}

private void startChildActivityExecution(final TextView durationLabel) {
taskRecyclerViewAdapter.setCurrentTaskState(ChildActivityState.IN_PROGRESS);
final Handler timerHandler = new Handler();
String durationStr = durationLabel.getText().toString();
Integer duration = Integer.parseInt(durationStr.replaceAll("[^0-9]", ""));

Runnable updater = new ChildActivityExecutor(duration, durationLabel, timerHandler,
new ChildActivityExecutor.ActivityCompletedListener(){
new ChildActivityExecutor.ActivityCompletedListener() {
@Override
public void onActivityCompleted() {
taskRecyclerViewAdapter.activityCompleted();
// endSound.start();
}
});
timerHandler.post(updater);
Expand All @@ -97,6 +122,12 @@ protected void onCreate(Bundle savedInstanceState) {
}

void setRecyclerView() {
AppComponent appComponent = ((App) getApplication()).getAppComponent();
MediaPlayer startSound = MediaPlayer.create(this, R.raw.beep);
MediaPlayer endSound = SoundHelper.getSoundHelper(appComponent).prepareLoopSound();
taskItemClickListener.setStartSound(startSound);
taskItemClickListener.setEndSound(endSound);
taskItemClickListener.setAppComponent(appComponent);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_child_app_task_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
Expand All @@ -109,6 +140,8 @@ void setRecyclerView() {
recyclerView.setAdapter(taskRecyclerViewAdapter);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null && data.getStringExtra(Consts.RETURN_MESSAGE_KEY) != null)
Expand All @@ -118,15 +151,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
goToPlanFinishedScreen();
}

private void goToPlanFinishedScreen(){
private void goToPlanFinishedScreen() {
Intent intentWithResult = new Intent();
intentWithResult.putExtra(Consts.RETURN_MESSAGE_KEY, Consts.MESSAGE_TASKS_COMPLETED);
setResult(Consts.RETURN_MESSAGE_CODE, intentWithResult);
finish();
}

@Override
public void onBackPressed(){
public void onBackPressed() {
Intent intentWithResult = new Intent();
intentWithResult.putExtra(Consts.RETURN_MESSAGE_KEY, Consts.MESSAGE_TASKS_NOT_COMPLETED);
setResult(Consts.RETURN_MESSAGE_CODE, intentWithResult);
Expand Down
Loading

0 comments on commit e176b01

Please sign in to comment.