OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present 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 BaseSettingsFragment | |
29 <ListenerClass extends BaseSettingsFragment.Listener> | |
30 extends PreferenceFragment | |
31 { | |
32 protected final String TAG = Utils.getTag(this.getClass()); | |
33 protected AdblockSettings settings; | |
34 protected Provider provider; | |
35 protected ListenerClass listener; | |
36 | |
37 /** | |
38 * Provides AdblockEngine and SharedPreferences to store settings | |
39 * (activity holding BaseSettingsFragment fragment should implement this inter
face) | |
40 */ | |
41 public interface Provider | |
42 { | |
43 AdblockEngine getAdblockEngine(); | |
44 AdblockSettingsStorage getAdblockSettingsStorage(); | |
45 } | |
46 | |
47 /** | |
48 * Listens for Adblock settings events | |
49 */ | |
50 public interface Listener | |
51 { | |
52 /** | |
53 * `Settings were changed` callback | |
54 * Note: settings are available using BaseSettingsFragment.getSettings() | |
55 * | |
56 * @param fragment fragment | |
57 */ | |
58 void onAdblockSettingsChanged(BaseSettingsFragment fragment); | |
59 } | |
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 public AdblockSettings getSettings() | |
111 { | |
112 return settings; | |
113 } | |
114 | |
115 @Override | |
116 public void onDetach() | |
117 { | |
118 super.onDetach(); | |
119 provider = null; | |
120 listener = null; | |
121 } | |
122 } | |
OLD | NEW |