| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 UrlInputOpenerPreference extends EditTextPreference implements Text Watcher, |
|
diegocarloslima
2017/09/08 15:06:07
I would suggest to rename the class to something l
jens
2017/09/13 08:50:29
Acknowledged.
| |
| 40 TextView.OnEditorActionListener | 41 TextView.OnEditorActionListener |
| 41 { | 42 { |
| 42 | 43 |
| 43 private OnUrlReadyListener onUrlReadyListener; | 44 private OnUrlReadyListener onUrlReadyListener; |
| 44 private AlertDialog mAlertDialog; | 45 private AlertDialog mAlertDialog; |
| 46 private boolean validateTld; | |
|
anton
2017/08/25 10:40:40
sorry, what is `Tld` (did i miss anything)? Can yo
jens
2017/08/25 11:35:06
It stands for top-level domain. I thought it is a
anton
2017/08/25 11:43:30
Okay, why not just call it 'validateDomain' for si
diegocarloslima
2017/09/08 15:06:07
I would suggest to create an enum for that, so we
jens
2017/09/13 08:50:29
Acknowledged.
| |
| 45 | 47 |
| 46 public UrlInputOpenerPreference(Context context) | 48 public UrlInputOpenerPreference(Context context, final boolean validateTld) |
| 47 { | 49 { |
| 48 this(context, null); | 50 this(context, null, validateTld); |
| 49 } | 51 } |
| 50 | 52 |
| 51 public UrlInputOpenerPreference(Context context, AttributeSet attrs) | 53 public UrlInputOpenerPreference(Context context, AttributeSet attrs, final boo lean validateTld) |
| 52 { | 54 { |
| 53 super(context, attrs); | 55 super(context, attrs); |
| 54 | 56 |
| 55 // Setting defaults | 57 // Setting defaults |
| 58 this.validateTld = validateTld; | |
| 56 this.setIcon(android.R.drawable.ic_menu_add); | 59 this.setIcon(android.R.drawable.ic_menu_add); |
| 57 this.setPositiveButtonText(android.R.string.ok); | 60 this.setPositiveButtonText(android.R.string.ok); |
| 58 this.setNegativeButtonText(android.R.string.cancel); | 61 this.setNegativeButtonText(android.R.string.cancel); |
| 59 final EditText editText = getEditText(); | 62 final EditText editText = getEditText(); |
| 60 editText.addTextChangedListener(this); | 63 editText.addTextChangedListener(this); |
| 61 editText.setOnEditorActionListener(this); | 64 editText.setOnEditorActionListener(this); |
| 62 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); | 65 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); |
| 63 } | 66 } |
| 64 | 67 |
| 65 @Override | 68 @Override |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 92 | 95 |
| 93 @Override | 96 @Override |
| 94 public void onTextChanged(CharSequence s, int start, int before, int count) | 97 public void onTextChanged(CharSequence s, int start, int before, int count) |
| 95 { | 98 { |
| 96 // Ignored | 99 // Ignored |
| 97 } | 100 } |
| 98 | 101 |
| 99 @Override | 102 @Override |
| 100 public void afterTextChanged(Editable s) | 103 public void afterTextChanged(Editable s) |
| 101 { | 104 { |
| 102 setPositiveButtonEnabled(isValidDomain()); | 105 setPositiveButtonEnabled(validate()); |
| 103 } | 106 } |
| 104 | 107 |
| 105 @Override | 108 @Override |
| 106 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) | 109 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) |
| 107 { | 110 { |
| 108 if (actionId == EditorInfo.IME_ACTION_DONE) | 111 if (actionId == EditorInfo.IME_ACTION_DONE) |
| 109 { | 112 { |
| 110 if (this.isValidDomain()) | 113 if (this.validate()) |
| 111 { | 114 { |
| 112 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); | 115 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
| 113 } | 116 } |
| 114 else | 117 else |
| 115 { | 118 { |
| 116 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro r, Toast.LENGTH_SHORT).show(); | 119 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro r, Toast.LENGTH_SHORT).show(); |
| 117 } | 120 } |
| 118 return true; | 121 return true; |
| 119 } | 122 } |
| 120 return false; | 123 return false; |
| 121 } | 124 } |
| 122 | 125 |
| 123 @Override | 126 @Override |
| 124 protected void onBindView(View view) | 127 protected void onBindView(View view) |
| 125 { | 128 { |
| 126 super.onBindView(view); | 129 super.onBindView(view); |
| 127 PreferenceUtils.setMultilineTitle(view); | 130 PreferenceUtils.setMultilineTitle(view); |
| 128 } | 131 } |
| 129 | 132 |
| 130 public void setOnUrlReadyListener(OnUrlReadyListener listener) | 133 public void setOnUrlReadyListener(OnUrlReadyListener listener) |
| 131 { | 134 { |
| 132 this.onUrlReadyListener = listener; | 135 this.onUrlReadyListener = listener; |
| 133 } | 136 } |
| 134 | 137 |
| 135 private boolean isValidDomain() | 138 private boolean isValidDomain() |
| 136 { | 139 { |
| 137 return DomainValidator.getInstance().isValid(getUrl()); | 140 return DomainValidator.getInstance().isValid(getUrl()); |
| 138 } | 141 } |
| 139 | 142 |
| 143 private boolean isValidUrl() | |
| 144 { | |
| 145 return Patterns.WEB_URL.matcher(getUrl()).matches(); | |
| 146 } | |
| 147 | |
| 148 private boolean validate() | |
|
anton
2017/08/25 10:40:40
improvement suggestion: `validate` makes me think
jens
2017/08/25 11:35:06
Acknowledged.
diegocarloslima
2017/09/08 15:06:07
I would further suggest to rename it to isValidInp
jens
2017/09/13 08:50:30
Acknowledged.
| |
| 149 { | |
| 150 if (validateTld) | |
| 151 { | |
| 152 return isValidDomain(); | |
| 153 } | |
| 154 else | |
| 155 { | |
| 156 return isValidUrl(); | |
| 157 } | |
| 158 } | |
| 159 | |
| 140 private String getUrl() | 160 private String getUrl() |
| 141 { | 161 { |
| 142 return getEditText().getText().toString(); | 162 return getEditText().getText().toString(); |
| 143 } | 163 } |
| 144 | 164 |
| 145 private void setPositiveButtonEnabled(boolean enabled) | 165 private void setPositiveButtonEnabled(boolean enabled) |
| 146 { | 166 { |
| 147 if (mAlertDialog != null) | 167 if (mAlertDialog != null) |
| 148 { | 168 { |
| 149 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); | 169 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
| 150 } | 170 } |
| 151 } | 171 } |
| 152 | 172 |
| 153 public interface OnUrlReadyListener | 173 public interface OnUrlReadyListener |
|
diegocarloslima
2017/09/08 15:06:07
I would also suggest to rename the listener to OnI
jens
2017/09/13 08:50:30
Acknowledged.
| |
| 154 { | 174 { |
| 155 void onUrlReady(String url); | 175 void onUrlReady(String url); |
| 156 } | 176 } |
| 157 } | 177 } |
| OLD | NEW |