summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/settings/SettingsUpgrader.java
diff options
context:
space:
mode:
authorAlan Newberger <alann@google.com>2014-08-05 09:56:17 -0700
committerAlan Newberger <alann@google.com>2014-08-12 10:04:44 -0700
commit2307b60abdf0af8639d71ba53cf10fdaf8dfe7e7 (patch)
treeb6974363001ce3d4202bffebf4807c936e944283 /src/com/android/camera/settings/SettingsUpgrader.java
parent967b782837a22974f565f5bbc71bef7bc05fc878 (diff)
downloadandroid_packages_apps_Camera2-2307b60abdf0af8639d71ba53cf10fdaf8dfe7e7.tar.gz
android_packages_apps_Camera2-2307b60abdf0af8639d71ba53cf10fdaf8dfe7e7.tar.bz2
android_packages_apps_Camera2-2307b60abdf0af8639d71ba53cf10fdaf8dfe7e7.zip
Refactor Upgrade/UpgradeAosp into SettingsUpgrader
This CL does three things: - removes the Upgrade/UpgradeAosp classes which were bunches of statics, in favor of a SettingsUpgrader abstract class that can be overridden - moves 'upgrade to strings' hack logic into AppUpgrader so it only is checked and run once, not for any of the former UpgradeSteps. logic used to be in Upgrade and run regardless of key, when it was using detection only meant for KEY.KEY_UPGRADE_VERSION - adds existence checks for all hack in-place key string upgrades so they are no-ops if the key was not persisted in the first place. Bug: 16573087 Change-Id: Ife42f7aa4a7611ba2d143410a3450057b7464901
Diffstat (limited to 'src/com/android/camera/settings/SettingsUpgrader.java')
-rw-r--r--src/com/android/camera/settings/SettingsUpgrader.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/com/android/camera/settings/SettingsUpgrader.java b/src/com/android/camera/settings/SettingsUpgrader.java
new file mode 100644
index 000000000..2e6457db2
--- /dev/null
+++ b/src/com/android/camera/settings/SettingsUpgrader.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.camera.settings;
+
+import android.content.SharedPreferences;
+
+import com.android.camera.debug.Log;
+
+/**
+ * The SettingsUpgrader class can be used to define an upgrade flow that
+ * executes upgrade logic to a target version when a version number has changed.
+ */
+public abstract class SettingsUpgrader {
+ private static final Log.Tag TAG = new Log.Tag("SettingsUpgrader");
+
+ private final String mVersionKey;
+ private final int mTargetVersion;
+
+ public SettingsUpgrader(String versionKey, int targetVersion) {
+ mVersionKey = versionKey;
+ mTargetVersion = targetVersion;
+ }
+
+ /**
+ * Execute an upgrade callback if an upgrade version has changed. Third
+ * party modules also use this to upgrade settings local to them.
+ */
+ public void upgrade(SettingsManager settingsManager) {
+ int lastVersion = getLastVersion(settingsManager);
+ if (lastVersion != mTargetVersion) {
+ upgrade(settingsManager, lastVersion, mTargetVersion);
+ }
+ settingsManager.set(SettingsManager.SCOPE_GLOBAL, mVersionKey, mTargetVersion);
+ }
+
+ /**
+ * Perform upgrades to bring any settings up to date to the version
+ * specified in currentVersion, where the settings were last upgraded to
+ * lastVersion. Typically an Upgrader will check whether lastVersion is less
+ * than some known version for a particular setting, and apply upgrade logic
+ * if lastVersion is less than that known version.
+ */
+ protected abstract void upgrade(SettingsManager settingsManager, int lastVersion,
+ int targetVersion);
+
+ /**
+ * Retrieve the last persisted version for the particular upgrader.
+ * Typically will be stored in SCOPE_GLOBAL in SettingsManager, but an
+ * Upgrader may need to perform an upgrade analysis on the version
+ * persistence itself and should do so here.
+ *
+ * @throws a {@link ClassCastException} if the value for Version is not a
+ * String
+ */
+ protected int getLastVersion(SettingsManager settingsManager) {
+ return settingsManager.getInteger(SettingsManager.SCOPE_GLOBAL, mVersionKey);
+ }
+
+ /**
+ * A helper function that is used to remove a setting stored as a boolean,
+ * and return the value that was removed.
+ * <p>
+ * This is used in the upgrade path to change all underlying
+ * SharedPreferences values to Strings. It can be used by third party
+ * modules to upgrade their boolean settings to Strings.
+ */
+ protected boolean removeBoolean(SharedPreferences oldPreferencesLocation, String key) {
+ boolean value = oldPreferencesLocation.getBoolean(key, false);
+ oldPreferencesLocation.edit().remove(key);
+ return value;
+ }
+
+ /**
+ * A helper function that is used to remove a setting stored as an Integer,
+ * and return the value that was removed.
+ * <p>
+ * This is used in the upgrade path to change all underlying
+ * SharedPreferences values to Strings. It can be used by third party
+ * modules to upgrade their Integer settings to Strings.
+ */
+ protected int removeInteger(SharedPreferences oldPreferencesLocation, String key) {
+ int value = oldPreferencesLocation.getInt(key, 0);
+ oldPreferencesLocation.edit().remove(key);
+ return value;
+ }
+
+}