Home | History | Annotate | Download | only in setup
      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 android.app.Activity;
     20 import android.app.FragmentTransaction;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.view.View;
     24 import android.view.View.OnClickListener;
     25 import android.widget.Button;
     26 
     27 import com.android.email.R;
     28 import com.android.email.activity.ActivityHelper;
     29 import com.android.email.activity.UiUtilities;
     30 import com.android.email.service.EmailServiceUtils;
     31 import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
     32 import com.android.emailcommon.provider.Account;
     33 import com.android.emailcommon.provider.HostAuth;
     34 
     35 /**
     36  * Provides setup flow for IMAP/POP accounts.
     37  *
     38  * Uses AccountSetupIncomingFragment for primary UI.  Uses AccountCheckSettingsFragment to validate
     39  * the settings as entered.  If the account is OK, proceeds to AccountSetupOutgoing.
     40  */
     41 public class AccountSetupIncoming extends AccountSetupActivity
     42         implements AccountSetupIncomingFragment.Callback, OnClickListener {
     43 
     44     /* package */ AccountServerBaseFragment mFragment;
     45     private Button mNextButton;
     46     /* package */ boolean mNextButtonEnabled;
     47     private boolean mStartedAutoDiscovery;
     48     private EmailServiceInfo mServiceInfo;
     49 
     50     // Keys for savedInstanceState
     51     private final static String STATE_STARTED_AUTODISCOVERY =
     52             "AccountSetupExchange.StartedAutoDiscovery";
     53 
     54     // Extras for AccountSetupIncoming intent
     55 
     56     public static void actionIncomingSettings(Activity fromActivity, SetupData setupData) {
     57         final Intent intent = new Intent(fromActivity, AccountSetupIncoming.class);
     58         // Add the additional information to the intent, in case the Email process is killed.
     59         intent.putExtra(SetupData.EXTRA_SETUP_DATA, setupData);
     60         fromActivity.startActivity(intent);
     61     }
     62 
     63     @Override
     64     public void onCreate(Bundle savedInstanceState) {
     65         super.onCreate(savedInstanceState);
     66         ActivityHelper.debugSetWindowFlags(this);
     67 
     68         final HostAuth hostAuth = mSetupData.getAccount().mHostAuthRecv;
     69         mServiceInfo = EmailServiceUtils.getServiceInfo(this, hostAuth.mProtocol);
     70 
     71         setContentView(R.layout.account_setup_incoming);
     72         mFragment = (AccountServerBaseFragment)
     73                 getFragmentManager().findFragmentById(R.id.setup_fragment);
     74 
     75         // Configure fragment
     76         mFragment.setCallback(this);
     77 
     78         mNextButton = UiUtilities.getView(this, R.id.next);
     79         mNextButton.setOnClickListener(this);
     80         UiUtilities.getView(this, R.id.previous).setOnClickListener(this);
     81 
     82         // One-shot to launch autodiscovery at the entry to this activity (but not if it restarts)
     83         if (mServiceInfo.usesAutodiscover) {
     84             mStartedAutoDiscovery = false;
     85             if (savedInstanceState != null) {
     86                 mStartedAutoDiscovery = savedInstanceState.getBoolean(STATE_STARTED_AUTODISCOVERY);
     87             }
     88             if (!mStartedAutoDiscovery) {
     89                 startAutoDiscover();
     90             }
     91         }
     92 
     93         // If we've got a default prefix for this protocol, use it
     94         final String prefix = mServiceInfo.inferPrefix;
     95         if (prefix != null && !hostAuth.mAddress.startsWith(prefix + ".")) {
     96             hostAuth.mAddress = prefix + "." + hostAuth.mAddress;
     97         }
     98     }
     99 
    100     /**
    101      * Implements View.OnClickListener
    102      */
    103     @Override
    104     public void onClick(View view) {
    105         switch (view.getId()) {
    106             case R.id.next:
    107                 mFragment.onNext();
    108                 break;
    109             case R.id.previous:
    110                 onBackPressed();
    111                 break;
    112         }
    113     }
    114 
    115     @Override
    116     public void onSaveInstanceState(Bundle outState) {
    117         super.onSaveInstanceState(outState);
    118         outState.putBoolean(STATE_STARTED_AUTODISCOVERY, mStartedAutoDiscovery);
    119     }
    120 
    121     /**
    122      * If the conditions are right, launch the autodiscover fragment.  If it succeeds (even
    123      * partially) it will prefill the setup fields and we can proceed as if the user entered them.
    124      *
    125      * Conditions for skipping:
    126      *  Editing existing account
    127      *  AutoDiscover blocked (used for unit testing only)
    128      *  Username or password not entered yet
    129      */
    130     private void startAutoDiscover() {
    131         // Note that we've started autodiscovery - even if we decide not to do it,
    132         // this prevents repeating.
    133         mStartedAutoDiscovery = true;
    134 
    135         if (!mSetupData.isAllowAutodiscover()) {
    136             return;
    137         }
    138 
    139         final Account account = mSetupData.getAccount();
    140         // If we've got a username and password and we're NOT editing, try autodiscover
    141         final String username = account.mHostAuthRecv.mLogin;
    142         final String password = account.mHostAuthRecv.mPassword;
    143         if (username != null && password != null) {
    144             onProceedNext(SetupData.CHECK_AUTODISCOVER, mFragment);
    145         }
    146     }
    147 
    148     public void onAutoDiscoverComplete(int result, SetupData setupData) {
    149         // If authentication failed, exit immediately (to re-enter credentials)
    150         mSetupData = setupData;
    151         if (result == AccountCheckSettingsFragment.AUTODISCOVER_AUTHENTICATION) {
    152             finish();
    153             return;
    154         }
    155 
    156         // If data was returned, proceed to next screen
    157         if (result == AccountCheckSettingsFragment.AUTODISCOVER_OK) {
    158             mFragment.onNext();
    159         }
    160         // Otherwise, proceed into this activity for manual setup
    161     }
    162 
    163     /**
    164      * Implements AccountServerBaseFragment.Callback
    165      *
    166      * Launches the account checker.  Positive results are reported to onCheckSettingsOk().
    167      */
    168     @Override
    169     public void onProceedNext(int checkMode, AccountServerBaseFragment target) {
    170         AccountCheckSettingsFragment checkerFragment =
    171             AccountCheckSettingsFragment.newInstance(checkMode, target);
    172         FragmentTransaction transaction = getFragmentManager().beginTransaction();
    173         transaction.add(checkerFragment, AccountCheckSettingsFragment.TAG);
    174         transaction.addToBackStack("back");
    175         transaction.commit();
    176     }
    177 
    178     /**
    179      * Implements AccountServerBaseFragment.Callback
    180      */
    181     @Override
    182     public void onEnableProceedButtons(boolean enable) {
    183         mNextButtonEnabled = enable;
    184         mNextButton.setEnabled(enable);
    185     }
    186 
    187     /**
    188      * Implements AccountServerBaseFragment.Callback
    189      *
    190      * If the checked settings are OK, proceed to outgoing settings screen
    191      */
    192     @Override
    193     public void onCheckSettingsComplete(int result, SetupData setupData) {
    194         mSetupData = setupData;
    195         if (result == AccountCheckSettingsFragment.CHECK_SETTINGS_OK) {
    196             if (mServiceInfo.usesSmtp) {
    197                 AccountSetupOutgoing.actionOutgoingSettings(this, mSetupData);
    198             } else {
    199                 AccountSetupOptions.actionOptions(this, mSetupData);
    200                 finish();
    201             }
    202         }
    203     }
    204 }
    205