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

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

Issue 29361445: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Patch Set: fixes according to Diego's suggestions Created Nov. 9, 2016, 12:27 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
OLDNEW
(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.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.Utils;
26
27 import java.util.concurrent.atomic.AtomicInteger;
28
29 /**
30 * Adblock shared resources
31 * (singleton)
32 */
33 public class Adblock
34 {
35 private static final String TAG = Utils.getTag(Adblock.class);
36
37 /**
38 * Suggested preference name
39 */
40 public static final String PREFERENCE_NAME = "ADBLOCK";
41
42 // singleton
43 protected Adblock()
44 {
45 // prevents instantiation
46 }
47
48 private static Adblock _instance;
49
50 /**
51 * Use to get Adblock instance
52 * @return adblock instance
53 */
54 public static synchronized Adblock get()
55 {
56 if (_instance == null)
57 {
58 _instance = new Adblock();
59 }
60
61 return _instance;
62 }
63
64 private Context context;
65 private boolean developmentBuild;
66 private String preferenceName;
67
68 private AdblockEngine engine;
69
70 public AdblockEngine getEngine()
71 {
72 return engine;
73 }
74
75 private AdblockSettingsStorage storage;
76
77 public AdblockSettingsStorage getStorage()
78 {
79 return storage;
80 }
81
82 /**
83 * Init with context
84 * @param context application context
85 * @param developmentBuild debug or release?
86 * @param preferenceName Shared Preferences name
87 */
88 public void init(Context context, boolean developmentBuild, String preferenceN ame)
89 {
90 this.context = context.getApplicationContext();
91 this.developmentBuild = developmentBuild;
92 this.preferenceName = preferenceName;
93 }
94
95 private void createAdblock()
96 {
97 Log.d(TAG, "Creating adblock engine ...");
98
99 engine = AdblockEngine.create(
100 context,
101 AdblockEngine.generateAppInfo(context, developmentBuild),
102 context.getCacheDir().getAbsolutePath(),
103 true); // `true` as we need element hiding
104 Log.d(TAG, "Adblock engine created");
105
106 // read and apply current settings
107 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte xt.MODE_PRIVATE);
108 storage = new AdblockSettingsSharedPrefsStorage(prefs);
109
110 AdblockSettings settings = storage.load();
111 if (settings != null)
112 {
113 Log.d(TAG, "Applying saved adblock settings to adblock engine");
114 // apply last saved settings to adblock engine
115
116 // all the settings except `enabled` and whitelisted domains are saved by adblock engine itself
117 engine.setEnabled(settings.isAdblockEnabled());
118 engine.setWhitelistedDomains(settings.getWhitelistedDomains());
119 }
120 else
121 {
122 Log.w(TAG, "No saved adblock settings");
123 }
124 }
125
126 private void disposeAdblock()
127 {
128 Log.w(TAG, "Disposing adblock engine");
129
130 engine.dispose();
131 engine = null;
132
133 storage = null;
134 }
135
136 /*
137 Simple ARC management for AdblockEngine
138 Use `retain` and `release`
139 */
140
141 private AtomicInteger referenceCounter = new AtomicInteger(0);
142
143 /**
144 * Register Adblock engine client
145 */
146 public synchronized void retain()
147 {
148 if (referenceCounter.getAndIncrement() == 0)
149 {
150 createAdblock();
151 }
152 }
153
154 /**
155 * Unregister Adblock engine client
156 */
157 public synchronized void release()
158 {
159 if (referenceCounter.decrementAndGet() == 0)
160 {
161 disposeAdblock();
162 }
163 }
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld