1/*
2 * Copyright (C) 2009 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;
20
21import android.content.Context;
22import android.util.AttributeSet;
23import android.widget.MultiAutoCompleteTextView;
24
25/**
26 * This is a MultiAutoCompleteTextView which sets the error state
27 * (@see TextView.setError) when email address validation fails.
28 */
29class AddressTextView extends MultiAutoCompleteTextView {
30    private class ForwardValidator implements Validator {
31        private Validator mValidator = null;
32
33        public CharSequence fixText(CharSequence invalidText) {
34            mIsValid = false;
35            return invalidText;
36        }
37
38        public boolean isValid(CharSequence text) {
39            return mValidator != null ? mValidator.isValid(text) : true;
40        }
41
42        public void setValidator(Validator validator) {
43            mValidator = validator;
44        }
45    }
46
47    private boolean mIsValid = true;
48    private final ForwardValidator mInternalValidator = new ForwardValidator();
49
50    public AddressTextView(Context context, AttributeSet attrs) {
51        super(context, attrs);
52        super.setValidator(mInternalValidator);
53    }
54
55    @Override
56    public void setValidator(Validator validator) {
57        mInternalValidator.setValidator(validator);
58    }
59
60    @Override
61    public void performValidation() {
62        mIsValid = true;
63        super.performValidation();
64        markError(!mIsValid);
65    }
66
67    private void markError(boolean enable) {
68        if (enable) {
69            setError(getContext().getString(R.string.message_compose_error_invalid_email));
70        } else {
71            setError(null);
72        }
73    }
74}
75