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