OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 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.browser; |
| 19 |
| 20 import org.mozilla.gecko.R; |
| 21 |
| 22 import android.app.Dialog; |
| 23 import android.content.Context; |
| 24 import android.os.Bundle; |
| 25 import android.text.Editable; |
| 26 import android.text.TextWatcher; |
| 27 import android.util.Patterns; |
| 28 import android.view.KeyEvent; |
| 29 import android.view.View; |
| 30 import android.view.View.OnClickListener; |
| 31 import android.view.inputmethod.EditorInfo; |
| 32 import android.widget.Button; |
| 33 import android.widget.EditText; |
| 34 import android.widget.TextView; |
| 35 import android.widget.TextView.OnEditorActionListener; |
| 36 |
| 37 public class UrlInputDialog extends Dialog implements TextWatcher, |
| 38 OnEditorActionListener, OnClickListener |
| 39 { |
| 40 private final Type type; |
| 41 private final UrlReadyCallback callback; |
| 42 |
| 43 private EditText editText = null; |
| 44 private Button button = null; |
| 45 |
| 46 private boolean submitEnabled = false; |
| 47 private boolean triedSubmit = false; |
| 48 private String currentEditTextValue = null; |
| 49 |
| 50 public enum Type |
| 51 { |
| 52 ADD_BLOCKING_LIST, |
| 53 ADD_WHITELIST |
| 54 } |
| 55 |
| 56 public UrlInputDialog(final Context context, final Type type, final UrlReadyCa
llback callback) |
| 57 { |
| 58 super(context); |
| 59 this.type = type; |
| 60 this.callback = callback; |
| 61 } |
| 62 |
| 63 @Override |
| 64 protected void onCreate(final Bundle savedInstanceState) |
| 65 { |
| 66 super.onCreate(savedInstanceState); |
| 67 this.setContentView(R.layout.abb_url_input_dialog); |
| 68 |
| 69 this.editText = (EditText) this.findViewById(R.id.abb_listadd_textinput); |
| 70 this.button = (Button) this.findViewById(R.id.abb_listadd_button); |
| 71 |
| 72 this.setCanceledOnTouchOutside(true); |
| 73 this.setCancelable(true); |
| 74 |
| 75 this.editText.addTextChangedListener(this); |
| 76 this.editText.setOnEditorActionListener(this); |
| 77 this.editText.requestFocus(); |
| 78 |
| 79 this.button.setOnClickListener(this); |
| 80 |
| 81 if (this.currentEditTextValue != null) |
| 82 { |
| 83 this.editText.setText(this.currentEditTextValue); |
| 84 } |
| 85 this.updateButtonState(); |
| 86 |
| 87 switch (this.type) |
| 88 { |
| 89 case ADD_BLOCKING_LIST: |
| 90 this.button.setText(R.string.abb_add_filter_list_button); |
| 91 this.editText.setHint(R.string.abb_add_filter_list_url); |
| 92 this.setTitle(R.string.abb_pref_category_add_other_list); |
| 93 break; |
| 94 default: |
| 95 break; |
| 96 } |
| 97 } |
| 98 |
| 99 private void updateButtonState() |
| 100 { |
| 101 this.button.setEnabled(this.submitEnabled); |
| 102 this.button.setBackgroundColor(this.submitEnabled |
| 103 ? this.getContext().getResources().getColor(R.color.abb_url_input_button
_enabled) |
| 104 : this.getContext().getResources().getColor(R.color.abb_url_input_button
_disabled)); |
| 105 this.button.invalidate(); |
| 106 } |
| 107 |
| 108 @Override |
| 109 public void afterTextChanged(final Editable s) |
| 110 { |
| 111 this.currentEditTextValue = s.toString(); |
| 112 this.submitEnabled = this.validateUrl(this.currentEditTextValue) != null; |
| 113 if (this.submitEnabled) |
| 114 { |
| 115 this.editText.setError(null); |
| 116 } |
| 117 else if (this.triedSubmit) |
| 118 { |
| 119 this.editText.setError(this.getContext().getString(R.string.abb_invalid_ur
l)); |
| 120 } |
| 121 this.updateButtonState(); |
| 122 } |
| 123 |
| 124 @Override |
| 125 public void beforeTextChanged(final CharSequence s, final int start, final int
count, |
| 126 final int after) |
| 127 { |
| 128 // ignored |
| 129 } |
| 130 |
| 131 @Override |
| 132 public void onTextChanged(final CharSequence s, final int start, final int bef
ore, final int count) |
| 133 { |
| 134 // ignored |
| 135 } |
| 136 |
| 137 private String validateUrl(final String possibleUrl) |
| 138 { |
| 139 String url = possibleUrl.trim(); |
| 140 if (Patterns.WEB_URL.matcher(url).matches()) |
| 141 { |
| 142 return url; |
| 143 } |
| 144 url = "http://" + url; |
| 145 if (Patterns.WEB_URL.matcher(url).matches()) |
| 146 { |
| 147 return url; |
| 148 } |
| 149 return null; |
| 150 } |
| 151 |
| 152 private void validateAndSubmit(final String inputText) |
| 153 { |
| 154 final String url = this.validateUrl(inputText); |
| 155 this.triedSubmit = true; |
| 156 if (url == null) |
| 157 { |
| 158 this.editText.setError(this.getContext().getString(R.string.abb_invalid_ur
l)); |
| 159 } |
| 160 else |
| 161 { |
| 162 this.editText.setError(null); |
| 163 this.dismiss(); |
| 164 this.callback.callback(url); |
| 165 } |
| 166 } |
| 167 |
| 168 @Override |
| 169 public boolean onEditorAction(final TextView v, final int actionId, final KeyE
vent event) |
| 170 { |
| 171 if (actionId == EditorInfo.IME_ACTION_DONE) |
| 172 { |
| 173 if (this.currentEditTextValue != null) |
| 174 { |
| 175 this.validateAndSubmit(this.currentEditTextValue); |
| 176 } |
| 177 return true; |
| 178 } |
| 179 return false; |
| 180 } |
| 181 |
| 182 public interface UrlReadyCallback |
| 183 { |
| 184 public void callback(String url); |
| 185 } |
| 186 |
| 187 @Override |
| 188 public void onClick(final View v) |
| 189 { |
| 190 if (this.currentEditTextValue != null) |
| 191 { |
| 192 this.validateAndSubmit(this.currentEditTextValue); |
| 193 } |
| 194 } |
| 195 } |
OLD | NEW |