|
| 1 | +/** |
| 2 | + * Copyright 2014 Zhenguo Jin (jingle1267@163.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.worthed.utils; |
| 17 | + |
| 18 | +import java.lang.reflect.InvocationTargetException; |
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
| 21 | +import android.annotation.SuppressLint; |
| 22 | +import android.content.Context; |
| 23 | +import android.content.SharedPreferences; |
| 24 | +import android.content.SharedPreferences.Editor; |
| 25 | +import android.preference.PreferenceManager; |
| 26 | + |
| 27 | +/** |
| 28 | + * 应用配置工具类 |
| 29 | + * @author jingle1267@163.com |
| 30 | + * |
| 31 | + */ |
| 32 | +@SuppressLint("CommitPrefEdits") |
| 33 | +public class SettingUtils { |
| 34 | + |
| 35 | + private final static boolean DEBUG = true; |
| 36 | + private final static String TAG = "SettingUtils"; |
| 37 | + |
| 38 | + public static boolean contains(Context context, int resId) { |
| 39 | + return contains(context, context.getString(resId)); |
| 40 | + } |
| 41 | + |
| 42 | + public static boolean contains(Context context, String key) { |
| 43 | + SharedPreferences prefs = PreferenceManager |
| 44 | + .getDefaultSharedPreferences(context); |
| 45 | + return prefs.contains(key); |
| 46 | + } |
| 47 | + |
| 48 | + public static void remove(Context context, int resId) { |
| 49 | + remove(context, context.getString(resId)); |
| 50 | + } |
| 51 | + |
| 52 | + public static void remove(Context context, String key) { |
| 53 | + SharedPreferences prefs = PreferenceManager |
| 54 | + .getDefaultSharedPreferences(context); |
| 55 | + SharedPreferences.Editor editor = prefs.edit(); |
| 56 | + editor.remove(key); |
| 57 | + commitOrApply(editor); |
| 58 | + } |
| 59 | + |
| 60 | + public static void set(Context context, int resId, boolean value) { |
| 61 | + set(context, context.getString(resId), value); |
| 62 | + } |
| 63 | + |
| 64 | + public static void set(Context context, String key, boolean value) { |
| 65 | + SharedPreferences prefs = PreferenceManager |
| 66 | + .getDefaultSharedPreferences(context); |
| 67 | + SharedPreferences.Editor editor = prefs.edit(); |
| 68 | + editor.putBoolean(key, value); |
| 69 | + commitOrApply(editor); |
| 70 | + } |
| 71 | + |
| 72 | + public static void set(Context context, int resId, float value) { |
| 73 | + set(context, context.getString(resId), value); |
| 74 | + } |
| 75 | + |
| 76 | + public static void set(Context context, String key, float value) { |
| 77 | + SharedPreferences prefs = PreferenceManager |
| 78 | + .getDefaultSharedPreferences(context); |
| 79 | + SharedPreferences.Editor editor = prefs.edit(); |
| 80 | + editor.putFloat(key, value); |
| 81 | + commitOrApply(editor); |
| 82 | + } |
| 83 | + |
| 84 | + public static void set(Context context, int resId, int value) { |
| 85 | + set(context, context.getString(resId), value); |
| 86 | + } |
| 87 | + |
| 88 | + public static void set(Context context, String key, int value) { |
| 89 | + SharedPreferences prefs = PreferenceManager |
| 90 | + .getDefaultSharedPreferences(context); |
| 91 | + SharedPreferences.Editor editor = prefs.edit(); |
| 92 | + editor.putInt(key, value); |
| 93 | + commitOrApply(editor); |
| 94 | + } |
| 95 | + |
| 96 | + public static void set(Context context, int resId, long value) { |
| 97 | + set(context, context.getString(resId), value); |
| 98 | + } |
| 99 | + |
| 100 | + public static void set(Context context, String key, long value) { |
| 101 | + SharedPreferences prefs = PreferenceManager |
| 102 | + .getDefaultSharedPreferences(context); |
| 103 | + SharedPreferences.Editor editor = prefs.edit(); |
| 104 | + editor.putLong(key, value); |
| 105 | + commitOrApply(editor); |
| 106 | + } |
| 107 | + |
| 108 | + public static void set(Context context, int resId, String value) { |
| 109 | + set(context, context.getString(resId), value); |
| 110 | + } |
| 111 | + |
| 112 | + public static void set(Context context, String key, String value) { |
| 113 | + SharedPreferences prefs = PreferenceManager |
| 114 | + .getDefaultSharedPreferences(context); |
| 115 | + SharedPreferences.Editor editor = prefs.edit(); |
| 116 | + editor.putString(key, value); |
| 117 | + commitOrApply(editor); |
| 118 | + } |
| 119 | + |
| 120 | + public static boolean get(Context context, int resId, boolean defValue) { |
| 121 | + return get(context, context.getString(resId), defValue); |
| 122 | + } |
| 123 | + |
| 124 | + public static boolean get(Context context, String key, boolean defValue) { |
| 125 | + SharedPreferences prefs = PreferenceManager |
| 126 | + .getDefaultSharedPreferences(context); |
| 127 | + return prefs.getBoolean(key, defValue); |
| 128 | + } |
| 129 | + |
| 130 | + public static float get(Context context, int resId, float defValue) { |
| 131 | + return get(context, context.getString(resId), defValue); |
| 132 | + } |
| 133 | + |
| 134 | + public static float get(Context context, String key, float defValue) { |
| 135 | + SharedPreferences prefs = PreferenceManager |
| 136 | + .getDefaultSharedPreferences(context); |
| 137 | + return prefs.getFloat(key, defValue); |
| 138 | + } |
| 139 | + |
| 140 | + public static int get(Context context, int resId, int defValue) { |
| 141 | + return get(context, context.getString(resId), defValue); |
| 142 | + } |
| 143 | + |
| 144 | + public static int get(Context context, String key, int defValue) { |
| 145 | + SharedPreferences prefs = PreferenceManager |
| 146 | + .getDefaultSharedPreferences(context); |
| 147 | + return prefs.getInt(key, defValue); |
| 148 | + } |
| 149 | + |
| 150 | + public static long get(Context context, int resId, long defValue) { |
| 151 | + return get(context, context.getString(resId), defValue); |
| 152 | + } |
| 153 | + |
| 154 | + public static long get(Context context, String key, long defValue) { |
| 155 | + SharedPreferences prefs = PreferenceManager |
| 156 | + .getDefaultSharedPreferences(context); |
| 157 | + return prefs.getLong(key, defValue); |
| 158 | + } |
| 159 | + |
| 160 | + public static String get(Context context, int resId, String defValue) { |
| 161 | + return get(context, context.getString(resId), defValue); |
| 162 | + } |
| 163 | + |
| 164 | + public static String get(Context context, String key, String defValue) { |
| 165 | + SharedPreferences prefs = PreferenceManager |
| 166 | + .getDefaultSharedPreferences(context); |
| 167 | + return prefs.getString(key, defValue); |
| 168 | + } |
| 169 | + |
| 170 | + public static SharedPreferences.Editor getEditor(Context context) { |
| 171 | + return PreferenceManager.getDefaultSharedPreferences(context).edit(); |
| 172 | + } |
| 173 | + |
| 174 | + // //////////////////////////////////////////////////////////////////////// |
| 175 | + // Apply method via reflection |
| 176 | + |
| 177 | + private static final Method APPLY_METHOD = findApplyMethod(); |
| 178 | + |
| 179 | + private static Method findApplyMethod() { |
| 180 | + try { |
| 181 | + Class<Editor> cls = SharedPreferences.Editor.class; |
| 182 | + return cls.getMethod("apply"); |
| 183 | + } catch (NoSuchMethodException unused) { |
| 184 | + if (DEBUG) { |
| 185 | + LogUtils.d(TAG, "Failed to retrieve Editor.apply(); probably doesn't exist on this phone's OS. Using Editor.commit() instead."); |
| 186 | + } |
| 187 | + return null; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public static void commitOrApply(Editor editor) { |
| 192 | + if (APPLY_METHOD != null) { |
| 193 | + try { |
| 194 | + APPLY_METHOD.invoke(editor); |
| 195 | + return; |
| 196 | + } catch (InvocationTargetException e) { |
| 197 | + if (DEBUG) { |
| 198 | + LogUtils.d(TAG, "Failed while using Editor.apply(). Using Editor.commit() instead."); |
| 199 | + } |
| 200 | + } catch (IllegalAccessException e) { |
| 201 | + if (DEBUG) { |
| 202 | + LogUtils.d(TAG, "Failed while using Editor.apply(). Using Editor.commit() instead."); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + editor.commit(); |
| 208 | + } |
| 209 | +} |
0 commit comments