/*
 * Copyright (C)  Tony Green, LitePal Framework 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 org.litepal.util;

import org.litepal.LitePalApplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * LitePal used shared preferences a lot for storing versions and a lot of other
 * necessary values. This utility helps LitePal save and read each key-value
 * pairs from shared preferences file.
 * 
 * @author Tony Green
 * @since 1.0
 */
public class SharedUtil {

	private static final String VERSION = "litepal_version";

	private static final String LITEPAL_PREPS = "litepal_prefs";

	/**
	 * Each time database upgrade, the version of database stored in shared
	 * preference will update.
	 * @param extraKeyName
	 * 			Pass the name of the using database usually. Pass null if it's default database.
	 * @param newVersion
     *          new version of database
	 */
	public static void updateVersion(String extraKeyName, int newVersion) {
		SharedPreferences.Editor sEditor = LitePalApplication.getContext()
				.getSharedPreferences(LITEPAL_PREPS, Context.MODE_PRIVATE).edit();
		if (TextUtils.isEmpty(extraKeyName)) {
			sEditor.putInt(VERSION, newVersion);
		} else {
			sEditor.putInt(VERSION + extraKeyName, newVersion);
		}
		sEditor.apply();
	}

	/**
	 * Get the last database version.
	 * @param extraKeyName
	 * 			Pass the name of the using database usually. Pass null if it's default database.
	 * @return the last database version
	 */
	public static int getLastVersion(String extraKeyName) {
		SharedPreferences sPref = LitePalApplication.getContext().getSharedPreferences(
				LITEPAL_PREPS, Context.MODE_PRIVATE);
		if (TextUtils.isEmpty(extraKeyName)) {
			return sPref.getInt(VERSION, 0);
		} else {
			return sPref.getInt(VERSION + extraKeyName, 0);
		}
	}

}

@@ -1804,32 +1804,38 @@
 putInt(VERSION +
+ %22_%22 +
  extraKeyName, n
@@ -2369,16 +2369,22 @@
 ERSION +
+ %22_%22 +
  extraKe

Modify the shared key for database version.