2010-07-21 23:15:28 -04:00
|
|
|
package com.fsck.k9.activity;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2010-11-13 16:40:56 -05:00
|
|
|
import android.util.Log;
|
2010-07-21 23:15:28 -04:00
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.widget.BaseExpandableListAdapter;
|
|
|
|
import android.widget.ExpandableListAdapter;
|
|
|
|
import android.widget.ExpandableListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.fsck.k9.Account;
|
|
|
|
import com.fsck.k9.Identity;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.Preferences;
|
|
|
|
import com.fsck.k9.R;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity displaying list of accounts/identity for user choice
|
|
|
|
*
|
|
|
|
* @see K9ExpandableListActivity
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public class ChooseAccount extends K9ExpandableListActivity {
|
2010-07-21 23:15:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link Intent} extended data name for storing {@link Account#getUuid()
|
|
|
|
* account UUID}
|
|
|
|
*/
|
|
|
|
public static final String EXTRA_ACCOUNT = ChooseAccount.class.getName() + "_account";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link Intent} extended data name for storing serialized {@link Identity}
|
|
|
|
*/
|
|
|
|
public static final String EXTRA_IDENTITY = ChooseAccount.class.getName() + "_identity";
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
2010-07-21 23:15:28 -04:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
|
|
|
setContentView(R.layout.choose_account);
|
|
|
|
|
|
|
|
final ExpandableListView expandableListView = getExpandableListView();
|
|
|
|
expandableListView.setItemsCanFocus(false);
|
|
|
|
|
|
|
|
final ExpandableListAdapter adapter = createAdapter();
|
|
|
|
setListAdapter(adapter);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
|
2010-07-21 23:15:28 -04:00
|
|
|
@Override
|
|
|
|
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
|
2011-02-06 17:09:48 -05:00
|
|
|
int childPosition, long id) {
|
2010-07-21 23:15:28 -04:00
|
|
|
final Identity identity = (Identity) adapter.getChild(groupPosition, childPosition);
|
|
|
|
final Account account = (Account) adapter.getGroup(groupPosition);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!account.isAvailable(v.getContext())) {
|
2010-11-13 16:40:56 -05:00
|
|
|
Log.i(K9.LOG_TAG, "Refusing selection of unavailable account");
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-21 23:15:28 -04:00
|
|
|
final Intent intent = new Intent();
|
|
|
|
intent.putExtra(EXTRA_ACCOUNT, account.getUuid());
|
|
|
|
intent.putExtra(EXTRA_IDENTITY, identity);
|
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final Bundle extras = getIntent().getExtras();
|
|
|
|
final String uuid = extras.getString(EXTRA_ACCOUNT);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (uuid != null) {
|
2010-07-21 23:15:28 -04:00
|
|
|
final Account[] accounts = Preferences.getPreferences(this).getAccounts();
|
|
|
|
final int length = accounts.length;
|
2011-02-06 17:09:48 -05:00
|
|
|
for (int i = 0; i < length; i++) {
|
2010-07-21 23:15:28 -04:00
|
|
|
final Account account = accounts[i];
|
2011-02-06 17:09:48 -05:00
|
|
|
if (uuid.equals(account.getUuid())) {
|
2010-07-21 23:15:28 -04:00
|
|
|
// setSelectedChild() doesn't seem to obey the
|
|
|
|
// shouldExpandGroup parameter (2.1), manually expanding
|
|
|
|
// group
|
|
|
|
expandableListView.expandGroup(i);
|
|
|
|
|
|
|
|
final List<Identity> identities = account.getIdentities();
|
|
|
|
final Identity identity = (Identity) extras.getSerializable(EXTRA_IDENTITY);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (identity == null) {
|
2010-07-21 23:15:28 -04:00
|
|
|
expandableListView.setSelectedChild(i, 0, true);
|
|
|
|
break;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
for (int j = 0; j < identities.size(); j++) {
|
2010-07-21 23:15:28 -04:00
|
|
|
final Identity loopIdentity = identities.get(j);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (identity.equals(loopIdentity)) {
|
2010-07-21 23:15:28 -04:00
|
|
|
expandableListView.setSelectedChild(i, j, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private ExpandableListAdapter createAdapter() {
|
2010-07-21 23:15:28 -04:00
|
|
|
return new IdentitiesAdapter(this, getLayoutInflater());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically provides accounts/identities data for
|
|
|
|
* {@link ExpandableListView#setAdapter(ExpandableListAdapter)}:
|
|
|
|
*
|
|
|
|
* <ul>
|
|
|
|
* <li>Groups represent {@link Account accounts}</li>
|
|
|
|
* <li>Children represent {@link Identity identities} of the parent account</li>
|
|
|
|
* </ul>
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static class IdentitiesAdapter extends BaseExpandableListAdapter {
|
2010-07-21 23:15:28 -04:00
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private LayoutInflater mLayoutInflater;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public IdentitiesAdapter(final Context context, final LayoutInflater layoutInflater) {
|
2010-07-21 23:15:28 -04:00
|
|
|
mContext = context;
|
|
|
|
mLayoutInflater = layoutInflater;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public Object getChild(int groupPosition, int childPosition) {
|
2010-07-21 23:15:28 -04:00
|
|
|
return getAccounts()[groupPosition].getIdentity(childPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public long getChildId(int groupPosition, int childPosition) {
|
2010-07-21 23:15:28 -04:00
|
|
|
return Integer.valueOf(childPosition).longValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public int getChildrenCount(int groupPosition) {
|
2010-07-21 23:15:28 -04:00
|
|
|
return getAccounts()[groupPosition].getIdentities().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public Object getGroup(int groupPosition) {
|
2010-07-21 23:15:28 -04:00
|
|
|
return getAccounts()[groupPosition];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public int getGroupCount() {
|
2010-07-21 23:15:28 -04:00
|
|
|
return getAccounts().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public long getGroupId(int groupPosition) {
|
2010-07-21 23:15:28 -04:00
|
|
|
return Integer.valueOf(groupPosition).longValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
|
2011-02-06 17:09:48 -05:00
|
|
|
ViewGroup parent) {
|
2010-07-21 23:15:28 -04:00
|
|
|
final View v;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (convertView == null) {
|
2010-07-21 23:15:28 -04:00
|
|
|
v = mLayoutInflater.inflate(R.layout.choose_account_item, parent, false);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-07-21 23:15:28 -04:00
|
|
|
v = convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
final TextView description = (TextView) v.findViewById(R.id.description);
|
|
|
|
final Account account = getAccounts()[groupPosition];
|
|
|
|
description.setText(account.getDescription());
|
|
|
|
description.setTextSize(TypedValue.COMPLEX_UNIT_DIP, K9.getFontSizes().getAccountName());
|
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
// display unavailable accounts translucent
|
|
|
|
/*
|
|
|
|
* 20101030/fiouzy: NullPointerException on null getBackground()
|
|
|
|
*
|
|
|
|
if (account.isAvailable(parent.getContext()))
|
|
|
|
{
|
|
|
|
description.getBackground().setAlpha(255);
|
|
|
|
description.getBackground().setAlpha(255);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
description.getBackground().setAlpha(127);
|
|
|
|
description.getBackground().setAlpha(127);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2010-07-21 23:15:28 -04:00
|
|
|
v.findViewById(R.id.chip).setBackgroundColor(account.getChipColor());
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
|
2011-02-06 17:09:48 -05:00
|
|
|
View convertView, ViewGroup parent) {
|
2010-07-21 23:15:28 -04:00
|
|
|
final Account account = getAccounts()[groupPosition];
|
|
|
|
final Identity identity = account.getIdentity(childPosition);
|
|
|
|
|
|
|
|
final View v;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (convertView == null) {
|
2010-07-21 23:15:28 -04:00
|
|
|
v = mLayoutInflater.inflate(R.layout.choose_identity_item, parent, false);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-07-21 23:15:28 -04:00
|
|
|
v = convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
final TextView name = (TextView) v.findViewById(R.id.name);
|
|
|
|
final TextView description = (TextView) v.findViewById(R.id.description);
|
|
|
|
name.setTextSize(TypedValue.COMPLEX_UNIT_DIP, K9.getFontSizes().getAccountName());
|
|
|
|
description.setTextSize(TypedValue.COMPLEX_UNIT_DIP, K9.getFontSizes().getAccountDescription());
|
|
|
|
|
|
|
|
name.setText(identity.getDescription());
|
|
|
|
description.setText(String.format("%s <%s>", identity.getName(), identity.getEmail()));
|
|
|
|
|
|
|
|
v.findViewById(R.id.chip).setBackgroundColor(account.getChipColor());
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean hasStableIds() {
|
2010-07-21 23:15:28 -04:00
|
|
|
// returning false since accounts/identities are mutable
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean isChildSelectable(int groupPosition, int childPosition) {
|
2010-07-21 23:15:28 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private Account[] getAccounts() {
|
2010-07-21 23:15:28 -04:00
|
|
|
return Preferences.getPreferences(mContext).getAccounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|