1e420de7fca9005d7084be275f268962a14094a22Chiao Cheng/* 2e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Copyright (C) 2010 The Android Open Source Project 3e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * 4e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); 5e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * you may not use this file except in compliance with the License. 6e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * You may obtain a copy of the License at 7e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * 8e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 9e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * 10e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Unless required by applicable law or agreed to in writing, software 11e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, 12e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * See the License for the specific language governing permissions and 14e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * limitations under the License. 15e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */ 16e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 17e420de7fca9005d7084be275f268962a14094a22Chiao Chengpackage com.android.contacts.common.util; 18e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 19e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.content.Context; 20e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.text.TextUtils.TruncateAt; 21e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.view.LayoutInflater; 22e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.view.View; 23e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.view.ViewGroup; 24e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.widget.BaseAdapter; 25e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.widget.ImageView; 26e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.widget.TextView; 27e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 28e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.R; 29e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.AccountTypeManager; 30e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.account.AccountType; 31e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.account.AccountWithDataSet; 32e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 33e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport java.util.ArrayList; 34e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport java.util.List; 35e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 36e420de7fca9005d7084be275f268962a14094a22Chiao Cheng/** 37e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * List-Adapter for Account selection 38e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */ 39e420de7fca9005d7084be275f268962a14094a22Chiao Chengpublic final class AccountsListAdapter extends BaseAdapter { 40e420de7fca9005d7084be275f268962a14094a22Chiao Cheng private final LayoutInflater mInflater; 41e420de7fca9005d7084be275f268962a14094a22Chiao Cheng private final List<AccountWithDataSet> mAccounts; 42e420de7fca9005d7084be275f268962a14094a22Chiao Cheng private final AccountTypeManager mAccountTypes; 43e420de7fca9005d7084be275f268962a14094a22Chiao Cheng private final Context mContext; 4474a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang private int mCustomLayout = -1; 45e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 46e420de7fca9005d7084be275f268962a14094a22Chiao Cheng /** 47e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Filters that affect the list of accounts that is displayed by this adapter. 48e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */ 49e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public enum AccountListFilter { 50e420de7fca9005d7084be275f268962a14094a22Chiao Cheng ALL_ACCOUNTS, // All read-only and writable accounts 51e420de7fca9005d7084be275f268962a14094a22Chiao Cheng ACCOUNTS_CONTACT_WRITABLE, // Only where the account type is contact writable 52e420de7fca9005d7084be275f268962a14094a22Chiao Cheng ACCOUNTS_GROUP_WRITABLE // Only accounts where the account type is group writable 53e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 54e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 55e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public AccountsListAdapter(Context context, AccountListFilter accountListFilter) { 56e420de7fca9005d7084be275f268962a14094a22Chiao Cheng this(context, accountListFilter, null); 57e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 58e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 59e420de7fca9005d7084be275f268962a14094a22Chiao Cheng /** 60e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * @param currentAccount the Account currently selected by the user, which should come 61e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * first in the list. Can be null. 62e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */ 63e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public AccountsListAdapter(Context context, AccountListFilter accountListFilter, 64e420de7fca9005d7084be275f268962a14094a22Chiao Cheng AccountWithDataSet currentAccount) { 65e420de7fca9005d7084be275f268962a14094a22Chiao Cheng mContext = context; 66e420de7fca9005d7084be275f268962a14094a22Chiao Cheng mAccountTypes = AccountTypeManager.getInstance(context); 67e420de7fca9005d7084be275f268962a14094a22Chiao Cheng mAccounts = getAccounts(accountListFilter); 68e420de7fca9005d7084be275f268962a14094a22Chiao Cheng if (currentAccount != null 69e420de7fca9005d7084be275f268962a14094a22Chiao Cheng && !mAccounts.isEmpty() 70e420de7fca9005d7084be275f268962a14094a22Chiao Cheng && !mAccounts.get(0).equals(currentAccount) 71e420de7fca9005d7084be275f268962a14094a22Chiao Cheng && mAccounts.remove(currentAccount)) { 72e420de7fca9005d7084be275f268962a14094a22Chiao Cheng mAccounts.add(0, currentAccount); 73e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 74e420de7fca9005d7084be275f268962a14094a22Chiao Cheng mInflater = LayoutInflater.from(context); 75e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 76e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 77e420de7fca9005d7084be275f268962a14094a22Chiao Cheng private List<AccountWithDataSet> getAccounts(AccountListFilter accountListFilter) { 78e420de7fca9005d7084be275f268962a14094a22Chiao Cheng if (accountListFilter == AccountListFilter.ACCOUNTS_GROUP_WRITABLE) { 79e420de7fca9005d7084be275f268962a14094a22Chiao Cheng return new ArrayList<AccountWithDataSet>(mAccountTypes.getGroupWritableAccounts()); 80e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 81e420de7fca9005d7084be275f268962a14094a22Chiao Cheng return new ArrayList<AccountWithDataSet>(mAccountTypes.getAccounts( 82e420de7fca9005d7084be275f268962a14094a22Chiao Cheng accountListFilter == AccountListFilter.ACCOUNTS_CONTACT_WRITABLE)); 83e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 84e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 8574a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang public void setCustomLayout(int customLayout) { 8674a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang mCustomLayout = customLayout; 8774a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang } 8874a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang 89e420de7fca9005d7084be275f268962a14094a22Chiao Cheng @Override 90e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public View getView(int position, View convertView, ViewGroup parent) { 9174a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang final View resultView = convertView != null ? convertView : 9274a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang mInflater.inflate(mCustomLayout > 0 ? mCustomLayout : 9374a99cb36dd63c90164b4fe92c273e90a20409a7Wenyi Wang R.layout.account_selector_list_item, parent, false); 94e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 95e420de7fca9005d7084be275f268962a14094a22Chiao Cheng final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1); 96e420de7fca9005d7084be275f268962a14094a22Chiao Cheng final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2); 97e420de7fca9005d7084be275f268962a14094a22Chiao Cheng final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon); 98e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 99e420de7fca9005d7084be275f268962a14094a22Chiao Cheng final AccountWithDataSet account = mAccounts.get(position); 100e420de7fca9005d7084be275f268962a14094a22Chiao Cheng final AccountType accountType = mAccountTypes.getAccountType(account.type, account.dataSet); 101e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 102e420de7fca9005d7084be275f268962a14094a22Chiao Cheng text1.setText(accountType.getDisplayLabel(mContext)); 103e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 104e420de7fca9005d7084be275f268962a14094a22Chiao Cheng // For email addresses, we don't want to truncate at end, which might cut off the domain 105e420de7fca9005d7084be275f268962a14094a22Chiao Cheng // name. 106e420de7fca9005d7084be275f268962a14094a22Chiao Cheng text2.setText(account.name); 107e420de7fca9005d7084be275f268962a14094a22Chiao Cheng text2.setEllipsize(TruncateAt.MIDDLE); 108e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 109e420de7fca9005d7084be275f268962a14094a22Chiao Cheng icon.setImageDrawable(accountType.getDisplayIcon(mContext)); 110e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 111e420de7fca9005d7084be275f268962a14094a22Chiao Cheng return resultView; 112e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 113e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 114e420de7fca9005d7084be275f268962a14094a22Chiao Cheng @Override 115e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public int getCount() { 116e420de7fca9005d7084be275f268962a14094a22Chiao Cheng return mAccounts.size(); 117e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 118e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 119e420de7fca9005d7084be275f268962a14094a22Chiao Cheng @Override 120e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public AccountWithDataSet getItem(int position) { 121e420de7fca9005d7084be275f268962a14094a22Chiao Cheng return mAccounts.get(position); 122e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 123e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 124e420de7fca9005d7084be275f268962a14094a22Chiao Cheng @Override 125e420de7fca9005d7084be275f268962a14094a22Chiao Cheng public long getItemId(int position) { 126e420de7fca9005d7084be275f268962a14094a22Chiao Cheng return position; 127e420de7fca9005d7084be275f268962a14094a22Chiao Cheng } 128e420de7fca9005d7084be275f268962a14094a22Chiao Cheng} 129e420de7fca9005d7084be275f268962a14094a22Chiao Cheng 130