OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.sbrowser.contentblocker; |
| 19 |
| 20 import android.app.AlertDialog; |
| 21 import android.content.Context; |
| 22 import android.os.Bundle; |
| 23 import android.preference.EditTextPreference; |
| 24 import android.support.annotation.NonNull; |
| 25 import android.text.Editable; |
| 26 import android.text.InputType; |
| 27 import android.text.TextWatcher; |
| 28 import android.util.AttributeSet; |
| 29 import android.util.Log; |
| 30 import android.util.Patterns; |
| 31 import android.view.KeyEvent; |
| 32 import android.view.View; |
| 33 import android.view.inputmethod.EditorInfo; |
| 34 import android.widget.EditText; |
| 35 import android.widget.TextView; |
| 36 import android.widget.Toast; |
| 37 |
| 38 import org.adblockplus.adblockplussbrowser.R; |
| 39 import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
| 40 import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils; |
| 41 import org.apache.commons.validator.routines.DomainValidator; |
| 42 |
| 43 public class InputValidatorDialogPreference extends EditTextPreference implement
s TextWatcher, |
| 44 TextView.OnEditorActionListener |
| 45 { |
| 46 |
| 47 public enum ValidationType |
| 48 { |
| 49 DOMAIN, |
| 50 URL |
| 51 } |
| 52 |
| 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) |
| 59 { |
| 60 this(context, null); |
| 61 } |
| 62 |
| 63 public InputValidatorDialogPreference(Context context, AttributeSet attrs) |
| 64 { |
| 65 super(context, attrs); |
| 66 |
| 67 // Setting defaults |
| 68 this.setIcon(android.R.drawable.ic_menu_add); |
| 69 this.setPositiveButtonText(android.R.string.ok); |
| 70 this.setNegativeButtonText(android.R.string.cancel); |
| 71 final EditText editText = getEditText(); |
| 72 editText.addTextChangedListener(this); |
| 73 editText.setOnEditorActionListener(this); |
| 74 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); |
| 75 } |
| 76 |
| 77 @Override |
| 78 protected void showDialog(Bundle state) |
| 79 { |
| 80 super.showDialog(state); |
| 81 |
| 82 alertDialog = (AlertDialog) getDialog(); |
| 83 // Positive button is disabled until a valid URL is entered |
| 84 this.setPositiveButtonEnabled(false); |
| 85 } |
| 86 |
| 87 @Override |
| 88 protected void onDialogClosed(boolean positiveResult) |
| 89 { |
| 90 super.onDialogClosed(positiveResult); |
| 91 |
| 92 alertDialog = null; |
| 93 if (positiveResult && this.onInputReadyListener != null) |
| 94 { |
| 95 this.onInputReadyListener.onInputReady(getInput()); |
| 96 } |
| 97 } |
| 98 |
| 99 @Override |
| 100 public void beforeTextChanged(CharSequence s, int start, int count, int after) |
| 101 { |
| 102 // Ignored |
| 103 } |
| 104 |
| 105 @Override |
| 106 public void onTextChanged(CharSequence s, int start, int before, int count) |
| 107 { |
| 108 // Ignored |
| 109 } |
| 110 |
| 111 @Override |
| 112 public void afterTextChanged(Editable s) |
| 113 { |
| 114 setPositiveButtonEnabled(isValidInput()); |
| 115 } |
| 116 |
| 117 @Override |
| 118 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) |
| 119 { |
| 120 if (actionId == EditorInfo.IME_ACTION_DONE) |
| 121 { |
| 122 if (this.isValidInput()) |
| 123 { |
| 124 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
| 125 } |
| 126 else |
| 127 { |
| 128 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro
r, Toast.LENGTH_SHORT).show(); |
| 129 } |
| 130 return true; |
| 131 } |
| 132 return false; |
| 133 } |
| 134 |
| 135 @Override |
| 136 protected void onBindView(View view) |
| 137 { |
| 138 super.onBindView(view); |
| 139 PreferenceUtils.setMultilineTitle(view); |
| 140 } |
| 141 |
| 142 public void setValidationType(@NonNull final ValidationType validationType) |
| 143 { |
| 144 this.validationType = validationType; |
| 145 } |
| 146 |
| 147 public void setOnInputReadyListener(OnInputReadyListener listener) |
| 148 { |
| 149 this.onInputReadyListener = listener; |
| 150 } |
| 151 |
| 152 private boolean isValidDomain() |
| 153 { |
| 154 return DomainValidator.getInstance().isValid(getInput()); |
| 155 } |
| 156 |
| 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() |
| 181 { |
| 182 return getEditText().getText().toString(); |
| 183 } |
| 184 |
| 185 private void setPositiveButtonEnabled(boolean enabled) |
| 186 { |
| 187 if (alertDialog != null) |
| 188 { |
| 189 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
| 190 } |
| 191 } |
| 192 |
| 193 public interface OnInputReadyListener |
| 194 { |
| 195 void onInputReady(String input); |
| 196 } |
| 197 } |
OLD | NEW |