| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 package org.adblockplus.libadblockplus.android.settings; | |
| 19 | |
| 20 import android.app.Activity; | |
| 21 import android.os.Bundle; | |
| 22 import android.preference.PreferenceFragment; | |
| 23 import android.util.Log; | |
| 24 | |
| 25 import org.adblockplus.libadblockplus.android.AdblockEngine; | |
| 26 import org.adblockplus.libadblockplus.android.Utils; | |
| 27 | |
| 28 public abstract class AdblockSettingsFragment | |
| 29 <ListenerClass extends AdblockSettingsFragment.Listener> | |
| 30 extends PreferenceFragment | |
| 31 { | |
| 32 protected final String TAG = Utils.getTag(this.getClass()); | |
| 33 | |
| 34 /** | |
| 35 * Provides AdblockEngine and SharedPreferences to store settings | |
| 36 * (activity holding AdblockGeneralSettingsFragment fragment should implement this interface) | |
| 37 */ | |
| 38 public interface Provider | |
| 39 { | |
| 40 AdblockEngine getAdblockEngine(); | |
| 41 AdblockSettingsStorage getAdblockSettingsStorage(); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Listens for Adblock settings events | |
| 46 */ | |
| 47 public interface Listener | |
| 48 { | |
| 49 /** | |
| 50 * `Settings were changed` callback | |
| 51 * Note: settings are available using AdblockSettingsFragment.getSettings() | |
| 52 * | |
| 53 * @param fragment fragment | |
| 54 */ | |
| 55 void onAdblockSettingsChanged(AdblockSettingsFragment fragment); | |
| 56 } | |
| 57 | |
| 58 protected Provider provider; | |
| 59 protected ListenerClass listener; | |
| 60 | |
| 61 protected <T> T castOrThrow(Activity activity, Class<T> clazz) | |
| 62 { | |
| 63 if (!(activity instanceof Provider)) | |
| 64 { | |
| 65 String message = activity.getClass().getSimpleName() | |
| 66 + " should implement " | |
| 67 + clazz.getSimpleName() | |
| 68 + " interface"; | |
| 69 | |
| 70 Log.e(TAG, message); | |
| 71 throw new RuntimeException(message); | |
| 72 } | |
| 73 | |
| 74 return (T) activity; | |
| 75 } | |
| 76 | |
| 77 public void loadSettings() | |
| 78 { | |
| 79 settings = provider.getAdblockSettingsStorage().load(); | |
| 80 if (settings == null) | |
| 81 { | |
| 82 Log.w(TAG, "No adblock settings, yet. Using defdault ones from adblock eng ine"); | |
| 83 | |
| 84 // null because it was not saved yet | |
| 85 settings = AdblockSettingsStorage.getDefaultSettings(provider.getAdblockEn gine()); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 @Override | |
| 90 public void onCreate(Bundle savedInstanceState) | |
| 91 { | |
| 92 super.onCreate(savedInstanceState); | |
| 93 loadSettings(); | |
| 94 } | |
| 95 | |
| 96 @Override | |
| 97 public void onAttach(Activity activity) | |
| 98 { | |
| 99 super.onAttach(activity); | |
| 100 provider = castOrThrow(activity, Provider.class); | |
| 101 } | |
| 102 | |
| 103 @Override | |
| 104 public void onResume() | |
| 105 { | |
| 106 super.onResume(); | |
| 107 loadSettings(); | |
| 108 } | |
| 109 | |
| 110 protected AdblockSettings settings; | |
|
diegocarloslima
2016/11/08 16:30:43
I feel like having member variable declarations mi
anton
2016/11/09 12:30:34
I feel comfortable with both styles because of adv
| |
| 111 | |
| 112 public AdblockSettings getSettings() | |
| 113 { | |
| 114 return settings; | |
| 115 } | |
| 116 | |
| 117 @Override | |
| 118 public void onDetach() | |
| 119 { | |
| 120 super.onDetach(); | |
| 121 provider = null; | |
| 122 listener = null; | |
| 123 } | |
| 124 } | |
| OLD | NEW |