AccountSetupNames.java revision 2b0c619f1edd9fd89dc06bf35d99ece91f415f1e
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.Account;
20import com.android.email.Email;
21import com.android.email.Preferences;
22import com.android.email.R;
23import com.android.email.Utility;
24import com.android.email.activity.FolderMessageList;
25
26import android.app.Activity;
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 = "account";
40
41    private EditText mDescription;
42
43    private EditText mName;
44
45    private Account mAccount;
46
47    private Button mDoneButton;
48
49    public static void actionSetNames(Activity fromActivity, Account account) {
50        Intent i = new Intent(fromActivity, AccountSetupNames.class);
51        i.putExtra(EXTRA_ACCOUNT, account);
52        fromActivity.startActivity(i);
53    }
54
55    @Override
56    public void onCreate(Bundle savedInstanceState) {
57        super.onCreate(savedInstanceState);
58        setContentView(R.layout.account_setup_names);
59        mDescription = (EditText)findViewById(R.id.account_description);
60        mName = (EditText)findViewById(R.id.account_name);
61        mDoneButton = (Button)findViewById(R.id.done);
62        mDoneButton.setOnClickListener(this);
63
64        TextWatcher validationTextWatcher = new TextWatcher() {
65            public void afterTextChanged(Editable s) {
66                validateFields();
67            }
68
69            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
70            }
71
72            public void onTextChanged(CharSequence s, int start, int before, int count) {
73            }
74        };
75        mName.addTextChangedListener(validationTextWatcher);
76
77        mName.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
78
79        mAccount = (Account)getIntent().getSerializableExtra(EXTRA_ACCOUNT);
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.getName() != null) {
88            mName.setText(mAccount.getName());
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    /**
104     * TODO: Validator should also trim the description string before checking it.
105     */
106    private void onNext() {
107        if (Utility.requiredFieldValid(mDescription)) {
108            mAccount.setDescription(mDescription.getText().toString());
109        }
110        mAccount.setName(mName.getText().toString());
111        mAccount.save(Preferences.getPreferences(this));
112        FolderMessageList.actionHandleAccount(this, mAccount, Email.INBOX);
113        finish();
114    }
115
116    public void onClick(View v) {
117        switch (v.getId()) {
118            case R.id.done:
119                onNext();
120                break;
121        }
122    }
123}
124