1 /* 2 * Copyright (C) 2008 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.common; 18 19 import android.text.InputFilter; 20 import android.text.Spanned; 21 import android.text.SpannableStringBuilder; 22 23 /** 24 * Implements special address cleanup rules: 25 * The first space key entry following an "@" symbol that is followed by any combination 26 * of letters and symbols, including one+ dots and zero commas, should insert an extra 27 * comma (followed by the space). 28 * 29 * @hide 30 */ 31 public class Rfc822InputFilter implements InputFilter { 32 33 public CharSequence filter(CharSequence source, int start, int end, Spanned dest, 34 int dstart, int dend) { 35 36 // quick check - did they enter a single space? 37 if (end-start != 1 || source.charAt(start) != ' ') { 38 return null; 39 } 40 41 // determine if the characters before the new space fit the pattern 42 // follow backwards and see if we find a comma, dot, or @ 43 int scanBack = dstart; 44 boolean dotFound = false; 45 while (scanBack > 0) { 46 char c = dest.charAt(--scanBack); 47 switch (c) { 48 case '.': 49 dotFound = true; // one or more dots are req'd 50 break; 51 case ',': 52 return null; 53 case '@': 54 if (!dotFound) { 55 return null; 56 } 57 // we have found a comma-insert case. now just do it 58 // in the least expensive way we can. 59 if (source instanceof Spanned) { 60 SpannableStringBuilder sb = new SpannableStringBuilder(","); 61 sb.append(source); 62 return sb; 63 } else { 64 return ", "; 65 } 66 default: 67 // just keep going 68 } 69 } 70 71 // no termination cases were found, so don't edit the input 72 return null; 73 } 74 } 75