Skip to content

Commit

Permalink
Add option to have louder notifications + vibration
Browse files Browse the repository at this point in the history
  • Loading branch information
benoiton committed Aug 12, 2015
1 parent 96b4492 commit 90ddad8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion trente30/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
versionName "1.1"
}
buildTypes {
release {
Expand Down
2 changes: 1 addition & 1 deletion trente30/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
android:exported="false" >
</service>
</application>

<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@
package net.friry.android.trente30;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;

public class CountdownService extends Service {
private final static String TAG = "CountdownService";
private final static boolean LOCAL_LOGV = false;

private final static float VOLUME = (float) 0.7;
private final static float VOLUME_NORMAL = (float) 0.6;
private final static float VOLUME_LOUD = (float) 1.0;
private float VOLUME;

private final static int VIBRATION_DURATION = 1000;

private Vibrator vibrator;

public final static String COUNTDOWN_BROADCAST = "net.friry.android.trente30.countdown_broadcast";
public final static String COUNTDOWN_STEP = "net.friry.android.trente30.countdown_step";
Expand All @@ -41,6 +49,7 @@ public class CountdownService extends Service {
private long iterationCount;
private long fastDuration;
private long slowDuration;
private boolean loud;

private SoundPool soundPool;
private int fastSound;
Expand All @@ -66,7 +75,7 @@ public void onCreate() {
.setAudioAttributes(audioAttributes)
.build();
*/
Log.d(TAG, "Creating soundPool created");
Log.d(TAG, "Creating soundPool");
soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
fastSound = soundPool.load(this, R.raw.fast, 1);
slowSound = soundPool.load(this, R.raw.slow, 1);
Expand Down Expand Up @@ -102,8 +111,17 @@ public int onStartCommand(Intent intent, int flags, int startId) {
iterationCount = intent.getLongExtra(MainActivity.ITERATION_COUNT, MainActivity.ITERATION_COUNT_default);
fastDuration = intent.getLongExtra(MainActivity.FAST_DURATION, MainActivity.FAST_DURATION_default);
slowDuration = intent.getLongExtra(MainActivity.SLOW_DURATION, MainActivity.SLOW_DURATION_default);
loud = intent.getBooleanExtra(MainActivity.LOUDER, MainActivity.LOUDER_default);

Log.d(TAG, "Received intent (warmupDuration="+Long.toString(warmupDuration)+", iterationCount="+Long.toString(iterationCount)+", fastDuration="+Long.toString(fastDuration)+", slowDuration="+Long.toString(slowDuration)+")");
Log.d(TAG, "Received intent (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", loud=" + Boolean.toString(loud) + ")");

if (loud) {
VOLUME = VOLUME_LOUD;
Log.d(TAG, "Creating vibrator");
vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
} else {
VOLUME = VOLUME_NORMAL;
}

Log.d(TAG,"Starting steps");
nextStep();
Expand Down Expand Up @@ -154,6 +172,9 @@ private void nextStep() {
} else if (step > 1+2*iterationCount) {
Log.d(TAG, "Playing end sound");
soundPool.play(endSound, VOLUME, VOLUME, 0, 0, 1);
if (loud) {
vibrator.vibrate(VIBRATION_DURATION);
}
Intent broadcastIntent = new Intent(COUNTDOWN_BROADCAST);
broadcastIntent.putExtra(COUNTDOWN_STEP, 0);
broadcastIntent.putExtra(COUNTDOWN_REMAINING_TIME, (long) 0);
Expand All @@ -172,11 +193,17 @@ private void nextStep() {
runCountdown(fastDuration * 1000);
Log.d(TAG, "Playing fast sound");
soundPool.play(fastSound, VOLUME, VOLUME, 0, 0, 1 );
if (loud) {
vibrator.vibrate(VIBRATION_DURATION);
}
} else if ((step - 1) % 2 == 0) {
Log.d(TAG, "Starting countdown (duration="+slowDuration+")");
runCountdown(slowDuration * 1000);
Log.d(TAG, "Playing slow sound");
soundPool.play(slowSound, VOLUME, VOLUME, 0, 0, 1 );
if (loud) {
vibrator.vibrate(VIBRATION_DURATION);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class MainActivity extends AppCompatActivity {
public final static long SLOW_DURATION_default = 30;
public final static String KEEP_SCREEN_ON = "net.friry.android.Trente30.KEEP_SCREEN_ON";
public final static boolean KEEP_SCREEN_ON_default = false;
public final static String LOUDER = "net.friry.android.Trente30.LOUDER";
public final static boolean LOUDER_default = false;

private EditText warmupDurationText;
private long warmupDuration;
Expand All @@ -57,6 +59,9 @@ public class MainActivity extends AppCompatActivity {
private MenuItem keepScreenOnItem;
private boolean keepScreenOn;

private MenuItem louderItem;
private boolean louder;

private SharedPreferences sharedPref;

@Override
Expand Down Expand Up @@ -86,6 +91,9 @@ public boolean onCreateOptionsMenu(Menu menu) {
keepScreenOnItem = menu.findItem(R.id.action_keepscreenon);
keepScreenOnItem.setChecked(keepScreenOn);

louderItem = menu.findItem(R.id.action_louder);
louderItem.setChecked(louder);

return true;
}

Expand Down Expand Up @@ -118,6 +126,20 @@ public void toggleScreenOnOff(MenuItem menuItem) {
}
}

public void toggleLouder(MenuItem menuItem) {
if (LOCAL_LOGV) { Log.v(TAG, "Beginning toggleLouder"); }

if (louder) {
Log.d(TAG, "Receive setting: louder off");
louder = false;
menuItem.setChecked(false);
} else {
Log.d(TAG, "Receiving setting: louder on");
louder = true;
menuItem.setChecked(true);
}
}

public void goMessage(View view) {
if (LOCAL_LOGV) { Log.v(TAG, "Beginning goMessage"); }

Expand All @@ -129,6 +151,7 @@ public void goMessage(View view) {
intent.putExtra(FAST_DURATION, fastDuration);
intent.putExtra(SLOW_DURATION, slowDuration);
intent.putExtra(KEEP_SCREEN_ON, keepScreenOn);
intent.putExtra(LOUDER, louder);

Log.d(TAG, "Starting run activity (warmupDuration="+Long.toString(warmupDuration)+", iterationCount="+Long.toString(iterationCount)+", fastDuration="+Long.toString(fastDuration)+", slowDuration="+Long.toString(slowDuration)+", keepScreenOn="+Boolean.toString(keepScreenOn)+")");
startActivity(intent);
Expand Down Expand Up @@ -179,6 +202,10 @@ private void setValuesToFields() {
// At first call, from onCreate, menu is not created yet
keepScreenOnItem.setChecked(keepScreenOn);
}
if (louderItem != null) {
// At first call, from onCreate, menu is not created yet
louderItem.setChecked(louder);
}
}

private void getValuesFromPrefs() {
Expand All @@ -189,7 +216,8 @@ private void getValuesFromPrefs() {
fastDuration = sharedPref.getLong(FAST_DURATION, FAST_DURATION_default);
slowDuration = sharedPref.getLong(SLOW_DURATION, SLOW_DURATION_default);
keepScreenOn = sharedPref.getBoolean(KEEP_SCREEN_ON, KEEP_SCREEN_ON_default);
Log.d(TAG, "Loaded prefs (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", keepScreenOn=" + Boolean.toString(keepScreenOn) + ")");
louder = sharedPref.getBoolean(LOUDER, LOUDER_default);
Log.d(TAG, "Loaded prefs (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", keepScreenOn=" + Boolean.toString(keepScreenOn) + ", louder=" + Boolean.toString(louder) + ")");
}

private void setValuesToPrefs() {
Expand All @@ -200,7 +228,8 @@ private void setValuesToPrefs() {
editor.putLong(FAST_DURATION, fastDuration);
editor.putLong(SLOW_DURATION, slowDuration);
editor.putBoolean(KEEP_SCREEN_ON, keepScreenOn);
Log.d(TAG, "Saving prefs (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", keepScreenOn=" + Boolean.toString(keepScreenOn) + ")");
editor.putBoolean(LOUDER, louder);
Log.d(TAG, "Saving prefs (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", keepScreenOn=" + Boolean.toString(keepScreenOn) + ", louder=" + Boolean.toString(louder) + ")");
editor.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ protected void onCreate(Bundle savedInstanceState) {
long fastDuration = intent.getLongExtra(MainActivity.FAST_DURATION, MainActivity.FAST_DURATION_default);
long slowDuration = intent.getLongExtra(MainActivity.SLOW_DURATION, MainActivity.SLOW_DURATION_default);
boolean keepScreenOn = intent.getBooleanExtra(MainActivity.KEEP_SCREEN_ON, MainActivity.KEEP_SCREEN_ON_default);
boolean louder = intent.getBooleanExtra(MainActivity.LOUDER, MainActivity.LOUDER_default);

Log.d(TAG, "Received intent (warmupDuration="+Long.toString(warmupDuration)+", iterationCount="+Long.toString(iterationCount)+", fastDuration="+Long.toString(fastDuration)+", slowDuration="+Long.toString(slowDuration)+", keepScreenOn="+Boolean.toString(keepScreenOn)+")");
Log.d(TAG, "Received intent (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration="+Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", keepScreenOn="+Boolean.toString(keepScreenOn) + ", louder=" + Boolean.toString(louder) + ")");

if (keepScreenOn) {
Log.d(TAG, "Setting screen to be kept on");
Expand All @@ -66,8 +67,9 @@ protected void onCreate(Bundle savedInstanceState) {
countdownService.putExtra(MainActivity.ITERATION_COUNT, iterationCount);
countdownService.putExtra(MainActivity.FAST_DURATION, fastDuration);
countdownService.putExtra(MainActivity.SLOW_DURATION, slowDuration);
countdownService.putExtra(MainActivity.LOUDER, louder);

Log.d(TAG, "Starting countdown service (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ")");
Log.d(TAG, "Starting countdown service (warmupDuration=" + Long.toString(warmupDuration) + ", iterationCount=" + Long.toString(iterationCount) + ", fastDuration=" + Long.toString(fastDuration) + ", slowDuration=" + Long.toString(slowDuration) + ", louder=" + Boolean.toString(louder) + ")");
startService(countdownService);
}

Expand Down
7 changes: 7 additions & 0 deletions trente30/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
android:onClick="toggleScreenOnOff"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="@+id/action_louder"
android:title="@string/action_louder"
android:orderInCategory="103"
android:onClick="toggleLouder"
android:checkable="true"
app:showAsAction="never" />
</menu>
1 change: 1 addition & 0 deletions trente30/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<string name="action_loadprefs">Charger les valeurs par défaut</string>
<string name="action_saveprefs">Enregistrer ces valeurs par défaut</string>
<string name="action_keepscreenon">Garder l\'écran allumé</string>
<string name="action_louder">Son plus fort et vibration</string>

<string name="warmup_duration_label">Durée d\'échauffement :</string>
<string name="iteration_count_label">Nombre de cycles :</string>
Expand Down
1 change: 1 addition & 0 deletions trente30/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<string name="action_loadprefs">Load defaults</string>
<string name="action_saveprefs">Save defaults</string>
<string name="action_keepscreenon">Keep screen on</string>
<string name="action_louder">Louder and vibration</string>

<string name="warmup_duration_label">Warmup duration:</string>
<string name="iteration_count_label">Number of iterations:</string>
Expand Down

0 comments on commit 90ddad8

Please sign in to comment.