| 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..6769a9c501ef34f4f5e0098a3ec21f985af76112 |
| --- /dev/null |
| +++ b/mobile/android/thirdparty/org/adblockplus/browser/UrlInputDialog.java |
| @@ -0,0 +1,195 @@ |
| +/* |
| + * 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 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_SUBSCRIPTION, |
| + ADD_WHITELISTED_SITE |
| + } |
| + |
| + 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_SUBSCRIPTION: |
| + this.button.setText(R.string.abb_add_subscription_button); |
| + this.editText.setHint(R.string.abb_add_subscription_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 |
| + ? this.getContext().getResources().getColor(R.color.abb_url_input_button_enabled) |
| + : this.getContext().getResources().getColor(R.color.abb_url_input_button_disabled)); |
| + 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); |
| + } |
| + } |
| +} |