 Issue 29322610:
  Issue 2720 - [Adblocking settings] Add the other filter lists category  (Closed)
    
  
    Issue 29322610:
  Issue 2720 - [Adblocking settings] Add the other filter lists category  (Closed) 
  | 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; | 
| 
Felix Dahlke
2015/07/26 17:06:23
As you know, I don't think we should have hard cod
 
René Jeschke
2015/07/28 10:55:27
Done.
 | 
| + 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, | 
| 
Felix Dahlke
2015/07/29 20:32:07
Nit: As said elsewhere, we should avoid hard codin
 
René Jeschke
2015/07/30 09:26:44
Done.
 | 
| + ADD_WHITELIST | 
| 
Felix Dahlke
2015/07/26 17:06:22
It seems to me we're not using ADD_WHITELIST, nor
 
René Jeschke
2015/07/28 10:55:27
Because the same dialogue will be needed to implem
 
Felix Dahlke
2015/07/29 20:32:07
Acknowledged. Nit then though: Maybe call it ADD_W
 
René Jeschke
2015/07/30 09:26:44
Done.
 | 
| + } | 
| + | 
| + 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; | 
| 
Felix Dahlke
2015/07/26 17:06:22
IMHO we should just consider URLs without the prot
 
René Jeschke
2015/07/28 10:55:26
Do you really want to force all Android users, to
 
Felix Dahlke
2015/07/29 20:32:07
Hm, fair enough with the typing... Maybe it'd be s
 | 
| + 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); | 
| + } | 
| + } | 
| +} |