AccountSetupNames.java revision bcf32320e2600e96c8a9e997a8903bfc3893b35e
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.AccountBackupRestore;
20import com.android.email.R;
21import com.android.email.Utility;
22import com.android.email.activity.Welcome;
23import com.android.email.provider.EmailContent.Account;
24import com.android.email.provider.EmailContent.AccountColumns;
25import com.android.email.provider.EmailContent.HostAuth;
26
27import android.app.Activity;
28import android.content.ContentUris;
29import android.content.ContentValues;
30import android.content.Intent;
31import android.database.Cursor;
32import android.os.AsyncTask;
33import android.os.Bundle;
34import android.text.Editable;
35import android.text.TextWatcher;
36import android.text.method.TextKeyListener;
37import android.text.method.TextKeyListener.Capitalize;
38import android.view.View;
39import android.view.View.OnClickListener;
40import android.widget.Button;
41import android.widget.EditText;
42
43public class AccountSetupNames extends AccountSetupActivity implements OnClickListener {
44    private static final int REQUEST_SECURITY = 0;
45
46    private EditText mDescription;
47    private EditText mName;
48    private Button mDoneButton;
49    private boolean mEasAccount = false;
50
51    private CheckAccountStateTask mCheckAccountStateTask;
52
53    public static void actionSetNames(Activity fromActivity) {
54        fromActivity.startActivity(new Intent(fromActivity, AccountSetupNames.class));
55    }
56
57    @Override
58    public void onCreate(Bundle savedInstanceState) {
59        super.onCreate(savedInstanceState);
60        setContentView(R.layout.account_setup_names);
61        mDescription = (EditText)findViewById(R.id.account_description);
62        mName = (EditText)findViewById(R.id.account_name);
63        mDoneButton = (Button)findViewById(R.id.done);
64        mDoneButton.setOnClickListener(this);
65
66        TextWatcher validationTextWatcher = new TextWatcher() {
67            public void afterTextChanged(Editable s) {
68                validateFields();
69            }
70
71            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
72            }
73
74            public void onTextChanged(CharSequence s, int start, int before, int count) {
75            }
76        };
77        mName.addTextChangedListener(validationTextWatcher);
78        mName.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
79
80        Account account = SetupData.getAccount();
81        // Shouldn't happen, but it could
82        if (account == null) {
83            onBackPressed();
84            return;
85        }
86        // Get the hostAuth for receiving
87        HostAuth hostAuth = HostAuth.restoreHostAuthWithId(this, account.mHostAuthKeyRecv);
88        if (hostAuth == null) {
89            onBackPressed();
90            return;
91        }
92
93        // Remember whether we're an EAS account, since it doesn't require the user name field
94        mEasAccount = hostAuth.mProtocol.equals("eas");
95        if (mEasAccount) {
96            mName.setVisibility(View.GONE);
97            findViewById(R.id.account_name_label).setVisibility(View.GONE);
98        }
99        /*
100         * Since this field is considered optional, we don't set this here. If
101         * the user fills in a value we'll reset the current value, otherwise we
102         * just leave the saved value alone.
103         */
104        // mDescription.setText(mAccount.getDescription());
105        if (account != null && account.getSenderName() != null) {
106            mName.setText(account.getSenderName());
107        }
108
109        // Make sure the "done" button is in the proper state
110        validateFields();
111    }
112
113    @Override
114    protected void onDestroy() {
115        super.onDestroy();
116
117        if (mCheckAccountStateTask != null &&
118                mCheckAccountStateTask.getStatus() != CheckAccountStateTask.Status.FINISHED) {
119            mCheckAccountStateTask.cancel(true);
120            mCheckAccountStateTask = null;
121        }
122    }
123
124    /**
125     * TODO: Validator should also trim the name string before checking it.
126     */
127    private void validateFields() {
128        if (!mEasAccount) {
129            mDoneButton.setEnabled(Utility.isTextViewNotEmpty(mName));
130        }
131        Utility.setCompoundDrawablesAlpha(mDoneButton, mDoneButton.isEnabled() ? 255 : 128);
132    }
133
134    @Override
135    public void onBackPressed() {
136        if (SetupData.getFlowMode() != SetupData.FLOW_MODE_NORMAL) {
137            AccountSetupBasics.actionAccountCreateFinishedAccountFlow(this);
138        } else {
139            Account account = SetupData.getAccount();
140            if (account != null) {
141                AccountSetupBasics.actionAccountCreateFinished(this, account.mId);
142            } else {
143                // Safety check here;  If mAccount is null (due to external issues or bugs)
144                // just rewind back to Welcome, which can handle any configuration of accounts
145                Welcome.actionStart(this);
146            }
147        }
148        finish();
149    }
150
151    /**
152     * After having a chance to input the display names, we normally jump directly to the
153     * inbox for the new account.  However if we're in EAS flow mode (externally-launched
154     * account creation) we simply "pop" here which should return us to the Accounts activities.
155     *
156     * TODO: Validator should also trim the description string before checking it.
157     */
158    private void onNext() {
159        Account account = SetupData.getAccount();
160        if (Utility.isTextViewNotEmpty(mDescription)) {
161            account.setDisplayName(mDescription.getText().toString());
162        }
163        String name = mName.getText().toString();
164        account.setSenderName(name);
165        ContentValues cv = new ContentValues();
166        cv.put(AccountColumns.DISPLAY_NAME, account.getDisplayName());
167        cv.put(AccountColumns.SENDER_NAME, name);
168        account.update(this, cv);
169        // Update the backup (side copy) of the accounts
170        AccountBackupRestore.backupAccounts(this);
171
172        // Before proceeding, launch an AsyncTask to test the account for any syncing problems,
173        // and if there's a problem, bring up the UI to update the security level.
174        mCheckAccountStateTask = new CheckAccountStateTask(account.mId);
175        mCheckAccountStateTask.execute();
176    }
177
178    public void onClick(View v) {
179        switch (v.getId()) {
180            case R.id.done:
181                onNext();
182                break;
183        }
184    }
185
186    /**
187     * This async task is launched just before exiting.  It's a last chance test, before leaving
188     * this activity, for the account being in a "hold" state, and gives the user a chance to
189     * update security, enter a device PIN, etc. for a more seamless account setup experience.
190     *
191     * TODO: If there was *any* indication that security might be required, we could at least
192     * force the DeviceAdmin activation step, without waiting for the initial sync/handshake
193     * to fail.
194     * TODO: If the user doesn't update the security, don't go to the MessageList.
195     */
196    private class CheckAccountStateTask extends AsyncTask<Void, Void, Boolean> {
197
198        private long mAccountId;
199
200        public CheckAccountStateTask(long accountId) {
201            mAccountId = accountId;
202        }
203
204        @Override
205        protected Boolean doInBackground(Void... params) {
206            return Account.isSecurityHold(AccountSetupNames.this, mAccountId);
207        }
208
209        @Override
210        protected void onPostExecute(Boolean isSecurityHold) {
211            if (!isCancelled()) {
212                if (isSecurityHold) {
213                    Intent i = AccountSecurity.actionUpdateSecurityIntent(
214                            AccountSetupNames.this, mAccountId);
215                    AccountSetupNames.this.startActivityForResult(i, REQUEST_SECURITY);
216                } else {
217                    onBackPressed();
218                }
219            }
220        }
221    }
222
223    /**
224     * Handle the eventual result from the security update activity
225     *
226     * TODO: If the user doesn't update the security, don't go to the MessageList.
227     */
228    @Override
229    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
230        switch (requestCode) {
231            case REQUEST_SECURITY:
232                onBackPressed();
233        }
234        super.onActivityResult(requestCode, resultCode, data);
235    }
236
237}
238