OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present 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.libadblockplus.android.settings; | |
19 | |
20 import android.content.SharedPreferences; | |
21 | |
22 import org.adblockplus.libadblockplus.android.ConnectionType; | |
23 import org.adblockplus.libadblockplus.android.Subscription; | |
24 | |
25 import java.util.LinkedList; | |
26 import java.util.List; | |
27 | |
28 /** | |
29 * Settings storage implementation in Shared Preferences | |
30 */ | |
31 public class SharedPrefsStorage extends AdblockSettingsStorage | |
32 { | |
33 private static final String SETTINGS_ENABLED_KEY = "enabled"; | |
34 private static final String SETTINGS_AA_ENABLED_KEY = "aa_enabled"; | |
35 private static final String SETTINGS_SUBSCRIPTIONS_KEY = "subscriptions"; | |
36 private static final String SETTINGS_SUBSCRIPTION_KEY = "subscription"; | |
37 private static final String SETTINGS_SUBSCRIPTION_URL_KEY = "url"; | |
38 private static final String SETTINGS_SUBSCRIPTION_SPECIALIZATION_KEY = "specia
lization"; | |
39 private static final String SETTINGS_SUBSCRIPTION_TITLE_KEY = "title"; | |
40 private static final String SETTINGS_WL_DOMAINS_KEY = "whitelisted_domains"; | |
41 private static final String SETTINGS_WL_DOMAIN_KEY = "domain"; | |
42 private static final String SETTINGS_ALLOWED_CONNECTION_TYPE_KEY = "allowed_co
nnection_type"; | |
43 | |
44 private SharedPreferences prefs; | |
45 private boolean commit = true; | |
46 | |
47 public SharedPrefsStorage(SharedPreferences prefs) | |
48 { | |
49 this.prefs = prefs; | |
50 } | |
51 | |
52 public boolean isCommit() | |
53 { | |
54 return commit; | |
55 } | |
56 | |
57 /** | |
58 * Do commit the changes in save() before return | |
59 * | |
60 * @param commit `true` to commit, `false` | |
61 */ | |
62 public void setCommit(boolean commit) | |
63 { | |
64 this.commit = commit; | |
65 } | |
66 | |
67 @Override | |
68 public AdblockSettings load() | |
69 { | |
70 if (!prefs.contains(SETTINGS_ENABLED_KEY)) | |
71 { | |
72 // settings were not saved yet | |
73 return null; | |
74 } | |
75 | |
76 AdblockSettings settings = new AdblockSettings(); | |
77 settings.setAdblockEnabled(prefs.getBoolean(SETTINGS_ENABLED_KEY, true)); | |
78 settings.setAcceptableAdsEnabled(prefs.getBoolean(SETTINGS_AA_ENABLED_KEY, t
rue)); | |
79 String connectionType = prefs.getString(SETTINGS_ALLOWED_CONNECTION_TYPE_KEY
, null); | |
80 settings.setAllowedConnectionType(ConnectionType.findByValue(connectionType)
); | |
81 | |
82 loadSubscriptions(settings); | |
83 loadWhitelistedDomains(settings); | |
84 | |
85 return settings; | |
86 } | |
87 | |
88 private void loadWhitelistedDomains(AdblockSettings settings) | |
89 { | |
90 if (prefs.contains(SETTINGS_WL_DOMAINS_KEY)) | |
91 { | |
92 // count | |
93 int whitelistedDomainsCount = prefs.getInt(SETTINGS_WL_DOMAINS_KEY, 0); | |
94 | |
95 // each domain | |
96 List<String> whitelistedDomains = new LinkedList<String>(); | |
97 for (int i = 0; i < whitelistedDomainsCount; i++) | |
98 { | |
99 String whitelistedDomain = prefs.getString(getArrayItemKey(i, SETTINGS_W
L_DOMAIN_KEY), ""); | |
100 whitelistedDomains.add(whitelistedDomain); | |
101 } | |
102 settings.setWhitelistedDomains(whitelistedDomains); | |
103 } | |
104 } | |
105 | |
106 private void loadSubscriptions(AdblockSettings settings) | |
107 { | |
108 if (prefs.contains(SETTINGS_SUBSCRIPTIONS_KEY)) | |
109 { | |
110 // count | |
111 int subscriptionsCount = prefs.getInt(SETTINGS_SUBSCRIPTIONS_KEY, 0); | |
112 | |
113 // each subscription | |
114 List<Subscription> subscriptions = new LinkedList<Subscription>(); | |
115 for (int i = 0; i < subscriptionsCount; i++) | |
116 { | |
117 Subscription subscription = new Subscription(); | |
118 subscription.title = prefs.getString(getSubscriptionTitleKey(i), ""); | |
119 subscription.url = prefs.getString(getSubscriptionURLKey(i), ""); | |
120 subscription.specialization = prefs.getString(getSubscriptionSpecializat
ionKey(i), ""); | |
121 subscriptions.add(subscription); | |
122 } | |
123 settings.setSubscriptions(subscriptions); | |
124 } | |
125 } | |
126 | |
127 private String getArrayItemKey(int index, String entity) | |
128 { | |
129 // f.e. "domain0" | |
130 return entity + index; | |
131 } | |
132 | |
133 private String getArrayItemKey(int index, String entity, String field) | |
134 { | |
135 // f.e. `subscription0.field` | |
136 return getArrayItemKey(index, entity) + "." + field; | |
137 } | |
138 | |
139 private String getSubscriptionTitleKey(int index) | |
140 { | |
141 return getArrayItemKey(index, SETTINGS_SUBSCRIPTION_KEY, SETTINGS_SUBSCRIPTI
ON_TITLE_KEY); | |
142 } | |
143 | |
144 private String getSubscriptionURLKey(int index) | |
145 { | |
146 return getArrayItemKey(index, SETTINGS_SUBSCRIPTION_KEY, SETTINGS_SUBSCRIPTI
ON_URL_KEY); | |
147 } | |
148 | |
149 private String getSubscriptionSpecializationKey(int index) | |
150 { | |
151 return getArrayItemKey(index, SETTINGS_SUBSCRIPTION_KEY, SETTINGS_SUBSCRIPTI
ON_SPECIALIZATION_KEY); | |
152 } | |
153 | |
154 @Override | |
155 public void save(AdblockSettings settings) | |
156 { | |
157 SharedPreferences.Editor editor = prefs | |
158 .edit() | |
159 .clear() | |
160 .putBoolean(SETTINGS_ENABLED_KEY, settings.isAdblockEnabled()) | |
161 .putBoolean(SETTINGS_AA_ENABLED_KEY, settings.isAcceptableAdsEnabled()); | |
162 | |
163 if (settings.getAllowedConnectionType() != null) | |
164 { | |
165 editor.putString(SETTINGS_ALLOWED_CONNECTION_TYPE_KEY, settings.getAllowed
ConnectionType().getValue()); | |
166 } | |
167 | |
168 saveSubscriptions(settings, editor); | |
169 saveWhitelistedDomains(settings, editor); | |
170 | |
171 if (commit) | |
172 { | |
173 editor.commit(); | |
174 } | |
175 else | |
176 { | |
177 // faster but not finished most likely before return | |
178 editor.apply(); | |
179 } | |
180 } | |
181 | |
182 private void saveWhitelistedDomains(AdblockSettings settings, SharedPreference
s.Editor editor) | |
183 { | |
184 if (settings.getWhitelistedDomains() != null) | |
185 { | |
186 // count | |
187 editor.putInt(SETTINGS_WL_DOMAINS_KEY, settings.getWhitelistedDomains().si
ze()); | |
188 | |
189 // each domain | |
190 for (int i = 0; i < settings.getWhitelistedDomains().size(); i++) | |
191 { | |
192 String eachDomain = settings.getWhitelistedDomains().get(i); | |
193 editor.putString(getArrayItemKey(i, SETTINGS_WL_DOMAIN_KEY), eachDomain)
; | |
194 } | |
195 } | |
196 } | |
197 | |
198 private void saveSubscriptions(AdblockSettings settings, SharedPreferences.Edi
tor editor) | |
199 { | |
200 if (settings.getSubscriptions() != null) | |
201 { | |
202 // count | |
203 editor.putInt(SETTINGS_SUBSCRIPTIONS_KEY, settings.getSubscriptions().size
()); | |
204 | |
205 // each subscription | |
206 for (int i = 0; i < settings.getSubscriptions().size(); i++) | |
207 { | |
208 Subscription eachSubscription = settings.getSubscriptions().get(i); | |
209 | |
210 // warning: saving `title`, `url` and `specialization` fields only | |
211 editor.putString(getSubscriptionTitleKey(i), eachSubscription.title); | |
212 editor.putString(getSubscriptionURLKey(i), eachSubscription.url); | |
213 editor.putString(getSubscriptionSpecializationKey(i), eachSubscription.s
pecialization); | |
214 } | |
215 } | |
216 } | |
217 } | |
OLD | NEW |