1/*
2 * Copyright (C) 2010 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
17package com.android.email.activity.setup;
18
19import com.android.emailcommon.provider.Account;
20import com.android.emailcommon.provider.Policy;
21
22import android.accounts.AccountAuthenticatorResponse;
23import android.os.Bundle;
24import android.os.Parcel;
25import android.os.Parcelable;
26
27public class SetupData implements Parcelable {
28    // The "extra" name for the Bundle saved with SetupData
29    public static final String EXTRA_SETUP_DATA = "com.android.email.setupdata";
30
31    // NORMAL is the standard entry from the Email app; EAS and POP_IMAP are used when entering via
32    // Settings -> Accounts
33    public static final int FLOW_MODE_UNSPECIFIED = -1;
34    public static final int FLOW_MODE_NORMAL = 0;
35    public static final int FLOW_MODE_ACCOUNT_MANAGER_EAS = 1;
36    public static final int FLOW_MODE_ACCOUNT_MANAGER_POP_IMAP = 2;
37    public static final int FLOW_MODE_EDIT = 3;
38    public static final int FLOW_MODE_FORCE_CREATE = 4;
39    // The following two modes are used to "pop the stack" and return from the setup flow.  We
40    // either return to the caller (if we're in an account type flow) or go to the message list
41    public static final int FLOW_MODE_RETURN_TO_CALLER = 5;
42    public static final int FLOW_MODE_RETURN_TO_MESSAGE_LIST = 6;
43
44    // For debug logging
45    private static final String[] FLOW_MODES = {"normal", "eas", "pop/imap", "edit", "force",
46            "rtc", "rtl"};
47
48    // Mode bits for AccountSetupCheckSettings, indicating the type of check requested
49    public static final int CHECK_INCOMING = 1;
50    public static final int CHECK_OUTGOING = 2;
51    public static final int CHECK_AUTODISCOVER = 4;
52
53    // All access will be through getters/setters
54    private int mFlowMode = FLOW_MODE_NORMAL;
55    private Account mAccount;
56    private String mUsername;
57    private String mPassword;
58    private int mCheckSettingsMode = 0;
59    private boolean mAllowAutodiscover = true;
60    private Policy mPolicy;
61    private boolean mAutoSetup = false;
62    private boolean mDefault = false;
63    private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
64
65    // We only have one instance of SetupData; if/when the process is destroyed, this data will be
66    // saved in the savedInstanceState Bundle
67    private static SetupData INSTANCE = null;
68
69    public static synchronized SetupData getInstance() {
70        if (INSTANCE == null) {
71            INSTANCE = new SetupData();
72        }
73        return INSTANCE;
74    }
75
76    // Don't allow instantiation outside of this class
77    private SetupData() {
78    }
79
80    static public int getFlowMode() {
81        return getInstance().mFlowMode;
82    }
83
84    static public void setFlowMode(int mFlowMode) {
85        getInstance().mFlowMode = mFlowMode;
86    }
87
88    static public Account getAccount() {
89        return getInstance().mAccount;
90    }
91
92    static public void setAccount(Account mAccount) {
93        getInstance().mAccount = mAccount;
94    }
95
96    static public String getUsername() {
97        return getInstance().mUsername;
98    }
99
100    static public void setUsername(String mUsername) {
101        getInstance().mUsername = mUsername;
102    }
103
104    static public String getPassword() {
105        return getInstance().mPassword;
106    }
107
108    static public void setPassword(String mPassword) {
109        getInstance().mPassword = mPassword;
110    }
111
112    static public void setCheckSettingsMode(int mCheckSettingsMode) {
113        getInstance().mCheckSettingsMode = mCheckSettingsMode;
114    }
115
116    static public boolean isCheckIncoming() {
117        return (getInstance().mCheckSettingsMode & CHECK_INCOMING) != 0;
118    }
119
120    static public boolean isCheckOutgoing() {
121        return (getInstance().mCheckSettingsMode & CHECK_OUTGOING) != 0;
122    }
123    static public boolean isCheckAutodiscover() {
124        return (getInstance().mCheckSettingsMode & CHECK_AUTODISCOVER) != 0;
125    }
126    static public boolean isAllowAutodiscover() {
127        return getInstance().mAllowAutodiscover;
128    }
129
130    static public void setAllowAutodiscover(boolean mAllowAutodiscover) {
131        getInstance().mAllowAutodiscover = mAllowAutodiscover;
132    }
133
134    static public Policy getPolicy() {
135        return getInstance().mPolicy;
136    }
137
138    static public void setPolicy(Policy policy) {
139        SetupData data = getInstance();
140        data.mPolicy = policy;
141        data.mAccount.mPolicy = policy;
142    }
143
144    static public boolean isAutoSetup() {
145        return getInstance().mAutoSetup;
146    }
147
148    static public void setAutoSetup(boolean autoSetup) {
149        getInstance().mAutoSetup = autoSetup;
150    }
151
152    static public boolean isDefault() {
153        return getInstance().mDefault;
154    }
155
156    static public void setDefault(boolean _default) {
157        getInstance().mDefault = _default;
158    }
159
160    static public AccountAuthenticatorResponse getAccountAuthenticatorResponse() {
161        return getInstance().mAccountAuthenticatorResponse;
162    }
163
164    static public void setAccountAuthenticatorResponse(AccountAuthenticatorResponse response) {
165        getInstance().mAccountAuthenticatorResponse = response;
166    }
167
168    public static void init(int flowMode) {
169        SetupData data = getInstance();
170        data.commonInit();
171        data.mFlowMode = flowMode;
172    }
173
174    public static void init(int flowMode, Account account) {
175        SetupData data = getInstance();
176        data.commonInit();
177        data.mFlowMode = flowMode;
178        data.mAccount = account;
179    }
180
181    void commonInit() {
182        mPolicy = null;
183        mAutoSetup = false;
184        mAllowAutodiscover = true;
185        mCheckSettingsMode = 0;
186        mAccount = new Account();
187        mDefault = false;
188        mUsername = null;
189        mPassword = null;
190        mAccountAuthenticatorResponse = null;
191    }
192
193    // Parcelable methods
194    public int describeContents() {
195        return 0;
196    }
197
198    public static final Parcelable.Creator<SetupData> CREATOR =
199            new Parcelable.Creator<SetupData>() {
200        public SetupData createFromParcel(Parcel in) {
201            return new SetupData(in);
202        }
203
204        public SetupData[] newArray(int size) {
205            return new SetupData[size];
206        }
207    };
208
209    public void writeToParcel(Parcel dest, int flags) {
210        dest.writeInt(mFlowMode);
211        dest.writeParcelable(mAccount, 0);
212        dest.writeString(mUsername);
213        dest.writeString(mPassword);
214        dest.writeInt(mCheckSettingsMode);
215        dest.writeInt(mAllowAutodiscover ? 1 : 0);
216        dest.writeParcelable(mPolicy, 0);
217        dest.writeInt(mAutoSetup ? 1 : 0);
218        dest.writeInt(mDefault ? 1 : 0);
219        dest.writeParcelable(mAccountAuthenticatorResponse, 0);
220    }
221
222    public SetupData(Parcel in) {
223        ClassLoader loader = getClass().getClassLoader();
224        mFlowMode = in.readInt();
225        mAccount = in.readParcelable(loader);
226        mUsername = in.readString();
227        mPassword = in.readString();
228        mCheckSettingsMode = in.readInt();
229        mAllowAutodiscover = in.readInt() == 1;
230        mPolicy = in.readParcelable(loader);
231        mAutoSetup = in.readInt() == 1;
232        mDefault = in.readInt() == 1;
233        mAccountAuthenticatorResponse = in.readParcelable(loader);
234    }
235
236    // Save/restore our SetupData (used in AccountSetupActivity)
237    static public void save(Bundle bundle) {
238        bundle.putParcelable(EXTRA_SETUP_DATA, getInstance());
239    }
240
241    static public synchronized SetupData restore(Bundle bundle) {
242        if (bundle != null && bundle.containsKey(EXTRA_SETUP_DATA)) {
243            INSTANCE = bundle.getParcelable(EXTRA_SETUP_DATA);
244            return INSTANCE;
245        } else {
246            return getInstance();
247        }
248    }
249
250    public static String debugString() {
251        StringBuilder sb = new StringBuilder("SetupData");
252        SetupData data = getInstance();
253        sb.append(":flow=" + FLOW_MODES[data.mFlowMode]);
254        sb.append(":acct=" + (data.mAccount == null ? "none" : data.mAccount.mId));
255        if (data.mUsername != null) {
256            sb.append(":user=" + data.mUsername);
257        }
258        if (data.mPassword != null) {
259            sb.append(":pass=" + data.mPassword);
260        }
261        sb.append(":a/d=" + data.mAllowAutodiscover);
262        sb.append(":auto=" + data.mAutoSetup);
263        sb.append(":default=" + data.mDefault);
264        sb.append(":check=");
265        if (SetupData.isCheckIncoming()) sb.append("in+");
266        if (SetupData.isCheckOutgoing()) sb.append("out+");
267        if (SetupData.isCheckAutodiscover()) sb.append("a/d");
268        sb.append(":policy=" + (data.mPolicy == null ? "none" : "exists"));
269        return sb.toString();
270    }
271}
272