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

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

Issue 29361445: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Patch Set: fixed 2 more members in AdblockEngine Created Dec. 16, 2016, 8:31 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
Index: libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockSettingsFragment.java
diff --git a/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockSettingsFragment.java b/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockSettingsFragment.java
new file mode 100644
index 0000000000000000000000000000000000000000..239be5f5d17e25c03eaff281a9f8a2c3c2ac3d41
--- /dev/null
+++ b/libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockSettingsFragment.java
@@ -0,0 +1,122 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * 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.settings;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.preference.PreferenceFragment;
+import android.util.Log;
+
+import org.adblockplus.libadblockplus.android.AdblockEngine;
+import org.adblockplus.libadblockplus.android.Utils;
+
+public abstract class AdblockSettingsFragment
+ <ListenerClass extends AdblockSettingsFragment.Listener>
+ extends PreferenceFragment
+{
+ protected final String TAG = Utils.getTag(this.getClass());
+ protected AdblockSettings settings;
+ protected Provider provider;
+ protected ListenerClass listener;
+
+ /**
+ * Provides AdblockEngine and SharedPreferences to store settings
+ * (activity holding AdblockGeneralSettingsFragment fragment should implement this interface)
+ */
+ public interface Provider
+ {
+ AdblockEngine getAdblockEngine();
+ AdblockSettingsStorage getAdblockSettingsStorage();
+ }
+
+ /**
+ * Listens for Adblock settings events
+ */
+ public interface Listener
+ {
+ /**
+ * `Settings were changed` callback
+ * Note: settings are available using AdblockSettingsFragment.getSettings()
+ *
+ * @param fragment fragment
+ */
+ void onAdblockSettingsChanged(AdblockSettingsFragment fragment);
+ }
+
+ protected <T> T castOrThrow(Activity activity, Class<T> clazz)
+ {
+ if (!(activity instanceof Provider))
+ {
+ String message = activity.getClass().getSimpleName()
+ + " should implement "
+ + clazz.getSimpleName()
+ + " interface";
+
+ Log.e(TAG, message);
+ throw new RuntimeException(message);
+ }
+
+ return (T) activity;
+ }
+
+ public void loadSettings()
+ {
+ settings = provider.getAdblockSettingsStorage().load();
+ if (settings == null)
+ {
+ Log.w(TAG, "No adblock settings, yet. Using defdault ones from adblock engine");
+
+ // null because it was not saved yet
+ settings = AdblockSettingsStorage.getDefaultSettings(provider.getAdblockEngine());
+ }
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState)
+ {
+ super.onCreate(savedInstanceState);
+ loadSettings();
+ }
+
+ @Override
+ public void onAttach(Activity activity)
+ {
+ super.onAttach(activity);
+ provider = castOrThrow(activity, Provider.class);
+ }
+
+ @Override
+ public void onResume()
+ {
+ super.onResume();
+ loadSettings();
+ }
+
+ public AdblockSettings getSettings()
+ {
+ return settings;
+ }
+
+ @Override
+ public void onDetach()
+ {
+ super.onDetach();
+ provider = null;
+ listener = null;
+ }
+}

Powered by Google App Engine
This is Rietveld