1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-14 05:25:07 -05:00

Moved the previous abstraction up in the 'call tree', making it static so every class can now easily get a blank account object. This is needed for changes to come.

This commit is contained in:
dzan 2011-07-17 20:35:51 +02:00 committed by Andrew Chen
parent 9df96d07c5
commit 1f0f229b76
2 changed files with 70 additions and 77 deletions

View File

@ -9,6 +9,7 @@ import android.util.Log;
import com.fsck.k9.crypto.Apg; import com.fsck.k9.crypto.Apg;
import com.fsck.k9.crypto.CryptoProvider; import com.fsck.k9.crypto.CryptoProvider;
import com.fsck.k9.helper.Contacts;
import com.fsck.k9.helper.Utility; import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
@ -18,6 +19,11 @@ import com.fsck.k9.mail.store.StorageManager;
import com.fsck.k9.mail.store.StorageManager.StorageProvider; import com.fsck.k9.mail.store.StorageManager.StorageProvider;
import com.fsck.k9.view.ColorChip; import com.fsck.k9.view.ColorChip;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
@ -112,6 +118,67 @@ public class Account implements BaseAccount {
public static final boolean DEFAULT_SORT_ASCENDING = false; public static final boolean DEFAULT_SORT_ASCENDING = false;
/*
This method is used several times to initialise a blank account when only given the arguments as data.
This happens possibly in some of the account setup steps.
It could use a review, I pasted it together from previous code.
*/
public static Account getBlankAccount(Context context, String email, String password){
String[] emailParts = email.split("@");
String user = (emailParts.length > 0) ? emailParts[0] : "";
String domain = (emailParts.length > 1) ? emailParts[1] : "";
Account mAccount = Preferences.getPreferences(context).newAccount();
// decide on owners name
String name = "";
try {
name = Contacts.getInstance(context).getOwnerName();
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Could not get owner name, using default account name", e);
}
if (name == null || name.length() == 0) {
try {
Account account = Preferences.getPreferences(context).getDefaultAccount();
if (account != null) {
name = account.getName();
}
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Could not get default account name", e);
}
}
if (name == null) {
name = "";
}
// continue..
mAccount.setName(name);
mAccount.setEmail(email);
try {
String userEnc = URLEncoder.encode(user, "UTF-8");
String passwordEnc = URLEncoder.encode(password, "UTF-8");
URI uri = new URI("placeholder", userEnc + ":" + passwordEnc, "mail." + domain, -1, null,
null, null);
mAccount.setStoreUri(uri.toString());
mAccount.setTransportUri(uri.toString());
} catch (UnsupportedEncodingException enc) {
// This really shouldn't happen since the encoding is hardcoded to UTF-8
Log.e(K9.LOG_TAG, "Couldn't urlencode username or password.", enc);
} catch (URISyntaxException use) {
/*
* If we can't set up the URL we just continue. It's only for
* convenience.
*/
}
mAccount.setDraftsFolderName(context.getResources().getString(R.string.special_mailbox_name_drafts));
mAccount.setTrashFolderName(context.getResources().getString(R.string.special_mailbox_name_trash));
mAccount.setSentFolderName(context.getResources().getString(R.string.special_mailbox_name_sent));
return mAccount;
}
/** /**
* <pre> * <pre>
* 0 - Never (DELETE_POLICY_NEVER) * 0 - Never (DELETE_POLICY_NEVER)

View File

@ -53,81 +53,6 @@ public class AccountSetupAccountType extends K9Activity implements OnClickListen
context.startActivity(i); context.startActivity(i);
} }
/*
Initialises a new account blank.
*/
private void initialiseAccount(String email, String password) {
String[] emailParts = splitEmail(email);
String user = emailParts[0];
String domain = emailParts[1];
mAccount = Preferences.getPreferences(this).newAccount();
mAccount.setName(getOwnerName());
mAccount.setEmail(email);
try {
String userEnc = URLEncoder.encode(user, "UTF-8");
String passwordEnc = URLEncoder.encode(password, "UTF-8");
URI uri = new URI("placeholder", userEnc + ":" + passwordEnc, "mail." + domain, -1, null,
null, null);
mAccount.setStoreUri(uri.toString());
mAccount.setTransportUri(uri.toString());
} catch (UnsupportedEncodingException enc) {
// This really shouldn't happen since the encoding is hardcoded to UTF-8
Log.e(K9.LOG_TAG, "Couldn't urlencode username or password.", enc);
} catch (URISyntaxException use) {
/*
* If we can't set up the URL we just continue. It's only for
* convenience.
*/
}
mAccount.setDraftsFolderName(getString(R.string.special_mailbox_name_drafts));
mAccount.setTrashFolderName(getString(R.string.special_mailbox_name_trash));
mAccount.setSentFolderName(getString(R.string.special_mailbox_name_sent));
}
/*
Helper methods for the above, these could be in a static class in the helper package. But since
this is the only place they are used for now I'll leave them here.
*/
private String[] splitEmail(String email) {
String[] retParts = new String[2];
String[] emailParts = email.split("@");
retParts[0] = (emailParts.length > 0) ? emailParts[0] : "";
retParts[1] = (emailParts.length > 1) ? emailParts[1] : "";
return retParts;
}
private String getOwnerName() {
String name = null;
try {
name = Contacts.getInstance(this).getOwnerName();
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Could not get owner name, using default account name", e);
}
if (name == null || name.length() == 0) {
try {
name = getDefaultAccountName();
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Could not get default account name", e);
}
}
if (name == null) {
name = "";
}
return name;
}
private String getDefaultAccountName() {
String name = null;
Account account = Preferences.getPreferences(this).getDefaultAccount();
if (account != null) {
name = account.getName();
}
return name;
}
/* /*
'Constructor' 'Constructor'
*/ */
@ -140,7 +65,8 @@ public class AccountSetupAccountType extends K9Activity implements OnClickListen
((Button)findViewById(R.id.webdav)).setOnClickListener(this); ((Button)findViewById(R.id.webdav)).setOnClickListener(this);
if( getIntent().getStringExtra(EXTRA_IS_MANUAL) != null){ if( getIntent().getStringExtra(EXTRA_IS_MANUAL) != null){
initialiseAccount(getIntent().getStringExtra(EXTRA_EMAIL), mAccount = Account.getBlankAccount(this,
getIntent().getStringExtra(EXTRA_EMAIL),
getIntent().getStringExtra(EXTRA_PASSWORD)); getIntent().getStringExtra(EXTRA_PASSWORD));
}else{ }else{
String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT); String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);