Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/util/SharedPrefsUtils.java

Issue 29674689: Issue 6289 - Fix OnSharedPreferenceChangeListener (Closed)
Patch Set: Created Jan. 19, 2018, 2:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/MainPreferences.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 } 98 }
99 catch (ClassCastException e) 99 catch (ClassCastException e)
100 { 100 {
101 return defValues; 101 return defValues;
102 } 102 }
103 } 103 }
104 104
105 public static void registerOnSharedPreferenceChangeListener(Context context, 105 public static void registerOnSharedPreferenceChangeListener(Context context,
106 OnSharedPreferenceChangeListener listener) 106 OnSharedPreferenceChangeListener listener)
107 { 107 {
108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r( 108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r(listener);
109 new OnSharedPreferenceChangeListenerWrapper(listener)
110 );
111 } 109 }
112 110
113 public static void unregisterOnSharedPreferenceChangeListener(Context context, 111 public static void unregisterOnSharedPreferenceChangeListener(Context context,
114 OnSharedPreferenceChangeListener listener) 112 OnSharedPreferenceChangeListener listener)
115 { 113 {
116 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner( 114 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner(listener);
117 new OnSharedPreferenceChangeListenerWrapper(listener)
118 );
119 } 115 }
120 116
121 private static SharedPreferences getDefaultSharedPreferences(Context context) 117 private static SharedPreferences getDefaultSharedPreferences(Context context)
122 { 118 {
123 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); 119 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext());
124 } 120 }
125 121
126 private static class OnSharedPreferenceChangeListenerWrapper 122 public abstract static class OnSharedPreferenceChangeListener
anton 2018/01/22 05:41:13 are you sure that it's better to use class hierarc
diegocarloslima 2018/01/22 11:09:26 With an interface, we cannot make onSharedPreferen
127 implements SharedPreferences.OnSharedPreferenceChangeListener 123 implements SharedPreferences.OnSharedPreferenceChangeListener
128 { 124 {
129 125
130 private final OnSharedPreferenceChangeListener listener; 126 @Override
131 127 public final void onSharedPreferenceChanged(SharedPreferences sharedPreferen ces, String key)
132 OnSharedPreferenceChangeListenerWrapper(OnSharedPreferenceChangeListener lis tener) { 128 {
133 this.listener = listener; 129 this.onSharedPreferenceChanged(key);
134 } 130 }
135 131
136 @Override 132 protected abstract void onSharedPreferenceChanged(String key);
137 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, S tring key)
138 {
139 this.listener.onSharedPreferenceChanged(key);
140 }
141
142 @Override
143 public boolean equals(Object obj)
144 {
145 if (this == obj)
146 {
147 return true;
148 }
149 else if (obj instanceof OnSharedPreferenceChangeListenerWrapper)
150 {
151 return this.listener.equals(((OnSharedPreferenceChangeListenerWrapper) o bj).listener);
152 }
153 return false;
154 }
155
156 @Override
157 public int hashCode()
158 {
159 return this.listener.hashCode();
160 }
161 }
162
163 public interface OnSharedPreferenceChangeListener
164 {
165 void onSharedPreferenceChanged(String key);
166 } 133 }
167 } 134 }
OLDNEW
« no previous file with comments | « adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/MainPreferences.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld