Fix preference copy migration and incorrect HDR+ sticky bit

A sticky bit upgrader particularly for HDR+ was looking in
globals but was actually a module setting. The copy migrator
was not properly handling booleans! Modified the copy migrator
to properly handle supported types and removed the custom
incorrect upgrader.

Bug: 17875895
Change-Id: Id36abc429d12a77c5462e5c49ef7786e3d092f2c
(cherry picked from commit 90672aa08379e598426c12b529a86afd4b61011e)
diff --git a/src/com/android/camera/settings/AppUpgrader.java b/src/com/android/camera/settings/AppUpgrader.java
index bb15d38..64dad64 100644
--- a/src/com/android/camera/settings/AppUpgrader.java
+++ b/src/com/android/camera/settings/AppUpgrader.java
@@ -209,14 +209,6 @@
             }
         }
 
-        // Request return to HDR+: boolean -> String, from module.
-        if (defaultPreferences.contains(Keys.KEY_REQUEST_RETURN_HDR_PLUS)) {
-            boolean requestReturnHdrPlus = removeBoolean(defaultPreferences,
-                    Keys.KEY_REQUEST_RETURN_HDR_PLUS);
-            settingsManager.set(SettingsManager.SCOPE_GLOBAL, Keys.KEY_REQUEST_RETURN_HDR_PLUS,
-                    requestReturnHdrPlus);
-        }
-
         // Should show refocus viewer cling: boolean -> String, from default.
         if (defaultPreferences.contains(Keys.KEY_SHOULD_SHOW_REFOCUS_VIEWER_CLING)) {
             boolean shouldShowRefocusViewer = removeBoolean(defaultPreferences,
@@ -328,6 +320,8 @@
     /**
      * Part of the AOSP upgrade path, copies all of the keys and values in a
      * SharedPreferences file to another SharedPreferences file, as Strings.
+     * Settings that are not a known supported format (int/boolean/String)
+     * are dropped with warning.
      */
     private void copyPreferences(SharedPreferences oldPrefs,
             SharedPreferences newPrefs) {
@@ -335,10 +329,31 @@
         for (Map.Entry<String, ?> entry : entries.entrySet()) {
             String key = entry.getKey();
             Object value = entry.getValue();
-            if (value != null) {
-                newPrefs.edit().putString(key, String.valueOf(value)).apply();
-            } else {
+            if (value == null) {
                 Log.w(TAG, "skipped upgrade for null key " + key);
+            } else if (value instanceof Boolean) {
+                String boolValue = SettingsManager.convert((Boolean) value);
+                newPrefs.edit().putString(key, boolValue).apply();
+            } else if (value instanceof Integer) {
+                String intValue = SettingsManager.convert((Integer) value);
+                newPrefs.edit().putString(key, intValue).apply();
+            } else if (value instanceof Long){
+                // New SettingsManager only supports int values. Attempt to
+                // recover any longs which happen to be present if they are
+                // within int range.
+                long longValue = (Long) value;
+                if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) {
+                    String intValue = SettingsManager.convert((int) longValue);
+                    newPrefs.edit().putString(key, intValue).apply();
+                } else {
+                    Log.w(TAG, "skipped upgrade for out of bounds long key " +
+                            key + " : " + longValue);
+                }
+            } else if (value instanceof String){
+                newPrefs.edit().putString(key, (String) value).apply();
+            } else {
+                Log.w(TAG,"skipped upgrade for unrecognized key type " +
+                        key + " : " + value.getClass());
             }
         }
     }
diff --git a/src/com/android/camera/settings/SettingsManager.java b/src/com/android/camera/settings/SettingsManager.java
index 2418882..297bacb 100644
--- a/src/com/android/camera/settings/SettingsManager.java
+++ b/src/com/android/camera/settings/SettingsManager.java
@@ -433,7 +433,7 @@
      * to String occurs before this value is stored in SharedPreferences.
      */
     public void set(String scope, String key, int value) {
-        set(scope, key, Integer.toString(value));
+        set(scope, key, convert(value));
     }
 
     /**
@@ -442,7 +442,7 @@
      * stored in SharedPreferences.
      */
     public void set(String scope, String key, boolean value) {
-        set(scope, key, value ? "1" : "0");
+        set(scope, key, convert(value));
     }
 
     /**
@@ -504,4 +504,26 @@
         SharedPreferences preferences = getPreferencesFromScope(scope);
         preferences.edit().remove(key).apply();
     }
+
+    /**
+     * Package private conversion method to turn ints into preferred
+     * String storage format.
+     *
+     * @param value int to be stored in Settings
+     * @return String which represents the int
+     */
+    static String convert(int value) {
+        return Integer.toString(value);
+    }
+
+    /**
+     * Package private conversion method to turn booleans into preferred
+     * String storage format.
+     *
+     * @param value boolean to be stored in Settings
+     * @return String which represents the boolean
+     */
+    static String convert(boolean value) {
+        return value ? "1" : "0";
+    }
 }