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

Side by Side Diff: libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockWhitelistedDomainsSettingsFragment.java

Issue 29361445: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Patch Set: added retaining in asynchronous mode Created Nov. 25, 2016, 7:08 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.BaseAdapter;
27 import android.widget.EditText;
28 import android.widget.ImageView;
29 import android.widget.ListView;
30 import android.widget.TextView;
31
32 import org.adblockplus.libadblockplus.android.Utils;
33
34 import java.util.LinkedList;
35 import java.util.List;
36
37 /**
38 * Whitelisted domains adblock fragment.
39 * Use the {@link AdblockWhitelistedDomainsSettingsFragment#newInstance} factory method to
40 * create an instance of this fragment.
41 */
42 public class AdblockWhitelistedDomainsSettingsFragment
43 extends AdblockSettingsFragment<AdblockWhitelistedDomainsSettingsFragment.List ener>
44 {
45 private static final String TAG = Utils.getTag(AdblockWhitelistedDomainsSettin gsFragment.class);
46
47 /**
48 * Listener with additional `isValidDomain` method
49 */
50 public interface Listener extends AdblockSettingsFragment.Listener
51 {
52 boolean isValidDomain(AdblockWhitelistedDomainsSettingsFragment fragment,
53 String domain, AdblockSettings settings);
54 }
55
56 public AdblockWhitelistedDomainsSettingsFragment()
57 {
58 // required empty public constructor
59 }
60
61 /**
62 * Use this factory method to create a new instance of
63 * this fragment using the provided parameters.
64 *
65 */
66 public static AdblockWhitelistedDomainsSettingsFragment newInstance()
67 {
68 return new AdblockWhitelistedDomainsSettingsFragment();
69 }
70
71 @Override
72 public void onAttach(Activity activity)
73 {
74 super.onAttach(activity);
75 listener = castOrThrow(activity, Listener.class);
76 }
77
78 private EditText domain;
79 private ImageView addDomainButton;
80 private ListView listView;
81 private Adapter adapter;
82
83 @Override
84 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
85 {
86 View rootView = inflater.inflate(
87 R.layout.fragment_adblock_whitelisted_domains_settings,
88 container,
89 false);
90
91 bindControls(rootView);
92
93 return rootView;
94 }
95
96 @Override
97 public void onResume()
98 {
99 super.onResume();
100 initControls();
101 }
102
103 private void bindControls(View rootView)
104 {
105 domain = (EditText) rootView.findViewById(R.id.fragment_adblock_wl_add_label );
106 addDomainButton = (ImageView) rootView.findViewById(R.id.fragment_adblock_wl _add_button);
107 listView = (ListView) rootView.findViewById(R.id.fragment_adblock_wl_listvie w);
108 }
109
110 // Holder for listview items
111 private class Holder {
112 TextView domain;
113 ImageView removeButton;
114
115 Holder(View rootView)
116 {
117 domain = (TextView) rootView.findViewById(R.id.fragment_adblock_wl_item_ti tle);
118 removeButton = (ImageView) rootView.findViewById(R.id.fragment_adblock_wl_ item_remove);
119 }
120 }
121
122 private View.OnClickListener removeDomainClickListener = new View.OnClickListe ner()
123 {
124 @Override
125 public void onClick(View v)
126 {
127 // update and save settings
128 int position = ((Integer) v.getTag()).intValue();
129 String removeDomain = settings.getWhitelistedDomains().get(position);
130 Log.w(TAG, "Removing domain: " + removeDomain);
131 settings.getWhitelistedDomains().remove(position);
132 provider.getAdblockSettingsStorage().save(settings);
133
134 // apply settings
135 provider.getAdblockEngine().setWhitelistedDomains(settings.getWhitelistedD omains());
136
137 // signal event
138 listener.onAdblockSettingsChanged(AdblockWhitelistedDomainsSettingsFragmen t.this);
139
140 // update UI
141 adapter.notifyDataSetChanged();
142 }
143 };
144
145 // Adapter
146 private class Adapter extends BaseAdapter
147 {
148 @Override
149 public int getCount()
150 {
151 return settings.getWhitelistedDomains() != null
152 ? settings.getWhitelistedDomains().size()
153 : 0;
154 }
155
156 @Override
157 public Object getItem(int position)
158 {
159 return settings.getWhitelistedDomains().get(position);
160 }
161
162 @Override
163 public long getItemId(int position)
164 {
165 return getItem(position).hashCode();
166 }
167
168 @Override
169 public View getView(int position, View convertView, ViewGroup parent)
170 {
171 if (convertView == null)
172 {
173 LayoutInflater inflater = LayoutInflater.from(getActivity());
174 convertView = inflater.inflate(R.layout.fragment_adblock_whitelisted_dom ain_item,
175 parent, false);
176 convertView.setTag(new Holder(convertView));
177 }
178
179 String domain = (String) getItem(position);
180
181 Holder holder = (Holder) convertView.getTag();
182 holder.domain.setText(domain);
183
184 holder.removeButton.setOnClickListener(removeDomainClickListener);
185 holder.removeButton.setTag(Integer.valueOf(position));
186
187 return convertView;
188 }
189 }
190
191 private void initControls()
192 {
193 addDomainButton.setOnClickListener(new View.OnClickListener()
194 {
195 @Override
196 public void onClick(View v)
197 {
198 String preparedDomain = prepareDomain(domain.getText().toString());
199
200 if (listener.isValidDomain(
201 AdblockWhitelistedDomainsSettingsFragment.this,
202 preparedDomain,
203 settings))
204 {
205 addDomain(preparedDomain);
206 }
207 else
208 {
209 Log.w(TAG, "Domain " + preparedDomain + " is not valid");
210 }
211 }
212 });
213
214 adapter = new Adapter();
215 listView.setAdapter(adapter);
216 }
217
218 private String prepareDomain(String domain)
219 {
220 return domain.trim();
221 }
222
223 public void addDomain(String newDomain)
224 {
225 Log.d(TAG, "New domain added: " + newDomain);
226
227 List<String> whitelistedDomains = settings.getWhitelistedDomains();
228 if (whitelistedDomains == null)
229 {
230 whitelistedDomains = new LinkedList<String>();
diegocarloslima 2016/11/29 17:47:44 I wouldn't insist, but I would advise to use the d
anton 2016/11/30 06:34:43 Diamond operator is supported starting Java 7 and
231 settings.setWhitelistedDomains(whitelistedDomains);
232 }
233
234 // update and save settings
235 whitelistedDomains.add(newDomain);
236 provider.getAdblockSettingsStorage().save(settings);
237
238 // apply settings
239 provider.getAdblockEngine().setWhitelistedDomains(whitelistedDomains);
240
241 // signal event
242 listener.onAdblockSettingsChanged(AdblockWhitelistedDomainsSettingsFragment. this);
243
244 // update UI
245 adapter.notifyDataSetChanged();
246 domain.getText().clear();
247 domain.clearFocus();
248 }
249 }
OLDNEW

Powered by Google App Engine
This is Rietveld