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

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

Issue 29389555: Issue 5010 - Allow to preload subscription files (Closed)
Patch Set: diego's suggestions 2 Created March 30, 2017, 10:26 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « README.md ('k') | libadblockplus-android-tests/build.gradle » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java
diff --git a/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java b/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java
index bd2a25719538651340710c400e0180401770b476..7dc60ce2675257d6705549ea3b8eb78dde84c3a7 100644
--- a/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java
+++ b/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java
@@ -22,8 +22,11 @@ import android.content.SharedPreferences;
import android.util.Log;
import org.adblockplus.libadblockplus.android.AdblockEngine;
+import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper;
import org.adblockplus.libadblockplus.android.Utils;
+import java.io.File;
+import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
@@ -36,14 +39,21 @@ public class AdblockHelper
private static final String TAG = Utils.getTag(AdblockHelper.class);
/**
- * Suggested preference name
+ * Suggested preference name to store settings
*/
public static final String PREFERENCE_NAME = "ADBLOCK";
+
+ /**
+ * Suggested preference name to store intercepted subscription requests
+ */
+ public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD";
private static AdblockHelper _instance;
private Context context;
private boolean developmentBuild;
- private String preferenceName;
+ private String settingsPreferenceName;
+ private String preloadedPreferenceName;
+ private Map<String, Integer> urlToResourceIdMap;
private AdblockEngine engine;
private AdblockSettingsStorage storage;
private CountDownLatch engineCreated;
@@ -89,13 +99,25 @@ public class AdblockHelper
* Init with context
* @param context application context
* @param developmentBuild debug or release?
- * @param preferenceName Shared Preferences name
+ * @param preferenceName Shared Preferences name to store adlock settings
*/
- public void init(Context context, boolean developmentBuild, String preferenceName)
+ public AdblockHelper init(Context context, boolean developmentBuild, String preferenceName)
{
this.context = context.getApplicationContext();
this.developmentBuild = developmentBuild;
- this.preferenceName = preferenceName;
+ this.settingsPreferenceName = preferenceName;
+ return this;
+ }
+
+ /**
+ * Use preloaded subscriptions
+ * @param preferenceName Shared Preferences name to store intercepted requests stats
+ * @param urlToResourceIdMap
+ */
+ public void preloadSubscriptions(String preferenceName, Map<String, Integer> urlToResourceIdMap)
+ {
+ this.preloadedPreferenceName = preferenceName;
+ this.urlToResourceIdMap = urlToResourceIdMap;
}
private void createAdblock()
@@ -103,13 +125,32 @@ public class AdblockHelper
Log.d(TAG, "Creating adblock engine ...");
// read and apply current settings
- SharedPreferences prefs = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
- storage = new SharedPrefsStorage(prefs);
+ SharedPreferences settingsPrefs = context.getSharedPreferences(
+ settingsPreferenceName,
+ Context.MODE_PRIVATE);
+ storage = new SharedPrefsStorage(settingsPrefs);
+
+ File basePath = context.getDir(AdblockEngine.BASE_PATH_DIRECTORY, Context.MODE_PRIVATE);
+ AdblockEngine.Builder builder = AdblockEngine
+ .builder(
+ AdblockEngine.generateAppInfo(context, developmentBuild),
+ basePath.getAbsolutePath())
+ .enableElementHiding(true);
+
+ // if preloaded subscriptions provided
+ if (preloadedPreferenceName != null)
+ {
+ SharedPreferences preloadedSubscriptionsPrefs = context.getSharedPreferences(
+ preloadedPreferenceName,
+ Context.MODE_PRIVATE);
+ builder.preloadSubscriptions(
+ context,
+ urlToResourceIdMap,
+ new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscriptionsPrefs));
+ }
+
+ engine = builder.build();
- engine = AdblockEngine.create(
- AdblockEngine.generateAppInfo(context, developmentBuild),
- context.getCacheDir().getAbsolutePath(),
- true); // `true` as we need element hiding
Log.d(TAG, "AdblockHelper engine created");
AdblockSettings settings = storage.load();
« no previous file with comments | « README.md ('k') | libadblockplus-android-tests/build.gradle » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld