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