下面列出了怎么用android.text.util.Rfc822Tokenizer的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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;
}
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;
}
private static String tokenizeAddress(String destination) {
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(destination);
if (tokens != null && tokens.length > 0) {
return tokens[0].getAddress();
}
return destination;
}
private static String tokenizeAddress(String destination) {
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(destination);
if (tokens != null && tokens.length > 0) {
return tokens[0].getAddress();
}
return destination;
}
/**
* 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 */);
}
@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());
}
@SmallTest
public void testRfc822TokenizeItemWithError() {
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("\"Foo Bar\\");
assertNotNull(tokens);
assertEquals(1, tokens.length);
assertEquals("Foo Bar", tokens[0].getAddress());
}
@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));
}
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;
}
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());
}
/**
* 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 */);
}
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);
}
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);
}
/**
* 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;
}
@SmallTest
public void testRfc822FindTokenWithError() {
assertEquals(9, new Rfc822Tokenizer().findTokenEnd("\"Foo Bar\\", 0));
}
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);
}