ChipsAddressTextView.java revision 87763f0ae851c76d5e7cf672a6e3acf21705549c
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.activity;
18
19import com.android.email.R;
20import com.android.ex.chips.RecipientEditTextView;
21
22import android.content.Context;
23import android.util.AttributeSet;
24import android.widget.MultiAutoCompleteTextView;
25
26/**
27 * This is a MultiAutoCompleteTextView which sets the error state
28 * (@see TextView.setError) when email address validation fails.
29 */
30class ChipsAddressTextView extends RecipientEditTextView {
31    private class ForwardValidator implements Validator {
32        private Validator mValidator = null;
33
34        public CharSequence fixText(CharSequence invalidText) {
35            mIsValid = false;
36            return invalidText;
37        }
38
39        public boolean isValid(CharSequence text) {
40            return mValidator != null ? mValidator.isValid(text) : true;
41        }
42
43        public void setValidator(Validator validator) {
44            mValidator = validator;
45        }
46    }
47
48    private boolean mIsValid = true;
49    private final ForwardValidator mInternalValidator = new ForwardValidator();
50
51    public ChipsAddressTextView(Context context, AttributeSet attrs) {
52        super(context, attrs);
53        super.setValidator(mInternalValidator);
54    }
55
56    @Override
57    public void setValidator(Validator validator) {
58        mInternalValidator.setValidator(validator);
59    }
60
61    @Override
62    public void performValidation() {
63        mIsValid = true;
64        super.performValidation();
65        markError(!mIsValid);
66    }
67
68    private void markError(boolean enable) {
69        if (enable) {
70            setError(getContext().getString(R.string.message_compose_error_invalid_email));
71        } else {
72            setError(null);
73        }
74    }
75}
76