AccountSetupExchange.java revision e6c6587b04a589eeb04006f75759c09ea10811e0
1/*
2 * Copyright (C) 2009 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.email.R;
20import com.android.email.activity.ActivityHelper;
21import com.android.email.provider.EmailContent.Account;
22import com.android.email.provider.EmailContent.HostAuth;
23
24import android.app.Activity;
25import android.app.FragmentTransaction;
26import android.content.Intent;
27import android.os.Bundle;
28import android.view.View;
29import android.view.View.OnClickListener;
30import android.widget.Button;
31
32/**
33 * Provides generic setup for Exchange accounts.  The following fields are supported:
34 *
35 *  Email Address   (from previous setup screen)
36 *  Server
37 *  Domain
38 *  Requires SSL?
39 *  User (login)
40 *  Password
41 *
42 * There are two primary paths through this activity:
43 *   Edit existing:
44 *     Load existing values from account into fields
45 *     When user clicks 'next':
46 *       Confirm not a duplicate account
47 *       Try new values (check settings)
48 *       If new values are OK:
49 *         Write new values (save to provider)
50 *         finish() (pop to previous)
51 *
52 *   Creating New:
53 *     Try Auto-discover to get details from server
54 *     If Auto-discover reports an authentication failure:
55 *       finish() (pop to previous, to re-enter username & password)
56 *     If Auto-discover succeeds:
57 *       write server's account details into account
58 *     Load values from account into fields
59 *     Confirm not a duplicate account
60 *     Try new values (check settings)
61 *     If new values are OK:
62 *       Write new values (save to provider)
63 *       Proceed to options screen
64 *       finish() (removes self from back stack)
65 */
66public class AccountSetupExchange extends AccountSetupActivity
67        implements AccountSetupExchangeFragment.Callback, OnClickListener {
68
69    // Keys for savedInstanceState
70    private final static String STATE_STARTED_AUTODISCOVERY =
71            "AccountSetupExchange.StartedAutoDiscovery";
72
73    boolean mStartedAutoDiscovery;
74    /* package */ AccountSetupExchangeFragment mFragment;
75    private Button mNextButton;
76    /* package */ boolean mNextButtonEnabled;
77
78    public static void actionIncomingSettings(Activity fromActivity, int mode, Account account) {
79        SetupData.setFlowMode(mode);
80        SetupData.setAccount(account);
81        fromActivity.startActivity(new Intent(fromActivity, AccountSetupExchange.class));
82    }
83
84    @Override
85    public void onCreate(Bundle savedInstanceState) {
86        super.onCreate(savedInstanceState);
87        ActivityHelper.debugSetWindowFlags(this);
88        setContentView(R.layout.account_setup_exchange);
89
90        mFragment = (AccountSetupExchangeFragment)
91                getFragmentManager().findFragmentById(R.id.setup_fragment);
92        mFragment.setCallback(this);
93        // TODO temp code to inhibit the options menu - still needed for AccountSettings
94        mFragment.mNextButtonDisplayed = false;
95        invalidateOptionsMenu();
96
97        mNextButton = (Button) findViewById(R.id.next);
98        mNextButton.setOnClickListener(this);
99        findViewById(R.id.previous).setOnClickListener(this);
100
101        // One-shot to launch autodiscovery at the entry to this activity (but not if it restarts)
102        mStartedAutoDiscovery = false;
103        if (savedInstanceState != null) {
104            mStartedAutoDiscovery = savedInstanceState.getBoolean(STATE_STARTED_AUTODISCOVERY);
105        }
106        if (!mStartedAutoDiscovery) {
107            startAutoDiscover();
108        }
109    }
110
111    /**
112     * Implements View.OnClickListener
113     */
114    @Override
115    public void onClick(View view) {
116        switch (view.getId()) {
117            case R.id.next:
118                mFragment.onNext();
119                break;
120            case R.id.previous:
121                onBackPressed();
122                break;
123        }
124    }
125
126    @Override
127    public void onSaveInstanceState(Bundle outState) {
128        super.onSaveInstanceState(outState);
129        outState.putBoolean(STATE_STARTED_AUTODISCOVERY, mStartedAutoDiscovery);
130    }
131
132    /**
133     * If the conditions are right, launch the autodiscover fragment.  If it succeeds (even
134     * partially) it will prefill the setup fields and we can proceed as if the user entered them.
135     *
136     * Conditions for skipping:
137     *  Editing existing account
138     *  AutoDiscover blocked (used for unit testing only)
139     *  Username or password not entered yet
140     */
141    private void startAutoDiscover() {
142        // Note that we've started autodiscovery - even if we decide not to do it,
143        // this prevents repeating.
144        mStartedAutoDiscovery = true;
145
146        if (!SetupData.isAllowAutodiscover()) {
147            return;
148        }
149
150        Account account = SetupData.getAccount();
151        // If we've got a username and password and we're NOT editing, try autodiscover
152        String username = account.mHostAuthRecv.mLogin;
153        String password = account.mHostAuthRecv.mPassword;
154        if (username != null && password != null) {
155            onProceedNext(SetupData.CHECK_AUTODISCOVER, mFragment);
156        }
157    }
158
159    /**
160     * Implements AccountCheckSettingsFragment.Callbacks
161     *
162     * @param result configuration data returned by AD server, or null if no data available
163     */
164    public void onAutoDiscoverComplete(int result, HostAuth hostAuth) {
165        // If authentication failed, exit immediately (to re-enter credentials)
166        if (result == AccountCheckSettingsFragment.AUTODISCOVER_AUTHENTICATION) {
167            finish();
168            return;
169        }
170
171        // If data was returned, populate the account & populate the UI fields and validate it
172        if (result == AccountCheckSettingsFragment.AUTODISCOVER_OK) {
173            boolean valid = mFragment.setHostAuthFromAutodiscover(hostAuth);
174            if (valid) {
175                // "click" next to launch server verification
176                mFragment.onNext();
177            }
178        }
179        // Otherwise, proceed into this activity for manual setup
180    }
181
182    /**
183     * Implements AccountServerBaseFragment.Callback
184     */
185    public void onProceedNext(int checkMode, AccountServerBaseFragment target) {
186        AccountCheckSettingsFragment checkerFragment =
187            AccountCheckSettingsFragment.newInstance(checkMode, target);
188        FragmentTransaction transaction = getFragmentManager().openTransaction();
189        transaction.replace(R.id.setup_fragment, checkerFragment);
190        transaction.addToBackStack("back");
191        transaction.commit();
192    }
193
194    /**
195     * Implements AccountServerBaseFragment.Callback
196     */
197    public void onEnableProceedButtons(boolean enable) {
198        mNextButtonEnabled = enable;
199        mNextButton.setEnabled(enable);
200    }
201
202    /**
203     * Implements AccountServerBaseFragment.Callback
204     *
205     * If the checked settings are OK, proceed to options screen.  If the user rejects security,
206     * exit this screen.  For all other errors, remain here for editing.
207     */
208    public void onCheckSettingsComplete(int result, int setupMode) {
209        switch (result) {
210            case AccountCheckSettingsFragment.CHECK_SETTINGS_OK:
211                AccountSetupOptions.actionOptions(this);
212                finish();
213                break;
214            case AccountCheckSettingsFragment.CHECK_SETTINGS_SECURITY_USER_DENY:
215                finish();
216                break;
217            default:
218            case AccountCheckSettingsFragment.CHECK_SETTINGS_SERVER_ERROR:
219                // Do nothing - remain in this screen
220                break;
221        }
222    }
223}
224