1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00

Dialogs in place to continue after selecting an account. No input validation for now.

This commit is contained in:
Sander Bogaert 2011-06-28 14:17:19 +02:00 committed by Andrew Chen
parent 002f1a7429
commit 694b643a7c
4 changed files with 270 additions and 23 deletions

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView android:id="@+id/form_label_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_setup_basics_email_hint"
android:layout_marginBottom="10dp"/>
<EditText android:id="@+id/account_dialog_address_field"
android:singleLine="true"
android:inputType="textEmailAddress"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView android:id="@+id/form_label_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_setup_basics_password_hint"
android:layout_marginBottom="10dp"/>
<EditText android:id="@+id/account_dialog_password_field"
android:inputType="textPassword"
android:password="true"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:nextFocusDown="@+id/next"/>
<CheckBox android:id="@+id/account_dialog_manual_box"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/account_setup_basics_manual_setup_action"/>
<Button android:id="@+id/account_dialog_next"
android:text="@string/next_action"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawableRight="@drawable/button_indicator_next" />
<!-- android:layout_marginBottom="-4dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="false"-->
</LinearLayout>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView android:id="@+id/account_dialog_password_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"/>
<EditText
android:id="@+id/account_dialog_password_field"
android:inputType="textPassword"
android:password="true"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:nextFocusDown="@+id/next"
/>
<CheckBox android:id="@+id/account_dialog_manual_box"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/account_setup_basics_manual_setup_action"/>
<Button android:id="@+id/account_dialog_next"
android:text="@string/next_action"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawableRight="@drawable/button_indicator_next" />
<!-- android:layout_marginBottom="-4dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="false"-->
</LinearLayout>

View File

@ -351,6 +351,7 @@ http://k9mail.googlecode.com/
<string name="accounts_setup_help"> <string name="accounts_setup_help">
Select an account here to setup, press menu to load more backup files or create an new account. Select an account here to setup, press menu to load more backup files or create an new account.
</string> </string>
<string name="account_setup_device_sectionheader">Device</string>
<string name="account_setup_basics_instructions">Enter this account\'s email address:</string> <string name="account_setup_basics_instructions">Enter this account\'s email address:</string>
<string name="account_setup_basics_email_hint">Email address</string> <string name="account_setup_basics_email_hint">Email address</string>
<string name="account_setup_basics_password_hint">Password</string> <string name="account_setup_basics_password_hint">Password</string>

View File

@ -6,37 +6,57 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.*; import android.widget.*;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import com.fsck.k9.EmailAddressValidator; import com.fsck.k9.*;
import com.fsck.k9.R;
import com.fsck.k9.activity.K9ListActivity; import com.fsck.k9.activity.K9ListActivity;
import com.fsck.k9.helper.Contacts;
import com.fsck.k9.helper.SectionListAdapter; import com.fsck.k9.helper.SectionListAdapter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import static com.fsck.k9.activity.setup.AccountSetupBasics.*; import static com.fsck.k9.activity.setup.AccountSetupBasics.*;
/** /**
* User: dzan * User: dzan
* Date: 18/06/11 * Date: 18/06/11
*
* TODO: Encapsulate accountAdaptor. No need for this to be global except due the test-data.
*/ */
public class AccountSetupIndex extends K9ListActivity implements OnItemClickListener, OnClickListener { public class AccountSetupIndex extends K9ListActivity implements OnItemClickListener, OnClickListener {
private static final String DEVICE_STRING = "Device";
private static final int DIALOG_NEW_ACCOUNT = 0; private static final int DIALOG_NEW_ACCOUNT = 0;
private static final int DIALOG_DEVICE_ACCOUNT = 1; private static final int DIALOG_DEVICE_ACCOUNT = 1;
private static final int DIALOG_BACKUP_ACCOUNT = 2; private static final int DIALOG_BACKUP_ACCOUNT = 2;
private static final String BUNDLE_TYPE_SUGGESTION = "SUGGESTION";
public enum SuggestionType { DEVICE, BACKUP, NEW } public enum SuggestionType { DEVICE, BACKUP, NEW }
// temp hard coded value ( since we don't ask if it should be default account for now )
private boolean bTmpDefaultAccount = true;
private Account mAccount;
private Button mNewAccountDialogButton;
private Button mPasswordDialogButton;
private EmailAddressValidator mEmailValidator = new EmailAddressValidator();
public static void actionNewAccount(Context context) { public static void actionNewAccount(Context context) {
Intent i = new Intent(context, AccountSetupIndex.class); Intent i = new Intent(context, AccountSetupIndex.class);
context.startActivity(i); context.startActivity(i);
} }
private Button mNewAccountButton;
private SectionListAdapter<AccountSuggestion> accountAdapter; private SectionListAdapter<AccountSuggestion> accountAdapter;
@Override @Override
@ -45,7 +65,7 @@ public class AccountSetupIndex extends K9ListActivity implements OnItemClickList
// layout // layout
setContentView(R.layout.account_setup_index); setContentView(R.layout.account_setup_index);
mNewAccountButton = (Button)findViewById(R.id.new_account); Button mNewAccountButton = (Button)findViewById(R.id.new_account);
// button callbacks // button callbacks
mNewAccountButton.setOnClickListener(this); mNewAccountButton.setOnClickListener(this);
@ -64,54 +84,153 @@ public class AccountSetupIndex extends K9ListActivity implements OnItemClickList
} }
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), SectionListAdapter<AccountSuggestion> adapter = (SectionListAdapter<AccountSuggestion>)adapterView.getAdapter();
Toast.LENGTH_SHORT).show(); AccountSuggestion suggestion = (AccountSuggestion)adapter.getItem(pos);
Bundle tmp = new Bundle();
tmp.putString(BUNDLE_TYPE_SUGGESTION,suggestion.getAccount());
switch(suggestion.getSuggestionType()){
case DEVICE:
showDialog(DIALOG_DEVICE_ACCOUNT,tmp);
break;
case BACKUP:
showDialog(DIALOG_BACKUP_ACCOUNT,tmp);
break;
default:
return;
}
} }
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.new_account: case R.id.new_account:
onNew(); showDialog(DIALOG_NEW_ACCOUNT);
break; break;
} }
} }
protected void onNew() {
AccountSetupBasics.actionNewAccount(this);
}
/* /*
Dialogues Dialogues
*/ */
protected Dialog onCreateDialog(int dialog_id){ protected Dialog onCreateDialog(int dialog_id){
Dialog dialog;
switch (dialog_id){ Dialog dialog = new Dialog(this);
switch(dialog_id){
case DIALOG_NEW_ACCOUNT: case DIALOG_NEW_ACCOUNT:
dialog = new Dialog(this); dialog.setContentView(R.layout.account_dialog_new);
dialog.setTitle("Setup a new account.");
final EditText emailField = ((EditText)dialog.findViewById(R.id.account_dialog_address_field));
final EditText passwordField = ((EditText)dialog.findViewById(R.id.account_dialog_password_field));
final CheckBox manualCheck = (CheckBox)dialog.findViewById(R.id.account_dialog_manual_box);
mNewAccountDialogButton = ((Button)dialog.findViewById(R.id.account_dialog_next));
mNewAccountDialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String email = emailField.getText().toString();
String password = passwordField.getText().toString();
// TODO: replace this with a few listeners on the fields that activate/deactive the button on acceptable values
if( !mEmailValidator.isValidAddressOnly(email) || password.isEmpty() ) return;
if(manualCheck.isChecked())
onManualSetup(email, password);
else startSettingsDetection(email, password);
}
});
break; break;
case DIALOG_DEVICE_ACCOUNT: case DIALOG_DEVICE_ACCOUNT:
dialog = new Dialog(this);
break;
case DIALOG_BACKUP_ACCOUNT: case DIALOG_BACKUP_ACCOUNT:
dialog = new Dialog(this); dialog.setContentView(R.layout.account_dialog_password);
dialog.setTitle("Enter password.");
mPasswordDialogButton = ((Button)dialog.findViewById(R.id.account_dialog_next));
break; break;
default:
dialog = null; default: dialog = null;
} }
dialog.show();
return dialog; return dialog;
} }
protected void onPrepareDialog(int id, Dialog dialog, final Bundle args){
switch(id){
case DIALOG_DEVICE_ACCOUNT:
case DIALOG_BACKUP_ACCOUNT:
((TextView)dialog.findViewById(R.id.account_dialog_password_help)).setText
("Enter the password for the '" + args.get(BUNDLE_TYPE_SUGGESTION) + "' account: ");
final EditText passwordField = ((EditText) dialog.findViewById(R.id.account_dialog_password_field));
final CheckBox manualCheck = (CheckBox)dialog.findViewById(R.id.account_dialog_manual_box);
mPasswordDialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String email = args.get(BUNDLE_TYPE_SUGGESTION).toString();
String password = passwordField.getText().toString();
// TODO: replace this with a few listeners on the fields that activate/deactive the button on acceptable values
if( password.isEmpty() ) return;
if (manualCheck.isChecked())
onManualSetup(email, password);
else startSettingsDetection(email, password);
}
});
break;
default: return;
}
}
private void onManualSetup(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));
AccountSetupAccountType.actionSelectAccountType(this, mAccount, bTmpDefaultAccount);
finish();
}
private void startSettingsDetection(String email, String password) {
}
/* /*
Scans device for email addresses and adds them as suggestions Scans device for email addresses and adds them as suggestions
*/ */
private void fillFromDevice(SectionListAdapter<AccountSuggestion> adp){ private void fillFromDevice(SectionListAdapter<AccountSuggestion> adp){
EmailAddressValidator mEmailValidator = new EmailAddressValidator();
for( android.accounts.Account acc : AccountManager.get(this).getAccounts() ) for( android.accounts.Account acc : AccountManager.get(this).getAccounts() )
if( mEmailValidator.isValidAddressOnly(acc.name) ) if( mEmailValidator.isValidAddressOnly(acc.name) )
adp.add(DEVICE_STRING, new AccountSuggestion(acc.name, SuggestionType.DEVICE)); adp.add(getString(R.string.account_setup_device_sectionheader), new AccountSuggestion(acc.name, SuggestionType.DEVICE));
// TEMP DATA TO TEST // TEMP DATA TO TEST
accountAdapter.add("file1", new AccountSuggestion("piet@snot.com",SuggestionType.BACKUP)); accountAdapter.add("file1", new AccountSuggestion("piet@snot.com",SuggestionType.BACKUP));
@ -135,6 +254,46 @@ public class AccountSetupIndex extends K9ListActivity implements OnItemClickList
} }
} }
/*
Some helpers copy pasted from previous setup. Need a review.
*/
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;
}
/* /*
Simple class to wrap account suggestion data Simple class to wrap account suggestion data
*/ */