AccountSetupBasics.java revision a75bab550423e0cd0b2d58379d8210ce6c7c7aa9
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 com.android.email.R;
20import com.android.email.Utility;
21import com.android.email.VendorPolicyLoader;
22import com.android.email.activity.Debug;
23import com.android.email.activity.MessageList;
24import com.android.email.provider.EmailContent.Account;
25import com.android.email.provider.EmailContent.Mailbox;
26
27import android.app.Activity;
28import android.content.Context;
29import android.content.Intent;
30import android.os.Bundle;
31import android.view.View;
32import android.view.View.OnClickListener;
33import android.widget.Button;
34
35/**
36 * Prompts the user for the email address and password. Also prompts for
37 * "Use this account as default" if this is the 2nd+ account being set up.
38 * Attempts to lookup default settings for the domain the user specified. If the
39 * domain is known the settings are handed off to the AccountSetupCheckSettings
40 * activity. If no settings are found the settings are handed off to the
41 * AccountSetupAccountType activity.
42 */
43public class AccountSetupBasics extends AccountSetupActivity
44        implements OnClickListener, AccountSetupBasicsFragment.Callback {
45
46    private AccountSetupBasicsFragment mFragment;
47    private Button mNextButton;
48    private Button mManualSetupButton;
49
50    public static void actionNewAccount(Activity fromActivity) {
51        SetupData.init(SetupData.FLOW_MODE_NORMAL);
52        fromActivity.startActivity(new Intent(fromActivity, AccountSetupBasics.class));
53    }
54
55    public static void actionNewAccountWithCredentials(Activity fromActivity,
56            String username, String password, int accountFlowMode) {
57        SetupData.init(accountFlowMode, username, password);
58        fromActivity.startActivity(new Intent(fromActivity, AccountSetupBasics.class));
59    }
60
61    /**
62     * This generates setup data that can be used to start a self-contained account creation flow
63     * for exchange accounts.
64     */
65    public static Intent actionSetupExchangeIntent(Context context) {
66        SetupData.init(SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS);
67        return new Intent(context, AccountSetupBasics.class);
68    }
69
70    /**
71     * This generates setup data that can be used to start a self-contained account creation flow
72     * for pop/imap accounts.
73     */
74    public static Intent actionSetupPopImapIntent(Context context) {
75        SetupData.init(SetupData.FLOW_MODE_ACCOUNT_MAANGER_POP_IMAP);
76        return new Intent(context, AccountSetupBasics.class);
77    }
78
79    public static void actionAccountCreateFinishedAccountFlow(Activity fromActivity) {
80        Intent i= new Intent(fromActivity, AccountSetupBasics.class);
81        // If we're in the "account flow" (from AccountManager), we want to return to the caller
82        // (in the settings app)
83        SetupData.init(SetupData.FLOW_MODE_RETURN_TO_CALLER);
84        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
85        fromActivity.startActivity(i);
86    }
87
88    public static void actionAccountCreateFinished(final Activity fromActivity,
89            final long accountId) {
90        Utility.runAsync(new Runnable() {
91           public void run() {
92               Intent i = new Intent(fromActivity, AccountSetupBasics.class);
93               // If we're not in the "account flow" (from AccountManager), we want to show the
94               // message list for the new inbox
95               Account account = Account.restoreAccountWithId(fromActivity, accountId);
96               SetupData.init(SetupData.FLOW_MODE_RETURN_TO_MESSAGE_LIST, account);
97               i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
98               fromActivity.startActivity(i);
99            }});
100    }
101
102    @Override
103    public void onCreate(Bundle savedInstanceState) {
104        super.onCreate(savedInstanceState);
105
106        int flowMode = SetupData.getFlowMode();
107        if (flowMode == SetupData.FLOW_MODE_RETURN_TO_CALLER) {
108            // Return to the caller who initiated account creation
109            finish();
110            return;
111        } else if (flowMode == SetupData.FLOW_MODE_RETURN_TO_MESSAGE_LIST) {
112            Account account = SetupData.getAccount();
113            if (account != null && account.mId >= 0) {
114                // Show the message list for the new account
115                MessageList.actionHandleAccount(this, account.mId, Mailbox.TYPE_INBOX);
116                finish();
117                return;
118            }
119        }
120
121        setContentView(R.layout.account_setup_basics);
122
123        mFragment = (AccountSetupBasicsFragment) findFragmentById(R.id.setup_basics_fragment);
124        mNextButton = (Button) findViewById(R.id.next);
125        mManualSetupButton = (Button) findViewById(R.id.manual_setup);
126
127        mNextButton.setOnClickListener(this);
128        mManualSetupButton.setOnClickListener(this);
129
130        boolean alternateStrings = false;
131        if (flowMode == SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS) {
132            // No need for manual button -> next is appropriate
133            mManualSetupButton.setVisibility(View.GONE);
134            // Swap welcome text for EAS-specific text
135            alternateStrings = VendorPolicyLoader.getInstance(this).useAlternateExchangeStrings();
136            setTitle(alternateStrings
137                    ? R.string.account_setup_basics_exchange_title_alternate
138                            : R.string.account_setup_basics_exchange_title);
139        }
140
141        // Configure fragment
142        mFragment.setCallback(this, alternateStrings);
143    }
144
145    /**
146     * This is used in automatic setup mode to jump directly down to the names screen.
147     *
148     * NOTE:  With this organization, it is *not* possible to auto-create an exchange account,
149     * because certain necessary actions happen during AccountSetupOptions (which we are
150     * skipping here).
151     */
152    @Override
153    public void onActivityResult(int requestCode, int resultCode, Intent data) {
154        if (resultCode == RESULT_OK) {
155            AccountSetupOptions.actionOptions(this);
156            finish();
157        }
158    }
159
160    public void onClick(View v) {
161        switch (v.getId()) {
162            case R.id.next:
163                mFragment.onNext();
164                break;
165            case R.id.manual_setup:
166                // no AutoDiscover - user clicked "manual"
167                mFragment.onManualSetup(false);
168                break;
169        }
170    }
171
172    /**
173     * Implements AccountSetupBasicsFragment.Callback
174     */
175    @Override
176    public void onEnableProceedButtons(boolean enable) {
177        mNextButton.setEnabled(enable);
178        mManualSetupButton.setEnabled(enable);
179        /*
180         * Dim the next button's icon to 50% if the button is disabled.
181         * TODO this can probably be done with a stateful drawable. Check into it.
182         * android:state_enabled
183         */
184        Utility.setCompoundDrawablesAlpha(mNextButton, mNextButton.isEnabled() ? 255 : 128);
185    }
186
187    /**
188     * Implements AccountSetupBasicsFragment.Callback
189     */
190    @Override
191    public void onProceedAutomatic() {
192        AccountSetupCheckSettings.actionCheckSettings(this,
193                SetupData.CHECK_INCOMING | SetupData.CHECK_OUTGOING);
194    }
195
196    /**
197     * Implements AccountSetupBasicsFragment.Callback
198     */
199    @Override
200    public void onProceedDebugSettings() {
201        Debug.actionShow(this);
202    }
203
204    /**
205     * Implements AccountSetupBasicsFragment.Callback
206     */
207    @Override
208    public void onProceedManual() {
209        AccountSetupAccountType.actionSelectAccountType(this);
210    }
211}
212