12013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann/* 22013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Copyright (C) 2010 The Android Open Source Project 32013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * 42013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License"); 52013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * you may not use this file except in compliance with the License. 62013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * You may obtain a copy of the License at 72013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * 82013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * http://www.apache.org/licenses/LICENSE-2.0 92013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * 102013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Unless required by applicable law or agreed to in writing, software 112013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS, 122013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * See the License for the specific language governing permissions and 142013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * limitations under the License. 152013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann */ 162013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 172013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannpackage com.android.contacts.util; 182013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 192013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.content.Context; 203e6991e5ee91ea2cc82a02bd3ea4ed7e941b08afMakoto Onukiimport android.text.TextUtils.TruncateAt; 212013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.view.LayoutInflater; 222013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.view.View; 232013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.view.ViewGroup; 242013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.widget.BaseAdapter; 252013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.widget.ImageView; 262013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.widget.TextView; 272013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 28e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R; 29e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.model.AccountTypeManager; 30851222a96b5d68602fb361ea3527101e893f67e3Maurice Chuimport com.android.contacts.model.account.AccountType; 31851222a96b5d68602fb361ea3527101e893f67e3Maurice Chuimport com.android.contacts.model.account.AccountWithDataSet; 32e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng 33a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawaimport java.util.ArrayList; 342013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport java.util.List; 352013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 362013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann/** 372013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * List-Adapter for Account selection 382013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann */ 392013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannpublic final class AccountsListAdapter extends BaseAdapter { 402013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann private final LayoutInflater mInflater; 412b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbcDave Santoro private final List<AccountWithDataSet> mAccounts; 42a07fa5f37031e4c5cd2933de02d2db41ec153e2bDmitri Plotnikov private final AccountTypeManager mAccountTypes; 432013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann private final Context mContext; 442013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 456f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki /** 466f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki * Filters that affect the list of accounts that is displayed by this adapter. 476f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki */ 486f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki public enum AccountListFilter { 496f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki ALL_ACCOUNTS, // All read-only and writable accounts 506f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki ACCOUNTS_CONTACT_WRITABLE, // Only where the account type is contact writable 516f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki ACCOUNTS_GROUP_WRITABLE // Only accounts where the account type is group writable 526f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki } 536f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki 546f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki public AccountsListAdapter(Context context, AccountListFilter accountListFilter) { 556f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki this(context, accountListFilter, null); 56a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa } 57a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa 58a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa /** 59a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa * @param currentAccount the Account currently selected by the user, which should come 60a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa * first in the list. Can be null. 61a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa */ 626f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki public AccountsListAdapter(Context context, AccountListFilter accountListFilter, 632b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbcDave Santoro AccountWithDataSet currentAccount) { 642013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann mContext = context; 65a07fa5f37031e4c5cd2933de02d2db41ec153e2bDmitri Plotnikov mAccountTypes = AccountTypeManager.getInstance(context); 666f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki mAccounts = getAccounts(accountListFilter); 67a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa if (currentAccount != null 68a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa && !mAccounts.isEmpty() 69a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa && !mAccounts.get(0).equals(currentAccount) 70a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa && mAccounts.remove(currentAccount)) { 71a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa mAccounts.add(0, currentAccount); 72a6c8f2daa80f1816dd25237457d7f6287c6b1b7cDaisuke Miyakawa } 732013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann mInflater = LayoutInflater.from(context); 742013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 752013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 766f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki private List<AccountWithDataSet> getAccounts(AccountListFilter accountListFilter) { 776f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki if (accountListFilter == AccountListFilter.ACCOUNTS_GROUP_WRITABLE) { 786f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki return new ArrayList<AccountWithDataSet>(mAccountTypes.getGroupWritableAccounts()); 796f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki } 806f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki return new ArrayList<AccountWithDataSet>(mAccountTypes.getAccounts( 816f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki accountListFilter == AccountListFilter.ACCOUNTS_CONTACT_WRITABLE)); 826f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki } 836f74c0f3313cbb08ee8a8fbb79bfefc5b03fe215Makoto Onuki 842013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 852013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public View getView(int position, View convertView, ViewGroup parent) { 862013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final View resultView = convertView != null ? convertView 872013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann : mInflater.inflate(R.layout.account_selector_list_item, parent, false); 882013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 890353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1); 900353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2); 910353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon); 922013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 932b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbcDave Santoro final AccountWithDataSet account = mAccounts.get(position); 942b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbcDave Santoro final AccountType accountType = mAccountTypes.getAccountType(account.type, account.dataSet); 952013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 960353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan text1.setText(accountType.getDisplayLabel(mContext)); 973e6991e5ee91ea2cc82a02bd3ea4ed7e941b08afMakoto Onuki 983e6991e5ee91ea2cc82a02bd3ea4ed7e941b08afMakoto Onuki // For email addresses, we don't want to truncate at end, which might cut off the domain 993e6991e5ee91ea2cc82a02bd3ea4ed7e941b08afMakoto Onuki // name. 1000353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan text2.setText(account.name); 1010353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan text2.setEllipsize(TruncateAt.MIDDLE); 1020353a24d97cc0266eb9bf0ee6c7d6821aeb1896eKatherine Kuan 10369f9e6f0cd9b5401da55f251e9bd98e69643d7dfDmitri Plotnikov icon.setImageDrawable(accountType.getDisplayIcon(mContext)); 1042013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 1052013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return resultView; 1062013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 1072013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 1082013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 1092013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public int getCount() { 1102013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return mAccounts.size(); 1112013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 1122013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 1132013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 1142b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbcDave Santoro public AccountWithDataSet getItem(int position) { 1152013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return mAccounts.get(position); 1162013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 1172013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 1182013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 1192013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public long getItemId(int position) { 1202013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return position; 1212013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 1222013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann} 1232013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 124