• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 package com.android.mail;
17 
18 import android.text.Html;
19 import android.text.util.Rfc822Token;
20 import android.text.util.Rfc822Tokenizer;
21 import com.android.mail.utils.LogTag;
22 import com.android.mail.utils.LogUtils;
23 
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 
27 /**
28  * Class representing a single email address.  Thread-safe, immutable value class suitable for
29  * caching.
30  * TODO(pwestbro): move to provider
31  */
32 public class EmailAddress {
33     public static final String LOG_TAG = LogTag.getLogTag();
34 
35     private final String mName;
36 
37     private final String mAddress;
38 
39     private static final Matcher sEmailMatcher =
40             Pattern.compile("\\\"?([^\"<]*?)\\\"?\\s*<(.*)>").matcher("");
41 
EmailAddress(String name, String address)42     private EmailAddress(String name, String address) {
43         mName = name;
44         mAddress = address;
45     }
46 
getName()47     public String getName() {
48         return mName;
49     }
50 
51     /**
52      * @return either the parsed out e-mail address, or the full raw address if it is not in
53      *     an expected format. This is not guaranteed to be HTML safe.
54      */
getAddress()55     public String getAddress() {
56         return mAddress;
57     }
58 
59     // TODO (pwestbro): move to provider
getEmailAddress(String rawAddress)60     public static synchronized EmailAddress getEmailAddress(String rawAddress) {
61         String name, address;
62         if (rawAddress == null) {
63             LogUtils.e(LOG_TAG, "null rawAddress in EmailAddress#getEmailAddress");
64             rawAddress = "";
65         }
66         Matcher m = sEmailMatcher.reset(rawAddress);
67         if (m.matches()) {
68             name = m.group(1);
69             address = m.group(2);
70             if (name == null) {
71                 name = "";
72             } else {
73                 name = Html.fromHtml(name.trim()).toString();
74             }
75             if (address == null) {
76                 address = "";
77             } else {
78                 address = Html.fromHtml(address).toString();
79             }
80         } else {
81             // Try and tokenize the string
82             final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(rawAddress);
83             if (tokens.length > 0) {
84                 final String tokenizedName = tokens[0].getName();
85                 name = tokenizedName != null ? Html.fromHtml(tokenizedName.trim()).toString() : "";
86                 address = Html.fromHtml(tokens[0].getAddress()).toString();
87             } else {
88                 name = "";
89                 address = Html.fromHtml(rawAddress).toString();
90             }
91         }
92         return new EmailAddress(name, address);
93     }
94 }