AccountSetupNames.java revision 5e91cccd4b530eb2aeace5c5bf4f3328a5b5d69d
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.provider.EmailContent;
23import com.android.email.provider.EmailContent.AccountColumns;
24
25import android.app.Activity;
26import android.content.ContentValues;
27import android.content.Intent;
28import android.os.Bundle;
29import android.text.Editable;
30import android.text.TextWatcher;
31import android.text.method.TextKeyListener;
32import android.text.method.TextKeyListener.Capitalize;
33import android.view.View;
34import android.view.View.OnClickListener;
35import android.widget.Button;
36import android.widget.EditText;
37
38public class AccountSetupNames extends Activity implements OnClickListener {
39    private static final String EXTRA_ACCOUNT_ID = "accountId";
40    private static final String EXTRA_EAS_FLOW = "easFlow";
41
42    private EditText mDescription;
43    private EditText mName;
44    private EmailContent.Account mAccount;
45    private Button mDoneButton;
46
47    public static void actionSetNames(Activity fromActivity, long accountId, boolean easFlowMode) {
48        Intent i = new Intent(fromActivity, AccountSetupNames.class);
49        i.putExtra(EXTRA_ACCOUNT_ID, accountId);
50        i.putExtra(EXTRA_EAS_FLOW, easFlowMode);
51        fromActivity.startActivity(i);
52    }
53
54    @Override
55    public void onCreate(Bundle savedInstanceState) {
56        super.onCreate(savedInstanceState);
57        setContentView(R.layout.account_setup_names);
58        mDescription = (EditText)findViewById(R.id.account_description);
59        mName = (EditText)findViewById(R.id.account_name);
60        mDoneButton = (Button)findViewById(R.id.done);
61        mDoneButton.setOnClickListener(this);
62
63        TextWatcher validationTextWatcher = new TextWatcher() {
64            public void afterTextChanged(Editable s) {
65                validateFields();
66            }
67
68            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
69            }
70
71            public void onTextChanged(CharSequence s, int start, int before, int count) {
72            }
73        };
74        mName.addTextChangedListener(validationTextWatcher);
75
76        mName.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
77
78        long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1);
79        mAccount = EmailContent.Account.restoreAccountWithId(this, accountId);
80
81        /*
82         * Since this field is considered optional, we don't set this here. If
83         * the user fills in a value we'll reset the current value, otherwise we
84         * just leave the saved value alone.
85         */
86        // mDescription.setText(mAccount.getDescription());
87        if (mAccount != null && mAccount.getSenderName() != null) {
88            mName.setText(mAccount.getSenderName());
89        }
90        if (!Utility.requiredFieldValid(mName)) {
91            mDoneButton.setEnabled(false);
92        }
93    }
94
95    /**
96     * TODO: Validator should also trim the name string before checking it.
97     */
98    private void validateFields() {
99        mDoneButton.setEnabled(Utility.requiredFieldValid(mName));
100        Utility.setCompoundDrawablesAlpha(mDoneButton, mDoneButton.isEnabled() ? 255 : 128);
101    }
102
103    @Override
104    public void onBackPressed() {
105        boolean easFlowMode = getIntent().getBooleanExtra(EXTRA_EAS_FLOW, false);
106        if (easFlowMode) {
107            AccountSetupBasics.actionAccountCreateFinishedEas(this);
108        } else {
109            AccountSetupBasics.actionAccountCreateFinished(this, mAccount.mId);
110        }
111        finish();
112    }
113
114    /**
115     * After having a chance to input the display names, we normally jump directly to the
116     * inbox for the new account.  However if we're in EAS flow mode (externally-launched
117     * account creation) we simply "pop" here which should return us to the Accounts activities.
118     *
119     * TODO: Validator should also trim the description string before checking it.
120     */
121    private void onNext() {
122        if (Utility.requiredFieldValid(mDescription)) {
123            mAccount.setDisplayName(mDescription.getText().toString());
124        }
125        String name = mName.getText().toString();
126        mAccount.setSenderName(name);
127        ContentValues cv = new ContentValues();
128        cv.put(AccountColumns.DISPLAY_NAME, mAccount.getDisplayName());
129        cv.put(AccountColumns.SENDER_NAME, name);
130        mAccount.update(this, cv);
131        // Update the backup (side copy) of the accounts
132        AccountBackupRestore.backupAccounts(this);
133        onBackPressed();
134    }
135
136    public void onClick(View v) {
137        switch (v.getId()) {
138            case R.id.done:
139                onNext();
140                break;
141        }
142    }
143}
144