blob: 94a7c29ac158054b06e7611c58524a670e6e166c [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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.util;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080018
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
Arthur Wang3f6a2442016-12-05 14:51:59 -080027import com.android.contacts.R;
Marcus Hagerottc2093f32016-12-12 10:18:12 -080028import com.android.contacts.list.ContactListFilter;
Gary Mai69c182a2016-12-05 13:07:03 -080029import com.android.contacts.model.AccountTypeManager;
Gary Mai0a49afa2016-12-05 15:53:58 -080030import com.android.contacts.model.account.AccountDisplayInfo;
31import com.android.contacts.model.account.AccountDisplayInfoFactory;
Marcus Hagerottc2093f32016-12-12 10:18:12 -080032import com.android.contacts.model.account.AccountType;
Gary Mai69c182a2016-12-05 13:07:03 -080033import com.android.contacts.model.account.AccountWithDataSet;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080034
35import java.util.ArrayList;
36import java.util.List;
37
38/**
39 * List-Adapter for Account selection
40 */
41public final class AccountsListAdapter extends BaseAdapter {
42 private final LayoutInflater mInflater;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070043 private final List<AccountDisplayInfo> mAccountDisplayInfoList;
Marcus Hagerottc2093f32016-12-12 10:18:12 -080044 private final List<AccountWithDataSet> mAccounts;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080045 private final Context mContext;
Wenyi Wang80e02582015-11-03 17:26:54 -080046 private int mCustomLayout = -1;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080047
Chiao Cheng53d4bf62012-12-04 14:59:50 -080048 public enum AccountListFilter {
Marcus Hagerottc2093f32016-12-12 10:18:12 -080049 ALL_ACCOUNTS {
50 @Override
51 public List<AccountWithDataSet> getAccounts(Context context) {
52 return AccountTypeManager.getInstance(context).getAccounts(false);
53 }
54 },
55 ACCOUNTS_CONTACT_WRITABLE {
56 @Override
57 public List<AccountWithDataSet> getAccounts(Context context) {
58 return AccountTypeManager.getInstance(context).getAccounts(true);
59 }
60 },
61 ACCOUNTS_GROUP_WRITABLE {
62 @Override
63 public List<AccountWithDataSet> getAccounts(Context context) {
64 return AccountTypeManager.getInstance(context).getGroupWritableAccounts();
65 }
66 };
67
68 public abstract List<AccountWithDataSet> getAccounts(Context context);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080069 }
70
Marcus Hagerottc2093f32016-12-12 10:18:12 -080071 public AccountsListAdapter(Context context, AccountListFilter filter) {
72 this(context, filter.getAccounts(context), null);
73 }
74
75 public AccountsListAdapter(Context context, AccountListFilter filter,
76 AccountWithDataSet currentAccount) {
77 this(context, filter.getAccounts(context), currentAccount);
78 }
79
80 public AccountsListAdapter(Context context, List<AccountWithDataSet> accounts) {
81 this(context, accounts, null);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080082 }
83
84 /**
85 * @param currentAccount the Account currently selected by the user, which should come
86 * first in the list. Can be null.
87 */
Marcus Hagerottc2093f32016-12-12 10:18:12 -080088 public AccountsListAdapter(Context context, List<AccountWithDataSet> accounts,
Chiao Cheng53d4bf62012-12-04 14:59:50 -080089 AccountWithDataSet currentAccount) {
90 mContext = context;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080091 if (currentAccount != null
Marcus Hagerottfac695a2016-08-24 17:02:40 -070092 && !accounts.isEmpty()
93 && !accounts.get(0).equals(currentAccount)
94 && accounts.remove(currentAccount)) {
95 accounts.add(0, currentAccount);
96 }
97
98 final AccountDisplayInfoFactory factory = new AccountDisplayInfoFactory(context,
99 accounts);
100 mAccountDisplayInfoList = new ArrayList<>(accounts.size());
101 for (AccountWithDataSet account : accounts) {
102 mAccountDisplayInfoList.add(factory.getAccountDisplayInfo(account));
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800103 }
104 mInflater = LayoutInflater.from(context);
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800105
Marcus Hagerottc2093f32016-12-12 10:18:12 -0800106 mAccounts = accounts;
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800107 }
108
Wenyi Wang80e02582015-11-03 17:26:54 -0800109 public void setCustomLayout(int customLayout) {
110 mCustomLayout = customLayout;
111 }
112
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800113 @Override
114 public View getView(int position, View convertView, ViewGroup parent) {
Wenyi Wang80e02582015-11-03 17:26:54 -0800115 final View resultView = convertView != null ? convertView :
116 mInflater.inflate(mCustomLayout > 0 ? mCustomLayout :
guanxiongliu84a6e652016-07-29 15:43:13 -0700117 R.layout.account_selector_list_item_condensed, parent, false);
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800118
119 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
120 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
121 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
122
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700123 text1.setText(mAccountDisplayInfoList.get(position).getTypeLabel());
124 text2.setText(mAccountDisplayInfoList.get(position).getNameLabel());
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800125
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700126 icon.setImageDrawable(mAccountDisplayInfoList.get(position).getIcon());
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800127
128 return resultView;
129 }
130
131 @Override
132 public int getCount() {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700133 return mAccountDisplayInfoList.size();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800134 }
135
136 @Override
137 public AccountWithDataSet getItem(int position) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700138 return mAccountDisplayInfoList.get(position).getSource();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800139 }
140
141 @Override
142 public long getItemId(int position) {
143 return position;
144 }
145}
146