Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2014 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.os.Bundle;
     20 import android.text.Editable;
     21 import android.text.TextUtils;
     22 import android.text.TextWatcher;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.EditText;
     27 
     28 import com.android.email.R;
     29 import com.android.email.activity.UiUtilities;
     30 import com.android.emailcommon.mail.Address;
     31 
     32 public class AccountSetupBasicsFragment extends AccountSetupFragment {
     33     private EditText mEmailView;
     34     private View mManualSetupView;
     35     private boolean mManualSetup;
     36 
     37     public interface Callback extends AccountSetupFragment.Callback {
     38     }
     39 
     40     public static AccountSetupBasicsFragment newInstance() {
     41         return new AccountSetupBasicsFragment();
     42     }
     43 
     44     public AccountSetupBasicsFragment() {}
     45 
     46     @Override
     47     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     48             Bundle savedInstanceState) {
     49         final View view = inflateTemplatedView(inflater, container,
     50                 R.layout.account_setup_basics_fragment, -1);
     51 
     52         mEmailView = UiUtilities.getView(view, R.id.account_email);
     53         mManualSetupView = UiUtilities.getView(view, R.id.manual_setup);
     54         mManualSetupView.setOnClickListener(this);
     55 
     56         final TextWatcher textWatcher = new TextWatcher() {
     57             @Override
     58             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
     59 
     60             @Override
     61             public void onTextChanged(CharSequence s, int start, int before, int count) {}
     62 
     63             @Override
     64             public void afterTextChanged(Editable s) {
     65                 validateFields();
     66             }
     67         };
     68 
     69         mEmailView.addTextChangedListener(textWatcher);
     70 
     71         setPreviousButtonVisibility(View.GONE);
     72 
     73         setManualSetupButtonVisibility(View.VISIBLE);
     74 
     75         return view;
     76     }
     77 
     78     @Override
     79     public void onViewStateRestored(Bundle savedInstanceState) {
     80         super.onViewStateRestored(savedInstanceState);
     81         validateFields();
     82     }
     83 
     84     @Override
     85     public void onClick(View v) {
     86         final int viewId = v.getId();
     87         final Callback callback = (Callback) getActivity();
     88 
     89         if (viewId == R.id.next) {
     90             // Handle "Next" button here so we can reset the manual setup diversion
     91             mManualSetup = false;
     92             callback.onNextButton();
     93         } else if (viewId == R.id.manual_setup) {
     94             mManualSetup = true;
     95             callback.onNextButton();
     96         } else {
     97             super.onClick(v);
     98         }
     99     }
    100 
    101     private void validateFields() {
    102         final String emailField = getEmail();
    103         final Address[] addresses = Address.parse(emailField);
    104 
    105         final boolean emailValid = !TextUtils.isEmpty(emailField)
    106                 && addresses.length == 1
    107                 && !TextUtils.isEmpty(addresses[0].getAddress());
    108 
    109         setNextButtonEnabled(emailValid);
    110     }
    111 
    112 
    113     /**
    114      * Set visibitlity of the "manual setup" button
    115      * @param visibility {@link View#INVISIBLE}, {@link View#VISIBLE}, {@link View#GONE}
    116      */
    117     public void setManualSetupButtonVisibility(int visibility) {
    118         mManualSetupView.setVisibility(visibility);
    119     }
    120 
    121     @Override
    122     public void setNextButtonEnabled(boolean enabled) {
    123         super.setNextButtonEnabled(enabled);
    124         mManualSetupView.setEnabled(enabled);
    125         final float manualButtonAlpha;
    126         if (enabled) {
    127             manualButtonAlpha =
    128                     getResources().getFraction(R.fraction.manual_setup_enabled_alpha, 1, 1);
    129         } else {
    130             manualButtonAlpha =
    131                     getResources().getFraction(R.fraction.manual_setup_disabled_alpha, 1, 1);
    132         }
    133         mManualSetupView.setAlpha(manualButtonAlpha);
    134     }
    135 
    136     public void setEmail(final String email) {
    137         mEmailView.setText(email);
    138     }
    139 
    140     public String getEmail() {
    141         return mEmailView.getText().toString().trim();
    142     }
    143 
    144     public boolean isManualSetup() {
    145         return mManualSetup;
    146     }
    147 }
    148