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