 Issue 29671734:
  Issue 6265 - Create shared AdblockEngine instance in AdblockWebView in background  (Closed)
    
  
    Issue 29671734:
  Issue 6265 - Create shared AdblockEngine instance in AdblockWebView in background  (Closed) 
  | 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 db3c7e8a8577958f72f4f13246e40c5d2a6ed736..309d60b7f709fbb30e97b7f60e0c8af737284355 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 | 
| @@ -21,8 +21,9 @@ import android.content.Context; | 
| import android.content.SharedPreferences; | 
| import android.util.Log; | 
| +import org.adblockplus.libadblockplus.android.AdblockEngine; | 
| import org.adblockplus.libadblockplus.android.AdblockEngineProvider; | 
| -import org.adblockplus.libadblockplus.android.SingletonEngineProvider; | 
| +import org.adblockplus.libadblockplus.android.SingleInstanceEngineProvider; | 
| import org.adblockplus.libadblockplus.android.Utils; | 
| /** | 
| @@ -44,9 +45,10 @@ public class AdblockHelper | 
| public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD"; | 
| private static AdblockHelper _instance; | 
| - private SingletonEngineProvider adblockEngineProvider; | 
| + private SingleInstanceEngineProvider provider; | 
| private AdblockSettingsStorage storage; | 
| - private Runnable engineCreatedCallback = new Runnable() | 
| + | 
| + private final Runnable engineCreatedCallback = new Runnable() | 
| { | 
| @Override | 
| public void run() | 
| @@ -55,19 +57,18 @@ public class AdblockHelper | 
| if (settings != null) | 
| { | 
| Log.d(TAG, "Applying saved adblock settings to adblock engine"); | 
| - // apply last saved 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 | 
| - adblockEngineProvider.getAdblockEngine().setEnabled(settings.isAdblockEnabled()); | 
| - adblockEngineProvider.getAdblockEngine().setWhitelistedDomains(settings.getWhitelistedDomains()); | 
| + provider.getEngine().setEnabled(settings.isAdblockEnabled()); | 
| + provider.getEngine().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); | 
| - adblockEngineProvider.getAdblockEngine().getFilterEngine().setAllowedConnectionType(connectionType); | 
| + provider.getEngine().getFilterEngine().setAllowedConnectionType(connectionType); | 
| } | 
| else | 
| { | 
| @@ -76,12 +77,12 @@ public class AdblockHelper | 
| } | 
| }; | 
| - private Runnable engineDisposedCallback = new Runnable() | 
| + private final Runnable engineDisposedCallback = new Runnable() | 
| { | 
| @Override | 
| public void run() | 
| { | 
| - Log.d(TAG, "Releasing adblock settings"); | 
| + Log.d(TAG, "Releasing adblock settings storage"); | 
| storage = null; | 
| } | 
| }; | 
| @@ -106,13 +107,21 @@ public class AdblockHelper | 
| return _instance; | 
| } | 
| - public AdblockEngineProvider getAdblockEngineProvider() | 
| + public AdblockEngineProvider getProvider() | 
| { | 
| - return adblockEngineProvider; | 
| + if (provider == null) | 
| + { | 
| + throw new IllegalStateException("Usage exception: call init(...) first"); | 
| + } | 
| + return provider; | 
| } | 
| public AdblockSettingsStorage getStorage() | 
| { | 
| + if (storage == null) | 
| + { | 
| + throw new IllegalStateException("Usage exception: call init(...) first"); | 
| + } | 
| return storage; | 
| } | 
| @@ -128,19 +137,19 @@ public class AdblockHelper | 
| * @param developmentBuild debug or release? | 
| * @param preferenceName Shared Preferences name to store adblock settings | 
| */ | 
| - public SingletonEngineProvider init(Context context, String basePath, | 
| - boolean developmentBuild, String preferenceName) | 
| + public SingleInstanceEngineProvider init(Context context, String basePath, | 
| + boolean developmentBuild, String preferenceName) | 
| { | 
| initProvider(context, basePath, developmentBuild); | 
| initStorage(context, preferenceName); | 
| - return adblockEngineProvider; | 
| + return provider; | 
| } | 
| private void initProvider(Context context, String basePath, boolean developmentBuild) | 
| { | 
| - this.adblockEngineProvider = new SingletonEngineProvider(context, basePath, developmentBuild); | 
| - this.adblockEngineProvider.setEngineCreatedCallback(engineCreatedCallback); | 
| - this.adblockEngineProvider.setEngineDisposedCallback(engineDisposedCallback); | 
| + provider = new SingleInstanceEngineProvider(context, basePath, developmentBuild); | 
| + provider.setEngineCreatedCallback(engineCreatedCallback); | 
| + provider.setEngineDisposedCallback(engineDisposedCallback); | 
| } | 
| private void initStorage(Context context, String settingsPreferenceName) | 
| @@ -150,6 +159,46 @@ public class AdblockHelper | 
| settingsPreferenceName, | 
| Context.MODE_PRIVATE); | 
| - this.storage = new SharedPrefsStorage(settingsPrefs); | 
| + storage = new SharedPrefsStorage(settingsPrefs); | 
| + } | 
| + | 
| + /** | 
| + * The method is deprecated: use .getProvider().retain() instead | 
| 
diegocarloslima
2018/01/19 12:59:19
It would be also good to add @deprecated in the ja
 | 
| + */ | 
| + @Deprecated public boolean retain(boolean asynchronous) | 
| 
diegocarloslima
2018/01/19 12:59:19
I would prefer this annotation as it was before, a
 | 
| + { | 
| + return provider.retain(asynchronous); | 
| + } | 
| + | 
| + /** | 
| + * The method is deprecated: use .getProvider().waitForReady() instead | 
| + */ | 
| + @Deprecated public void waitForReady() | 
| + { | 
| + provider.waitForReady(); | 
| + } | 
| + | 
| + /** | 
| + * The method is deprecated: use .getProvider().getEngine() instead | 
| + */ | 
| + @Deprecated public AdblockEngine getEngine() | 
| + { | 
| + return provider.getEngine(); | 
| + } | 
| + | 
| + /** | 
| + * The method is deprecated: use .getProvider().release() instead | 
| + */ | 
| + @Deprecated public boolean release() | 
| + { | 
| + return provider.release(); | 
| + } | 
| + | 
| + /** | 
| + * The method is deprecated: use .getProvider().getCounter() instead | 
| + */ | 
| + @Deprecated public int getCounter() | 
| + { | 
| + return provider.getCounter(); | 
| } | 
| } |