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.content.Intent;
21import android.os.AsyncTask;
22import android.os.Bundle;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.View.OnClickListener;
26import android.view.ViewGroup;
27import android.widget.Button;
28import android.widget.RelativeLayout;
29import android.widget.RelativeLayout.LayoutParams;
30
31import com.android.email.R;
32import com.android.email.activity.ActivityHelper;
33import com.android.email.activity.UiUtilities;
34import com.android.email.service.EmailServiceUtils;
35import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
36import com.android.emailcommon.provider.Account;
37import com.android.emailcommon.provider.HostAuth;
38import com.android.emailcommon.utility.Utility;
39import com.android.mail.utils.LogUtils;
40
41/**
42 * Prompts the user to select an account type. The account type, along with the
43 * passed in email address, password and makeDefault are then passed on to the
44 * AccountSetupIncoming activity.
45 */
46public class AccountSetupType extends AccountSetupActivity implements OnClickListener {
47
48    private boolean mButtonPressed;
49
50    public static void actionSelectAccountType(Activity fromActivity, SetupData setupData) {
51        final Intent i = new ForwardingIntent(fromActivity, AccountSetupType.class);
52        i.putExtra(SetupData.EXTRA_SETUP_DATA, setupData);
53        fromActivity.startActivity(i);
54    }
55
56    @Override
57    public void onCreate(Bundle savedInstanceState) {
58        super.onCreate(savedInstanceState);
59        ActivityHelper.debugSetWindowFlags(this);
60
61        final String accountType = mSetupData.getFlowAccountType();
62        // If we're in account setup flow mode, see if there's just one protocol that matches
63        if (mSetupData.getFlowMode() == SetupData.FLOW_MODE_ACCOUNT_MANAGER) {
64            int matches = 0;
65            String protocol = null;
66            for (EmailServiceInfo info: EmailServiceUtils.getServiceInfoList(this)) {
67                if (info.accountType.equals(accountType)) {
68                    protocol = info.protocol;
69                    matches++;
70                }
71            }
72            // If so, select it...
73            if (matches == 1) {
74                onSelect(protocol);
75                return;
76            }
77        }
78
79        // Otherwise proceed into this screen
80        setContentView(R.layout.account_setup_account_type);
81        final ViewGroup parent = UiUtilities.getView(this, R.id.accountTypes);
82        View lastView = parent.getChildAt(0);
83        int i = 1;
84        for (EmailServiceInfo info: EmailServiceUtils.getServiceInfoList(this)) {
85            if (EmailServiceUtils.isServiceAvailable(this, info.protocol)) {
86                // If we're looking for a specific account type, reject others
87                // Don't show types with "hide" set
88                if (info.hide || (accountType != null && !accountType.equals(info.accountType))) {
89                    continue;
90                }
91                LayoutInflater.from(this).inflate(R.layout.account_type, parent);
92                final Button button = (Button)parent.getChildAt(i);
93                if (parent instanceof RelativeLayout) {
94                    final LayoutParams params = (LayoutParams)button.getLayoutParams();
95                    params.addRule(RelativeLayout.BELOW, lastView.getId());
96                 }
97                button.setId(i);
98                button.setTag(info.protocol);
99                button.setText(info.name);
100                button.setOnClickListener(this);
101                lastView = button;
102                i++;
103                // TODO: Remember vendor overlay for exchange name
104            }
105       }
106        final Button previousButton = (Button) findViewById(R.id.previous); // xlarge only
107        if (previousButton != null) previousButton.setOnClickListener(this);
108    }
109
110    /**
111     * The user has selected an exchange account type. Set the mail delete policy here, because
112     * there is no UI (for exchange), and switch the default sync interval to "push".
113     */
114    private void onSelect(String protocol) {
115        final Account account = mSetupData.getAccount();
116        final HostAuth recvAuth = account.getOrCreateHostAuthRecv(this);
117        recvAuth.setConnection(protocol, recvAuth.mAddress, recvAuth.mPort, recvAuth.mFlags);
118        final EmailServiceInfo info = EmailServiceUtils.getServiceInfo(this, protocol);
119
120        new DuplicateCheckTask(account.mEmailAddress, info.accountType)
121                .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
122    }
123
124    private void onProceedNext() {
125        final Account account = mSetupData.getAccount();
126        final HostAuth recvAuth = account.getOrCreateHostAuthRecv(this);
127        final EmailServiceInfo info = EmailServiceUtils.getServiceInfo(this, recvAuth.mProtocol);
128        if (info.usesAutodiscover) {
129            mSetupData.setCheckSettingsMode(SetupData.CHECK_AUTODISCOVER);
130        } else {
131            mSetupData.setCheckSettingsMode(
132                    SetupData.CHECK_INCOMING | (info.usesSmtp ? SetupData.CHECK_OUTGOING : 0));
133        }
134        recvAuth.mLogin = recvAuth.mLogin + "@" + recvAuth.mAddress;
135        AccountSetupBasics.setDefaultsForProtocol(this, account);
136        AccountSetupIncoming.actionIncomingSettings(this, mSetupData);
137        // Back from the incoming screen returns to AccountSetupBasics
138        finish();
139    }
140
141    @Override
142    public void onClick(View v) {
143        switch (v.getId()) {
144            case R.id.previous:
145                finish();
146                break;
147            default:
148                if (!mButtonPressed) {
149                    mButtonPressed = true;
150                    onSelect((String)v.getTag());
151                }
152                break;
153        }
154    }
155
156    private class DuplicateCheckTask extends AsyncTask<Void, Void, String> {
157        private final String mAddress;
158        private final String mAuthority;
159
160        public DuplicateCheckTask(String address, String authority) {
161            mAddress = address;
162            mAuthority = authority;
163        }
164
165        @Override
166        protected String doInBackground(Void... params) {
167            return Utility.findExistingAccount(AccountSetupType.this, mAuthority, mAddress);
168        }
169
170        @Override
171        protected void onPostExecute(String duplicateAccountName) {
172            mButtonPressed = false;
173            if (duplicateAccountName != null) {
174                // Show duplicate account warning
175                final DuplicateAccountDialogFragment dialogFragment =
176                        DuplicateAccountDialogFragment.newInstance(duplicateAccountName);
177                dialogFragment.show(AccountSetupType.this.getFragmentManager(),
178                        DuplicateAccountDialogFragment.TAG);
179            } else {
180                // Otherwise, proceed with the save/check
181                onProceedNext();
182            }
183        }
184
185        @Override
186        protected void onCancelled(String s) {
187            mButtonPressed = false;
188            LogUtils.d(LogUtils.TAG, "Duplicate account check cancelled (AccountSetupType)");
189        }
190    }
191}
192