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.util;
     18 
     19 import android.content.Context;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.BaseAdapter;
     24 import android.widget.ImageView;
     25 import android.widget.TextView;
     26 
     27 import com.android.contacts.R;
     28 import com.android.contacts.model.AccountTypeManager;
     29 import com.android.contacts.model.account.AccountInfo;
     30 import com.android.contacts.model.account.AccountWithDataSet;
     31 
     32 import java.util.ArrayList;
     33 import java.util.Collections;
     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 List<AccountInfo> mAccounts;
     42     private int mCustomLayout = -1;
     43 
     44     public AccountsListAdapter(Context context) {
     45         this(context, Collections.<AccountInfo>emptyList(), null);
     46     }
     47 
     48     public AccountsListAdapter(Context context, List<AccountInfo> accounts) {
     49         this(context, accounts, null);
     50     }
     51 
     52     /**
     53      * @param currentAccount the Account currently selected by the user, which should come
     54      * first in the list. Can be null.
     55      */
     56     public AccountsListAdapter(Context context, List<AccountInfo> accounts,
     57             AccountWithDataSet currentAccount) {
     58         mInflater = LayoutInflater.from(context);
     59 
     60         mAccounts = new ArrayList<>(accounts.size());
     61         setAccounts(accounts, currentAccount);
     62     }
     63 
     64     public void setAccounts(List<AccountInfo> accounts, AccountWithDataSet currentAccount) {
     65         // If it's not empty use the previous "current" account (the first one in the list)
     66         final AccountInfo currentInfo = mAccounts.isEmpty()
     67                 ? AccountInfo.getAccount(accounts, currentAccount)
     68                 : AccountInfo.getAccount(accounts, mAccounts.get(0).getAccount());
     69 
     70         mAccounts.clear();
     71         mAccounts.addAll(accounts);
     72 
     73         if (currentInfo != null
     74                 && !mAccounts.isEmpty()
     75                 && !mAccounts.get(0).sameAccount(currentAccount)
     76                 && mAccounts.remove(currentInfo)) {
     77             mAccounts.add(0, currentInfo);
     78         }
     79         notifyDataSetChanged();
     80     }
     81 
     82     public void setCustomLayout(int customLayout) {
     83         mCustomLayout = customLayout;
     84     }
     85 
     86     @Override
     87     public View getView(int position, View convertView, ViewGroup parent) {
     88         final View resultView = convertView != null ? convertView :
     89                 mInflater.inflate(mCustomLayout > 0 ? mCustomLayout :
     90                         R.layout.account_selector_list_item_condensed, parent, false);
     91 
     92         final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
     93         final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
     94         final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
     95 
     96         text1.setText(mAccounts.get(position).getTypeLabel());
     97         text2.setText(mAccounts.get(position).getNameLabel());
     98 
     99         icon.setImageDrawable(mAccounts.get(position).getIcon());
    100 
    101         return resultView;
    102     }
    103 
    104     @Override
    105     public int getCount() {
    106         return mAccounts.size();
    107     }
    108 
    109     @Override
    110     public AccountWithDataSet getItem(int position) {
    111         return mAccounts.get(position).getAccount();
    112     }
    113 
    114     @Override
    115     public long getItemId(int position) {
    116         return position;
    117     }
    118 }
    119 
    120