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