blob: a437929b78cc4706b5b329dee4760157b5914ba2 [file] [log] [blame]
Chiao Cheng53d4bf62012-12-04 14:59:50 -08001/*
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
17package com.android.contacts.common.util;
18
19import android.content.Context;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080020import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.BaseAdapter;
24import android.widget.ImageView;
25import android.widget.TextView;
26
Marcus Hagerottfac695a2016-08-24 17:02:40 -070027import com.android.contacts.common.model.account.AccountDisplayInfo;
28import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080029import com.android.contacts.common.R;
30import com.android.contacts.common.model.AccountTypeManager;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080031import com.android.contacts.common.model.account.AccountWithDataSet;
32
33import java.util.ArrayList;
34import java.util.List;
35
36/**
37 * List-Adapter for Account selection
38 */
39public final class AccountsListAdapter extends BaseAdapter {
40 private final LayoutInflater mInflater;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070041 private final List<AccountDisplayInfo> mAccountDisplayInfoList;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080042 private final Context mContext;
Wenyi Wang80e02582015-11-03 17:26:54 -080043 private int mCustomLayout = -1;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080044
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
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 */
62 public AccountsListAdapter(Context context, AccountListFilter accountListFilter,
63 AccountWithDataSet currentAccount) {
64 mContext = context;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070065 final List<AccountWithDataSet> accounts = getAccounts(accountListFilter);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080066 if (currentAccount != null
Marcus Hagerottfac695a2016-08-24 17:02:40 -070067 && !accounts.isEmpty()
68 && !accounts.get(0).equals(currentAccount)
69 && accounts.remove(currentAccount)) {
70 accounts.add(0, currentAccount);
71 }
72
73 final AccountDisplayInfoFactory factory = new AccountDisplayInfoFactory(context,
74 accounts);
75 mAccountDisplayInfoList = new ArrayList<>(accounts.size());
76 for (AccountWithDataSet account : accounts) {
77 mAccountDisplayInfoList.add(factory.getAccountDisplayInfo(account));
Chiao Cheng53d4bf62012-12-04 14:59:50 -080078 }
79 mInflater = LayoutInflater.from(context);
80 }
81
82 private List<AccountWithDataSet> getAccounts(AccountListFilter accountListFilter) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -070083 final AccountTypeManager typeManager = AccountTypeManager.getInstance(mContext);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080084 if (accountListFilter == AccountListFilter.ACCOUNTS_GROUP_WRITABLE) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -070085 return new ArrayList<AccountWithDataSet>(typeManager.getGroupWritableAccounts());
Chiao Cheng53d4bf62012-12-04 14:59:50 -080086 }
Marcus Hagerottfac695a2016-08-24 17:02:40 -070087 return new ArrayList<AccountWithDataSet>(typeManager.getAccounts(
Chiao Cheng53d4bf62012-12-04 14:59:50 -080088 accountListFilter == AccountListFilter.ACCOUNTS_CONTACT_WRITABLE));
89 }
90
Wenyi Wang80e02582015-11-03 17:26:54 -080091 public void setCustomLayout(int customLayout) {
92 mCustomLayout = customLayout;
93 }
94
Chiao Cheng53d4bf62012-12-04 14:59:50 -080095 @Override
96 public View getView(int position, View convertView, ViewGroup parent) {
Wenyi Wang80e02582015-11-03 17:26:54 -080097 final View resultView = convertView != null ? convertView :
98 mInflater.inflate(mCustomLayout > 0 ? mCustomLayout :
guanxiongliu84a6e652016-07-29 15:43:13 -070099 R.layout.account_selector_list_item_condensed, parent, false);
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800100
101 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
102 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
103 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
104
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700105 text1.setText(mAccountDisplayInfoList.get(position).getTypeLabel());
106 text2.setText(mAccountDisplayInfoList.get(position).getNameLabel());
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800107
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700108 icon.setImageDrawable(mAccountDisplayInfoList.get(position).getIcon());
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800109
110 return resultView;
111 }
112
113 @Override
114 public int getCount() {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700115 return mAccountDisplayInfoList.size();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800116 }
117
118 @Override
119 public AccountWithDataSet getItem(int position) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700120 return mAccountDisplayInfoList.get(position).getSource();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800121 }
122
123 @Override
124 public long getItemId(int position) {
125 return position;
126 }
127}
128