/* * Copyright (C) 2012 Paul Watts (paulcwatts@gmail.com) * * 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.gpstest.util; import com.android.gpstest.Application; import android.annotation.TargetApi; import android.content.SharedPreferences; import android.os.Build; /** * A class containing utility methods related to preferences */ public class PreferenceUtils { @TargetApi(9) public static void saveString(SharedPreferences prefs, String key, String value) { SharedPreferences.Editor edit = prefs.edit(); edit.putString(key, value); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { edit.apply(); } else { edit.commit(); } } public static void saveString(String key, String value) { saveString(Application.getPrefs(), key, value); } @TargetApi(9) public static void saveInt(SharedPreferences prefs, String key, int value) { SharedPreferences.Editor edit = prefs.edit(); edit.putInt(key, value); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { edit.apply(); } else { edit.commit(); } } public static void saveInt(String key, int value) { saveInt(Application.getPrefs(), key, value); } @TargetApi(9) public static void saveLong(SharedPreferences prefs, String key, long value) { SharedPreferences.Editor edit = prefs.edit(); edit.putLong(key, value); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { edit.apply(); } else { edit.commit(); } } public static void saveLong(String key, long value) { saveLong(Application.getPrefs(), key, value); } @TargetApi(9) public static void saveBoolean(SharedPreferences prefs, String key, boolean value) { SharedPreferences.Editor edit = prefs.edit(); edit.putBoolean(key, value); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { edit.apply(); } else { edit.commit(); } } public static void saveBoolean(String key, boolean value) { saveBoolean(Application.getPrefs(), key, value); } @TargetApi(9) public static void saveFloat(SharedPreferences prefs, String key, float value) { SharedPreferences.Editor edit = prefs.edit(); edit.putFloat(key, value); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { edit.apply(); } else { edit.commit(); } } public static void saveFloat(String key, float value) { saveFloat(Application.getPrefs(), key, value); } @TargetApi(9) public static void saveDouble(SharedPreferences prefs, String key, double value) { SharedPreferences.Editor edit = prefs.edit(); edit.putLong(key, Double.doubleToRawLongBits(value)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { edit.apply(); } else { edit.commit(); } } @TargetApi(9) public static void saveDouble(String key, double value) { saveDouble(Application.getPrefs(), key, value); } /** * Gets a double for the provided key from preferences, or the default value if the preference * doesn't currently have a value * * @param key key for the preference * @param defaultValue the default value to return if the key doesn't have a value * @return a double from preferences, or the default value if it doesn't exist */ public static Double getDouble(String key, double defaultValue) { if (!Application.getPrefs().contains(key)) { return defaultValue; } return Double.longBitsToDouble(Application.getPrefs().getLong(key, 0)); } public static String getString(String key) { return Application.getPrefs().getString(key, null); } public static long getLong(String key, long defaultValue) { return Application.getPrefs().getLong(key, defaultValue); } public static float getFloat(String key, float defaultValue) { return Application.getPrefs().getFloat(key, defaultValue); } }