Index: libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
diff --git a/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java b/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
similarity index 60% |
copy from libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java |
copy to libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
index 965bafcbc97203707c19a503e44d02894808da06..d73e6215ea81eedabe7e474df2ad120e17e36666 100644 |
--- a/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockHelper.java |
+++ b/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
@@ -14,52 +14,36 @@ |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
+package org.adblockplus.libadblockplus.android; |
-package org.adblockplus.libadblockplus.android.settings; |
+import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
import android.content.Context; |
import android.content.SharedPreferences; |
import android.net.ConnectivityManager; |
import android.util.Log; |
-import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
-import org.adblockplus.libadblockplus.android.AdblockEngine; |
-import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper; |
-import org.adblockplus.libadblockplus.android.Utils; |
- |
import java.util.Map; |
import java.util.concurrent.CountDownLatch; |
import java.util.concurrent.atomic.AtomicInteger; |
/** |
- * AdblockHelper shared resources |
- * (singleton) |
+ * Provides single instance of AdblockEngine shared between registered clients |
*/ |
-public class AdblockHelper |
+public class SingleInstanceEngineProvider implements AdblockEngineProvider |
{ |
- private static final String TAG = Utils.getTag(AdblockHelper.class); |
- |
- /** |
- * 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 static final String TAG = Utils.getTag(SingleInstanceEngineProvider.class); |
private Context context; |
private String basePath; |
private boolean developmentBuild; |
- private String settingsPreferenceName; |
private String preloadedPreferenceName; |
private Map<String, Integer> urlToResourceIdMap; |
private AdblockEngine engine; |
- private AdblockSettingsStorage storage; |
private CountDownLatch engineCreated; |
private Long v8IsolateProviderPtr; |
+ private Runnable engineCreatedCallback; |
+ private Runnable engineDisposedCallback; |
/* |
Simple ARC management for AdblockEngine |
@@ -68,36 +52,6 @@ public class AdblockHelper |
private AtomicInteger referenceCounter = new AtomicInteger(0); |
- // singleton |
- protected AdblockHelper() |
- { |
- // prevents instantiation |
- } |
- |
- /** |
- * Use to get AdblockHelper instance |
- * @return adblock instance |
- */ |
- public static synchronized AdblockHelper get() |
- { |
- if (_instance == null) |
- { |
- _instance = new AdblockHelper(); |
- } |
- |
- return _instance; |
- } |
- |
- public AdblockEngine getEngine() |
- { |
- return engine; |
- } |
- |
- public AdblockSettingsStorage getStorage() |
- { |
- return storage; |
- } |
- |
/** |
* Init with context |
* @param context application context |
@@ -108,16 +62,12 @@ public class AdblockHelper |
* cleared out occasionally. Using `context.getCacheDir().getAbsolutePath()` is not |
* recommended because it can be cleared by the system. |
* @param developmentBuild debug or release? |
- * @param preferenceName Shared Preferences name to store adblock settings |
*/ |
- public AdblockHelper init(Context context, String basePath, |
- boolean developmentBuild, String preferenceName) |
+ public SingleInstanceEngineProvider(Context context, String basePath, boolean developmentBuild) |
{ |
this.context = context.getApplicationContext(); |
this.basePath = basePath; |
this.developmentBuild = developmentBuild; |
- this.settingsPreferenceName = preferenceName; |
- return this; |
} |
/** |
@@ -125,33 +75,41 @@ public class AdblockHelper |
* @param preferenceName Shared Preferences name to store intercepted requests stats |
* @param urlToResourceIdMap |
*/ |
- public AdblockHelper preloadSubscriptions(String preferenceName, Map<String, Integer> urlToResourceIdMap) |
+ public SingleInstanceEngineProvider preloadSubscriptions(String preferenceName, |
+ Map<String, Integer> urlToResourceIdMap) |
{ |
this.preloadedPreferenceName = preferenceName; |
this.urlToResourceIdMap = urlToResourceIdMap; |
return this; |
} |
- public AdblockHelper useV8IsolateProvider(long ptr) |
+ public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) |
{ |
this.v8IsolateProviderPtr = ptr; |
return this; |
} |
+ public SingleInstanceEngineProvider setEngineCreatedCallback(Runnable callback) |
+ { |
+ this.engineCreatedCallback = callback; |
+ return this; |
+ } |
+ |
+ public SingleInstanceEngineProvider setEngineDisposedCallback(Runnable callback) |
+ { |
+ this.engineDisposedCallback = callback; |
+ return this; |
+ } |
+ |
private void createAdblock() |
{ |
ConnectivityManager connectivityManager = |
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
- IsAllowedConnectionCallback isAllowedConnectionCallback = new IsAllowedConnectionCallbackImpl(connectivityManager); |
+ IsAllowedConnectionCallback isAllowedConnectionCallback = |
+ new IsAllowedConnectionCallbackImpl(connectivityManager); |
Log.d(TAG, "Creating adblock engine ..."); |
- // read and apply current settings |
- SharedPreferences settingsPrefs = context.getSharedPreferences( |
- settingsPreferenceName, |
- Context.MODE_PRIVATE); |
- storage = new SharedPrefsStorage(settingsPrefs); |
- |
AdblockEngine.Builder builder = AdblockEngine |
.builder( |
AdblockEngine.generateAppInfo(context, developmentBuild), |
@@ -180,79 +138,14 @@ public class AdblockHelper |
Log.d(TAG, "AdblockHelper engine created"); |
- AdblockSettings settings = storage.load(); |
- if (settings != null) |
- { |
- Log.d(TAG, "Applying saved adblock settings to adblock engine"); |
- // apply last saved settings to adblock engine |
- |
- // all the settings except `enabled` and whitelisted domains list |
- // are saved by adblock engine itself |
- engine.setEnabled(settings.isAdblockEnabled()); |
- engine.setWhitelistedDomains(settings.getWhitelistedDomains()); |
- |
- // allowed connection type is saved by filter engine but we need to override it |
- // as filter engine can be not created when changing |
- String connectionType = (settings.getAllowedConnectionType() != null |
- ? settings.getAllowedConnectionType().getValue() |
- : null); |
- engine.getFilterEngine().setAllowedConnectionType(connectionType); |
- } |
- else |
- { |
- Log.w(TAG, "No saved adblock settings"); |
- } |
- } |
- |
- /** |
- * Wait until everything is ready (used for `retain(true)`) |
- * Warning: locks current thread |
- */ |
- public void waitForReady() |
- { |
- if (engineCreated == null) |
- { |
- throw new RuntimeException("AdblockHelper Plus usage exception: call retain(true) first"); |
- } |
- |
- try |
+ // sometimes we need to init AdblockEngine instance, eg. set user settings |
+ if (engineCreatedCallback != null) |
{ |
- Log.d(TAG, "Waiting for ready ..."); |
- engineCreated.await(); |
- Log.d(TAG, "Ready"); |
- } |
- catch (InterruptedException e) |
- { |
- Log.w(TAG, "Interrupted", e); |
+ engineCreatedCallback.run(); |
} |
} |
- private void disposeAdblock() |
- { |
- Log.w(TAG, "Disposing adblock engine"); |
- |
- engine.dispose(); |
- engine = null; |
- |
- storage = null; |
- } |
- |
- /** |
- * Get registered clients count |
- * @return registered clients count |
- */ |
- public int getCounter() |
- { |
- return referenceCounter.get(); |
- } |
- |
- /** |
- * Register AdblockHelper engine client |
- * @param asynchronous If `true` engines will be created in background thread without locking of |
- * current thread. Use waitForReady() before getEngine() later. |
- * If `false` locks current thread. |
- * @return if a new instance is allocated |
- */ |
+ @Override |
public synchronized boolean retain(boolean asynchronous) |
{ |
boolean firstInstance = false; |
@@ -286,10 +179,33 @@ public class AdblockHelper |
return firstInstance; |
} |
- /** |
- * Unregister AdblockHelper engine client |
- * @return `true` if the last instance is destroyed |
- */ |
+ @Override |
+ public void waitForReady() |
+ { |
+ if (engineCreated == null) |
+ { |
+ throw new IllegalStateException("Usage exception: call retain(true) first"); |
+ } |
+ |
+ try |
+ { |
+ Log.d(TAG, "Waiting for ready ..."); |
+ engineCreated.await(); |
+ Log.d(TAG, "Ready"); |
+ } |
+ catch (InterruptedException e) |
+ { |
+ Log.w(TAG, "Interrupted", e); |
+ } |
+ } |
+ |
+ @Override |
+ public AdblockEngine getEngine() |
+ { |
+ return engine; |
+ } |
+ |
+ @Override |
public synchronized boolean release() |
{ |
boolean lastInstance = false; |
@@ -315,4 +231,25 @@ public class AdblockHelper |
} |
return lastInstance; |
} |
+ |
+ private void disposeAdblock() |
+ { |
+ Log.w(TAG, "Disposing adblock engine"); |
+ |
+ engine.dispose(); |
+ engine = null; |
+ |
+ // sometimes we need to deinit something after AdblockEngine instance disposed |
+ // eg. release user settings |
+ if (engineDisposedCallback != null) |
+ { |
+ engineDisposedCallback.run(); |
+ } |
+ } |
+ |
+ @Override |
+ public int getCounter() |
+ { |
+ return referenceCounter.get(); |
+ } |
} |