2012-02-06 19:00:16 -05:00
|
|
|
package com.fsck.k9.activity;
|
|
|
|
|
2012-02-07 14:19:10 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2012-02-06 19:00:16 -05:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.fsck.k9.Account;
|
2012-02-07 14:19:10 -05:00
|
|
|
import com.fsck.k9.BaseAccount;
|
2012-02-06 19:00:16 -05:00
|
|
|
import com.fsck.k9.FontSizes;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.Preferences;
|
|
|
|
import com.fsck.k9.R;
|
2012-10-13 14:03:40 -04:00
|
|
|
import com.fsck.k9.search.SearchAccount;
|
2012-02-06 19:00:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity displaying the list of accounts.
|
|
|
|
*
|
|
|
|
* <p>
|
2012-02-07 14:19:10 -05:00
|
|
|
* Classes extending this abstract class have to provide an {@link #onAccountSelected(BaseAccount)}
|
2012-02-06 19:00:16 -05:00
|
|
|
* method to perform an action when an account is selected.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public abstract class AccountList extends K9ListActivity implements OnItemClickListener {
|
|
|
|
private FontSizes mFontSizes = K9.getFontSizes();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
|
|
|
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
|
|
|
|
setContentView(R.layout.account_list);
|
|
|
|
|
|
|
|
ListView listView = getListView();
|
|
|
|
listView.setOnItemClickListener(this);
|
|
|
|
listView.setItemsCanFocus(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload list of accounts when this activity is resumed.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
new LoadAccounts().execute();
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:19:10 -05:00
|
|
|
@Override
|
2012-02-06 19:00:16 -05:00
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2012-02-07 14:19:10 -05:00
|
|
|
BaseAccount account = (BaseAccount) parent.getItemAtPosition(position);
|
2012-02-06 19:00:16 -05:00
|
|
|
onAccountSelected(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new {@link AccountsAdapter} instance and assign it to the {@link ListView}.
|
|
|
|
*
|
2012-02-07 14:19:10 -05:00
|
|
|
* @param realAccounts
|
2012-02-06 19:00:16 -05:00
|
|
|
* An array of accounts to display.
|
|
|
|
*/
|
2012-02-07 14:19:10 -05:00
|
|
|
public void populateListView(Account[] realAccounts) {
|
|
|
|
List<BaseAccount> accounts = new ArrayList<BaseAccount>();
|
|
|
|
|
|
|
|
if (displaySpecialAccounts() && !K9.isHideSpecialAccounts()) {
|
2012-02-28 21:03:20 -05:00
|
|
|
BaseAccount unifiedInboxAccount = SearchAccount.createUnifiedInboxAccount(this);
|
|
|
|
BaseAccount allMessagesAccount = SearchAccount.createAllMessagesAccount(this);
|
2012-02-07 14:19:10 -05:00
|
|
|
|
2012-02-28 21:03:20 -05:00
|
|
|
accounts.add(unifiedInboxAccount);
|
|
|
|
accounts.add(allMessagesAccount);
|
2012-02-07 14:19:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
accounts.addAll(Arrays.asList(realAccounts));
|
2012-02-06 19:00:16 -05:00
|
|
|
AccountsAdapter adapter = new AccountsAdapter(accounts);
|
|
|
|
ListView listView = getListView();
|
|
|
|
listView.setAdapter(adapter);
|
|
|
|
listView.invalidate();
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:19:10 -05:00
|
|
|
/**
|
|
|
|
* Implementing decide whether or not to display special accounts in the list.
|
|
|
|
*
|
|
|
|
* @return {@code true}, if special accounts should be listed. {@code false}, otherwise.
|
|
|
|
*/
|
|
|
|
protected abstract boolean displaySpecialAccounts();
|
|
|
|
|
2012-02-06 19:00:16 -05:00
|
|
|
/**
|
|
|
|
* This method will be called when an account was selected.
|
|
|
|
*
|
|
|
|
* @param account
|
|
|
|
* The account the user selected.
|
|
|
|
*/
|
2012-02-07 14:19:10 -05:00
|
|
|
protected abstract void onAccountSelected(BaseAccount account);
|
2012-02-06 19:00:16 -05:00
|
|
|
|
2012-02-07 14:19:10 -05:00
|
|
|
class AccountsAdapter extends ArrayAdapter<BaseAccount> {
|
|
|
|
public AccountsAdapter(List<BaseAccount> accounts) {
|
2012-02-06 19:00:16 -05:00
|
|
|
super(AccountList.this, 0, accounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
2012-02-07 14:19:10 -05:00
|
|
|
final BaseAccount account = getItem(position);
|
2012-02-06 19:00:16 -05:00
|
|
|
|
|
|
|
final View view;
|
|
|
|
if (convertView != null) {
|
|
|
|
view = convertView;
|
|
|
|
} else {
|
|
|
|
view = getLayoutInflater().inflate(R.layout.accounts_item, parent, false);
|
|
|
|
view.findViewById(R.id.active_icons).setVisibility(View.GONE);
|
|
|
|
view.findViewById(R.id.folders).setVisibility(View.GONE);
|
|
|
|
view.getBackground().setAlpha(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountViewHolder holder = (AccountViewHolder) view.getTag();
|
|
|
|
if (holder == null) {
|
|
|
|
holder = new AccountViewHolder();
|
|
|
|
holder.description = (TextView) view.findViewById(R.id.description);
|
|
|
|
holder.email = (TextView) view.findViewById(R.id.email);
|
|
|
|
holder.chip = view.findViewById(R.id.chip);
|
|
|
|
|
|
|
|
view.setTag(holder);
|
|
|
|
}
|
|
|
|
|
|
|
|
String description = account.getDescription();
|
|
|
|
if (account.getEmail().equals(description)) {
|
|
|
|
holder.email.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
holder.email.setVisibility(View.VISIBLE);
|
|
|
|
holder.email.setText(account.getEmail());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (description == null || description.length() == 0) {
|
|
|
|
description = account.getEmail();
|
|
|
|
}
|
|
|
|
|
|
|
|
holder.description.setText(description);
|
|
|
|
|
2012-02-07 14:19:10 -05:00
|
|
|
if (account instanceof Account) {
|
|
|
|
Account realAccount = (Account) account;
|
|
|
|
holder.chip.setBackgroundColor(realAccount.getChipColor());
|
|
|
|
} else {
|
|
|
|
holder.chip.setBackgroundColor(0xff999999);
|
|
|
|
}
|
|
|
|
|
2012-02-06 19:00:16 -05:00
|
|
|
holder.chip.getBackground().setAlpha(255);
|
|
|
|
|
|
|
|
holder.description.setTextSize(TypedValue.COMPLEX_UNIT_SP,
|
|
|
|
mFontSizes.getAccountName());
|
|
|
|
holder.email.setTextSize(TypedValue.COMPLEX_UNIT_SP,
|
|
|
|
mFontSizes.getAccountDescription());
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AccountViewHolder {
|
|
|
|
public TextView description;
|
|
|
|
public TextView email;
|
|
|
|
public View chip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load accounts in a background thread
|
|
|
|
*/
|
|
|
|
class LoadAccounts extends AsyncTask<Void, Void, Account[]> {
|
|
|
|
@Override
|
|
|
|
protected Account[] doInBackground(Void... params) {
|
|
|
|
Account[] accounts = Preferences.getPreferences(getApplicationContext()).getAccounts();
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Account[] accounts) {
|
|
|
|
populateListView(accounts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|