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

Side by Side Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/InputValidatorDialogPreference.java

Issue 29524668: Issue 3916 - Supporting Adding filter lists via URL (Closed)
Patch Set: Renamed URLInputPreference and adjusted layout design Created Sept. 15, 2017, 12:36 p.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
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.sbrowser.contentblocker; 18 package org.adblockplus.sbrowser.contentblocker;
19 19
20 import android.app.AlertDialog; 20 import android.app.AlertDialog;
21 import android.content.Context; 21 import android.content.Context;
22 import android.os.Bundle; 22 import android.os.Bundle;
23 import android.preference.EditTextPreference; 23 import android.preference.EditTextPreference;
24 import android.text.Editable; 24 import android.text.Editable;
25 import android.text.InputType; 25 import android.text.InputType;
26 import android.text.TextWatcher; 26 import android.text.TextWatcher;
27 import android.util.AttributeSet; 27 import android.util.AttributeSet;
28 import android.util.Patterns;
28 import android.view.KeyEvent; 29 import android.view.KeyEvent;
29 import android.view.View; 30 import android.view.View;
30 import android.view.inputmethod.EditorInfo; 31 import android.view.inputmethod.EditorInfo;
31 import android.widget.EditText; 32 import android.widget.EditText;
32 import android.widget.TextView; 33 import android.widget.TextView;
33 import android.widget.Toast; 34 import android.widget.Toast;
34 35
35 import org.adblockplus.adblockplussbrowser.R; 36 import org.adblockplus.adblockplussbrowser.R;
36 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils; 37 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils;
37 import org.apache.commons.validator.routines.DomainValidator; 38 import org.apache.commons.validator.routines.DomainValidator;
38 39
39 public class UrlInputOpenerPreference extends EditTextPreference implements Text Watcher, 40 public class InputValidatorDialogPreference extends EditTextPreference implement s TextWatcher,
40 TextView.OnEditorActionListener 41 TextView.OnEditorActionListener
41 { 42 {
42 43
43 private OnUrlReadyListener onUrlReadyListener; 44 public enum ValidationType
44 private AlertDialog mAlertDialog;
45
46 public UrlInputOpenerPreference(Context context)
47 { 45 {
48 this(context, null); 46 DOMAIN,
47 URL
49 } 48 }
50 49
51 public UrlInputOpenerPreference(Context context, AttributeSet attrs) 50 private OnInputReadyListener onInputReadyListener;
51 private AlertDialog mAlertDialog;
diegocarloslima 2017/09/22 17:43:42 This variable passed through the last code review,
jens 2017/09/26 10:25:01 For me it's totally fine to change the name of the
52 private ValidationType validationType;
53
54 public InputValidatorDialogPreference(Context context, final ValidationType va lidationType)
55 {
56 this(context, null, validationType);
57 }
58
59 public InputValidatorDialogPreference(Context context, AttributeSet attrs,
60 final ValidationType validationType)
diegocarloslima 2017/09/22 17:43:42 This type of constructor will fail if the class is
jens 2017/09/26 10:25:01 Oh, good catch. Should we create a separate ticket
52 { 61 {
53 super(context, attrs); 62 super(context, attrs);
54 63
55 // Setting defaults 64 // Setting defaults
65 this.validationType = validationType;
56 this.setIcon(android.R.drawable.ic_menu_add); 66 this.setIcon(android.R.drawable.ic_menu_add);
57 this.setPositiveButtonText(android.R.string.ok); 67 this.setPositiveButtonText(android.R.string.ok);
58 this.setNegativeButtonText(android.R.string.cancel); 68 this.setNegativeButtonText(android.R.string.cancel);
59 final EditText editText = getEditText(); 69 final EditText editText = getEditText();
60 editText.addTextChangedListener(this); 70 editText.addTextChangedListener(this);
61 editText.setOnEditorActionListener(this); 71 editText.setOnEditorActionListener(this);
62 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); 72 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
63 } 73 }
64 74
65 @Override 75 @Override
66 protected void showDialog(Bundle state) 76 protected void showDialog(Bundle state)
67 { 77 {
68 super.showDialog(state); 78 super.showDialog(state);
69 79
70 mAlertDialog = (AlertDialog) getDialog(); 80 mAlertDialog = (AlertDialog) getDialog();
71 // Positive button is disabled until a valid URL is entered 81 // Positive button is disabled until a valid URL is entered
72 this.setPositiveButtonEnabled(false); 82 this.setPositiveButtonEnabled(false);
73 } 83 }
74 84
75 @Override 85 @Override
76 protected void onDialogClosed(boolean positiveResult) 86 protected void onDialogClosed(boolean positiveResult)
77 { 87 {
78 super.onDialogClosed(positiveResult); 88 super.onDialogClosed(positiveResult);
79 89
80 mAlertDialog = null; 90 mAlertDialog = null;
81 if (positiveResult && this.onUrlReadyListener != null) 91 if (positiveResult && this.onInputReadyListener != null)
82 { 92 {
83 this.onUrlReadyListener.onUrlReady(getUrl()); 93 this.onInputReadyListener.onInputReady(getUrl());
84 } 94 }
85 } 95 }
86 96
87 @Override 97 @Override
88 public void beforeTextChanged(CharSequence s, int start, int count, int after) 98 public void beforeTextChanged(CharSequence s, int start, int count, int after)
89 { 99 {
90 // Ignored 100 // Ignored
91 } 101 }
92 102
93 @Override 103 @Override
94 public void onTextChanged(CharSequence s, int start, int before, int count) 104 public void onTextChanged(CharSequence s, int start, int before, int count)
95 { 105 {
96 // Ignored 106 // Ignored
97 } 107 }
98 108
99 @Override 109 @Override
100 public void afterTextChanged(Editable s) 110 public void afterTextChanged(Editable s)
101 { 111 {
102 setPositiveButtonEnabled(isValidDomain()); 112 setPositiveButtonEnabled(isValidInput());
103 } 113 }
104 114
105 @Override 115 @Override
106 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 116 public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
107 { 117 {
108 if (actionId == EditorInfo.IME_ACTION_DONE) 118 if (actionId == EditorInfo.IME_ACTION_DONE)
109 { 119 {
110 if (this.isValidDomain()) 120 if (this.isValidInput())
111 { 121 {
112 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); 122 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
113 } 123 }
114 else 124 else
115 { 125 {
116 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro r, Toast.LENGTH_SHORT).show(); 126 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro r, Toast.LENGTH_SHORT).show();
diegocarloslima 2017/09/22 17:43:42 We might consider making possible to change the te
jens 2017/09/26 10:25:01 o have different error messages for different Vali
117 } 127 }
118 return true; 128 return true;
119 } 129 }
120 return false; 130 return false;
121 } 131 }
122 132
123 @Override 133 @Override
124 protected void onBindView(View view) 134 protected void onBindView(View view)
125 { 135 {
126 super.onBindView(view); 136 super.onBindView(view);
127 PreferenceUtils.setMultilineTitle(view); 137 PreferenceUtils.setMultilineTitle(view);
128 } 138 }
129 139
130 public void setOnUrlReadyListener(OnUrlReadyListener listener) 140 public void setOnInputReadyListener(OnInputReadyListener listener)
131 { 141 {
132 this.onUrlReadyListener = listener; 142 this.onInputReadyListener = listener;
133 } 143 }
134 144
135 private boolean isValidDomain() 145 private boolean isValidDomain()
136 { 146 {
137 return DomainValidator.getInstance().isValid(getUrl()); 147 return DomainValidator.getInstance().isValid(getUrl());
138 } 148 }
139 149
150 private boolean isValidUrl()
151 {
152 return Patterns.WEB_URL.matcher(getUrl()).matches();
153 }
154
155 private boolean isValidInput()
156 {
157 switch (validationType)
158 {
159 case DOMAIN:
160 return isValidDomain();
161 default:
162 return isValidUrl();
163 }
164 }
165
140 private String getUrl() 166 private String getUrl()
diegocarloslima 2017/09/22 17:43:42 This might be renamed to getInput()
jens 2017/09/26 10:25:01 Acknowledged.
141 { 167 {
142 return getEditText().getText().toString(); 168 return getEditText().getText().toString();
143 } 169 }
144 170
145 private void setPositiveButtonEnabled(boolean enabled) 171 private void setPositiveButtonEnabled(boolean enabled)
146 { 172 {
147 if (mAlertDialog != null) 173 if (mAlertDialog != null)
148 { 174 {
149 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); 175 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled);
150 } 176 }
151 } 177 }
152 178
153 public interface OnUrlReadyListener 179 public interface OnInputReadyListener
154 { 180 {
155 void onUrlReady(String url); 181 void onInputReady(String url);
diegocarloslima 2017/09/22 17:43:41 I would suggest changing from `String url` to `Str
jens 2017/09/26 10:25:01 Acknowledged.
156 } 182 }
157 } 183 }
OLDNEW

Powered by Google App Engine
This is Rietveld