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.sbrowser.contentblocker; | |
19 | |
20 import android.app.AlertDialog; | |
21 import android.content.Context; | |
22 import android.content.DialogInterface; | |
23 import android.content.SharedPreferences; | |
24 import android.preference.DialogPreference; | |
25 import android.preference.PreferenceCategory; | |
26 import android.preference.PreferenceManager; | |
27 import android.text.Html; | |
28 import android.util.AttributeSet; | |
29 import android.view.View; | |
30 | |
31 import org.adblockplus.adblockplussbrowser.R; | |
32 import org.adblockplus.sbrowser.contentblocker.engine.Engine; | |
33 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; | |
34 | |
35 import java.util.Collections; | |
36 import java.util.Set; | |
37 import java.util.TreeSet; | |
38 | |
39 public class WhitelistedWebsitesPreferenceCategory extends PreferenceCategory | |
40 { | |
41 | |
anton
2017/03/06 09:36:32
is empty line required?
diegocarloslima
2017/03/09 20:15:04
Not actually. Will fix that
| |
42 private final Set<String> whitelistedWebsites = new TreeSet<>(); | |
43 private Engine engine; | |
44 | |
45 public WhitelistedWebsitesPreferenceCategory(Context context, AttributeSet att rs) | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
Felix Dahlke
2017/03/07 15:43:38
For Java code it's 100, see: https://developer.moz
anton
2017/03/09 09:57:40
no problem
| |
46 { | |
47 super(context, attrs); | |
48 // This is required to remove the title TextView of the PreferenceCategory | |
49 this.setLayoutResource(R.layout.empty_view); | |
50 } | |
51 | |
52 @Override | |
53 protected void onAttachedToActivity() | |
54 { | |
55 super.onAttachedToActivity(); | |
56 EngineService.startService(this.getContext().getApplicationContext(), | |
57 new EngineService.OnEngineCreatedCallback() | |
58 { | |
59 @Override | |
60 public void onEngineCreated(Engine engine, boolean success) | |
61 { | |
62 if (!success) | |
63 { | |
64 return; | |
65 } | |
66 WhitelistedWebsitesPreferenceCategory.this.engine = engine; | |
67 WhitelistedWebsitesPreferenceCategory.this.initEntries(); | |
68 } | |
69 }); | |
70 } | |
71 | |
72 private void initEntries() | |
73 { | |
74 final SharedPreferences prefs = | |
75 PreferenceManager.getDefaultSharedPreferences(this.getContext().getAppli cationContext()); | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
76 final String key = this.getContext().getString(R.string.key_whitelisted_webs ites); | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
77 this.whitelistedWebsites.clear(); | |
78 this.whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String> emptySet())); | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
79 this.refreshEntries(); | |
80 } | |
81 | |
82 private void refreshEntries() | |
83 { | |
84 this.removeAll(); | |
85 for (final String url : this.whitelistedWebsites) | |
86 { | |
87 final WhitelistedWebsitePreference whitelistedWebsitePreference = | |
88 new WhitelistedWebsitePreference(this.getContext(), url, new DialogInt erface.OnClickListener() | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
89 { | |
90 @Override | |
91 public void onClick(DialogInterface dialog, int which) | |
92 { | |
93 removeWhitelistedWebsite(url); | |
94 } | |
95 }); | |
96 this.addPreference(whitelistedWebsitePreference); | |
97 } | |
98 | |
99 final UrlInputOpenerPreference urlPreference = new UrlInputOpenerPreference( this.getContext()); | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
100 urlPreference.setTitle(R.string.whitelisted_websites_add_button); | |
101 urlPreference.setDialogTitle(R.string.whitelist_website_dialog_title); | |
102 urlPreference.setDialogMessage(R.string.whitelist_website_dialog_message); | |
103 urlPreference.getEditText().setHint(R.string.whitelist_website_dialog_hint); | |
104 urlPreference.setOnUrlReadyListener(new UrlInputOpenerPreference.OnUrlReadyL istener() | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
105 { | |
106 @Override | |
107 public void onUrlReady(String url) | |
108 { | |
109 WhitelistedWebsitesPreferenceCategory.this.whitelistWebsite(url); | |
110 } | |
111 }); | |
112 this.addPreference(urlPreference); | |
113 } | |
114 | |
115 private void whitelistWebsite(String url) | |
116 { | |
117 this.whitelistedWebsites.add(url); | |
118 final SharedPreferences prefs = | |
119 PreferenceManager.getDefaultSharedPreferences(this.getContext().getAppli cationContext()); | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
120 final String key = this.getContext().getString(R.string.key_whitelisted_webs ites); | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
121 prefs.edit().putStringSet(key, this.whitelistedWebsites).apply(); | |
122 this.refreshEntries(); | |
123 this.engine.requestUpdateBroadcast(); | |
124 } | |
125 | |
126 private void removeWhitelistedWebsite(String url) | |
127 { | |
128 this.whitelistedWebsites.remove(url); | |
129 final SharedPreferences prefs = | |
130 PreferenceManager.getDefaultSharedPreferences(this.getContext().getAppli cationContext()); | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
131 final String key = this.getContext().getString(R.string.key_whitelisted_webs ites); | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
132 prefs.edit().putStringSet(key, this.whitelistedWebsites).apply(); | |
133 this.refreshEntries(); | |
134 this.engine.requestUpdateBroadcast(); | |
135 } | |
136 | |
137 private static class WhitelistedWebsitePreference extends DialogPreference | |
138 { | |
139 private final DialogInterface.OnClickListener onDeleteClickListener; | |
140 | |
141 WhitelistedWebsitePreference(Context context, String url, DialogInterface.On ClickListener onDeleteClickListener) | |
anton
2017/03/09 09:57:40
exceeds 100 characters line limit (not too signifi
| |
142 { | |
143 super(context); | |
144 this.onDeleteClickListener = onDeleteClickListener; | |
145 final String message = context.getString(R.string.whitelist_remove_dialog_ message, url); | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
146 setWidgetLayoutResource(R.layout.whitelisted_website_delete_widget); | |
147 setTitle(url); | |
148 setDialogTitle(R.string.whitelist_remove_dialog_title); | |
149 setDialogMessage(Html.fromHtml(message)); | |
150 setNegativeButtonText(android.R.string.cancel); | |
151 } | |
152 | |
153 @Override | |
154 protected void onBindView(View view) | |
155 { | |
156 super.onBindView(view); | |
157 final View deleteButton = view.findViewById(R.id.whitelisted_website_delet e_button); | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
158 deleteButton.setOnClickListener(new View.OnClickListener() | |
159 { | |
160 @Override | |
161 public void onClick(View v) | |
162 { | |
163 if (getDialog() == null || !getDialog().isShowing()) | |
164 { | |
165 showDialog(null); | |
166 } | |
167 } | |
168 }); | |
169 } | |
170 | |
171 @Override | |
172 protected void onClick() | |
173 { | |
174 // Overriding the default behaviour of showing a dialog here | |
175 // We just want to show a dialog when the delete button is clicked | |
176 } | |
177 | |
178 @Override | |
179 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) | |
180 { | |
181 builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClic kListener() | |
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
| |
182 { | |
183 @Override | |
184 public void onClick(DialogInterface dialog, int which) | |
185 { | |
186 if (WhitelistedWebsitePreference.this.onDeleteClickListener != null) | |
187 { | |
188 WhitelistedWebsitePreference.this.onDeleteClickListener.onClick(dial og, which); | |
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
| |
189 } | |
190 } | |
191 }); | |
192 } | |
193 } | |
194 } | |
OLD | NEW |