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