1 /* 2 * Copyright (C) 2013 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 android.app.LoaderManager; 20 import android.content.Context; 21 import android.content.CursorLoader; 22 import android.content.Loader; 23 import android.database.Cursor; 24 import android.os.Bundle; 25 import android.provider.ContactsContract; 26 import android.text.TextUtils; 27 import android.text.method.TextKeyListener; 28 import android.view.LayoutInflater; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.widget.EditText; 32 33 import com.android.email.R; 34 import com.android.email.activity.UiUtilities; 35 import com.android.email.service.EmailServiceUtils; 36 import com.android.email.setup.AuthenticatorSetupIntentHelper; 37 import com.android.emailcommon.provider.Account; 38 39 public class AccountSetupNamesFragment extends AccountSetupFragment { 40 private EditText mDescription; 41 private EditText mName; 42 private View mAccountNameLabel; 43 44 public interface Callback extends AccountSetupFragment.Callback { 45 46 } 47 48 public static AccountSetupNamesFragment newInstance() { 49 return new AccountSetupNamesFragment(); 50 } 51 52 @Override 53 public void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 } 56 57 @Override 58 public View onCreateView(LayoutInflater inflater, ViewGroup container, 59 Bundle savedInstanceState) { 60 final View view = inflateTemplatedView(inflater, container, 61 R.layout.account_setup_names_fragment, R.string.account_setup_names_headline); 62 mDescription = UiUtilities.getView(view, R.id.account_description); 63 mName = UiUtilities.getView(view, R.id.account_name); 64 mAccountNameLabel = UiUtilities.getView(view, R.id.account_name_label); 65 mName.setKeyListener(TextKeyListener.getInstance(false, TextKeyListener.Capitalize.WORDS)); 66 67 setPreviousButtonVisibility(View.INVISIBLE); 68 69 return view; 70 } 71 72 @Override 73 public void onActivityCreated(Bundle savedInstanceState) { 74 super.onActivityCreated(savedInstanceState); 75 76 // Make sure the layout is inflated before twiddling with it 77 getView(); 78 79 final SetupDataFragment setupData = 80 ((SetupDataFragment.SetupDataContainer) getActivity()).getSetupData(); 81 final int flowMode = setupData.getFlowMode(); 82 83 final Account account = setupData.getAccount(); 84 85 if (flowMode != AuthenticatorSetupIntentHelper.FLOW_MODE_FORCE_CREATE 86 && flowMode != AuthenticatorSetupIntentHelper.FLOW_MODE_EDIT) { 87 final String accountEmail = account.mEmailAddress; 88 mDescription.setText(accountEmail); 89 90 // Move cursor to the end so it's easier to erase in case the user doesn't like it. 91 mDescription.setSelection(accountEmail.length()); 92 } 93 94 // Remember whether we're an EAS account, since it doesn't require the user name field 95 final EmailServiceUtils.EmailServiceInfo info = 96 setupData.getIncomingServiceInfo(getActivity()); 97 if (!info.usesSmtp) { 98 mName.setVisibility(View.GONE); 99 mAccountNameLabel.setVisibility(View.GONE); 100 } else { 101 if (account.getSenderName() != null) { 102 mName.setText(account.getSenderName()); 103 } else if (flowMode != AuthenticatorSetupIntentHelper.FLOW_MODE_FORCE_CREATE 104 && flowMode != AuthenticatorSetupIntentHelper.FLOW_MODE_EDIT) { 105 // Attempt to prefill the name field from the profile if we don't have it, 106 final Context loaderContext = getActivity().getApplicationContext(); 107 getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() { 108 @Override 109 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 110 final String[] projection = 111 new String[] { ContactsContract.Profile.DISPLAY_NAME }; 112 return new CursorLoader(loaderContext, ContactsContract.Profile.CONTENT_URI, 113 projection, null, null, null); 114 } 115 116 @Override 117 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 118 if (data == null || !TextUtils.isEmpty(mName.getText())) { 119 return; 120 } 121 final String name; 122 if (data.moveToFirst()) { 123 name = data.getString(0); 124 } else { 125 name = ""; 126 } 127 mName.setText(name); 128 } 129 130 @Override 131 public void onLoaderReset(Loader<Cursor> loader) {} 132 }); 133 } 134 } 135 } 136 137 public String getDescription() { 138 return mDescription.getText().toString().trim(); 139 } 140 141 public String getSenderName() { 142 return mName.getText().toString().trim(); 143 } 144 } 145