Home | History | Annotate | Download | only in providers
      1 /**
      2  * Copyright (c) 2012, Google Inc.
      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.mail.providers;
     18 
     19 import android.net.Uri;
     20 import android.text.TextUtils;
     21 import android.text.util.Rfc822Token;
     22 import android.text.util.Rfc822Tokenizer;
     23 
     24 import com.android.mail.utils.LogTag;
     25 import com.android.mail.utils.LogUtils;
     26 import com.android.mail.utils.Utils;
     27 
     28 import org.json.JSONException;
     29 import org.json.JSONObject;
     30 
     31 import java.io.Serializable;
     32 import java.util.List;
     33 
     34 public class ReplyFromAccount implements Serializable {
     35     private static final long serialVersionUID = 1L;
     36 
     37     private static final String LOG_TAG = LogTag.getLogTag();
     38     private static final String BASE_ACCOUNT_URI = "baseAccountUri";
     39     private static final String ADDRESS_STRING = "address";
     40     private static final String REPLY_TO = "replyTo";
     41     private static final String NAME_STRING = "name";
     42     private static final String IS_DEFAULT = "isDefault";
     43     private static final String IS_CUSTOM_FROM = "isCustom";
     44 
     45     public Account account;
     46     Uri baseAccountUri;
     47     public String address;
     48     public String replyTo;
     49     public String name;
     50     public boolean isDefault;
     51     public boolean isCustomFrom;
     52 
     53     public ReplyFromAccount(Account account, Uri baseAccountUri, String address, String name,
     54             String replyTo, boolean isDefault, boolean isCustom) {
     55         this.account = account;
     56         this.baseAccountUri = baseAccountUri;
     57         this.address = address;
     58         this.name = name;
     59         this.replyTo = replyTo;
     60         this.isDefault = isDefault;
     61         this.isCustomFrom = isCustom;
     62     }
     63 
     64     public JSONObject serialize() {
     65         JSONObject json = new JSONObject();
     66         try {
     67             json.put(BASE_ACCOUNT_URI, baseAccountUri);
     68             json.put(ADDRESS_STRING, address);
     69             json.put(NAME_STRING, name);
     70             json.put(REPLY_TO, replyTo);
     71             json.put(IS_DEFAULT, isDefault);
     72             json.put(IS_CUSTOM_FROM, isCustomFrom);
     73         } catch (JSONException e) {
     74             LogUtils.wtf(LOG_TAG, e, "Could not serialize account with address " + address);
     75         }
     76         return json;
     77     }
     78 
     79     public static ReplyFromAccount deserialize(Account account, JSONObject json) {
     80         ReplyFromAccount replyFromAccount = null;
     81         try {
     82             Uri uri = Utils.getValidUri(json.getString(BASE_ACCOUNT_URI));
     83             String addressString = json.getString(ADDRESS_STRING);
     84             String nameString = json.optString(NAME_STRING, null);
     85             String replyTo = json.getString(REPLY_TO);
     86             boolean isDefault = json.getBoolean(IS_DEFAULT);
     87             boolean isCustomFrom = json.getBoolean(IS_CUSTOM_FROM);
     88             replyFromAccount = new ReplyFromAccount(account, uri, addressString, nameString,
     89                     replyTo, isDefault, isCustomFrom);
     90         } catch (JSONException e) {
     91             LogUtils.wtf(LOG_TAG, e, "Could not deserialize replyfromaccount");
     92         }
     93         return replyFromAccount;
     94     }
     95 
     96     public static ReplyFromAccount deserialize(Account account, String stringExtra) {
     97         ReplyFromAccount replyFromAccount = null;
     98         try {
     99             replyFromAccount =  deserialize(account, new JSONObject(stringExtra));
    100         } catch (JSONException e) {
    101             LogUtils.wtf(LOG_TAG, e, "Could not deserialize replyfromaccount");
    102         }
    103         return replyFromAccount;
    104     }
    105 
    106     /**
    107      * Determine if this address is the account itself or a custom from for the
    108      * account.
    109      */
    110     public static boolean matchesAccountOrCustomFrom(Account account, String possibleCustomFrom,
    111             List<ReplyFromAccount> replyFromAccounts) {
    112         if (TextUtils.isEmpty(possibleCustomFrom)) {
    113             return false;
    114         }
    115         Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(possibleCustomFrom);
    116         if (tokens != null && tokens.length > 0) {
    117             String parsedFromAddress = Utils.normalizeEmailAddress(tokens[0].getAddress());
    118             if (TextUtils.equals(Utils.normalizeEmailAddress(account.getEmailAddress()),
    119                     parsedFromAddress)) {
    120                 return true;
    121             }
    122             for (ReplyFromAccount replyFromAccount : replyFromAccounts) {
    123                 if (TextUtils.equals(Utils.normalizeEmailAddress(replyFromAccount.address),
    124                         parsedFromAddress) && replyFromAccount.isCustomFrom) {
    125                     return true;
    126                 }
    127             }
    128         }
    129         return false;
    130     }
    131 }
    132