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