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