android.text.util.Rfc822Tokenizer Java Examples

The following examples show how to use android.text.util.Rfc822Tokenizer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecipientEditTextView.java    From talk-android with MIT License 6 votes vote down vote up
String createAddressText(RecipientEntry entry) {
    String display = entry.getDisplayName();
    String address = entry.getDestination();
    if (TextUtils.isEmpty(display) || TextUtils.equals(display, address)) {
        display = null;
    }
    String trimmedDisplayText;
    if (isPhoneQuery() && isPhoneNumber(address)) {
        trimmedDisplayText = address.trim();
    } else {
        if (address != null) {
            // Tokenize out the address in case the address already
            // contained the username as well.
            Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(address);
            if (tokenized != null && tokenized.length > 0) {
                address = tokenized[0].getAddress();
            }
        }
        Rfc822Token token = new Rfc822Token(display, address, null);
        trimmedDisplayText = token.toString().trim();
    }
    int index = trimmedDisplayText.indexOf(",");
    return mTokenizer != null && !TextUtils.isEmpty(trimmedDisplayText)
            && index < trimmedDisplayText.length() - 1 ? (String) mTokenizer
            .terminateToken(trimmedDisplayText) : trimmedDisplayText;
}
 
Example #2
Source File: RecipientEditTextView.java    From ChipsLibrary with Apache License 2.0 6 votes vote down vote up
String createAddressText(final RecipientEntry entry)
{
String display=entry.getDisplayName();
String address=entry.getDestination();
if(TextUtils.isEmpty(display)||TextUtils.equals(display,address))
  display=null;
String trimmedDisplayText;
if(isPhoneQuery()&&isPhoneNumber(address))
  trimmedDisplayText=address.trim();
else
  {
  if(address!=null)
    {
    // Tokenize out the address in case the address already
    // contained the username as well.
    final Rfc822Token[] tokenized=Rfc822Tokenizer.tokenize(address);
    if(tokenized!=null&&tokenized.length>0)
      address=tokenized[0].getAddress();
    }
  final Rfc822Token token=new Rfc822Token(display,address,null);
  trimmedDisplayText=token.toString().trim();
  }
final int index=trimmedDisplayText.indexOf(",");
return mTokenizer!=null&&!TextUtils.isEmpty(trimmedDisplayText)&&index<trimmedDisplayText.length()-1 ? (String)mTokenizer.terminateToken(trimmedDisplayText) : trimmedDisplayText;
}
 
Example #3
Source File: TextChipsEditView.java    From talk-android with MIT License 6 votes vote down vote up
String createAddressText(RecipientEntry entry) {
    String display = entry.getDisplayName();
    String address = entry.getDestination();
    if (TextUtils.isEmpty(display) || TextUtils.equals(display, address)) {
        display = null;
    }
    String trimmedDisplayText;
    if (isPhoneQuery() && isPhoneNumber(address)) {
        trimmedDisplayText = address.trim();
    } else {
        if (address != null) {
            // Tokenize out the address in case the address already
            // contained the username as well.
            Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(address);
            if (tokenized != null && tokenized.length > 0) {
                address = tokenized[0].getAddress();
            }
        }
        Rfc822Token token = new Rfc822Token(display, address, null);
        trimmedDisplayText = token.toString().trim();
    }
    int index = trimmedDisplayText.indexOf(",");
    return mTokenizer != null && !TextUtils.isEmpty(trimmedDisplayText)
            && index < trimmedDisplayText.length() - 1 ? (String) mTokenizer
            .terminateToken(trimmedDisplayText) : trimmedDisplayText;
}
 
Example #4
Source File: TextUtilsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@SmallTest
public void testRfc822TokenizerFullAddress() {
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("Foo Bar (something) <[email protected]>");
    assertNotNull(tokens);
    assertEquals(1, tokens.length);
    assertEquals("[email protected]", tokens[0].getAddress());
    assertEquals("Foo Bar", tokens[0].getName());
    assertEquals("something",tokens[0].getComment());
}
 
Example #5
Source File: RecipientEntry.java    From ChipsLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a RecipientEntry from just an address that has been entered. This address has not been resolved to a
 * contact and therefore does not have a contact id or photo.
 */
public static RecipientEntry constructFakeEntry(final String address,final boolean isValid)
  {
  final Rfc822Token[] tokens=Rfc822Tokenizer.tokenize(address);
  final String tokenizedAddress=tokens.length>0 ? tokens[0].getAddress() : address;
  return new RecipientEntry(ENTRY_TYPE_PERSON,tokenizedAddress,tokenizedAddress,INVALID_DESTINATION_TYPE,null,INVALID_CONTACT,INVALID_CONTACT,null,true,isValid,false /* isGalContact */);
  }
 
Example #6
Source File: SingleRecipientArrayAdapter.java    From ChipsLibrary with Apache License 2.0 5 votes vote down vote up
private static void bindView(View view,RecipientEntry entry)
{
TextView display=(TextView)view.findViewById(android.R.id.title);
ImageView imageView=(ImageView)view.findViewById(android.R.id.icon);
display.setText(entry.getDisplayName());
display.setVisibility(View.VISIBLE);
imageView.setVisibility(View.VISIBLE);
TextView destination=(TextView)view.findViewById(android.R.id.text1);
destination.setText(Rfc822Tokenizer.tokenize(entry.getDestination())[0].getAddress());
}
 
Example #7
Source File: RecipientEditTextView.java    From ChipsLibrary with Apache License 2.0 5 votes vote down vote up
private static String tokenizeAddress(final String destination)
{
final Rfc822Token[] tokens=Rfc822Tokenizer.tokenize(destination);
if(tokens!=null&&tokens.length>0)
  return tokens[0].getAddress();
return destination;
}
 
Example #8
Source File: TextUtilsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@SmallTest
public void testRfc822FindToken() {
    Rfc822Tokenizer tokenizer = new Rfc822Tokenizer();
    //                0           1         2           3         4
    //                0 1234 56789012345678901234 5678 90123456789012345
    String address = "\"Foo\" <[email protected]>, \"Bar\" <[email protected]>";
    assertEquals(0, tokenizer.findTokenStart(address, 21));
    assertEquals(22, tokenizer.findTokenEnd(address, 21));
    assertEquals(24, tokenizer.findTokenStart(address, 25));
    assertEquals(46, tokenizer.findTokenEnd(address, 25));
}
 
Example #9
Source File: TextUtilsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@SmallTest
public void testRfc822TokenizeItemWithError() {
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("\"Foo Bar\\");
    assertNotNull(tokens);
    assertEquals(1, tokens.length);
    assertEquals("Foo Bar", tokens[0].getAddress());
}
 
Example #10
Source File: RecipientEntry.java    From talk-android with MIT License 5 votes vote down vote up
/**
 * Construct a RecipientEntry from just an address that has been entered.
 * This address has not been resolved to a contact and therefore does not
 * have a contact id or photo.
 */
public static RecipientEntry constructFakeEntry(final String address, final boolean isValid) {
    final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address);
    final String tokenizedAddress = tokens.length > 0 ? tokens[0].getAddress() : address;

    return new RecipientEntry(ENTRY_TYPE_PERSON, tokenizedAddress, tokenizedAddress,
            INVALID_DESTINATION_TYPE, null, INVALID_CONTACT, null /* directoryId */,
            INVALID_CONTACT, null, true, isValid, null /* lookupKey */);
}
 
Example #11
Source File: TextChipsEditView.java    From talk-android with MIT License 5 votes vote down vote up
private static String tokenizeAddress(String destination) {
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(destination);
    if (tokens != null && tokens.length > 0) {
        return tokens[0].getAddress();
    }
    return destination;
}
 
Example #12
Source File: RecipientEditTextView.java    From talk-android with MIT License 5 votes vote down vote up
private static String tokenizeAddress(String destination) {
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(destination);
    if (tokens != null && tokens.length > 0) {
        return tokens[0].getAddress();
    }
    return destination;
}
 
Example #13
Source File: RecipientEditTextView.java    From talk-android with MIT License 4 votes vote down vote up
RecipientEntry createTokenizedEntry(final String token) {
    if (TextUtils.isEmpty(token)) {
        return null;
    }
    if (isPhoneQuery() && isPhoneNumber(token)) {
        return RecipientEntry.constructFakePhoneEntry(token, true);
    }
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(token);
    String display = null;
    boolean isValid = isValid(token);
    if (isValid && tokens != null && tokens.length > 0) {
        // If we can get a name from tokenizing, then generate an entry from
        // this.
        display = tokens[0].getName();
        if (!TextUtils.isEmpty(display)) {
            return RecipientEntry.constructGeneratedEntry(display, tokens[0].getAddress(),
                    isValid);
        } else {
            display = tokens[0].getAddress();
            if (!TextUtils.isEmpty(display)) {
                return RecipientEntry.constructFakeEntry(display, isValid);
            }
        }
    }
    // Unable to validate the token or to create a valid token from it.
    // Just create a chip the user can edit.
    String validatedToken = null;
    if (mValidator != null && !isValid) {
        // Try fixing up the entry using the validator.
        validatedToken = mValidator.fixText(token).toString();
        if (!TextUtils.isEmpty(validatedToken)) {
            if (validatedToken.contains(token)) {
                // protect against the case of a validator with a null
                // domain,
                // which doesn't add a domain to the token
                Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(validatedToken);
                if (tokenized.length > 0) {
                    validatedToken = tokenized[0].getAddress();
                    isValid = true;
                }
            } else {
                // We ran into a case where the token was invalid and
                // removed
                // by the validator. In this case, just use the original
                // token
                // and let the user sort out the error chip.
                validatedToken = null;
                isValid = false;
            }
        }
    }
    // Otherwise, fallback to just creating an editable email address chip.
    return RecipientEntry.constructFakeEntry(
            !TextUtils.isEmpty(validatedToken) ? validatedToken : token, isValid);
}
 
Example #14
Source File: DropdownChipLayouter.java    From talk-android with MIT License 4 votes vote down vote up
/**
 * Layouts and binds recipient information to the view. If convertView is null, inflates a new
 * view with getItemLaytout().
 *
 * @param convertView The view to bind information to.
 * @param parent The parent to bind the view to if we inflate a new view.
 * @param entry The recipient entry to get information from.
 * @param position The position in the list.
 * @param type The adapter type that is requesting the bind.
 * @param constraint The constraint typed in the auto complete view.
 *
 * @return A view ready to be shown in the drop down list.
 */
public View bindView(View convertView, ViewGroup parent, RecipientEntry entry, int position,
    AdapterType type, String constraint) {
    // Default to show all the information
    String displayName = entry.getDisplayName();
    String destination = entry.getDestination();
    boolean showImage = true;
    CharSequence destinationType = getDestinationType(entry);

    final View itemView = reuseOrInflateView(convertView, parent, type);

    final ViewHolder viewHolder = new ViewHolder(itemView);

    // Hide some information depending on the entry type and adapter type
    switch (type) {
        case BASE_RECIPIENT:
            if (TextUtils.isEmpty(displayName) || TextUtils.equals(displayName, destination)) {
                displayName = destination;

                // We only show the destination for secondary entries, so clear it only for the
                // first level.
                if (entry.isFirstLevel()) {
                    destination = null;
                }
            }

            if (!entry.isFirstLevel()) {
                displayName = null;
                showImage = false;
            }
            break;
        case RECIPIENT_ALTERNATES:
            if (position != 0) {
                displayName = null;
                showImage = false;
            }
            break;
        case SINGLE_RECIPIENT:
            destination = Rfc822Tokenizer.tokenize(entry.getDestination())[0].getAddress();
            destinationType = null;
    }

    if (displayName == null && !showImage) {
        viewHolder.destinationView.setPadding(mContext.getResources().getDimensionPixelSize(R.dimen.padding_no_picture), 0, 0, 0);
    } else {
        viewHolder.destinationView.setPadding(0, 0, 0, 0);
    }

    // Bind the information to the view
    bindTextToView(displayName, viewHolder.displayNameView);
    bindTextToView(destination, viewHolder.destinationView);
    bindTextToView("(" + destinationType + ")", viewHolder.destinationTypeView);
    bindIconToView(showImage, entry, viewHolder.imageView, type);

    return itemView;
}
 
Example #15
Source File: TextUtilsTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@SmallTest
public void testRfc822FindTokenWithError() {
    assertEquals(9, new Rfc822Tokenizer().findTokenEnd("\"Foo Bar\\", 0));
}
 
Example #16
Source File: RecipientEditTextView.java    From ChipsLibrary with Apache License 2.0 4 votes vote down vote up
RecipientEntry createTokenizedEntry(final String token)
{
if(TextUtils.isEmpty(token))
  return null;
if(isPhoneQuery()&&isPhoneNumber(token))
  return RecipientEntry.constructFakePhoneEntry(token,true);
final Rfc822Token[] tokens=Rfc822Tokenizer.tokenize(token);
String display=null;
boolean isValid=isValid(token);
if(isValid&&tokens!=null&&tokens.length>0)
  {
  // If we can get a name from tokenizing, then generate an entry from
  // this.
  display=tokens[0].getName();
  if(!TextUtils.isEmpty(display))
    return RecipientEntry.constructGeneratedEntry(display,tokens[0].getAddress(),isValid);
  else
    {
    display=tokens[0].getAddress();
    if(!TextUtils.isEmpty(display))
      return RecipientEntry.constructFakeEntry(display,isValid);
    }
  }
// Unable to validate the token or to create a valid token from it.
// Just create a chip the user can edit.
String validatedToken=null;
if(mValidator!=null&&!isValid)
  {
  // Try fixing up the entry using the validator.
  validatedToken=mValidator.fixText(token).toString();
  if(!TextUtils.isEmpty(validatedToken))
    if(validatedToken.contains(token))
      {
      // protect against the case of a validator with a null
      // domain,
      // which doesn't add a domain to the token
      final Rfc822Token[] tokenized=Rfc822Tokenizer.tokenize(validatedToken);
      if(tokenized.length>0)
        {
        validatedToken=tokenized[0].getAddress();
        isValid=true;
        }
      }
    else
      {
      // We ran into a case where the token was invalid and
      // removed
      // by the validator. In this case, just use the original
      // token
      // and let the user sort out the error chip.
      validatedToken=null;
      isValid=false;
      }
  }
// Otherwise, fallback to just creating an editable email address chip.
return RecipientEntry.constructFakeEntry(!TextUtils.isEmpty(validatedToken) ? validatedToken : token,isValid);
}
 
Example #17
Source File: TextChipsEditView.java    From talk-android with MIT License 4 votes vote down vote up
RecipientEntry createTokenizedEntry(final String token) {
    if (TextUtils.isEmpty(token)) {
        return null;
    }
    if (isPhoneQuery() && isPhoneNumber(token)) {
        return RecipientEntry.constructFakePhoneEntry(token, true);
    }
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(token);
    String display = null;
    boolean isValid = isValid(token);
    if (isValid && tokens != null && tokens.length > 0) {
        // If we can get a name from tokenizing, then generate an entry from
        // this.
        display = tokens[0].getName();
        if (!TextUtils.isEmpty(display)) {
            return RecipientEntry.constructGeneratedEntry(display, tokens[0].getAddress(),
                    isValid);
        } else {
            display = tokens[0].getAddress();
            if (!TextUtils.isEmpty(display)) {
                return RecipientEntry.constructFakeEntry(display, isValid);
            }
        }
    }
    // Unable to validate the token or to create a valid token from it.
    // Just create a chip the user can edit.
    String validatedToken = null;
    if (mValidator != null && !isValid) {
        // Try fixing up the entry using the validator.
        validatedToken = mValidator.fixText(token).toString();
        if (!TextUtils.isEmpty(validatedToken)) {
            if (validatedToken.contains(token)) {
                // protect against the case of a validator with a null
                // domain,
                // which doesn't add a domain to the token
                Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(validatedToken);
                if (tokenized.length > 0) {
                    validatedToken = tokenized[0].getAddress();
                    isValid = true;
                }
            } else {
                // We ran into a case where the token was invalid and
                // removed
                // by the validator. In this case, just use the original
                // token
                // and let the user sort out the error chip.
                validatedToken = null;
                isValid = false;
            }
        }
    }
    // Otherwise, fallback to just creating an editable email address chip.
    return RecipientEntry.constructFakeEntry(
            !TextUtils.isEmpty(validatedToken) ? validatedToken : token, isValid);
}