From 854c0e51d420bbc51cddb46b5f77aaf2bf32c6b6 Mon Sep 17 00:00:00 2001 From: dzan Date: Mon, 15 Aug 2011 16:52:30 +0200 Subject: [PATCH] Handle usernames ( part of email or custom ). Small clean-ups. --- res/layout/account_setup_confirm.xml | 20 ++++- .../account_setup_confirm_spinners_item.xml | 7 -- .../setup/AbstractSetupConfirmActivity.java | 88 +++++++++++++++++-- .../setup/AccountSetupConfirmIncoming.java | 31 ++++++- .../configxmlparser/AutoconfigInfo.java | 24 ++++- 5 files changed, 148 insertions(+), 22 deletions(-) delete mode 100644 res/layout/account_setup_confirm_spinners_item.xml diff --git a/res/layout/account_setup_confirm.xml b/res/layout/account_setup_confirm.xml index 8693df528..b808e4189 100644 --- a/res/layout/account_setup_confirm.xml +++ b/res/layout/account_setup_confirm.xml @@ -70,13 +70,29 @@ - + + + + diff --git a/res/layout/account_setup_confirm_spinners_item.xml b/res/layout/account_setup_confirm_spinners_item.xml deleted file mode 100644 index 110a31c75..000000000 --- a/res/layout/account_setup_confirm_spinners_item.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - \ No newline at end of file diff --git a/src/com/fsck/k9/activity/setup/AbstractSetupConfirmActivity.java b/src/com/fsck/k9/activity/setup/AbstractSetupConfirmActivity.java index 531b3d301..b139ca4da 100644 --- a/src/com/fsck/k9/activity/setup/AbstractSetupConfirmActivity.java +++ b/src/com/fsck/k9/activity/setup/AbstractSetupConfirmActivity.java @@ -15,7 +15,9 @@ package com.fsck.k9.activity.setup; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.text.Editable; import android.text.Html; +import android.text.TextWatcher; import android.text.method.LinkMovementMethod; import android.text.method.MovementMethod; import android.view.View; @@ -30,6 +32,8 @@ import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.ServerType; import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.SocketType; import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.Server; import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.InformationBlock; +import org.w3c.dom.Text; + import java.util.List; public abstract class AbstractSetupConfirmActivity extends K9Activity implements View.OnClickListener, OnItemSelectedListener { @@ -39,6 +43,9 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements private static final String EXTRA_EMAIL = "email"; private static final String EXTRA_PASSWORD = "password"; + private final String LOCALPART_EMAIL = "%EMAILLOCALPART%"; + private final String WHOLE_EMAIL = "%EMAILADDRESS%"; + public static void actionConfirmIncoming(Context context, Account account, AutoconfigInfo info) { Intent i = new Intent(context, AccountSetupConfirmIncoming.class); i.putExtra(EXTRA_ACCOUNT, account.getUuid()); @@ -57,9 +64,13 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements // data protected Account mAccount; protected AutoconfigInfo mConfigInfo; + protected String mEmail; + protected String mUsername; + private boolean mCustomUsername = false; + protected String mPassword; // references to current selections ( easier to code with ) - private Server mCurrentServer; + protected Server mCurrentServer; private ServerType mCurrentType; private SocketType mCurrentSocket; private List mCurrentServerList; @@ -72,6 +83,8 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements private TextView mServerCountLabel; private RelativeLayout mServerBrowseButtons; private RelativeLayout mServerDocumentation; + private LinearLayout mUsernameView; + private EditText mUsernameField; private TextView mDocumentationLinks; // difference between incomming & outgoing @@ -91,30 +104,36 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements mOkButton = (Button) findViewById(R.id.confirm_ok_button); mServerCountLabel = (TextView) findViewById(R.id.server_count_label); mDocumentationLinks = (TextView) findViewById(R.id.server_documentation_content); + mUsernameField = (EditText) findViewById(R.id.account_username_field); + mUsernameView = (LinearLayout) findViewById(R.id.account_custom_username); mServerBrowseButtons = (RelativeLayout) findViewById(R.id.confirm_serverbrowse_buttons); mServerDocumentation = (RelativeLayout) findViewById(R.id.confirm_documentation_part); // get the data out of our intent // if no blank account passed make one + mEmail = getIntent().getStringExtra(EXTRA_EMAIL); + mPassword = getIntent().getStringExtra(EXTRA_PASSWORD); + String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT); if(accountUuid != null) mAccount = Preferences.getPreferences(this).getAccount(accountUuid); - else mAccount = Account.getBlankAccount(this, - getIntent().getStringExtra(EXTRA_EMAIL), - getIntent().getStringExtra(EXTRA_PASSWORD)); + else mAccount = Account.getBlankAccount(this, mEmail, mPassword); + mConfigInfo = getIntent().getParcelableExtra(EXTRA_CONFIG_INFO); // attach data to gui elements ArrayAdapter protocolAdapter = new ArrayAdapter(this, - R.layout.account_setup_confirm_spinners_item, getAvailableServerTypes()); + android.R.layout.simple_spinner_item, getAvailableServerTypes()); mProtocolSpinner.setAdapter(protocolAdapter); + protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); List matchingSocketTypeList = mConfigInfo.getAvailableSocketTypes( mConfigInfo.getFilteredServerList(getServers(),protocolAdapter.getItem(0), null, null)); ArrayAdapter socketTypeAdapter = new ArrayAdapter(this, - R.layout.account_setup_confirm_spinners_item, matchingSocketTypeList); + android.R.layout.simple_spinner_item, matchingSocketTypeList); mSocketTypeSpinner.setAdapter(socketTypeAdapter); + socketTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // if there is extra information, display it if( mConfigInfo.hasExtraInfo() ){ @@ -126,6 +145,18 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements mProtocolSpinner.setOnItemSelectedListener(this); mSocketTypeSpinner.setOnItemSelectedListener(this); mOkButton.setOnClickListener(this); + mUsernameField.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + if( mCustomUsername ) + if( ! mUsernameField.getText().toString().isEmpty() ) mOkButton.setEnabled(true); + else mOkButton.setEnabled(false); + } + @Override + public void afterTextChanged(Editable editable) {} + }); } @Override @@ -152,7 +183,9 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements // now we have to reset the options in sockettype spinner List newTypes = mConfigInfo.getAvailableSocketTypes( mConfigInfo.getFilteredServerList(getServers(),(ServerType) mProtocolSpinner.getAdapter().getItem(pos), null, null)); - mSocketTypeSpinner.setAdapter(new ArrayAdapter(this, R.layout.account_setup_confirm_spinners_item, newTypes)); + ArrayAdapter tmpAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, newTypes); + tmpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mSocketTypeSpinner.setAdapter(tmpAdapter); mSocketTypeSpinner.invalidate(); break; case R.id.spinner_sockettype: @@ -192,16 +225,55 @@ public abstract class AbstractSetupConfirmActivity extends K9Activity implements private void setServerInfo(Server mCurrentServer) { - // TODO: string resources + // TODO: use string resources mServerInfoText.setText("Host: "+mCurrentServer.hostname+"\n"+ "Port: "+mCurrentServer.port+"\n"+ "Authentication: "+mCurrentServer.authentication); + + // see if we need a custom username + if( mCurrentServer.username != null ){ + mServerInfoText.setText(mServerInfoText.getText().toString()+"\n"+ + "Username: "+determineUsername(mCurrentServer)); + mOkButton.setEnabled(true); + mCustomUsername = false; + mUsernameView.setVisibility(View.GONE); + }else{ + mOkButton.setEnabled(false); + mCustomUsername = true; + mUsernameView.setVisibility(View.VISIBLE); + } } + + + private String determineUsername(Server mCurrentServer) { + mCustomUsername = false; + if( mCurrentServer.username.equals(LOCALPART_EMAIL)){ + mUsername = mEmail.split("@")[0]; + }else if( mCurrentServer.username.equals(WHOLE_EMAIL)){ + mUsername = mEmail; + }else{ + mUsername = mCurrentServer.username; + } + return mUsername; + } + + private void setServerCount(int count){ mServerCountLabel.setText("( "+count+" / "+mCurrentServerList.size()+" )"); } @Override public void onNothingSelected(AdapterView adapterView) {} + + /* + Assuming the first one is set correctly, no checks are build in + */ + protected String getScheme(){ + String scheme = ""; + scheme = mCurrentType.getSchemeName(); + if( mCurrentSocket != SocketType.plain && mCurrentSocket != SocketType.UNSET ) + scheme += "+"+mCurrentSocket.getSchemeName(); + return scheme; + } } diff --git a/src/com/fsck/k9/activity/setup/AccountSetupConfirmIncoming.java b/src/com/fsck/k9/activity/setup/AccountSetupConfirmIncoming.java index 7696292a6..61eb3b0e2 100644 --- a/src/com/fsck/k9/activity/setup/AccountSetupConfirmIncoming.java +++ b/src/com/fsck/k9/activity/setup/AccountSetupConfirmIncoming.java @@ -6,11 +6,19 @@ package com.fsck.k9.activity.setup; the final settings. */ +import android.util.Log; +import com.fsck.k9.K9; import com.fsck.k9.helper.configxmlparser.AutoconfigInfo; import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.ServerType; +import org.apache.http.conn.scheme.Scheme; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; import java.util.List; -public class AccountSetupConfirmIncoming extends AbstractSetupConfirmActivity{ +public class AccountSetupConfirmIncoming extends AbstractSetupConfirmActivity { @Override protected List getServers() { return mConfigInfo.incomingServer; @@ -23,6 +31,25 @@ public class AccountSetupConfirmIncoming extends AbstractSetupConfirmActivity{ @Override protected void finishAction() { - //To change body of implemented methods use File | Settings | File Templates. + try { + // this should already be set, only the username could be different.. but anyway + String usernameEnc = URLEncoder.encode(mUsername, "UTF-8"); + String passwordEnc = URLEncoder.encode(mPassword, "UTF-8"); + + URI uri = new URI( + getScheme(), + usernameEnc + ":" + passwordEnc, + mCurrentServer.hostname, + mCurrentServer.port, + null,null,null); + mAccount.setTransportUri(uri.toString()); + } + catch (UnsupportedEncodingException enc) {} + catch (URISyntaxException use) { + /* + * If we can't set up the URL we just continue. It's only for + * convenience. + */ + } } } diff --git a/src/com/fsck/k9/helper/configxmlparser/AutoconfigInfo.java b/src/com/fsck/k9/helper/configxmlparser/AutoconfigInfo.java index 9310fe27a..6c57f1922 100644 --- a/src/com/fsck/k9/helper/configxmlparser/AutoconfigInfo.java +++ b/src/com/fsck/k9/helper/configxmlparser/AutoconfigInfo.java @@ -53,14 +53,28 @@ public class AutoconfigInfo implements Parcelable { Define types for some of the data *******************************************************************************/ public static enum AuthenticationType { plain, secure, NTLM, GSSAPI, clientIPaddress, TLSclientcert, none, UNSET }; - public static enum SocketType { plain, SSL, STARTTLS, UNSET}; + public static enum SocketType { plain(""), SSL("ssl"), STARTTLS("tls"), UNSET(""); + private String schemeName; + + SocketType(String schemeName){ this.schemeName = schemeName; } + public String getSchemeName() { + return schemeName; + } + }; + public static enum RestrictionType { clientIPAddress }; public static enum InformationType { ENABLE, INSTRUCTION, DOCUMENTATION } - public static enum ServerType{ IMAP(0), POP3(1), SMTP(2), UNSET(3), NO_VALUE(4), WRONG_TAG(5); + public static enum ServerType{ IMAP(0,"imap"), POP3(1,"pop3"), SMTP(2,"smtp"), UNSET(3,""), NO_VALUE(4,""), WRONG_TAG(5,""); private int type; - ServerType(int type){this.type = type;} + private String schemeName; + + ServerType(int type, String schemeName){ + this.type = type; + this.schemeName = schemeName; + } + public Server getServerObject(Parcel parcel){ // ugly here but cleanest solution to mix the parcelable and inheritance in the server classes ( user of super() ) switch(type){ @@ -83,6 +97,10 @@ public class AutoconfigInfo implements Parcelable { return NO_VALUE; } } + + public String getSchemeName() { + return schemeName; + } }