AccountSetupIncoming.java revision cc0185f07c9198008d8dc685ae9979f3e35e8539
1/*
2 * Copyright (C) 2008 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.app.Activity;
20import android.app.FragmentTransaction;
21import android.content.Intent;
22import android.os.Bundle;
23import android.view.View;
24import android.view.View.OnClickListener;
25import android.widget.Button;
26
27import com.android.email.R;
28import com.android.email.activity.ActivityHelper;
29import com.android.email.activity.UiUtilities;
30import com.android.email.service.EmailServiceUtils;
31import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
32import com.android.emailcommon.provider.Account;
33import com.android.emailcommon.provider.HostAuth;
34
35/**
36 * Provides setup flow for IMAP/POP accounts.
37 *
38 * Uses AccountSetupIncomingFragment for primary UI.  Uses AccountCheckSettingsFragment to validate
39 * the settings as entered.  If the account is OK, proceeds to AccountSetupOutgoing.
40 */
41public class AccountSetupIncoming extends AccountSetupActivity
42        implements AccountSetupIncomingFragment.Callback, OnClickListener {
43
44    /* package */ AccountServerBaseFragment mFragment;
45    private Button mNextButton;
46    /* package */ boolean mNextButtonEnabled;
47    private boolean mStartedAutoDiscovery;
48    private EmailServiceInfo mServiceInfo;
49
50    // Keys for savedInstanceState
51    private final static String STATE_STARTED_AUTODISCOVERY =
52            "AccountSetupExchange.StartedAutoDiscovery";
53
54    public static void actionIncomingSettings(Activity fromActivity, int mode, Account account) {
55        SetupData.setFlowMode(mode);
56        SetupData.setAccount(account);
57        fromActivity.startActivity(new Intent(fromActivity, AccountSetupIncoming.class));
58    }
59
60    @Override
61    public void onCreate(Bundle savedInstanceState) {
62        super.onCreate(savedInstanceState);
63        ActivityHelper.debugSetWindowFlags(this);
64
65        HostAuth hostAuth = SetupData.getAccount().mHostAuthRecv;
66        mServiceInfo = EmailServiceUtils.getServiceInfo(this, hostAuth.mProtocol);
67
68        setContentView(R.layout.account_setup_incoming);
69        mFragment = (AccountServerBaseFragment)
70                getFragmentManager().findFragmentById(R.id.setup_fragment);
71
72        // Configure fragment
73        mFragment.setCallback(this);
74
75        mNextButton = (Button) UiUtilities.getView(this, R.id.next);
76        mNextButton.setOnClickListener(this);
77        UiUtilities.getView(this, R.id.previous).setOnClickListener(this);
78
79        // One-shot to launch autodiscovery at the entry to this activity (but not if it restarts)
80        if (mServiceInfo.usesAutodiscover) {
81            mStartedAutoDiscovery = false;
82            if (savedInstanceState != null) {
83                mStartedAutoDiscovery = savedInstanceState.getBoolean(STATE_STARTED_AUTODISCOVERY);
84            }
85            if (!mStartedAutoDiscovery) {
86                startAutoDiscover();
87            }
88        }
89   }
90
91    /**
92     * Implements View.OnClickListener
93     */
94    @Override
95    public void onClick(View view) {
96        switch (view.getId()) {
97            case R.id.next:
98                mFragment.onNext();
99                break;
100            case R.id.previous:
101                onBackPressed();
102                break;
103        }
104    }
105
106    @Override
107    public void onSaveInstanceState(Bundle outState) {
108        super.onSaveInstanceState(outState);
109        outState.putBoolean(STATE_STARTED_AUTODISCOVERY, mStartedAutoDiscovery);
110    }
111
112    /**
113     * If the conditions are right, launch the autodiscover fragment.  If it succeeds (even
114     * partially) it will prefill the setup fields and we can proceed as if the user entered them.
115     *
116     * Conditions for skipping:
117     *  Editing existing account
118     *  AutoDiscover blocked (used for unit testing only)
119     *  Username or password not entered yet
120     */
121    private void startAutoDiscover() {
122        // Note that we've started autodiscovery - even if we decide not to do it,
123        // this prevents repeating.
124        mStartedAutoDiscovery = true;
125
126        if (!SetupData.isAllowAutodiscover()) {
127            return;
128        }
129
130        Account account = SetupData.getAccount();
131        // If we've got a username and password and we're NOT editing, try autodiscover
132        String username = account.mHostAuthRecv.mLogin;
133        String password = account.mHostAuthRecv.mPassword;
134        if (username != null && password != null) {
135            onProceedNext(SetupData.CHECK_AUTODISCOVER, mFragment);
136        }
137    }
138
139    /**
140     * Implements AccountCheckSettingsFragment.Callbacks
141     *
142     * @param result configuration data returned by AD server, or null if no data available
143     */
144    public void onAutoDiscoverComplete(int result, HostAuth hostAuth) {
145        // If authentication failed, exit immediately (to re-enter credentials)
146        if (result == AccountCheckSettingsFragment.AUTODISCOVER_AUTHENTICATION) {
147            finish();
148            return;
149        }
150
151        // If data was returned, populate the account & populate the UI fields and validate it
152        if (result == AccountCheckSettingsFragment.AUTODISCOVER_OK) {
153            boolean valid = mFragment.setHostAuthFromAutodiscover(hostAuth);
154            if (valid) {
155                // "click" next to launch server verification
156                mFragment.onNext();
157            }
158        }
159        // Otherwise, proceed into this activity for manual setup
160    }
161
162    /**
163     * Implements AccountServerBaseFragment.Callback
164     *
165     * Launches the account checker.  Positive results are reported to onCheckSettingsOk().
166     */
167    public void onProceedNext(int checkMode, AccountServerBaseFragment target) {
168        AccountCheckSettingsFragment checkerFragment =
169            AccountCheckSettingsFragment.newInstance(checkMode, target);
170        FragmentTransaction transaction = getFragmentManager().beginTransaction();
171        transaction.add(checkerFragment, AccountCheckSettingsFragment.TAG);
172        transaction.addToBackStack("back");
173        transaction.commit();
174    }
175
176    /**
177     * Implements AccountServerBaseFragment.Callback
178     */
179    public void onEnableProceedButtons(boolean enable) {
180        mNextButtonEnabled = enable;
181        mNextButton.setEnabled(enable);
182    }
183
184    /**
185     * Implements AccountServerBaseFragment.Callback
186     *
187     * If the checked settings are OK, proceed to outgoing settings screen
188     */
189    public void onCheckSettingsComplete(int result, int setupMode) {
190        if (result == AccountCheckSettingsFragment.CHECK_SETTINGS_OK) {
191            if (mServiceInfo.usesSmtp) {
192                AccountSetupOutgoing.actionOutgoingSettings(this, SetupData.getFlowMode(),
193                        SetupData.getAccount());
194            } else {
195                AccountSetupOptions.actionOptions(this);
196                finish();
197            }
198        }
199    }
200}
201