Home | History | Annotate | Download | only in setup
      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.Editable;
     27 import android.text.TextUtils;
     28 import android.text.TextWatcher;
     29 import android.text.method.TextKeyListener;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.widget.EditText;
     34 
     35 import com.android.email.R;
     36 import com.android.email.activity.UiUtilities;
     37 import com.android.email.service.EmailServiceUtils;
     38 import com.android.emailcommon.provider.Account;
     39 
     40 public class AccountSetupNamesFragment extends AccountSetupFragment {
     41     private EditText mDescription;
     42     private EditText mName;
     43     private View mAccountNameLabel;
     44     private boolean mRequiresName = true;
     45 
     46     public interface Callback extends AccountSetupFragment.Callback {
     47 
     48     }
     49 
     50     public static AccountSetupNamesFragment newInstance() {
     51         return new AccountSetupNamesFragment();
     52     }
     53 
     54     @Override
     55     public void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57     }
     58 
     59     @Override
     60     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     61             Bundle savedInstanceState) {
     62         final View view = inflateTemplatedView(inflater, container,
     63                 R.layout.account_setup_names_fragment, R.string.account_setup_names_headline);
     64         mDescription = UiUtilities.getView(view, R.id.account_description);
     65         mName = UiUtilities.getView(view, R.id.account_name);
     66         mAccountNameLabel = UiUtilities.getView(view, R.id.account_name_label);
     67 
     68         final TextWatcher validationTextWatcher = new TextWatcher() {
     69             @Override
     70             public void afterTextChanged(Editable s) {
     71                 validateFields();
     72             }
     73 
     74             @Override
     75             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     76             }
     77 
     78             @Override
     79             public void onTextChanged(CharSequence s, int start, int before, int count) {
     80             }
     81         };
     82         mName.addTextChangedListener(validationTextWatcher);
     83         mName.setKeyListener(TextKeyListener.getInstance(false, TextKeyListener.Capitalize.WORDS));
     84 
     85         setPreviousButtonVisibility(View.INVISIBLE);
     86 
     87         return view;
     88     }
     89 
     90     @Override
     91     public void onActivityCreated(Bundle savedInstanceState) {
     92         super.onActivityCreated(savedInstanceState);
     93 
     94         // Make sure the layout is inflated before twiddling with it
     95         getView();
     96 
     97         final SetupDataFragment setupData =
     98                 ((SetupDataFragment.SetupDataContainer) getActivity()).getSetupData();
     99         final int flowMode = setupData.getFlowMode();
    100 
    101         final Account account = setupData.getAccount();
    102 
    103         if (flowMode != SetupDataFragment.FLOW_MODE_FORCE_CREATE
    104                 && flowMode != SetupDataFragment.FLOW_MODE_EDIT) {
    105             final String accountEmail = account.mEmailAddress;
    106             mDescription.setText(accountEmail);
    107 
    108             // Move cursor to the end so it's easier to erase in case the user doesn't like it.
    109             mDescription.setSelection(accountEmail.length());
    110         }
    111 
    112         // Remember whether we're an EAS account, since it doesn't require the user name field
    113         final EmailServiceUtils.EmailServiceInfo info =
    114                 setupData.getIncomingServiceInfo(getActivity());
    115         if (!info.usesSmtp) {
    116             mRequiresName = false;
    117             mName.setVisibility(View.GONE);
    118             mAccountNameLabel.setVisibility(View.GONE);
    119         } else {
    120             if (account.getSenderName() != null) {
    121                 mName.setText(account.getSenderName());
    122             } else if (flowMode != SetupDataFragment.FLOW_MODE_FORCE_CREATE
    123                     && flowMode != SetupDataFragment.FLOW_MODE_EDIT) {
    124                 // Attempt to prefill the name field from the profile if we don't have it,
    125                 final Context loaderContext = getActivity().getApplicationContext();
    126                 getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
    127                     @Override
    128                     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    129                         final String[] projection =
    130                                 new String[] { ContactsContract.Profile.DISPLAY_NAME };
    131                         return new CursorLoader(loaderContext, ContactsContract.Profile.CONTENT_URI,
    132                                 projection, null, null, null);
    133                     }
    134 
    135                     @Override
    136                     public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    137                         if (data == null || !TextUtils.isEmpty(mName.getText())) {
    138                             return;
    139                         }
    140                         final String name;
    141                         if (data.moveToFirst()) {
    142                             name = data.getString(0);
    143                         } else {
    144                             name = "";
    145                         }
    146                         mName.setText(name);
    147                     }
    148 
    149                     @Override
    150                     public void onLoaderReset(Loader<Cursor> loader) {}
    151                 });
    152             }
    153         }
    154 
    155         // Make sure the "done" button is in the proper state
    156         validateFields();
    157     }
    158 
    159     /**
    160      * Check input fields for legal values and enable/disable next button
    161      */
    162     private void validateFields() {
    163         boolean enableNextButton = true;
    164         // Validation is based only on the "user name" field, not shown for EAS accounts
    165         if (mRequiresName) {
    166             final String userName = mName.getText().toString().trim();
    167             if (TextUtils.isEmpty(userName)) {
    168                 enableNextButton = false;
    169                 mName.setError(getString(R.string.account_setup_names_user_name_empty_error));
    170             } else {
    171                 mName.setError(null);
    172             }
    173         }
    174         setNextButtonEnabled(enableNextButton);
    175     }
    176 
    177     public String getDescription() {
    178         return mDescription.getText().toString().trim();
    179     }
    180 
    181     public String getSenderName() {
    182         return mName.getText().toString().trim();
    183     }
    184 }
    185