ReplyFromAccount.java revision cd970ddaba4e212b668588db2104dc46ad3ad793
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;
20
21import com.android.mail.utils.LogUtils;
22import com.android.mail.utils.Utils;
23
24import org.json.JSONException;
25import org.json.JSONObject;
26
27import java.io.Serializable;
28
29public class ReplyFromAccount implements Serializable {
30    private static final long serialVersionUID = 1L;
31
32    private static final String LOG_TAG = new LogUtils().getLogTag();
33    private static final String BASE_ACCOUNT_URI = "baseAccountUri";
34    private static final String ADDRESS_STRING = "address";
35    private static final String REPLY_TO = "replyTo";
36    private static final String NAME_STRING = "name";
37    private static final String IS_DEFAULT = "isDefault";
38    private static final String IS_CUSTOM_FROM = "isCustom";
39
40    public Account account;
41    Uri baseAccountUri;
42    public String address;
43    public String replyTo;
44    public String name;
45    public boolean isDefault;
46    public boolean isCustomFrom;
47
48    public ReplyFromAccount(Account account, Uri baseAccountUri, String address, String name,
49            String replyTo, boolean isDefault, boolean isCustom) {
50        this.account = account;
51        this.baseAccountUri = baseAccountUri;
52        this.address = address;
53        this.name = name;
54        this.replyTo = replyTo;
55        this.isDefault = isDefault;
56        this.isCustomFrom = isCustom;
57    }
58
59    public JSONObject serialize() {
60        JSONObject json = new JSONObject();
61        try {
62            json.put(BASE_ACCOUNT_URI, baseAccountUri);
63            json.put(ADDRESS_STRING, address);
64            json.put(NAME_STRING, name);
65            json.put(REPLY_TO, replyTo);
66            json.put(IS_DEFAULT, isDefault);
67            json.put(IS_CUSTOM_FROM, isCustomFrom);
68        } catch (JSONException e) {
69            LogUtils.wtf(LOG_TAG, e, "Could not serialize account with name " + name);
70        }
71        return json;
72    }
73
74    public static ReplyFromAccount deserialize(Account account, JSONObject json) {
75        ReplyFromAccount replyFromAccount = null;
76        try {
77            Uri uri = Utils.getValidUri(json.getString(BASE_ACCOUNT_URI));
78            String addressString = json.getString(ADDRESS_STRING);
79            String nameString = json.getString(NAME_STRING);
80            String replyTo = json.getString(REPLY_TO);
81            boolean isDefault = json.getBoolean(IS_DEFAULT);
82            boolean isCustomFrom = json.getBoolean(IS_CUSTOM_FROM);
83            replyFromAccount = new ReplyFromAccount(account, uri, addressString, nameString,
84                    replyTo, isDefault, isCustomFrom);
85        } catch (JSONException e) {
86            LogUtils.wtf(LOG_TAG, e, "Could not deserialize replyfromaccount");
87        }
88        return replyFromAccount;
89    }
90
91    public static ReplyFromAccount deserialize(Account account, String stringExtra) {
92        ReplyFromAccount replyFromAccount = null;
93        try {
94            replyFromAccount =  deserialize(account, new JSONObject(stringExtra));
95        } catch (JSONException e) {
96            LogUtils.wtf(LOG_TAG, e, "Could not deserialize replyfromaccount");
97        }
98        return replyFromAccount;
99    }
100}
101