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 29389555: Issue 5010 - Allow to preload subscription files (Closed)
Patch Set: Created March 20, 2017, 8:11 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
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus.android.settings; 18 package org.adblockplus.libadblockplus.android.settings;
19 19
20 import android.content.Context; 20 import android.content.Context;
21 import android.content.SharedPreferences; 21 import android.content.SharedPreferences;
22 import android.util.Log; 22 import android.util.Log;
23 23
24 import org.adblockplus.libadblockplus.android.AdblockEngine; 24 import org.adblockplus.libadblockplus.android.AdblockEngine;
25 import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper;
25 import org.adblockplus.libadblockplus.android.Utils; 26 import org.adblockplus.libadblockplus.android.Utils;
26 27
28 import java.io.File;
29 import java.util.Map;
27 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.atomic.AtomicInteger; 31 import java.util.concurrent.atomic.AtomicInteger;
29 32
30 /** 33 /**
31 * AdblockHelper shared resources 34 * AdblockHelper shared resources
32 * (singleton) 35 * (singleton)
33 */ 36 */
34 public class AdblockHelper 37 public class AdblockHelper
35 { 38 {
36 private static final String TAG = Utils.getTag(AdblockHelper.class); 39 private static final String TAG = Utils.getTag(AdblockHelper.class);
37 40
41 // basePath to store subscription files
42 public static final String BASE_PATH_DIRECTORY = "adblock";
diegocarloslima 2017/03/28 13:47:15 I would suggest to move this constant to AdblockEn
anton 2017/03/28 13:59:06 Acknowledged.
43
38 /** 44 /**
39 * Suggested preference name 45 * Suggested preference name to store settings
40 */ 46 */
41 public static final String PREFERENCE_NAME = "ADBLOCK"; 47 public static final String PREFERENCE_NAME = "ADBLOCK";
48
49 /**
50 * Suggested preference name to store intercepted subscription requests
51 */
52 public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD";
42 private static AdblockHelper _instance; 53 private static AdblockHelper _instance;
43 54
44 private Context context; 55 private Context context;
45 private boolean developmentBuild; 56 private boolean developmentBuild;
46 private String preferenceName; 57 private String settingsPreferenceName;
58 private String preloadedPreferenceName;
59 private Map<String, Integer> URLtoResourceIdMap;
diegocarloslima 2017/03/28 13:47:15 'toResource' should be 'ToResource'. Also, it's re
anton 2017/03/28 13:59:06 Acknowledged.
47 private AdblockEngine engine; 60 private AdblockEngine engine;
48 private AdblockSettingsStorage storage; 61 private AdblockSettingsStorage storage;
49 private CountDownLatch engineCreated; 62 private CountDownLatch engineCreated;
50 63
51 /* 64 /*
52 Simple ARC management for AdblockEngine 65 Simple ARC management for AdblockEngine
53 Use `retain` and `release` 66 Use `retain` and `release`
54 */ 67 */
55 68
56 private AtomicInteger referenceCounter = new AtomicInteger(0); 69 private AtomicInteger referenceCounter = new AtomicInteger(0);
(...skipping 25 matching lines...) Expand all
82 95
83 public AdblockSettingsStorage getStorage() 96 public AdblockSettingsStorage getStorage()
84 { 97 {
85 return storage; 98 return storage;
86 } 99 }
87 100
88 /** 101 /**
89 * Init with context 102 * Init with context
90 * @param context application context 103 * @param context application context
91 * @param developmentBuild debug or release? 104 * @param developmentBuild debug or release?
92 * @param preferenceName Shared Preferences name 105 * @param preferenceName Shared Preferences name to store adlock settings
93 */ 106 */
94 public void init(Context context, boolean developmentBuild, String preferenceN ame) 107 public AdblockHelper init(Context context, boolean developmentBuild, String pr eferenceName)
95 { 108 {
96 this.context = context.getApplicationContext(); 109 this.context = context.getApplicationContext();
97 this.developmentBuild = developmentBuild; 110 this.developmentBuild = developmentBuild;
98 this.preferenceName = preferenceName; 111 this.settingsPreferenceName = preferenceName;
112 return this;
113 }
114
115 /**
116 * Use preloaded subscriptions
117 * @param preferenceName Shared Preferences name to store intercepted requests stats
118 * @param URLtoResourceIdMap
119 */
120 public void preloadSubscriptions(String preferenceName, Map<String, Integer> U RLtoResourceIdMap)
diegocarloslima 2017/03/28 13:47:15 same suggestion here, change 'URLtoResourceIdMap'
anton 2017/03/28 13:59:06 Acknowledged.
121 {
122 this.preloadedPreferenceName = preferenceName;
123 this.URLtoResourceIdMap = URLtoResourceIdMap;
99 } 124 }
100 125
101 private void createAdblock() 126 private void createAdblock()
102 { 127 {
103 Log.d(TAG, "Creating adblock engine ..."); 128 Log.d(TAG, "Creating adblock engine ...");
104 129
105 // read and apply current settings 130 // read and apply current settings
106 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte xt.MODE_PRIVATE); 131 SharedPreferences settingsPrefs = context.getSharedPreferences(
107 storage = new SharedPrefsStorage(prefs); 132 settingsPreferenceName,
133 Context.MODE_PRIVATE);
134 storage = new SharedPrefsStorage(settingsPrefs);
108 135
109 engine = AdblockEngine.create( 136 File basePath = context.getDir(BASE_PATH_DIRECTORY, Context.MODE_PRIVATE);
110 AdblockEngine.generateAppInfo(context, developmentBuild), 137 AdblockEngine.Builder builder = AdblockEngine
111 context.getCacheDir().getAbsolutePath(), 138 .builder(
112 true); // `true` as we need element hiding 139 AdblockEngine.generateAppInfo(context, developmentBuild),
140 basePath.getAbsolutePath())
141 .enableElementHiding(true);
142
143 // if preloaded subscriptions provided
144 if (preloadedPreferenceName != null)
145 {
146 SharedPreferences preloadedSubscriptionsPrefs = context.getSharedPreferenc es(
147 preloadedPreferenceName,
148 Context.MODE_PRIVATE);
149 builder.preloadSubscriptions(
150 context,
151 URLtoResourceIdMap,
152 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs));
153 }
154
155 engine = builder.build();
156
113 Log.d(TAG, "AdblockHelper engine created"); 157 Log.d(TAG, "AdblockHelper engine created");
114 158
115 AdblockSettings settings = storage.load(); 159 AdblockSettings settings = storage.load();
116 if (settings != null) 160 if (settings != null)
117 { 161 {
118 Log.d(TAG, "Applying saved adblock settings to adblock engine"); 162 Log.d(TAG, "Applying saved adblock settings to adblock engine");
119 // apply last saved settings to adblock engine 163 // apply last saved settings to adblock engine
120 164
121 // all the settings except `enabled` and whitelisted domains are saved by adblock engine itself 165 // all the settings except `enabled` and whitelisted domains are saved by adblock engine itself
122 engine.setEnabled(settings.isAdblockEnabled()); 166 engine.setEnabled(settings.isAdblockEnabled());
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 engineCreated.countDown(); 265 engineCreated.countDown();
222 engineCreated = null; 266 engineCreated = null;
223 } 267 }
224 else 268 else
225 { 269 {
226 disposeAdblock(); 270 disposeAdblock();
227 } 271 }
228 } 272 }
229 } 273 }
230 } 274 }
OLDNEW

Powered by Google App Engine
This is Rietveld