AccountSetupNames.java revision 687f9962d7095e18ef994cd0e64337f02ed1a5bd
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.Context;
21import android.content.Intent;
22import android.os.Bundle;
23import android.text.Editable;
24import android.text.TextWatcher;
25import android.text.method.TextKeyListener;
26import android.text.method.TextKeyListener.Capitalize;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.Button;
30import android.widget.EditText;
31
32import com.android.email.Account;
33import com.android.email.Email;
34import com.android.email.Preferences;
35import com.android.email.R;
36import com.android.email.Utility;
37import com.android.email.activity.FolderMessageList;
38
39public class AccountSetupNames extends Activity implements OnClickListener {
40    private static final String EXTRA_ACCOUNT = "account";
41
42    private EditText mDescription;
43
44    private EditText mName;
45
46    private Account mAccount;
47
48    private Button mDoneButton;
49
50    public static void actionSetNames(Context context, Account account) {
51        Intent i = new Intent(context, AccountSetupNames.class);
52        i.putExtra(EXTRA_ACCOUNT, account);
53        context.startActivity(i);
54    }
55
56    @Override
57    public void onCreate(Bundle savedInstanceState) {
58        super.onCreate(savedInstanceState);
59        setContentView(R.layout.account_setup_names);
60        mDescription = (EditText)findViewById(R.id.account_description);
61        mName = (EditText)findViewById(R.id.account_name);
62        mDoneButton = (Button)findViewById(R.id.done);
63        mDoneButton.setOnClickListener(this);
64
65        TextWatcher validationTextWatcher = new TextWatcher() {
66            public void afterTextChanged(Editable s) {
67                validateFields();
68            }
69
70            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
71            }
72
73            public void onTextChanged(CharSequence s, int start, int before, int count) {
74            }
75        };
76        mName.addTextChangedListener(validationTextWatcher);
77
78        mName.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
79
80        mAccount = (Account)getIntent().getSerializableExtra(EXTRA_ACCOUNT);
81
82        /*
83         * Since this field is considered optional, we don't set this here. If
84         * the user fills in a value we'll reset the current value, otherwise we
85         * just leave the saved value alone.
86         */
87        // mDescription.setText(mAccount.getDescription());
88        if (mAccount.getName() != null) {
89            mName.setText(mAccount.getName());
90        }
91        if (!Utility.requiredFieldValid(mName)) {
92            mDoneButton.setEnabled(false);
93        }
94    }
95
96    /**
97     * TODO: Validator should also trim the name string before checking it.
98     */
99    private void validateFields() {
100        mDoneButton.setEnabled(Utility.requiredFieldValid(mName));
101        Utility.setCompoundDrawablesAlpha(mDoneButton, mDoneButton.isEnabled() ? 255 : 128);
102    }
103
104    /**
105     * TODO: Validator should also trim the description string before checking it.
106     */
107    private void onNext() {
108        if (Utility.requiredFieldValid(mDescription)) {
109            mAccount.setDescription(mDescription.getText().toString());
110        }
111        mAccount.setName(mName.getText().toString());
112        mAccount.save(Preferences.getPreferences(this));
113        FolderMessageList.actionHandleAccount(this, mAccount, Email.INBOX);
114        finish();
115    }
116
117    public void onClick(View v) {
118        switch (v.getId()) {
119            case R.id.done:
120                onNext();
121                break;
122        }
123    }
124}
125