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