From 371182c3a99a21f1f0ae1bb62c8127bded7b70ea Mon Sep 17 00:00:00 2001
From: Tzachi Dar <tzachi.dar@gmail.com>
Date: Wed, 9 Nov 2016 09:18:42 +0200
Subject: [PATCH 1/3] Fix error message. Don't print error negative time when
 not needed.

Signed-off-by: Tzachi Dar <tzachi.dar@gmail.com>
---
 .../dexdrip/UtilityModels/Notifications.java             | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

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 6bdcd7873f..137abc312c 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,10 +469,13 @@ 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;
         }

From 2922d043d95df24a456e3fb3e2e3b712426b9099 Mon Sep 17 00:00:00 2001
From: Tzachi Dar <tzachi.dar@gmail.com>
Date: Wed, 9 Nov 2016 09:23:23 +0200
Subject: [PATCH 2/3] Sleep for 1 sec instead of 1ms.

Signed-off-by: Tzachi Dar <tzachi.dar@gmail.com>
---
 .../com/eveningoutpost/dexdrip/UtilityModels/Notifications.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 137abc312c..5dd1094348 100644
--- a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java
+++ b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java
@@ -477,7 +477,7 @@ private void ArmTimer(Context ctx, boolean unclearAlert) {
              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);

From c13eb40e4add4639f00ca500085d46b4584050a8 Mon Sep 17 00:00:00 2001
From: Tzachi Dar <tzachi.dar@gmail.com>
Date: Wed, 9 Nov 2016 09:25:16 +0200
Subject: [PATCH 3/3] Return to the previous volume after an alert was snoozed.

Signed-off-by: Tzachi Dar <tzachi.dar@gmail.com>
---
 .../dexdrip/UtilityModels/AlertPlayer.java    | 28 ++++++++++++-------
 1 file changed, 18 insertions(+), 10 deletions(-)

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);