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 17 package com.android.email.activity; 18 19 import android.widget.AutoCompleteTextView.Validator; 20 import android.widget.MultiAutoCompleteTextView; 21 import android.view.KeyEvent; 22 import android.content.Context; 23 import android.util.AttributeSet; 24 import android.graphics.drawable.Drawable; 25 import android.graphics.Rect; 26 import 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 */ 32 class AddressTextView extends MultiAutoCompleteTextView { 33 private class ForwardValidator implements Validator { 34 private Validator mValidator = null; 35 fixText(CharSequence invalidText)36 public CharSequence fixText(CharSequence invalidText) { 37 mIsValid = false; 38 return invalidText; 39 } 40 isValid(CharSequence text)41 public boolean isValid(CharSequence text) { 42 return mValidator != null ? mValidator.isValid(text) : true; 43 } 44 setValidator(Validator validator)45 public void setValidator(Validator validator) { 46 mValidator = validator; 47 } 48 } 49 50 private boolean mIsValid = true; 51 private final ForwardValidator mInternalValidator = new ForwardValidator(); 52 AddressTextView(Context context, AttributeSet attrs)53 public AddressTextView(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 super.setValidator(mInternalValidator); 56 } 57 58 @Override setValidator(Validator validator)59 public void setValidator(Validator validator) { 60 mInternalValidator.setValidator(validator); 61 } 62 63 @Override performValidation()64 public void performValidation() { 65 mIsValid = true; 66 super.performValidation(); 67 markError(!mIsValid); 68 } 69 markError(boolean enable)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