 Issue 29449601:
  Issue 5235 - Refactoring on SharedPrefs  (Closed)
    
  
    Issue 29449601:
  Issue 5235 - Refactoring on SharedPrefs  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 package org.adblockplus.sbrowser.contentblocker.util; | |
| 19 | |
| 20 import android.content.Context; | |
| 21 import android.content.SharedPreferences; | |
| 22 import android.preference.PreferenceManager; | |
| 23 | |
| 24 import java.util.Set; | |
| 25 | |
| 26 public class SharedPrefsUtils | |
| 27 { | |
| 28 | |
| 29 public static void putBoolean(Context context, int keyResId, boolean value) | |
| 
anton
2017/05/29 05:33:04
general suggest for all similar methods.
I's assu
 
diegocarloslima
2017/05/31 14:32:09
Well for instance, the goal was to standardize the
 | |
| 30 { | |
| 31 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | |
| 32 editor.putBoolean(context.getString(keyResId), value).apply(); | |
| 33 } | |
| 34 | |
| 35 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) | |
| 36 { | |
| 37 final SharedPreferences preferences = getDefaultSharedPreferences(context); | |
| 38 try | |
| 39 { | |
| 40 return preferences.getBoolean(context.getString(keyResId), defValue); | |
| 41 } | |
| 42 catch (ClassCastException e) | |
| 43 { | |
| 44 return defValue; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 public static void putInt(Context context, int keyResId, int value) | |
| 49 { | |
| 50 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | |
| 51 editor.putInt(context.getString(keyResId), value).apply(); | |
| 52 } | |
| 53 | |
| 54 public static int getInt(Context context, int keyResId, int defValue) | |
| 55 { | |
| 56 final SharedPreferences preferences = getDefaultSharedPreferences(context); | |
| 57 try | |
| 58 { | |
| 59 return preferences.getInt(context.getString(keyResId), defValue); | |
| 60 } | |
| 61 catch (ClassCastException e) | |
| 62 { | |
| 63 return defValue; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 public static void putString(Context context, int keyResId, String value) | |
| 68 { | |
| 69 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | |
| 70 editor.putString(context.getString(keyResId), value).apply(); | |
| 71 } | |
| 72 | |
| 73 public static String getString(Context context, int keyResId, String defValue) | |
| 74 { | |
| 75 final SharedPreferences preferences = getDefaultSharedPreferences(context); | |
| 76 try | |
| 77 { | |
| 78 return preferences.getString(context.getString(keyResId), defValue); | |
| 79 } | |
| 80 catch (ClassCastException e) | |
| 81 { | |
| 82 return defValue; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 public static void putStringSet(Context context, int keyResId, Set<String> val ues) | |
| 87 { | |
| 88 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | |
| 89 editor.putStringSet(context.getString(keyResId), values).apply(); | |
| 90 } | |
| 91 | |
| 92 public static Set<String> getStringSet(Context context, int keyResId, Set<Stri ng> defValues) | |
| 93 { | |
| 94 final SharedPreferences preferences = getDefaultSharedPreferences(context); | |
| 95 try | |
| 96 { | |
| 97 return preferences.getStringSet(context.getString(keyResId), defValues); | |
| 98 } | |
| 99 catch (ClassCastException e) | |
| 100 { | |
| 101 return defValues; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 public static void registerOnSharedPreferenceChangeListener(Context context, | |
| 106 OnSharedPreferenceChangeListener listener) | |
| 107 { | |
| 108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r( | |
| 109 new OnSharedPreferenceChangeListenerWrapper(listener) | |
| 110 ); | |
| 111 } | |
| 112 | |
| 113 public static void unregisterOnSharedPreferenceChangeListener(Context context, | |
| 114 OnSharedPreferenceChangeListener listener) | |
| 115 { | |
| 116 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner( | |
| 117 new OnSharedPreferenceChangeListenerWrapper(listener) | |
| 
anton
2017/05/29 05:33:04
Creating new wrapper while unregistering looks str
 
diegocarloslima
2017/05/31 14:32:09
Really nice catch! I totally overlooked this. I cr
 | |
| 118 ); | |
| 119 } | |
| 120 | |
| 121 private static SharedPreferences getDefaultSharedPreferences(Context context) | |
| 122 { | |
| 123 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); | |
| 124 } | |
| 125 | |
| 126 private static class OnSharedPreferenceChangeListenerWrapper | |
| 127 implements SharedPreferences.OnSharedPreferenceChangeListener | |
| 128 { | |
| 129 | |
| 130 private final OnSharedPreferenceChangeListener listener; | |
| 131 | |
| 132 OnSharedPreferenceChangeListenerWrapper(OnSharedPreferenceChangeListener lis tener) { | |
| 133 this.listener = listener; | |
| 134 } | |
| 135 | |
| 136 @Override | |
| 137 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, S tring key) | |
| 138 { | |
| 139 this.listener.onSharedPreferenceChanged(key); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 public interface OnSharedPreferenceChangeListener | |
| 144 { | |
| 145 void onSharedPreferenceChanged(String key); | |
| 146 } | |
| 147 } | |
| OLD | NEW |