Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2010 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.Activity;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.text.Editable;
     23 import android.text.TextUtils;
     24 import android.text.TextWatcher;
     25 import android.text.method.DigitsKeyListener;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.AdapterView;
     30 import android.widget.ArrayAdapter;
     31 import android.widget.CheckBox;
     32 import android.widget.CompoundButton;
     33 import android.widget.CompoundButton.OnCheckedChangeListener;
     34 import android.widget.EditText;
     35 import android.widget.Spinner;
     36 
     37 import com.android.email.R;
     38 import com.android.email.activity.UiUtilities;
     39 import com.android.email.provider.AccountBackupRestore;
     40 import com.android.email2.ui.MailActivityEmail;
     41 import com.android.emailcommon.Logging;
     42 import com.android.emailcommon.provider.Account;
     43 import com.android.emailcommon.provider.HostAuth;
     44 import com.android.emailcommon.utility.Utility;
     45 import com.android.mail.utils.LogUtils;
     46 
     47 /**
     48  * Provides UI for SMTP account settings (for IMAP/POP accounts).
     49  *
     50  * This fragment is used by AccountSetupOutgoing (for creating accounts) and by AccountSettingsXL
     51  * (for editing existing accounts).
     52  */
     53 public class AccountSetupOutgoingFragment extends AccountServerBaseFragment
     54         implements OnCheckedChangeListener {
     55 
     56     private final static String STATE_KEY_LOADED = "AccountSetupOutgoingFragment.loaded";
     57 
     58     private static final int SMTP_PORT_NORMAL = 587;
     59     private static final int SMTP_PORT_SSL    = 465;
     60 
     61     private EditText mUsernameView;
     62     private EditText mPasswordView;
     63     private EditText mServerView;
     64     private EditText mPortView;
     65     private CheckBox mRequireLoginView;
     66     private Spinner mSecurityTypeView;
     67 
     68     // Support for lifecycle
     69     private boolean mStarted;
     70     private boolean mLoaded;
     71 
     72     // Public no-args constructor needed for fragment re-instantiation
     73     public AccountSetupOutgoingFragment() {}
     74 
     75     /**
     76      * Called to do initial creation of a fragment.  This is called after
     77      * {@link #onAttach(Activity)} and before {@link #onActivityCreated(Bundle)}.
     78      */
     79     @Override
     80     public void onCreate(Bundle savedInstanceState) {
     81         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
     82             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onCreate");
     83         }
     84         super.onCreate(savedInstanceState);
     85 
     86         if (savedInstanceState != null) {
     87             mLoaded = savedInstanceState.getBoolean(STATE_KEY_LOADED, false);
     88         }
     89         mBaseScheme = HostAuth.LEGACY_SCHEME_SMTP;
     90     }
     91 
     92     @Override
     93     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     94             Bundle savedInstanceState) {
     95         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
     96             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onCreateView");
     97         }
     98         final int layoutId = mSettingsMode
     99                 ? R.layout.account_settings_outgoing_fragment
    100                 : R.layout.account_setup_outgoing_fragment;
    101 
    102         final View view = inflater.inflate(layoutId, container, false);
    103         final Context context = getActivity();
    104 
    105         mUsernameView = UiUtilities.getView(view, R.id.account_username);
    106         mPasswordView = UiUtilities.getView(view, R.id.account_password);
    107         mServerView = UiUtilities.getView(view, R.id.account_server);
    108         mPortView = UiUtilities.getView(view, R.id.account_port);
    109         mRequireLoginView = UiUtilities.getView(view, R.id.account_require_login);
    110         mSecurityTypeView = UiUtilities.getView(view, R.id.account_security_type);
    111         mRequireLoginView.setOnCheckedChangeListener(this);
    112 
    113         // Note:  Strings are shared with AccountSetupIncomingFragment
    114         final SpinnerOption securityTypes[] = {
    115             new SpinnerOption(HostAuth.FLAG_NONE, context.getString(
    116                     R.string.account_setup_incoming_security_none_label)),
    117             new SpinnerOption(HostAuth.FLAG_SSL, context.getString(
    118                     R.string.account_setup_incoming_security_ssl_label)),
    119             new SpinnerOption(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, context.getString(
    120                     R.string.account_setup_incoming_security_ssl_trust_certificates_label)),
    121             new SpinnerOption(HostAuth.FLAG_TLS, context.getString(
    122                     R.string.account_setup_incoming_security_tls_label)),
    123             new SpinnerOption(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, context.getString(
    124                     R.string.account_setup_incoming_security_tls_trust_certificates_label)),
    125         };
    126 
    127         final ArrayAdapter<SpinnerOption> securityTypesAdapter =
    128                 new ArrayAdapter<SpinnerOption>(context, android.R.layout.simple_spinner_item,
    129                         securityTypes);
    130         securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    131         mSecurityTypeView.setAdapter(securityTypesAdapter);
    132 
    133         // Updates the port when the user changes the security type. This allows
    134         // us to show a reasonable default which the user can change.
    135         mSecurityTypeView.post(new Runnable() {
    136             @Override
    137             public void run() {
    138                 mSecurityTypeView.setOnItemSelectedListener(
    139                         new AdapterView.OnItemSelectedListener() {
    140                             @Override
    141                             public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    142                                     long arg3) {
    143                                 updatePortFromSecurityType();
    144                             }
    145 
    146                             @Override
    147                             public void onNothingSelected(AdapterView<?> arg0) {
    148                             }
    149                         });
    150             }});
    151 
    152         // Calls validateFields() which enables or disables the Next button
    153         final TextWatcher validationTextWatcher = new TextWatcher() {
    154             @Override
    155             public void afterTextChanged(Editable s) {
    156                 validateFields();
    157             }
    158 
    159             @Override
    160             public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    161             @Override
    162             public void onTextChanged(CharSequence s, int start, int before, int count) { }
    163         };
    164         mUsernameView.addTextChangedListener(validationTextWatcher);
    165         mPasswordView.addTextChangedListener(validationTextWatcher);
    166         mServerView.addTextChangedListener(validationTextWatcher);
    167         mPortView.addTextChangedListener(validationTextWatcher);
    168 
    169         // Only allow digits in the port field.
    170         mPortView.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
    171 
    172         // Additional setup only used while in "settings" mode
    173         onCreateViewSettingsMode(view);
    174 
    175         return view;
    176     }
    177 
    178     @Override
    179     public void onActivityCreated(Bundle savedInstanceState) {
    180         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    181             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onActivityCreated");
    182         }
    183         super.onActivityCreated(savedInstanceState);
    184     }
    185 
    186     /**
    187      * Called when the Fragment is visible to the user.
    188      */
    189     @Override
    190     public void onStart() {
    191         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    192             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onStart");
    193         }
    194         super.onStart();
    195         mStarted = true;
    196         loadSettings();
    197     }
    198 
    199     /**
    200      * Called when the fragment is visible to the user and actively running.
    201      */
    202     @Override
    203     public void onResume() {
    204         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    205             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onResume");
    206         }
    207         super.onResume();
    208         validateFields();
    209     }
    210 
    211     @Override
    212     public void onPause() {
    213         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    214             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onPause");
    215         }
    216         super.onPause();
    217     }
    218 
    219     /**
    220      * Called when the Fragment is no longer started.
    221      */
    222     @Override
    223     public void onStop() {
    224         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    225             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onStop");
    226         }
    227         super.onStop();
    228         mStarted = false;
    229     }
    230 
    231     /**
    232      * Called when the fragment is no longer in use.
    233      */
    234     @Override
    235     public void onDestroy() {
    236         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    237             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onDestroy");
    238         }
    239         super.onDestroy();
    240     }
    241 
    242     @Override
    243     public void onSaveInstanceState(Bundle outState) {
    244         if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
    245             LogUtils.d(Logging.LOG_TAG, "AccountSetupOutgoingFragment onSaveInstanceState");
    246         }
    247         super.onSaveInstanceState(outState);
    248 
    249         outState.putBoolean(STATE_KEY_LOADED, mLoaded);
    250     }
    251 
    252     /**
    253      * Activity provides callbacks here.  This also triggers loading and setting up the UX
    254      */
    255     @Override
    256     public void setCallback(Callback callback) {
    257         super.setCallback(callback);
    258         if (mStarted) {
    259             loadSettings();
    260         }
    261     }
    262 
    263     /**
    264      * Load the current settings into the UI
    265      */
    266     private void loadSettings() {
    267         if (mLoaded) return;
    268 
    269         final HostAuth sendAuth = mSetupData.getAccount().getOrCreateHostAuthSend(mContext);
    270         if ((sendAuth.mFlags & HostAuth.FLAG_AUTHENTICATE) != 0) {
    271             final String username = sendAuth.mLogin;
    272             if (username != null) {
    273                 mUsernameView.setText(username);
    274                 mRequireLoginView.setChecked(true);
    275             }
    276 
    277             final String password = sendAuth.mPassword;
    278             if (password != null) {
    279                 mPasswordView.setText(password);
    280             }
    281         }
    282 
    283         final int flags = sendAuth.mFlags & ~HostAuth.FLAG_AUTHENTICATE;
    284         SpinnerOption.setSpinnerOptionValue(mSecurityTypeView, flags);
    285 
    286         final String hostname = sendAuth.mAddress;
    287         if (hostname != null) {
    288             mServerView.setText(hostname);
    289         }
    290 
    291         final int port = sendAuth.mPort;
    292         if (port != -1) {
    293             mPortView.setText(Integer.toString(port));
    294         } else {
    295             updatePortFromSecurityType();
    296         }
    297 
    298         mLoadedSendAuth = sendAuth;
    299         mLoaded = true;
    300         validateFields();
    301     }
    302 
    303     /**
    304      * Preflight the values in the fields and decide if it makes sense to enable the "next" button
    305      */
    306     private void validateFields() {
    307         if (!mLoaded) return;
    308         boolean enabled =
    309             Utility.isServerNameValid(mServerView) && Utility.isPortFieldValid(mPortView);
    310 
    311         if (enabled && mRequireLoginView.isChecked()) {
    312             enabled = !TextUtils.isEmpty(mUsernameView.getText())
    313                     && !TextUtils.isEmpty(mPasswordView.getText());
    314         }
    315         enableNextButton(enabled);
    316         // Warn (but don't prevent) if password has leading/trailing spaces
    317         AccountSettingsUtils.checkPasswordSpaces(mContext, mPasswordView);
    318    }
    319 
    320     /**
    321      * implements OnCheckedChangeListener
    322      */
    323     @Override
    324     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    325         final int visibility = isChecked ? View.VISIBLE : View.GONE;
    326         UiUtilities.setVisibilitySafe(getView(), R.id.account_require_login_settings, visibility);
    327         UiUtilities.setVisibilitySafe(getView(), R.id.account_require_login_settings_2, visibility);
    328         validateFields();
    329     }
    330 
    331     private int getPortFromSecurityType() {
    332         final int securityType =
    333                 (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value;
    334         return (securityType & HostAuth.FLAG_SSL) != 0 ? SMTP_PORT_SSL : SMTP_PORT_NORMAL;
    335     }
    336 
    337     private void updatePortFromSecurityType() {
    338         final int port = getPortFromSecurityType();
    339         mPortView.setText(Integer.toString(port));
    340     }
    341 
    342     /**
    343      * Entry point from Activity after editing settings and verifying them.  Must be FLOW_MODE_EDIT.
    344      * Blocking - do not call from UI Thread.
    345      */
    346     @Override
    347     public void saveSettingsAfterEdit() {
    348         final Account account = mSetupData.getAccount();
    349         account.mHostAuthSend.update(mContext, account.mHostAuthSend.toContentValues());
    350         // Update the backup (side copy) of the accounts
    351         AccountBackupRestore.backup(mContext);
    352     }
    353 
    354     /**
    355      * Entry point from Activity after entering new settings and verifying them.  For setup mode.
    356      */
    357     @Override
    358     public void saveSettingsAfterSetup() {
    359     }
    360 
    361     /**
    362      * Entry point from Activity, when "next" button is clicked
    363      */
    364     @Override
    365     public void onNext() {
    366         final Account account = mSetupData.getAccount();
    367         final HostAuth sendAuth = account.getOrCreateHostAuthSend(mContext);
    368 
    369         if (mRequireLoginView.isChecked()) {
    370             final String userName = mUsernameView.getText().toString().trim();
    371             final String userPassword = mPasswordView.getText().toString();
    372             sendAuth.setLogin(userName, userPassword);
    373         } else {
    374             sendAuth.setLogin(null, null);
    375         }
    376 
    377         final String serverAddress = mServerView.getText().toString().trim();
    378         int serverPort;
    379         try {
    380             serverPort = Integer.parseInt(mPortView.getText().toString().trim());
    381         } catch (NumberFormatException e) {
    382             serverPort = getPortFromSecurityType();
    383             LogUtils.d(Logging.LOG_TAG, "Non-integer server port; using '" + serverPort + "'");
    384         }
    385         final int securityType =
    386                 (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value;
    387         sendAuth.setConnection(mBaseScheme, serverAddress, serverPort, securityType);
    388         sendAuth.mDomain = null;
    389 
    390         mCallback.onProceedNext(SetupData.CHECK_OUTGOING, this);
    391         clearButtonBounce();
    392     }
    393 }
    394