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.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 } | |
diegocarloslima
2016/11/08 16:30:40
private constructor would be more appropriate IMHO
anton
2016/11/09 12:30:31
It depends. For now i think `protected` still work
| |
47 | |
48 private static Adblock _instance; | |
diegocarloslima
2016/11/08 16:30:40
I believe that we don't name static members beginn
anton
2016/11/09 12:30:30
It's just my habit and i don't remember any specia
| |
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; | |
diegocarloslima
2016/11/08 16:30:41
I feel like having member variable declarations mi
| |
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; | |
diegocarloslima
2016/11/08 16:30:40
We should use this.context = context.getApplicatio
anton
2016/11/09 12:30:30
Acknowledged.
| |
91 this.developmentBuild = developmentBuild; | |
92 this.preferenceName = preferenceName; | |
93 } | |
94 | |
95 private void createAdblock() | |
96 { | |
97 // create engine | |
98 Log.d(TAG, "Creating adblock engine ..."); | |
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 engine.dispose(); | |
129 engine = null; | |
130 | |
131 storage = null; | |
132 } | |
133 | |
134 /* | |
135 Simple ARC management for AdblockEngine | |
136 Use `retain` and `release` | |
137 */ | |
138 | |
139 private AtomicInteger referenceCounter = new AtomicInteger(0); | |
diegocarloslima
2016/11/08 16:30:40
I feel like having member variable declarations mi
anton
2016/11/09 12:30:29
It's the result of 2 contradictory rules:
1) to ho
| |
140 | |
141 /** | |
142 * Register Adblock engine client | |
143 */ | |
144 public synchronized void retain() | |
145 { | |
146 if (referenceCounter.getAndIncrement() == 0) | |
147 { | |
148 createAdblock(); | |
149 } | |
150 } | |
151 | |
152 /** | |
153 * Unregister Adblock engine client | |
154 */ | |
155 public synchronized void release() | |
156 { | |
157 if (referenceCounter.decrementAndGet() == 0) | |
158 { | |
159 Log.w(TAG, "Disposing adblock engine"); | |
160 disposeAdblock(); | |
161 } | |
162 } | |
163 } | |
OLD | NEW |