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