AccountSetupNames.java revision 9e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857
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;
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
41    private EditText mDescription;
42    private EditText mName;
43    private EmailContent.Account mAccount;
44    private Button mDoneButton;
45
46    public static void actionSetNames(Activity fromActivity, long accountId) {
47        Intent i = new Intent(fromActivity, AccountSetupNames.class);
48        i.putExtra(EXTRA_ACCOUNT_ID, accountId);
49        fromActivity.startActivity(i);
50    }
51
52    @Override
53    public void onCreate(Bundle savedInstanceState) {
54        super.onCreate(savedInstanceState);
55        setContentView(R.layout.account_setup_names);
56        mDescription = (EditText)findViewById(R.id.account_description);
57        mName = (EditText)findViewById(R.id.account_name);
58        mDoneButton = (Button)findViewById(R.id.done);
59        mDoneButton.setOnClickListener(this);
60
61        TextWatcher validationTextWatcher = new TextWatcher() {
62            public void afterTextChanged(Editable s) {
63                validateFields();
64            }
65
66            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
67            }
68
69            public void onTextChanged(CharSequence s, int start, int before, int count) {
70            }
71        };
72        mName.addTextChangedListener(validationTextWatcher);
73
74        mName.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
75
76        long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1);
77        mAccount = EmailContent.Account.restoreAccountWithId(this, accountId);
78
79        /*
80         * Since this field is considered optional, we don't set this here. If
81         * the user fills in a value we'll reset the current value, otherwise we
82         * just leave the saved value alone.
83         */
84        // mDescription.setText(mAccount.getDescription());
85        if (mAccount.getSenderName() != null) {
86            mName.setText(mAccount.getSenderName());
87        }
88        if (!Utility.requiredFieldValid(mName)) {
89            mDoneButton.setEnabled(false);
90        }
91    }
92
93    /**
94     * TODO: Validator should also trim the name string before checking it.
95     */
96    private void validateFields() {
97        mDoneButton.setEnabled(Utility.requiredFieldValid(mName));
98        Utility.setCompoundDrawablesAlpha(mDoneButton, mDoneButton.isEnabled() ? 255 : 128);
99    }
100
101    /**
102     * TODO: Validator should also trim the description string before checking it.
103     */
104    private void onNext() {
105        if (Utility.requiredFieldValid(mDescription)) {
106            mAccount.setDisplayName(mDescription.getText().toString());
107        }
108        String name = mName.getText().toString();
109        mAccount.setSenderName(name);
110        ContentValues cv = new ContentValues();
111        cv.put(AccountColumns.DISPLAY_NAME, mAccount.getDisplayName());
112        cv.put(AccountColumns.SENDER_NAME, name);
113        mAccount.update(this, cv);
114        MessageList.actionHandleAccount(this, mAccount.mId, EmailContent.Mailbox.TYPE_INBOX);
115        finish();
116    }
117
118    public void onClick(View v) {
119        switch (v.getId()) {
120            case R.id.done:
121                onNext();
122                break;
123        }
124    }
125}
126