AccountSetupExchange.java revision 112ed496f817ebeab6b1ee1d5117259ef80342b2
1/* 2 * Copyright (C) 2009 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 com.android.email.R; 20import com.android.email.activity.ActivityHelper; 21import com.android.email.provider.EmailContent.Account; 22import com.android.email.provider.EmailContent.HostAuth; 23 24import android.app.Activity; 25import android.app.FragmentTransaction; 26import android.content.Intent; 27import android.os.Bundle; 28import android.view.View; 29import android.view.View.OnClickListener; 30import android.widget.Button; 31 32/** 33 * Provides generic setup for Exchange accounts. The following fields are supported: 34 * 35 * Email Address (from previous setup screen) 36 * Server 37 * Domain 38 * Requires SSL? 39 * User (login) 40 * Password 41 * 42 * There are two primary paths through this activity: 43 * Edit existing: 44 * Load existing values from account into fields 45 * When user clicks 'next': 46 * Confirm not a duplicate account 47 * Try new values (check settings) 48 * If new values are OK: 49 * Write new values (save to provider) 50 * finish() (pop to previous) 51 * 52 * Creating New: 53 * Try Auto-discover to get details from server 54 * If Auto-discover reports an authentication failure: 55 * finish() (pop to previous, to re-enter username & password) 56 * If Auto-discover succeeds: 57 * write server's account details into account 58 * Load values from account into fields 59 * Confirm not a duplicate account 60 * Try new values (check settings) 61 * If new values are OK: 62 * Write new values (save to provider) 63 * Proceed to options screen 64 * finish() (removes self from back stack) 65 */ 66public class AccountSetupExchange extends AccountSetupActivity 67 implements AccountSetupExchangeFragment.Callback, OnClickListener { 68 69 // Keys for savedInstanceState 70 private final static String STATE_STARTED_AUTODISCOVERY = 71 "AccountSetupExchange.StartedAutoDiscovery"; 72 73 boolean mStartedAutoDiscovery; 74 /* package */ AccountSetupExchangeFragment mFragment; 75 private Button mNextButton; 76 /* package */ boolean mNextButtonEnabled; 77 78 public static void actionIncomingSettings(Activity fromActivity, int mode, Account account) { 79 SetupData.setFlowMode(mode); 80 SetupData.setAccount(account); 81 fromActivity.startActivity(new Intent(fromActivity, AccountSetupExchange.class)); 82 } 83 84 @Override 85 public void onCreate(Bundle savedInstanceState) { 86 super.onCreate(savedInstanceState); 87 ActivityHelper.debugSetWindowFlags(this); 88 setContentView(R.layout.account_setup_exchange); 89 90 mFragment = (AccountSetupExchangeFragment) 91 getFragmentManager().findFragmentById(R.id.setup_fragment); 92 mFragment.setCallback(this); 93 94 mNextButton = (Button) findViewById(R.id.next); 95 mNextButton.setOnClickListener(this); 96 findViewById(R.id.previous).setOnClickListener(this); 97 98 // One-shot to launch autodiscovery at the entry to this activity (but not if it restarts) 99 mStartedAutoDiscovery = false; 100 if (savedInstanceState != null) { 101 mStartedAutoDiscovery = savedInstanceState.getBoolean(STATE_STARTED_AUTODISCOVERY); 102 } 103 if (!mStartedAutoDiscovery) { 104 startAutoDiscover(); 105 } 106 } 107 108 /** 109 * Implements View.OnClickListener 110 */ 111 @Override 112 public void onClick(View view) { 113 switch (view.getId()) { 114 case R.id.next: 115 mFragment.onNext(); 116 break; 117 case R.id.previous: 118 onBackPressed(); 119 break; 120 } 121 } 122 123 @Override 124 public void onSaveInstanceState(Bundle outState) { 125 super.onSaveInstanceState(outState); 126 outState.putBoolean(STATE_STARTED_AUTODISCOVERY, mStartedAutoDiscovery); 127 } 128 129 /** 130 * If the conditions are right, launch the autodiscover fragment. If it succeeds (even 131 * partially) it will prefill the setup fields and we can proceed as if the user entered them. 132 * 133 * Conditions for skipping: 134 * Editing existing account 135 * AutoDiscover blocked (used for unit testing only) 136 * Username or password not entered yet 137 */ 138 private void startAutoDiscover() { 139 // Note that we've started autodiscovery - even if we decide not to do it, 140 // this prevents repeating. 141 mStartedAutoDiscovery = true; 142 143 if (!SetupData.isAllowAutodiscover()) { 144 return; 145 } 146 147 Account account = SetupData.getAccount(); 148 // If we've got a username and password and we're NOT editing, try autodiscover 149 String username = account.mHostAuthRecv.mLogin; 150 String password = account.mHostAuthRecv.mPassword; 151 if (username != null && password != null) { 152 onProceedNext(SetupData.CHECK_AUTODISCOVER, mFragment); 153 } 154 } 155 156 /** 157 * Implements AccountCheckSettingsFragment.Callbacks 158 * 159 * @param result configuration data returned by AD server, or null if no data available 160 */ 161 public void onAutoDiscoverComplete(int result, HostAuth hostAuth) { 162 // If authentication failed, exit immediately (to re-enter credentials) 163 if (result == AccountCheckSettingsFragment.AUTODISCOVER_AUTHENTICATION) { 164 finish(); 165 return; 166 } 167 168 // If data was returned, populate the account & populate the UI fields and validate it 169 if (result == AccountCheckSettingsFragment.AUTODISCOVER_OK) { 170 boolean valid = mFragment.setHostAuthFromAutodiscover(hostAuth); 171 if (valid) { 172 // "click" next to launch server verification 173 mFragment.onNext(); 174 } 175 } 176 // Otherwise, proceed into this activity for manual setup 177 } 178 179 /** 180 * Implements AccountServerBaseFragment.Callback 181 */ 182 public void onProceedNext(int checkMode, AccountServerBaseFragment target) { 183 AccountCheckSettingsFragment checkerFragment = 184 AccountCheckSettingsFragment.newInstance(checkMode, target); 185 FragmentTransaction transaction = getFragmentManager().openTransaction(); 186 transaction.replace(R.id.setup_fragment, checkerFragment); 187 transaction.addToBackStack("back"); 188 transaction.commit(); 189 } 190 191 /** 192 * Implements AccountServerBaseFragment.Callback 193 */ 194 public void onEnableProceedButtons(boolean enable) { 195 mNextButtonEnabled = enable; 196 mNextButton.setEnabled(enable); 197 } 198 199 /** 200 * Implements AccountServerBaseFragment.Callback 201 * 202 * If the checked settings are OK, proceed to options screen. If the user rejects security, 203 * exit this screen. For all other errors, remain here for editing. 204 */ 205 public void onCheckSettingsComplete(int result, int setupMode) { 206 switch (result) { 207 case AccountCheckSettingsFragment.CHECK_SETTINGS_OK: 208 AccountSetupOptions.actionOptions(this); 209 finish(); 210 break; 211 case AccountCheckSettingsFragment.CHECK_SETTINGS_SECURITY_USER_DENY: 212 finish(); 213 break; 214 default: 215 case AccountCheckSettingsFragment.CHECK_SETTINGS_SERVER_ERROR: 216 // Do nothing - remain in this screen 217 break; 218 } 219 } 220} 221