AccountSetupNames.java revision f1efd65a5ebac0a2f1e218c0ae6bb8ceb1a19af6
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.provider.EmailContent;
22import com.android.email.provider.EmailContent.AccountColumns;
23
24import android.app.Activity;
25import android.content.ContentValues;
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    private static final String EXTRA_EAS_FLOW = "easFlow";
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, boolean easFlowMode) {
47        Intent i = new Intent(fromActivity, AccountSetupNames.class);
48        i.putExtra(EXTRA_ACCOUNT_ID, accountId);
49        i.putExtra(EXTRA_EAS_FLOW, easFlowMode);
50        fromActivity.startActivity(i);
51    }
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        setContentView(R.layout.account_setup_names);
57        mDescription = (EditText)findViewById(R.id.account_description);
58        mName = (EditText)findViewById(R.id.account_name);
59        mDoneButton = (Button)findViewById(R.id.done);
60        mDoneButton.setOnClickListener(this);
61
62        TextWatcher validationTextWatcher = new TextWatcher() {
63            public void afterTextChanged(Editable s) {
64                validateFields();
65            }
66
67            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
68            }
69
70            public void onTextChanged(CharSequence s, int start, int before, int count) {
71            }
72        };
73        mName.addTextChangedListener(validationTextWatcher);
74
75        mName.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
76
77        long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1);
78        mAccount = EmailContent.Account.restoreAccountWithId(this, accountId);
79
80        /*
81         * Since this field is considered optional, we don't set this here. If
82         * the user fills in a value we'll reset the current value, otherwise we
83         * just leave the saved value alone.
84         */
85        // mDescription.setText(mAccount.getDescription());
86        if (mAccount != null && mAccount.getSenderName() != null) {
87            mName.setText(mAccount.getSenderName());
88        }
89        if (!Utility.requiredFieldValid(mName)) {
90            mDoneButton.setEnabled(false);
91        }
92    }
93
94    /**
95     * TODO: Validator should also trim the name string before checking it.
96     */
97    private void validateFields() {
98        mDoneButton.setEnabled(Utility.requiredFieldValid(mName));
99        Utility.setCompoundDrawablesAlpha(mDoneButton, mDoneButton.isEnabled() ? 255 : 128);
100    }
101
102    @Override
103    public void onBackPressed() {
104        boolean easFlowMode = getIntent().getBooleanExtra(EXTRA_EAS_FLOW, false);
105        if (easFlowMode) {
106            AccountSetupBasics.actionAccountCreateFinishedEas(this);
107        } else {
108            AccountSetupBasics.actionAccountCreateFinished(this, mAccount.mId);
109        }
110        finish();
111    }
112
113    /**
114     * After having a chance to input the display names, we normally jump directly to the
115     * inbox for the new account.  However if we're in EAS flow mode (externally-launched
116     * account creation) we simply "pop" here which should return us to the Accounts activities.
117     *
118     * TODO: Validator should also trim the description string before checking it.
119     */
120    private void onNext() {
121        if (Utility.requiredFieldValid(mDescription)) {
122            mAccount.setDisplayName(mDescription.getText().toString());
123        }
124        String name = mName.getText().toString();
125        mAccount.setSenderName(name);
126        ContentValues cv = new ContentValues();
127        cv.put(AccountColumns.DISPLAY_NAME, mAccount.getDisplayName());
128        cv.put(AccountColumns.SENDER_NAME, name);
129        mAccount.update(this, cv);
130        onBackPressed();
131    }
132
133    public void onClick(View v) {
134        switch (v.getId()) {
135            case R.id.done:
136                onNext();
137                break;
138        }
139    }
140}
141