aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/prefs/EditTextPreferenceWithValidation.java
blob: 1ddb8ac80b670097229ff61e280ce18c0817070f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.wordpress.android.ui.prefs;

import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;

import org.wordpress.android.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditTextPreferenceWithValidation extends SummaryEditTextPreference {
    private ValidationType mValidationType = ValidationType.NONE;

    public EditTextPreferenceWithValidation(Context context) {
        super(context);
    }

    public EditTextPreferenceWithValidation(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public EditTextPreferenceWithValidation(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        final AlertDialog dialog = (AlertDialog) getDialog();
        Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        if (positiveButton != null) {
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String error = null;
                    CharSequence text = getEditText().getText();
                    if (mValidationType == ValidationType.EMAIL) {
                        error = validateEmail(text);
                    } else if (!TextUtils.isEmpty(text) && mValidationType == ValidationType.URL) {
                        error = validateUrl(text);
                    }

                    if (error != null) {
                        getEditText().setError(error);
                    } else {
                        callChangeListener(text);
                        dialog.dismiss();
                    }
                }
            });
        }

        CharSequence summary = getSummary();
        if (TextUtils.isEmpty(summary)) {
            getEditText().setText("");
        } else {
            getEditText().setText(summary);
            getEditText().setSelection(0, summary.length());
        }

        // clear previous errors
        getEditText().setError(null);
    }

    private String validateEmail(CharSequence text) {
        final Pattern emailRegExPattern = Patterns.EMAIL_ADDRESS;
        Matcher matcher = emailRegExPattern.matcher(text);
        if (!matcher.matches()) {
            return getContext().getString(R.string.invalid_email_message);
        }
        return null;
    }

    private String validateUrl(CharSequence text) {
        final Pattern urlRegExPattern = Patterns.WEB_URL;
        Matcher matcher = urlRegExPattern.matcher(text);
        if (!matcher.matches()) {
            return getContext().getString(R.string.invalid_url_message);
        }
        return null;
    }

    public void setValidationType(ValidationType validationType) {
        mValidationType = validationType;
    }

    public enum ValidationType {
        NONE, EMAIL, URL
    }
}