Index: mobile/android/thirdparty/org/adblockplus/browser/UrlInputDialog.java |
diff --git a/mobile/android/thirdparty/org/adblockplus/browser/UrlInputDialog.java b/mobile/android/thirdparty/org/adblockplus/browser/UrlInputDialog.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..41b8a16e80f245cb975612f05995af292b411f26 |
--- /dev/null |
+++ b/mobile/android/thirdparty/org/adblockplus/browser/UrlInputDialog.java |
@@ -0,0 +1,197 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+package org.adblockplus.browser; |
+ |
+import org.mozilla.gecko.R; |
+ |
+import android.app.Dialog; |
+import android.content.Context; |
+import android.os.Bundle; |
+import android.text.Editable; |
+import android.text.TextWatcher; |
+import android.util.Patterns; |
+import android.view.KeyEvent; |
+import android.view.View; |
+import android.view.View.OnClickListener; |
+import android.view.inputmethod.EditorInfo; |
+import android.widget.Button; |
+import android.widget.EditText; |
+import android.widget.TextView; |
+import android.widget.TextView.OnEditorActionListener; |
+ |
+public class UrlInputDialog extends Dialog implements TextWatcher, |
+ OnEditorActionListener, OnClickListener |
+{ |
+ private static final int BUTTON_ENABLED_COLOR = 0xff0099cc; |
+ private static final int BUTTON_DISABLED_COLOR = 0xffc5dfe8; |
+ private final Type type; |
+ private final UrlReadyCallback callback; |
+ |
+ private EditText editText = null; |
+ private Button button = null; |
+ |
+ private boolean submitEnabled = false; |
+ private boolean triedSubmit = false; |
+ private String currentEditTextValue = null; |
+ |
+ public enum Type |
+ { |
+ ADD_BLOCKING_LIST, |
+ ADD_WHITELIST |
+ } |
+ |
+ public UrlInputDialog(final Context context, final Type type, final UrlReadyCallback callback) |
+ { |
+ super(context); |
+ this.type = type; |
+ this.callback = callback; |
+ } |
+ |
+ @Override |
+ protected void onCreate(final Bundle savedInstanceState) |
+ { |
+ super.onCreate(savedInstanceState); |
+ this.setContentView(R.layout.abb_url_input_dialog); |
+ |
+ this.editText = (EditText) this.findViewById(R.id.abb_listadd_textinput); |
+ this.button = (Button) this.findViewById(R.id.abb_listadd_button); |
+ |
+ this.setCanceledOnTouchOutside(true); |
+ this.setCancelable(true); |
+ |
+ this.editText.addTextChangedListener(this); |
+ this.editText.setOnEditorActionListener(this); |
+ this.editText.requestFocus(); |
+ |
+ this.button.setOnClickListener(this); |
+ |
+ if (this.currentEditTextValue != null) |
+ { |
+ this.editText.setText(this.currentEditTextValue); |
+ } |
+ this.updateButtonState(); |
+ |
+ switch (this.type) |
+ { |
+ case ADD_BLOCKING_LIST: |
+ this.button.setText(R.string.abb_block_list_button); |
+ this.editText.setHint(R.string.abb_block_list_url); |
+ this.setTitle(R.string.abb_pref_category_add_other_list); |
+ break; |
+ default: |
+ break; |
+ } |
+ } |
+ |
+ private void updateButtonState() |
+ { |
+ this.button.setEnabled(this.submitEnabled); |
+ this.button.setBackgroundColor(this.submitEnabled |
+ ? BUTTON_ENABLED_COLOR |
+ : BUTTON_DISABLED_COLOR); |
+ this.button.invalidate(); |
+ } |
+ |
+ @Override |
+ public void afterTextChanged(final Editable s) |
+ { |
+ this.currentEditTextValue = s.toString(); |
+ this.submitEnabled = this.validateUrl(this.currentEditTextValue) != null; |
+ if (this.submitEnabled) |
+ { |
+ this.editText.setError(null); |
+ } |
+ else if (this.triedSubmit) |
+ { |
+ this.editText.setError(this.getContext().getString(R.string.abb_invalid_url)); |
+ } |
+ this.updateButtonState(); |
+ } |
+ |
+ @Override |
+ public void beforeTextChanged(final CharSequence s, final int start, final int count, |
+ final int after) |
+ { |
+ // ignored |
+ } |
+ |
+ @Override |
+ public void onTextChanged(final CharSequence s, final int start, final int before, final int count) |
+ { |
+ // ignored |
+ } |
+ |
+ private String validateUrl(final String possibleUrl) |
+ { |
+ String url = possibleUrl.trim(); |
+ if (Patterns.WEB_URL.matcher(url).matches()) |
+ { |
+ return url; |
+ } |
+ url = "http://" + url; |
+ if (Patterns.WEB_URL.matcher(url).matches()) |
+ { |
+ return url; |
+ } |
+ return null; |
+ } |
+ |
+ private void validateAndSubmit(final String inputText) |
+ { |
+ final String url = this.validateUrl(inputText); |
+ this.triedSubmit = true; |
+ if (url == null) |
+ { |
+ this.editText.setError(this.getContext().getString(R.string.abb_invalid_url)); |
+ } |
+ else |
+ { |
+ this.editText.setError(null); |
+ this.dismiss(); |
+ this.callback.callback(url); |
+ } |
+ } |
+ |
+ @Override |
+ public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) |
+ { |
+ if (actionId == EditorInfo.IME_ACTION_DONE) |
+ { |
+ if (this.currentEditTextValue != null) |
+ { |
+ this.validateAndSubmit(this.currentEditTextValue); |
+ } |
+ return true; |
+ } |
+ return false; |
+ } |
+ |
+ public interface UrlReadyCallback |
+ { |
+ public void callback(String url); |
+ } |
+ |
+ @Override |
+ public void onClick(final View v) |
+ { |
+ if (this.currentEditTextValue != null) |
+ { |
+ this.validateAndSubmit(this.currentEditTextValue); |
+ } |
+ } |
+} |