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..11d8d929241c4d389049e72fc04504559b5c509a 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 adblock 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(); |