-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax URL validation (closes #16), do validation before closing prefe…
…rence dialog
- Loading branch information
1 parent
10b44ab
commit d4f3a47
Showing
6 changed files
with
182 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
app/src/main/java/net/fabiszewski/ulogger/TrimmedEditTextPreference.java
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
app/src/main/java/net/fabiszewski/ulogger/UrlEditTextPreference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2017 Bartek Fabiszewski | ||
* http://www.fabiszewski.net | ||
* | ||
* This file is part of μlogger-android. | ||
* Licensed under GPL, either version 3, or any later. | ||
* See <http://www.gnu.org/licenses/> | ||
*/ | ||
|
||
package net.fabiszewski.ulogger; | ||
|
||
import android.content.Context; | ||
import android.content.DialogInterface; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.preference.EditTextPreference; | ||
import android.support.annotation.RequiresApi; | ||
import android.app.AlertDialog; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
/** | ||
* URL edit text preference | ||
* Validates and trims URL | ||
*/ | ||
|
||
class UrlEditTextPreference extends EditTextPreference { | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
public UrlEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
} | ||
|
||
public UrlEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
public UrlEditTextPreference(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public UrlEditTextPreference(Context context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
public void setText(String text) { | ||
super.setText(text.trim()); | ||
} | ||
|
||
@Override | ||
protected void showDialog(Bundle state) { | ||
super.showDialog(state); | ||
getEditText().setError(null); | ||
final AlertDialog dialog = (AlertDialog) getDialog(); | ||
View positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); | ||
positiveButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
onPositiveButtonClicked(); | ||
} | ||
}); | ||
} | ||
|
||
private void onPositiveButtonClicked() { | ||
if (WebHelper.isValidURL(getEditText().getText().toString().trim())) { | ||
getEditText().setError(null); | ||
onClick(getDialog(), DialogInterface.BUTTON_POSITIVE); | ||
getDialog().dismiss(); | ||
} else { | ||
getEditText().setError(getContext().getString(R.string.provide_valid_url)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
app/src/main/java/net/fabiszewski/ulogger/WebPatterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2017 Bartek Fabiszewski | ||
* http://www.fabiszewski.net | ||
* | ||
* This file is part of μlogger-android. | ||
* Licensed under GPL, either version 3, or any later. | ||
* See <http://www.gnu.org/licenses/> | ||
*/ | ||
|
||
package net.fabiszewski.ulogger; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* This is based on java.android.util.Patterns | ||
* with WEB_URL pattern relaxed | ||
*/ | ||
|
||
class WebPatterns { | ||
|
||
/** | ||
* Protocols limited to http(s). | ||
*/ | ||
private static final String PROTOCOL = "(?i:https?)://"; | ||
|
||
private static final String USER_INFO = "(?:[-a-zA-Z0-9$_.+!*'(),;?&=]|(?:%[a-fA-F0-9]{2})){1,64}" | ||
+ "(?::(?:[-a-zA-Z0-9$_.+!*'(),;?&=]|(?:%[a-fA-F0-9]{2})){1,25})?@"; | ||
|
||
/** | ||
* Valid UCS characters defined in RFC 3987. Excludes space characters. | ||
*/ | ||
private static final String UCS_CHAR = "[" + | ||
"\u00A0-\uD7FF" + | ||
"\uF900-\uFDCF" + | ||
"\uFDF0-\uFFEF" + | ||
"\uD800\uDC00-\uD83F\uDFFD" + | ||
"\uD840\uDC00-\uD87F\uDFFD" + | ||
"\uD880\uDC00-\uD8BF\uDFFD" + | ||
"\uD8C0\uDC00-\uD8FF\uDFFD" + | ||
"\uD900\uDC00-\uD93F\uDFFD" + | ||
"\uD940\uDC00-\uD97F\uDFFD" + | ||
"\uD980\uDC00-\uD9BF\uDFFD" + | ||
"\uD9C0\uDC00-\uD9FF\uDFFD" + | ||
"\uDA00\uDC00-\uDA3F\uDFFD" + | ||
"\uDA40\uDC00-\uDA7F\uDFFD" + | ||
"\uDA80\uDC00-\uDABF\uDFFD" + | ||
"\uDAC0\uDC00-\uDAFF\uDFFD" + | ||
"\uDB00\uDC00-\uDB3F\uDFFD" + | ||
"\uDB44\uDC00-\uDB7F\uDFFD" + | ||
"&&[^\u00A0[\u2000-\u200A]\u2028\u2029\u202F\u3000]]"; | ||
|
||
/** | ||
* Valid characters for IRI label defined in RFC 3987. | ||
*/ | ||
private static final String LABEL_CHAR = "a-zA-Z0-9" + UCS_CHAR; | ||
|
||
/** | ||
* RFC 1035 Section 2.3.4 limits the labels to a maximum 63 octets. | ||
*/ | ||
private static final String IRI_LABEL = "[" + LABEL_CHAR + "]" | ||
+ "(?:[" + LABEL_CHAR + "\\-]{0,61}[" + LABEL_CHAR + "])?"; | ||
|
||
/** | ||
* Regular expression that matches domain names without a TLD, also IP addresses | ||
*/ | ||
private static final String RELAXED_DOMAIN_NAME = | ||
"(?:(?:" + IRI_LABEL + "(?:\\.(?=[" + LABEL_CHAR + "]))?)+)"; | ||
|
||
private static final String PORT_NUMBER = ":\\d{1,5}"; | ||
|
||
/** | ||
* A word boundary or end of input. This is to stop foo.sure from matching as foo.su | ||
*/ | ||
private static final String WORD_BOUNDARY = "(?:\\b|$|^)"; | ||
|
||
/** | ||
* Path segment, exclude repeated slashes to rule out common error (http//example.com) | ||
*/ | ||
private static final String PATH_SEGMENT = | ||
"/(?:(?:[" + LABEL_CHAR + ";:@&=~\\-.+!*'(),_])|(?:%[a-fA-F0-9]{2})|" + WORD_BOUNDARY + ")+"; | ||
|
||
/** | ||
* Regular expression pattern to match most part of RFC 3987 | ||
* Internationalized URLs, aka IRIs. | ||
* Relaxed to accept domains without a TLD. | ||
* Will not accept query part. | ||
* Only http and https protocols. | ||
*/ | ||
static final Pattern WEB_URL_RELAXED = Pattern.compile( | ||
"(?:" + PROTOCOL + "(?:" + USER_INFO + ")?" + ")?" | ||
+ RELAXED_DOMAIN_NAME | ||
+ "(?:" + PORT_NUMBER + ")?" | ||
+ "(?:" + PATH_SEGMENT + ")*" | ||
+ WORD_BOUNDARY); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters