Switch volume button default behavior

Bug: 8151863
Changed the default to do nothing and added a check to make sure that on
the first boot after an upgrade, it will switch "Snooze" to "Do nothing"

Change-Id: I778e3eb58f465f2c3fc5be55859874cca8ab8b62
diff --git a/res/xml/settings.xml b/res/xml/settings.xml
index 50565c8..916b947 100644
--- a/res/xml/settings.xml
+++ b/res/xml/settings.xml
@@ -64,6 +64,6 @@
             android:dialogTitle="@string/volume_button_setting_title"
             android:entries="@array/volume_button_setting_entries"
             android:entryValues="@array/volume_button_setting_values"
-            android:defaultValue="1" />
+            android:defaultValue="0" />
     </PreferenceCategory>
 </PreferenceScreen>
diff --git a/src/com/android/deskclock/AlarmAlertFullScreen.java b/src/com/android/deskclock/AlarmAlertFullScreen.java
index 5515930..8f1582c 100644
--- a/src/com/android/deskclock/AlarmAlertFullScreen.java
+++ b/src/com/android/deskclock/AlarmAlertFullScreen.java
@@ -52,7 +52,6 @@
     private final boolean LOG = true;
     // These defaults must match the values in res/xml/settings.xml
     private static final String DEFAULT_SNOOZE = "10";
-    private static final String DEFAULT_VOLUME_BEHAVIOR = "2";
     protected static final String SCREEN_OFF = "screen_off";
 
     protected Alarm mAlarm;
@@ -119,7 +118,7 @@
         final String vol =
                 PreferenceManager.getDefaultSharedPreferences(this)
                 .getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
-                        DEFAULT_VOLUME_BEHAVIOR);
+                        SettingsActivity.DEFAULT_VOLUME_BEHAVIOR);
         mVolumeBehavior = Integer.parseInt(vol);
 
         final Window win = getWindow();
diff --git a/src/com/android/deskclock/AlarmInitReceiver.java b/src/com/android/deskclock/AlarmInitReceiver.java
index e3a8278..1dd7bb3 100644
--- a/src/com/android/deskclock/AlarmInitReceiver.java
+++ b/src/com/android/deskclock/AlarmInitReceiver.java
@@ -27,6 +27,9 @@
 
 public class AlarmInitReceiver extends BroadcastReceiver {
 
+    // A flag that indicates that switching the volume button default was done
+    private static final String PREF_VOLUME_DEF_DONE = "vol_def_done";
+
     /**
      * Sets alarm on ACTION_BOOT_COMPLETED.  Resets alarm on
      * TIME_SET, TIMEZONE_CHANGED
@@ -52,6 +55,12 @@
                     Log.v("AlarmInitReceiver - Reset timers and clear stopwatch data");
                     TimerObj.resetTimersInSharedPrefs(prefs);
                     Utils.clearSwSharedPref(prefs);
+
+                    if (!prefs.getBoolean(PREF_VOLUME_DEF_DONE, false)) {
+                        // Fix the default
+                        Log.v("AlarmInitReceiver - checking volume button default");
+                        switchVolumeButtonDefault(prefs);
+                    }
                 }
                 Alarms.setNextAlert(context);
                 result.finish();
@@ -60,4 +69,24 @@
             }
         });
     }
+
+    private void switchVolumeButtonDefault(SharedPreferences prefs) {
+        SharedPreferences.Editor editor = prefs.edit();
+
+        // Get the volume button behavior setting
+        final String vol = prefs.getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
+	        SettingsActivity.DEFAULT_VOLUME_BEHAVIOR);
+        // If the setting is "Snooze", change it to "Do Nothing"
+        if (SettingsActivity.OLD_DEFAULT_VOLUME_BEHAVIOR.equals(vol)) {
+                editor.putString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
+                    SettingsActivity.DEFAULT_VOLUME_BEHAVIOR);
+            Log.v("AlarmInitReceiver - Reset volume button default");
+        } else {
+            Log.v("AlarmInitReceiver - No need to reset volume button default");
+        }
+
+        // Make sure we do it only once
+        editor.putBoolean(PREF_VOLUME_DEF_DONE, true);
+        editor.apply();
+    }
 }
diff --git a/src/com/android/deskclock/SettingsActivity.java b/src/com/android/deskclock/SettingsActivity.java
index ef2243f..9465670 100644
--- a/src/com/android/deskclock/SettingsActivity.java
+++ b/src/com/android/deskclock/SettingsActivity.java
@@ -64,6 +64,10 @@
     static final String KEY_VOLUME_BUTTONS =
             "volume_button_setting";
 
+    // Old and new default preferences, needed to switch the default on upgrade
+    public static final String DEFAULT_VOLUME_BEHAVIOR = "0";
+    public static final String OLD_DEFAULT_VOLUME_BEHAVIOR = "1";
+
     private static CharSequence[][] mTimezones;
     private long mTime;