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