Index: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/InputValidatorDialogPreference.java |
=================================================================== |
rename from adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/UrlInputOpenerPreference.java |
rename to adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/InputValidatorDialogPreference.java |
--- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/UrlInputOpenerPreference.java |
+++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/InputValidatorDialogPreference.java |
@@ -16,44 +16,56 @@ |
*/ |
package org.adblockplus.sbrowser.contentblocker; |
import android.app.AlertDialog; |
import android.content.Context; |
import android.os.Bundle; |
import android.preference.EditTextPreference; |
+import android.support.annotation.NonNull; |
import android.text.Editable; |
import android.text.InputType; |
import android.text.TextWatcher; |
import android.util.AttributeSet; |
+import android.util.Log; |
+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; |
import org.adblockplus.adblockplussbrowser.R; |
+import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
import org.adblockplus.sbrowser.contentblocker.util.PreferenceUtils; |
import org.apache.commons.validator.routines.DomainValidator; |
-public class UrlInputOpenerPreference extends EditTextPreference implements TextWatcher, |
+public class InputValidatorDialogPreference extends EditTextPreference implements TextWatcher, |
TextView.OnEditorActionListener |
{ |
- private OnUrlReadyListener onUrlReadyListener; |
- private AlertDialog mAlertDialog; |
+ public enum ValidationType |
+ { |
+ DOMAIN, |
+ URL |
+ } |
- public UrlInputOpenerPreference(Context context) |
+ private static final String TAG = InputValidatorDialogPreference.class.getSimpleName(); |
+ private OnInputReadyListener onInputReadyListener; |
+ private AlertDialog alertDialog; |
+ private ValidationType validationType; |
+ |
+ public InputValidatorDialogPreference(Context context) |
{ |
this(context, null); |
} |
- public UrlInputOpenerPreference(Context context, AttributeSet attrs) |
+ public InputValidatorDialogPreference(Context context, AttributeSet attrs) |
{ |
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); |
final EditText editText = getEditText(); |
@@ -62,30 +74,30 @@ public class UrlInputOpenerPreference ex |
editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); |
} |
@Override |
protected void showDialog(Bundle state) |
{ |
super.showDialog(state); |
- mAlertDialog = (AlertDialog) getDialog(); |
+ alertDialog = (AlertDialog) getDialog(); |
// Positive button is disabled until a valid URL is entered |
this.setPositiveButtonEnabled(false); |
} |
@Override |
protected void onDialogClosed(boolean positiveResult) |
{ |
super.onDialogClosed(positiveResult); |
- mAlertDialog = null; |
- if (positiveResult && this.onUrlReadyListener != null) |
+ alertDialog = null; |
+ if (positiveResult && this.onInputReadyListener != null) |
{ |
- this.onUrlReadyListener.onUrlReady(getUrl()); |
+ this.onInputReadyListener.onInputReady(getInput()); |
} |
} |
@Override |
public void beforeTextChanged(CharSequence s, int start, int count, int after) |
{ |
// Ignored |
} |
@@ -94,27 +106,27 @@ public class UrlInputOpenerPreference ex |
public void onTextChanged(CharSequence s, int start, int before, int count) |
{ |
// Ignored |
} |
@Override |
public void afterTextChanged(Editable s) |
{ |
- setPositiveButtonEnabled(isValidDomain()); |
+ setPositiveButtonEnabled(isValidInput()); |
} |
@Override |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) |
{ |
if (actionId == EditorInfo.IME_ACTION_DONE) |
{ |
- if (this.isValidDomain()) |
+ if (this.isValidInput()) |
{ |
- mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
+ alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
} |
else |
{ |
Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_error, Toast.LENGTH_SHORT).show(); |
} |
return true; |
} |
return false; |
@@ -122,36 +134,64 @@ public class UrlInputOpenerPreference ex |
@Override |
protected void onBindView(View view) |
{ |
super.onBindView(view); |
PreferenceUtils.setMultilineTitle(view); |
} |
- public void setOnUrlReadyListener(OnUrlReadyListener listener) |
+ public void setValidationType(@NonNull final ValidationType validationType) |
{ |
- this.onUrlReadyListener = listener; |
+ this.validationType = validationType; |
+ } |
+ |
+ public void setOnInputReadyListener(OnInputReadyListener listener) |
+ { |
+ this.onInputReadyListener = listener; |
} |
private boolean isValidDomain() |
{ |
- return DomainValidator.getInstance().isValid(getUrl()); |
+ return DomainValidator.getInstance().isValid(getInput()); |
+ } |
+ |
+ private boolean isValidUrl() |
+ { |
+ return Patterns.WEB_URL.matcher(getInput()).matches(); |
} |
- private String getUrl() |
+ private boolean isValidInput() |
+ { |
+ if (validationType == null) |
+ { |
+ Log.i(TAG, "ValidationType was not defined and is therefore set to" + |
+ " ValidationType.URL per default. Please consider to set in manually."); |
+ validationType = ValidationType.URL; |
+ } |
+ |
+ switch (validationType) |
+ { |
+ case DOMAIN: |
+ return isValidDomain(); |
+ default: |
+ return isValidUrl(); |
+ } |
+ } |
+ |
+ private String getInput() |
{ |
return getEditText().getText().toString(); |
} |
private void setPositiveButtonEnabled(boolean enabled) |
{ |
- if (mAlertDialog != null) |
+ if (alertDialog != null) |
{ |
- mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
+ alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
} |
} |
- public interface OnUrlReadyListener |
+ public interface OnInputReadyListener |
{ |
- void onUrlReady(String url); |
+ void onInputReady(String input); |
} |
-} |
+} |