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