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