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

Side by Side Diff: libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java

Issue 29678581: Issue 6000 - Rename "libadblockplus-android" (Closed)
Patch Set: addressed comments Created Jan. 29, 2018, 11:04 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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.Context;
21 import android.content.SharedPreferences;
22 import android.util.Log;
23
24 import org.adblockplus.libadblockplus.android.AdblockEngine;
25 import org.adblockplus.libadblockplus.android.AdblockEngineProvider;
26 import org.adblockplus.libadblockplus.android.SingleInstanceEngineProvider;
27 import org.adblockplus.libadblockplus.android.Utils;
28
29 /**
30 * AdblockHelper shared resources
31 * (singleton)
32 */
33 public class AdblockHelper
34 {
35 private static final String TAG = Utils.getTag(AdblockHelper.class);
36
37 /**
38 * Suggested preference name to store settings
39 */
40 public static final String PREFERENCE_NAME = "ADBLOCK";
41
42 /**
43 * Suggested preference name to store intercepted subscription requests
44 */
45 public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD";
46 private static AdblockHelper _instance;
47
48 private SingleInstanceEngineProvider provider;
49 private AdblockSettingsStorage storage;
50
51 private final Runnable engineCreatedCallback = new Runnable()
52 {
53 @Override
54 public void run()
55 {
56 AdblockSettings settings = storage.load();
57 if (settings != null)
58 {
59 Log.d(TAG, "Applying saved adblock settings to adblock engine");
60 // apply last saved settings to adblock engine.
61 // all the settings except `enabled` and whitelisted domains list
62 // are saved by adblock engine itself
63 provider.getEngine().setEnabled(settings.isAdblockEnabled());
64 provider.getEngine().setWhitelistedDomains(settings.getWhitelistedDomain s());
65
66 // allowed connection type is saved by filter engine but we need to over ride it
67 // as filter engine can be not created when changing
68 String connectionType = (settings.getAllowedConnectionType() != null
69 ? settings.getAllowedConnectionType().getValue()
70 : null);
71 provider.getEngine().getFilterEngine().setAllowedConnectionType(connecti onType);
72 }
73 else
74 {
75 Log.w(TAG, "No saved adblock settings");
76 }
77 }
78 };
79
80 private final Runnable engineDisposedCallback = new Runnable()
81 {
82 @Override
83 public void run()
84 {
85 Log.d(TAG, "Releasing adblock settings storage");
86 storage = null;
87 }
88 };
89
90 // singleton
91 protected AdblockHelper()
92 {
93 // prevents instantiation
94 }
95
96 /**
97 * Use to get AdblockHelper instance
98 * @return adblock instance
99 */
100 public static synchronized AdblockHelper get()
101 {
102 if (_instance == null)
103 {
104 _instance = new AdblockHelper();
105 }
106
107 return _instance;
108 }
109
110 public AdblockEngineProvider getProvider()
111 {
112 if (provider == null)
113 {
114 throw new IllegalStateException("Usage exception: call init(...) first");
115 }
116 return provider;
117 }
118
119 public AdblockSettingsStorage getStorage()
120 {
121 if (storage == null)
122 {
123 throw new IllegalStateException("Usage exception: call init(...) first");
124 }
125 return storage;
126 }
127
128 /**
129 * Init with context
130 * @param context application context
131 * @param basePath file system root to store files
132 *
133 * Adblock Plus library will download subscription files and s tore them on
134 * the path passed. The path should exist and the directory co ntent should not be
135 * cleared out occasionally. Using `context.getCacheDir().getA bsolutePath()` is not
136 * recommended because it can be cleared by the system.
137 * @param developmentBuild debug or release?
138 * @param preferenceName Shared Preferences name to store adblock settings
139 */
140 public SingleInstanceEngineProvider init(Context context, String basePath,
141 boolean developmentBuild, String pref erenceName)
142 {
143 initProvider(context, basePath, developmentBuild);
144 initStorage(context, preferenceName);
145 return provider;
146 }
147
148 private void initProvider(Context context, String basePath, boolean developmen tBuild)
149 {
150 provider = new SingleInstanceEngineProvider(context, basePath, developmentBu ild);
151 provider.setEngineCreatedCallback(engineCreatedCallback);
152 provider.setEngineDisposedCallback(engineDisposedCallback);
153 }
154
155 private void initStorage(Context context, String settingsPreferenceName)
156 {
157 // read and apply current settings
158 SharedPreferences settingsPrefs = context.getSharedPreferences(
159 settingsPreferenceName,
160 Context.MODE_PRIVATE);
161
162 storage = new SharedPrefsStorage(settingsPrefs);
163 }
164
165 /**
166 * @deprecated The method is deprecated: use .getProvider().retain() instead
167 */
168 @Deprecated
169 public boolean retain(boolean asynchronous)
170 {
171 return provider.retain(asynchronous);
172 }
173
174 /**
175 * @deprecated The method is deprecated: use .getProvider().waitForReady() ins tead
176 */
177 @Deprecated
178 public void waitForReady()
179 {
180 provider.waitForReady();
181 }
182
183 /**
184 * @deprecated The method is deprecated: use .getProvider().getEngine() instea d
185 */
186 @Deprecated
187 public AdblockEngine getEngine()
188 {
189 return provider.getEngine();
190 }
191
192 /**
193 * @deprecated The method is deprecated: use .getProvider().release() instead
194 */
195 @Deprecated
196 public boolean release()
197 {
198 return provider.release();
199 }
200
201 /**
202 * @deprecated The method is deprecated: use .getProvider().getCounter() inste ad
203 */
204 @Deprecated
205 public int getCounter()
206 {
207 return provider.getCounter();
208 }
209 }
OLDNEW

Powered by Google App Engine
This is Rietveld