diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java index eba2616e4b..d7b8bc1a99 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java @@ -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; @@ -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 @@ -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() "); @@ -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); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java index 5de64d242f..1c0eae924b 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java @@ -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; /* @@ -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);