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: fixes Created Oct. 4, 2017, 3:14 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.support.annotation.NonNull;
24 import android.text.Editable; 25 import android.text.Editable;
25 import android.text.InputType; 26 import android.text.InputType;
26 import android.text.TextWatcher; 27 import android.text.TextWatcher;
27 import android.util.AttributeSet; 28 import android.util.AttributeSet;
29 import android.util.Log;
30 import android.util.Patterns;
28 import android.view.KeyEvent; 31 import android.view.KeyEvent;
29 import android.view.View; 32 import android.view.View;
30 import android.view.inputmethod.EditorInfo; 33 import android.view.inputmethod.EditorInfo;
31 import android.widget.EditText; 34 import android.widget.EditText;
32 import android.widget.TextView; 35 import android.widget.TextView;
33 import android.widget.Toast; 36 import android.widget.Toast;
34 37
35 import org.adblockplus.adblockplussbrowser.R; 38 import org.adblockplus.adblockplussbrowser.R;
36 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils; 39 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils;
37 import org.apache.commons.validator.routines.DomainValidator; 40 import org.apache.commons.validator.routines.DomainValidator;
38 41
39 public class UrlInputOpenerPreference extends EditTextPreference implements Text Watcher, 42 public class InputValidatorDialogPreference extends EditTextPreference implement s TextWatcher,
40 TextView.OnEditorActionListener 43 TextView.OnEditorActionListener
41 { 44 {
42 45
43 private OnUrlReadyListener onUrlReadyListener; 46 public enum ValidationType
44 private AlertDialog mAlertDialog; 47 {
48 DOMAIN,
49 URL
50 }
45 51
46 public UrlInputOpenerPreference(Context context) 52 private OnInputReadyListener onInputReadyListener;
53 private AlertDialog alertDialog;
54 private ValidationType validationType;
55
56 public InputValidatorDialogPreference(Context context)
47 { 57 {
48 this(context, null); 58 this(context, null);
49 } 59 }
50 60
51 public UrlInputOpenerPreference(Context context, AttributeSet attrs) 61 public InputValidatorDialogPreference(Context context, AttributeSet attrs)
52 { 62 {
53 super(context, attrs); 63 super(context, attrs);
54 64
55 // Setting defaults 65 // Setting defaults
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 alertDialog = (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 alertDialog = null;
81 if (positiveResult && this.onUrlReadyListener != null) 91 if (positiveResult && this.onInputReadyListener != null)
82 { 92 {
83 this.onUrlReadyListener.onUrlReady(getUrl()); 93 this.onInputReadyListener.onInputReady(getInput());
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 alertDialog.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();
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 setValidationType(@NonNull final ValidationType validationType)
131 { 141 {
132 this.onUrlReadyListener = listener; 142 this.validationType = validationType;
143 }
144
145 public void setOnInputReadyListener(OnInputReadyListener listener)
146 {
147 this.onInputReadyListener = listener;
133 } 148 }
134 149
135 private boolean isValidDomain() 150 private boolean isValidDomain()
136 { 151 {
137 return DomainValidator.getInstance().isValid(getUrl()); 152 return DomainValidator.getInstance().isValid(getInput());
138 } 153 }
139 154
140 private String getUrl() 155 private boolean isValidUrl()
156 {
157 return Patterns.WEB_URL.matcher(getInput()).matches();
158 }
159
160 private boolean isValidInput()
161 {
162 if (validationType == null)
163 {
164 Log.i(getClass().getSimpleName(), "ValidationType was not defined and is t herefore set to" +
diegocarloslima 2017/10/10 13:08:28 I know that the log tag is currently only used her
jens 2017/10/10 13:56:40 I was not sure if it's mandatory, as there are als
165 " ValidationType.URL per default. Please consider to set in manually." );
166 validationType = ValidationType.URL;
167 }
168
169 switch (validationType)
170 {
171 case DOMAIN:
172 return isValidDomain();
173 default:
174 return isValidUrl();
175 }
176 }
177
178 private String getInput()
141 { 179 {
142 return getEditText().getText().toString(); 180 return getEditText().getText().toString();
143 } 181 }
144 182
145 private void setPositiveButtonEnabled(boolean enabled) 183 private void setPositiveButtonEnabled(boolean enabled)
146 { 184 {
147 if (mAlertDialog != null) 185 if (alertDialog != null)
148 { 186 {
149 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); 187 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled);
150 } 188 }
151 } 189 }
152 190
153 public interface OnUrlReadyListener 191 public interface OnInputReadyListener
154 { 192 {
155 void onUrlReady(String url); 193 void onInputReady(String input);
156 } 194 }
157 } 195 }
OLDNEW

Powered by Google App Engine
This is Rietveld