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