Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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; | |
28 import android.util.Patterns; | 30 import android.util.Patterns; |
29 import android.view.KeyEvent; | 31 import android.view.KeyEvent; |
30 import android.view.View; | 32 import android.view.View; |
31 import android.view.inputmethod.EditorInfo; | 33 import android.view.inputmethod.EditorInfo; |
32 import android.widget.EditText; | 34 import android.widget.EditText; |
33 import android.widget.TextView; | 35 import android.widget.TextView; |
34 import android.widget.Toast; | 36 import android.widget.Toast; |
35 | 37 |
36 import org.adblockplus.adblockplussbrowser.R; | 38 import org.adblockplus.adblockplussbrowser.R; |
39 import org.adblockplus.sbrowser.contentblocker.engine.Engine; | |
37 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils; | 40 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils; |
38 import org.apache.commons.validator.routines.DomainValidator; | 41 import org.apache.commons.validator.routines.DomainValidator; |
39 | 42 |
40 public class InputValidatorDialogPreference extends EditTextPreference implement s TextWatcher, | 43 public class InputValidatorDialogPreference extends EditTextPreference implement s TextWatcher, |
41 TextView.OnEditorActionListener | 44 TextView.OnEditorActionListener |
42 { | 45 { |
43 | 46 |
44 public enum ValidationType | 47 public enum ValidationType |
45 { | 48 { |
46 DOMAIN, | 49 DOMAIN, |
47 URL | 50 URL |
48 } | 51 } |
49 | 52 |
53 private static final String TAG = InputValidatorDialogPreference.class.getSimp leName(); | |
50 private OnInputReadyListener onInputReadyListener; | 54 private OnInputReadyListener onInputReadyListener; |
51 private AlertDialog mAlertDialog; | 55 private AlertDialog alertDialog; |
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; | 56 private ValidationType validationType; |
53 | 57 |
54 public InputValidatorDialogPreference(Context context, final ValidationType va lidationType) | 58 public InputValidatorDialogPreference(Context context) |
55 { | 59 { |
56 this(context, null, validationType); | 60 this(context, null); |
57 } | 61 } |
58 | 62 |
59 public InputValidatorDialogPreference(Context context, AttributeSet attrs, | 63 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
| |
61 { | 64 { |
62 super(context, attrs); | 65 super(context, attrs); |
63 | 66 |
64 // Setting defaults | 67 // Setting defaults |
65 this.validationType = validationType; | |
66 this.setIcon(android.R.drawable.ic_menu_add); | 68 this.setIcon(android.R.drawable.ic_menu_add); |
67 this.setPositiveButtonText(android.R.string.ok); | 69 this.setPositiveButtonText(android.R.string.ok); |
68 this.setNegativeButtonText(android.R.string.cancel); | 70 this.setNegativeButtonText(android.R.string.cancel); |
69 final EditText editText = getEditText(); | 71 final EditText editText = getEditText(); |
70 editText.addTextChangedListener(this); | 72 editText.addTextChangedListener(this); |
71 editText.setOnEditorActionListener(this); | 73 editText.setOnEditorActionListener(this); |
72 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); | 74 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); |
73 } | 75 } |
74 | 76 |
75 @Override | 77 @Override |
76 protected void showDialog(Bundle state) | 78 protected void showDialog(Bundle state) |
77 { | 79 { |
78 super.showDialog(state); | 80 super.showDialog(state); |
79 | 81 |
80 mAlertDialog = (AlertDialog) getDialog(); | 82 alertDialog = (AlertDialog) getDialog(); |
81 // Positive button is disabled until a valid URL is entered | 83 // Positive button is disabled until a valid URL is entered |
82 this.setPositiveButtonEnabled(false); | 84 this.setPositiveButtonEnabled(false); |
83 } | 85 } |
84 | 86 |
85 @Override | 87 @Override |
86 protected void onDialogClosed(boolean positiveResult) | 88 protected void onDialogClosed(boolean positiveResult) |
87 { | 89 { |
88 super.onDialogClosed(positiveResult); | 90 super.onDialogClosed(positiveResult); |
89 | 91 |
90 mAlertDialog = null; | 92 alertDialog = null; |
91 if (positiveResult && this.onInputReadyListener != null) | 93 if (positiveResult && this.onInputReadyListener != null) |
92 { | 94 { |
93 this.onInputReadyListener.onInputReady(getUrl()); | 95 this.onInputReadyListener.onInputReady(getInput()); |
94 } | 96 } |
95 } | 97 } |
96 | 98 |
97 @Override | 99 @Override |
98 public void beforeTextChanged(CharSequence s, int start, int count, int after) | 100 public void beforeTextChanged(CharSequence s, int start, int count, int after) |
99 { | 101 { |
100 // Ignored | 102 // Ignored |
101 } | 103 } |
102 | 104 |
103 @Override | 105 @Override |
104 public void onTextChanged(CharSequence s, int start, int before, int count) | 106 public void onTextChanged(CharSequence s, int start, int before, int count) |
105 { | 107 { |
106 // Ignored | 108 // Ignored |
107 } | 109 } |
108 | 110 |
109 @Override | 111 @Override |
110 public void afterTextChanged(Editable s) | 112 public void afterTextChanged(Editable s) |
111 { | 113 { |
112 setPositiveButtonEnabled(isValidInput()); | 114 setPositiveButtonEnabled(isValidInput()); |
113 } | 115 } |
114 | 116 |
115 @Override | 117 @Override |
116 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) | 118 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) |
117 { | 119 { |
118 if (actionId == EditorInfo.IME_ACTION_DONE) | 120 if (actionId == EditorInfo.IME_ACTION_DONE) |
119 { | 121 { |
120 if (this.isValidInput()) | 122 if (this.isValidInput()) |
121 { | 123 { |
122 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); | 124 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
123 } | 125 } |
124 else | 126 else |
125 { | 127 { |
126 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro r, Toast.LENGTH_SHORT).show(); | 128 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro r, Toast.LENGTH_SHORT).show(); |
127 } | 129 } |
128 return true; | 130 return true; |
129 } | 131 } |
130 return false; | 132 return false; |
131 } | 133 } |
132 | 134 |
133 @Override | 135 @Override |
134 protected void onBindView(View view) | 136 protected void onBindView(View view) |
135 { | 137 { |
136 super.onBindView(view); | 138 super.onBindView(view); |
137 PreferenceUtils.setMultilineTitle(view); | 139 PreferenceUtils.setMultilineTitle(view); |
138 } | 140 } |
139 | 141 |
142 public void setValidationType(@NonNull final ValidationType validationType) | |
143 { | |
144 this.validationType = validationType; | |
145 } | |
146 | |
140 public void setOnInputReadyListener(OnInputReadyListener listener) | 147 public void setOnInputReadyListener(OnInputReadyListener listener) |
141 { | 148 { |
142 this.onInputReadyListener = listener; | 149 this.onInputReadyListener = listener; |
143 } | 150 } |
144 | 151 |
145 private boolean isValidDomain() | 152 private boolean isValidDomain() |
146 { | 153 { |
147 return DomainValidator.getInstance().isValid(getUrl()); | 154 return DomainValidator.getInstance().isValid(getInput()); |
148 } | 155 } |
149 | 156 |
150 private boolean isValidUrl() | 157 private boolean isValidUrl() |
151 { | 158 { |
152 return Patterns.WEB_URL.matcher(getUrl()).matches(); | 159 return Patterns.WEB_URL.matcher(getInput()).matches(); |
153 } | 160 } |
154 | 161 |
155 private boolean isValidInput() | 162 private boolean isValidInput() |
156 { | 163 { |
164 if (validationType == null) | |
165 { | |
166 Log.i(TAG, "ValidationType was not defined and is therefore set to" + | |
167 " ValidationType.URL per default. Please consider to set in manually." ); | |
168 validationType = ValidationType.URL; | |
169 } | |
170 | |
157 switch (validationType) | 171 switch (validationType) |
158 { | 172 { |
159 case DOMAIN: | 173 case DOMAIN: |
160 return isValidDomain(); | 174 return isValidDomain(); |
161 default: | 175 default: |
162 return isValidUrl(); | 176 return isValidUrl(); |
163 } | 177 } |
164 } | 178 } |
165 | 179 |
166 private String getUrl() | 180 private String getInput() |
167 { | 181 { |
168 return getEditText().getText().toString(); | 182 return getEditText().getText().toString(); |
169 } | 183 } |
170 | 184 |
171 private void setPositiveButtonEnabled(boolean enabled) | 185 private void setPositiveButtonEnabled(boolean enabled) |
172 { | 186 { |
173 if (mAlertDialog != null) | 187 if (alertDialog != null) |
174 { | 188 { |
175 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); | 189 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
176 } | 190 } |
177 } | 191 } |
178 | 192 |
179 public interface OnInputReadyListener | 193 public interface OnInputReadyListener |
180 { | 194 { |
181 void onInputReady(String url); | 195 void onInputReady(String input); |
diegocarloslima
2017/09/22 17:43:41
I would suggest changing from `String url` to `Str
jens
2017/09/26 10:25:01
Acknowledged.
| |
182 } | 196 } |
183 } | 197 } |
LEFT | RIGHT |