Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: mobile/android/thirdparty/org/adblockplus/browser/InputValidatorDialogPreference.java

Issue 29543774: Issue 2801 - Create 'Whitelisted websites' screen and add link to 'Ad blocking' screen (Closed)
Patch Set: Adjustments accordingly to Thomas's comments. Also, adjusting strings for multilocale build Created Sept. 19, 2017, 3:18 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mobile/android/thirdparty/org/adblockplus/browser/InputValidatorDialogPreference.java
===================================================================
rename from mobile/android/thirdparty/org/adblockplus/browser/UrlInputOpenerPreference.java
rename to mobile/android/thirdparty/org/adblockplus/browser/InputValidatorDialogPreference.java
--- a/mobile/android/thirdparty/org/adblockplus/browser/UrlInputOpenerPreference.java
+++ b/mobile/android/thirdparty/org/adblockplus/browser/InputValidatorDialogPreference.java
@@ -12,71 +12,202 @@
* 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 java.lang.ref.WeakReference;
-
+import android.app.AlertDialog;
import android.content.Context;
-import android.preference.Preference;
+import android.os.Bundle;
+import android.preference.EditTextPreference;
+import android.text.Editable;
+import android.text.InputType;
+import android.text.TextWatcher;
import android.util.AttributeSet;
+import android.util.Patterns;
+import android.view.KeyEvent;
+import android.view.View;
+import android.view.inputmethod.EditorInfo;
+import android.widget.EditText;
+import android.widget.TextView;
+import android.widget.Toast;
-public class UrlInputOpenerPreference extends Preference implements UrlInputDialog.UrlReadyCallback
+import org.apache.commons.validator.routines.DomainValidator;
+import org.mozilla.gecko.R;
+
+public class InputValidatorDialogPreference extends EditTextPreference implements TextWatcher,
+ TextView.OnEditorActionListener
{
- private static WeakReference<UrlInputDialog.UrlReadyCallback> redirectUrlReadyCallback = null;
+
+ public enum ValidationType
+ {
+ URL, DOMAIN
+ }
- public UrlInputOpenerPreference(Context context)
+ private ValidationType validationType;
+ private String errorMessage;
+ private OnInputReadyListener onInputReadyListener;
+ private AlertDialog alertDialog;
+
+ public InputValidatorDialogPreference(Context context)
{
super(context);
+ this.init();
+ }
+
+ public InputValidatorDialogPreference(Context context, AttributeSet attrs)
+ {
+ super(context, attrs);
+ this.init();
}
- public UrlInputOpenerPreference(Context context, AttributeSet attrs)
+ public InputValidatorDialogPreference(Context context, AttributeSet attrs, int defStyleAttr)
+ {
+ super(context, attrs, defStyleAttr);
+ this.init();
+ }
+
+ private void init()
{
- super(context, attrs);
+ // Setting defaults
+ this.setIcon(android.R.drawable.ic_menu_add);
+ this.setPositiveButtonText(android.R.string.ok);
+ this.setNegativeButtonText(android.R.string.cancel);
+ this.setValidationType(ValidationType.URL);
+ this.setErrorMessage(R.string.abb_invalid_url);
+ final EditText editText = this.getEditText();
+ editText.addTextChangedListener(this);
+ editText.setOnEditorActionListener(this);
+ editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
}
- public UrlInputOpenerPreference(Context context, AttributeSet attrs, int defStyleAttr)
+ @Override
+ protected void showDialog(Bundle state)
{
- super(context, attrs, defStyleAttr);
+ super.showDialog(state);
+
+ this.alertDialog = (AlertDialog) this.getDialog();
+ // Positive button is disabled until a valid input is entered
+ this.setPositiveButtonEnabled(false);
}
- protected static synchronized void setRedirectUrlReadyCallback(
- final UrlInputDialog.UrlReadyCallback callback)
+ @Override
+ protected void onDialogClosed(boolean positiveResult)
{
- redirectUrlReadyCallback = new WeakReference<UrlInputDialog.UrlReadyCallback>(callback);
+ super.onDialogClosed(positiveResult);
+
+ this.alertDialog = null;
+ if (positiveResult && this.onInputReadyListener != null)
+ {
+ this.onInputReadyListener.onInputReady(this.getInput());
+ }
+ }
+
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after)
+ {
+ // Ignored
}
@Override
- protected void onAttachedToActivity()
+ public void onTextChanged(CharSequence s, int start, int before, int count)
{
- this.setPersistent(false);
+ // Ignored
+ }
+
+ @Override
+ public void afterTextChanged(Editable s)
+ {
+ this.setPositiveButtonEnabled(this.isValidInput());
}
@Override
- protected void onClick()
+ public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
- new UrlInputDialog(this.getContext(), UrlInputDialog.Type.ADD_SUBSCRIPTION, this).show();
+ if (actionId == EditorInfo.IME_ACTION_DONE)
+ {
+ if (this.isValidInput())
+ {
+ this.alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
+ }
+ else
+ {
+ Toast.makeText(this.getContext(), this.errorMessage, Toast.LENGTH_SHORT).show();
+ }
+ return true;
+ }
+ return false;
}
@Override
- public void callback(String url)
+ protected void onBindView(View view)
{
- try
+ super.onBindView(view);
+ final TextView title = (TextView) view.findViewById(android.R.id.title);
+ if (title != null)
{
- if (redirectUrlReadyCallback != null)
- {
- final UrlInputDialog.UrlReadyCallback callback = redirectUrlReadyCallback.get();
- if (callback != null)
- {
- callback.callback(url);
- }
- }
- }
- catch (Throwable t)
- {
- // we do not care
+ title.setSingleLine(false);
+ title.setEllipsize(null);
}
}
+
+ public void setValidationType(ValidationType validationType)
+ {
+ this.validationType = validationType;
+ }
+
+ public void setErrorMessage(int msgResId)
+ {
+ this.setErrorMessage(this.getContext().getString(msgResId));
+ }
+
+ public void setErrorMessage(String message)
+ {
+ this.errorMessage = message;
+ }
+
+ public void setOnInputReadyListener(OnInputReadyListener listener)
+ {
+ this.onInputReadyListener = listener;
+ }
+
+ private boolean isValidInput()
+ {
+ switch (this.validationType)
+ {
+ case DOMAIN:
+ return this.isValidDomain();
+ default:
+ return this.isValidUrl();
+ }
+ }
+
+ private boolean isValidDomain()
+ {
+ return DomainValidator.getInstance().isValid(this.getInput());
+ }
+
+ private boolean isValidUrl()
+ {
+ return Patterns.WEB_URL.matcher(this.getInput()).matches();
+ }
+
+ private String getInput()
+ {
+ return this.getEditText().getText().toString();
+ }
+
+ private void setPositiveButtonEnabled(boolean enabled)
+ {
+ if (this.alertDialog != null)
+ {
+ this.alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled);
+ }
+ }
+
+ public interface OnInputReadyListener
+ {
+ void onInputReady(String input);
+ }
}

Powered by Google App Engine
This is Rietveld