Skip to content

Commit

Permalink
Merge pull request #36 from NightscoutFoundation/tzachi-snooze-volume2
Browse files Browse the repository at this point in the history
Return to the previous volume in the case that an alert has been snoozed and some changes to the armtimer
  • Loading branch information
jamorham authored Nov 16, 2016
2 parents 0d83955 + c13eb40 commit 9a8756e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public class AlertPlayer {

private final static String TAG = AlertPlayer.class.getSimpleName();
private MediaPlayer mediaPlayer;
int volumeBeforeAlert;
int volumeForThisAlert;
int volumeBeforeAlert = -1;
int volumeForThisAlert = -1;

final static int ALERT_PROFILE_HIGH = 1;
final static int ALERT_PROFILE_ASCENDING = 2;
Expand Down Expand Up @@ -147,6 +147,7 @@ public synchronized void stopAlert(Context ctx, boolean ClearData, boolean clear
mediaPlayer.release();
mediaPlayer = null;
}
revertCurrentVolume(ctx);
}

// default signature for user initiated interactive snoozes only
Expand Down Expand Up @@ -308,14 +309,7 @@ private synchronized void PlayFile(final Context ctx, String FileName, float Vol
@Override
public void onCompletion(MediaPlayer mp) {
Log.i(TAG, "PlayFile: onCompletion called (finished playing) ");
AudioManager manager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.i(TAG, "After playing volumeBeforeAlert " + volumeBeforeAlert + " volumeForThisAlert " + volumeForThisAlert
+ " currentVolume " + currentVolume);
if (volumeForThisAlert == currentVolume) {
// If the user has changed the volume, don't change it again.
manager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeBeforeAlert, 0);
}
revertCurrentVolume(ctx);
}
});
Log.i(TAG, "PlayFile: calling mediaPlayer.start() ");
Expand All @@ -331,6 +325,20 @@ public void onCompletion(MediaPlayer mp) {
Log.wtf(TAG, "PlayFile: Starting an alert failed, what should we do !!!");
}
}

private void revertCurrentVolume(final Context ctx) {
AudioManager manager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.i(TAG, "revertCurrentVolume volumeBeforeAlert " + volumeBeforeAlert + " volumeForThisAlert " + volumeForThisAlert
+ " currentVolume " + currentVolume);
if (volumeForThisAlert == currentVolume && (volumeBeforeAlert != -1) && (volumeForThisAlert != -1)) {
// If the user has changed the volume, don't change it again.
manager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeBeforeAlert, 0);
}
volumeBeforeAlert = -1;
volumeForThisAlert = - 1;

}

private PendingIntent notificationIntent(Context ctx, Intent intent){
return PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private long calcuatleArmTime(Context ctx, long now, boolean unclearAlert) {
Long wakeTimeUnclear = calcuatleArmTimeUnclearalert(ctx, now, unclearAlert);
Long wakeTime = Math.min(wakeTimeBg, wakeTimeUnclear);

Log.d("Notifications" , "calcuatleArmTimeBg returning: "+ new Date(wakeTime) +" in " + (wakeTime - now)/60000d + " minutes");
Log.d("Notifications" , "calcuatleArmTime returning: "+ new Date(wakeTime) +" in " + (wakeTime - now)/60000d + " minutes");
return wakeTime;

/*
Expand Down Expand Up @@ -469,12 +469,15 @@ private void ArmTimer(Context ctx, boolean unclearAlert) {
long wakeTime = calcuatleArmTime(ctx, now, unclearAlert);


if(wakeTime < now || wakeTime >= now + 6 * 60000 ) {
if(wakeTime < now ) {
Log.e("Notifications" , "ArmTimer recieved a negative time, will fire in 6 minutes");
wakeTime = now + 6 * 60000;
} else if (wakeTime == now) {
} else if (wakeTime >= now + 6 * 60000) {
Log.i("Notifications" , "ArmTimer recieved a biger time, will fire in 6 minutes");
wakeTime = now + 6 * 60000;
} else if (wakeTime == now) {
Log.e("Notifications", "should arm right now, waiting one more second to avoid infinitue loop");
wakeTime = now + 1;
wakeTime = now + 1000;
}

AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
Expand Down

0 comments on commit 9a8756e

Please sign in to comment.