• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
45     /**
46      * Filters that affect the list of accounts that is displayed by this adapter.
47      */
48     public enum AccountListFilter {
49         ALL_ACCOUNTS,                   // All read-only and writable accounts
50         ACCOUNTS_CONTACT_WRITABLE,      // Only where the account type is contact writable
51         ACCOUNTS_GROUP_WRITABLE         // Only accounts where the account type is group writable
52     }
53 
AccountsListAdapter(Context context, AccountListFilter accountListFilter)54     public AccountsListAdapter(Context context, AccountListFilter accountListFilter) {
55         this(context, accountListFilter, null);
56     }
57 
58     /**
59      * @param currentAccount the Account currently selected by the user, which should come
60      * first in the list. Can be null.
61      */
AccountsListAdapter(Context context, AccountListFilter accountListFilter, AccountWithDataSet currentAccount)62     public AccountsListAdapter(Context context, AccountListFilter accountListFilter,
63             AccountWithDataSet currentAccount) {
64         mContext = context;
65         mAccountTypes = AccountTypeManager.getInstance(context);
66         mAccounts = getAccounts(accountListFilter);
67         if (currentAccount != null
68                 && !mAccounts.isEmpty()
69                 && !mAccounts.get(0).equals(currentAccount)
70                 && mAccounts.remove(currentAccount)) {
71             mAccounts.add(0, currentAccount);
72         }
73         mInflater = LayoutInflater.from(context);
74     }
75 
getAccounts(AccountListFilter accountListFilter)76     private List<AccountWithDataSet> getAccounts(AccountListFilter accountListFilter) {
77         if (accountListFilter == AccountListFilter.ACCOUNTS_GROUP_WRITABLE) {
78             return new ArrayList<AccountWithDataSet>(mAccountTypes.getGroupWritableAccounts());
79         }
80         return new ArrayList<AccountWithDataSet>(mAccountTypes.getAccounts(
81                 accountListFilter == AccountListFilter.ACCOUNTS_CONTACT_WRITABLE));
82     }
83 
84     @Override
getView(int position, View convertView, ViewGroup parent)85     public View getView(int position, View convertView, ViewGroup parent) {
86         final View resultView = convertView != null ? convertView
87                 : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
88 
89         final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
90         final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
91         final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
92 
93         final AccountWithDataSet account = mAccounts.get(position);
94         final AccountType accountType = mAccountTypes.getAccountType(account.type, account.dataSet);
95 
96         text1.setText(accountType.getDisplayLabel(mContext));
97 
98         // For email addresses, we don't want to truncate at end, which might cut off the domain
99         // name.
100         text2.setText(account.name);
101         text2.setEllipsize(TruncateAt.MIDDLE);
102 
103         icon.setImageDrawable(accountType.getDisplayIcon(mContext));
104 
105         return resultView;
106     }
107 
108     @Override
getCount()109     public int getCount() {
110         return mAccounts.size();
111     }
112 
113     @Override
getItem(int position)114     public AccountWithDataSet getItem(int position) {
115         return mAccounts.get(position);
116     }
117 
118     @Override
getItemId(int position)119     public long getItemId(int position) {
120         return position;
121     }
122 }
123 
124