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