Cleanup various IMAP/POP manual account setup defaults

These defaults affect manual setup only.  There should be no changes
observed in automatic setup, and no changes observed in EAS setup.

* user $email instead of $user as default login
* guess "imap." or "pop3." for server name
* propagate the incoming server name to the outgoing server name, and
  replace "imap.", "pop3." or "pop." with "smtp."

Also, fixed a couple of leftover places where we were trimming
passwords (and should not be, since some people insist on having
spaces in their passwords.)

Bug: 2978634
Change-Id: I9b0e345aa9550b5e1cc29aaa22109f03da61af20
This commit is contained in:
Andy Stadler 2010-11-03 09:31:45 -07:00
parent c50f912676
commit ba4e72a947
5 changed files with 119 additions and 24 deletions

View File

@ -159,4 +159,49 @@ public class AccountSettingsUtils {
public String outgoingUsernameTemplate;
public String note;
}
/**
* Infer potential email server addresses from domain names
*
* Incoming: Prepend "imap" or "pop3" to domain, unless "pop", "pop3",
* "imap", or "mail" are found.
* Outgoing: Prepend "smtp" if "pop", "pop3", "imap" are found.
* Leave "mail" as-is.
* TBD: Are there any useful defaults for exchange?
*
* @param server name as we know it so far
* @param incoming "pop3" or "imap" (or null)
* @param outgoing "smtp" or null
* @return the post-processed name for use in the UI
*/
public static String inferServerName(String server, String incoming, String outgoing) {
// Default values cause entire string to be kept, with prepended server string
int keepFirstChar = 0;
int firstDotIndex = server.indexOf('.');
if (firstDotIndex != -1) {
// look at first word and decide what to do
String firstWord = server.substring(0, firstDotIndex).toLowerCase();
boolean isImapOrPop = "imap".equals(firstWord)
|| "pop3".equals(firstWord) || "pop".equals(firstWord);
boolean isMail = "mail".equals(firstWord);
// Now decide what to do
if (incoming != null) {
// For incoming, we leave imap/pop/pop3/mail alone, or prepend incoming
if (isImapOrPop || isMail) {
return server;
}
} else {
// For outgoing, replace imap/pop/pop3 with outgoing, leave mail alone, or
// prepend outgoing
if (isImapOrPop) {
keepFirstChar = firstDotIndex + 1;
} else if (isMail) {
return server;
} else {
// prepend
}
}
}
return ((incoming != null) ? incoming : outgoing) + '.' + server.substring(keepFirstChar);
}
}

View File

@ -22,6 +22,7 @@ import com.android.email.activity.ActivityHelper;
import com.android.email.mail.Store;
import com.android.email.provider.EmailContent;
import com.android.email.provider.EmailContent.Account;
import com.android.email.provider.EmailContent.HostAuth;
import android.app.Activity;
import android.content.Intent;
@ -77,18 +78,16 @@ public class AccountSetupAccountType extends AccountSetupActivity implements OnC
// TODO: Dynamic creation of buttons, instead of just hiding things we don't need
}
/**
* For POP accounts, we rewrite the username to the full user@domain, and we set the
* default server name to pop3.domain
*/
private void onPop() {
Account account = SetupData.getAccount();
try {
URI uri = new URI(account.getStoreUri(this));
uri = new URI("pop3", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
account.setStoreUri(this, uri.toString());
} catch (URISyntaxException use) {
/*
* This should not happen.
*/
throw new Error(use);
}
HostAuth hostAuth = account.mHostAuthRecv;
hostAuth.mProtocol = "pop3";
hostAuth.mLogin = hostAuth.mLogin + "@" + hostAuth.mAddress;
hostAuth.mAddress = AccountSettingsUtils.inferServerName(hostAuth.mAddress, "pop3", null);
SetupData.setCheckSettingsMode(SetupData.CHECK_INCOMING | SetupData.CHECK_OUTGOING);
AccountSetupIncoming.actionIncomingSettings(this, SetupData.getFlowMode(), account);
finish();
@ -100,16 +99,10 @@ public class AccountSetupAccountType extends AccountSetupActivity implements OnC
*/
private void onImap() {
Account account = SetupData.getAccount();
try {
URI uri = new URI(account.getStoreUri(this));
uri = new URI("imap", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
account.setStoreUri(this, uri.toString());
} catch (URISyntaxException use) {
/*
* This should not happen.
*/
throw new Error(use);
}
HostAuth hostAuth = account.mHostAuthRecv;
hostAuth.mProtocol = "imap";
hostAuth.mLogin = hostAuth.mLogin + "@" + hostAuth.mAddress;
hostAuth.mAddress = AccountSettingsUtils.inferServerName(hostAuth.mAddress, "imap", null);
// Delete policy must be set explicitly, because IMAP does not provide a UI selection
// for it. This logic needs to be followed in the auto setup flow as well.
account.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE);

View File

@ -411,7 +411,7 @@ public class AccountSetupBasicsFragment extends Fragment implements TextWatcher
*/
public void onManualSetup(boolean allowAutoDiscover) {
String email = mEmailView.getText().toString().trim();
String password = mPasswordView.getText().toString().trim();
String password = mPasswordView.getText().toString();
String[] emailParts = email.split("@");
String user = emailParts[0].trim();
String domain = emailParts[1].trim();

View File

@ -391,14 +391,17 @@ public class AccountSetupIncomingFragment extends AccountServerBaseFragment {
EmailContent.Account account = SetupData.getAccount();
// Set the username and password for the outgoing settings to the username and
// password the user just set for incoming.
// password the user just set for incoming. Use the verified host address to try and
// pick a smarter outgoing address.
try {
String hostName =
AccountSettingsUtils.inferServerName(account.mHostAuthRecv.mAddress, null, "smtp");
URI oldUri = new URI(account.getSenderUri(mContext));
URI uri = new URI(
oldUri.getScheme(),
mUsernameView.getText().toString().trim() + ":"
+ mPasswordView.getText().toString().trim(),
oldUri.getHost(),
+ mPasswordView.getText().toString(),
hostName,
oldUri.getPort(),
null,
null,

View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email.activity.setup;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
/**
* This is a series of unit tests for the AccountSettingsUtils class.
*
* To run these tests,
* runtest -c com.android.email.activity.setup.AccountSettingsUtilsTests email
*/
@SmallTest
public class AccountSettingsUtilsTests extends AndroidTestCase {
/**
* Test server name inferences
*
* Incoming: Prepend "imap" or "pop3" to domain, unless "pop", "pop3",
* "imap", or "mail" are found.
* Outgoing: Prepend "smtp" if "pop", "pop3", "imap" are found.
* Leave "mail" as-is.
* TBD: Are there any useful defaults for exchange?
*/
public void testGuessServerName() {
assertEquals("foo.x.y.z", AccountSettingsUtils.inferServerName("x.y.z", "foo", null));
assertEquals("Pop.y.z", AccountSettingsUtils.inferServerName("Pop.y.z", "foo", null));
assertEquals("poP3.y.z", AccountSettingsUtils.inferServerName("poP3.y.z", "foo", null));
assertEquals("iMAp.y.z", AccountSettingsUtils.inferServerName("iMAp.y.z", "foo", null));
assertEquals("MaiL.y.z", AccountSettingsUtils.inferServerName("MaiL.y.z", "foo", null));
assertEquals("bar.x.y.z", AccountSettingsUtils.inferServerName("x.y.z", null, "bar"));
assertEquals("bar.y.z", AccountSettingsUtils.inferServerName("Pop.y.z", null, "bar"));
assertEquals("bar.y.z", AccountSettingsUtils.inferServerName("poP3.y.z", null, "bar"));
assertEquals("bar.y.z", AccountSettingsUtils.inferServerName("iMAp.y.z", null, "bar"));
assertEquals("MaiL.y.z", AccountSettingsUtils.inferServerName("MaiL.y.z", null, "bar"));
}
}