blob: 005bb8d787cc1a9e8cab86faebef062f40161176 [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;
Gary Mai69c182a2016-12-05 13:07:03 -080028import com.android.contacts.model.AccountTypeManager;
Marcus Hagerott75895e72016-12-12 17:21:57 -080029import com.android.contacts.model.account.AccountInfo;
Gary Mai69c182a2016-12-05 13:07:03 -080030import com.android.contacts.model.account.AccountWithDataSet;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080031
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * List-Adapter for Account selection
37 */
38public final class AccountsListAdapter extends BaseAdapter {
39 private final LayoutInflater mInflater;
Marcus Hagerott75895e72016-12-12 17:21:57 -080040 private final List<AccountInfo> mAccounts;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080041 private final Context mContext;
Wenyi Wang80e02582015-11-03 17:26:54 -080042 private int mCustomLayout = -1;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080043
Chiao Cheng53d4bf62012-12-04 14:59:50 -080044 public enum AccountListFilter {
Marcus Hagerottc2093f32016-12-12 10:18:12 -080045 ALL_ACCOUNTS {
46 @Override
Marcus Hagerott75895e72016-12-12 17:21:57 -080047 public List<AccountWithDataSet> getSourceAccounts(Context context) {
Marcus Hagerottc2093f32016-12-12 10:18:12 -080048 return AccountTypeManager.getInstance(context).getAccounts(false);
49 }
50 },
51 ACCOUNTS_CONTACT_WRITABLE {
52 @Override
Marcus Hagerott75895e72016-12-12 17:21:57 -080053 public List<AccountWithDataSet> getSourceAccounts(Context context) {
Marcus Hagerottc2093f32016-12-12 10:18:12 -080054 return AccountTypeManager.getInstance(context).getAccounts(true);
55 }
56 },
57 ACCOUNTS_GROUP_WRITABLE {
58 @Override
Marcus Hagerott75895e72016-12-12 17:21:57 -080059 public List<AccountWithDataSet> getSourceAccounts(Context context) {
Marcus Hagerottc2093f32016-12-12 10:18:12 -080060 return AccountTypeManager.getInstance(context).getGroupWritableAccounts();
61 }
62 };
63
Marcus Hagerott75895e72016-12-12 17:21:57 -080064 private List<AccountInfo> getAccounts(Context context) {
65 final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(context);
66 final List<AccountInfo> result = new ArrayList<>();
67 final List<AccountWithDataSet> sourceAccounts = getSourceAccounts(context);
68 for (AccountWithDataSet account : sourceAccounts) {
69 result.add(accountTypeManager.getAccountInfoForAccount(account));
70 }
71 return result;
72 }
73
74 public abstract List<AccountWithDataSet> getSourceAccounts(Context context);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080075 }
76
Marcus Hagerottc2093f32016-12-12 10:18:12 -080077 public AccountsListAdapter(Context context, AccountListFilter filter) {
78 this(context, filter.getAccounts(context), null);
79 }
80
81 public AccountsListAdapter(Context context, AccountListFilter filter,
82 AccountWithDataSet currentAccount) {
83 this(context, filter.getAccounts(context), currentAccount);
84 }
85
Marcus Hagerott75895e72016-12-12 17:21:57 -080086 public AccountsListAdapter(Context context, List<AccountInfo> accounts) {
Marcus Hagerottc2093f32016-12-12 10:18:12 -080087 this(context, accounts, null);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080088 }
89
90 /**
91 * @param currentAccount the Account currently selected by the user, which should come
92 * first in the list. Can be null.
93 */
Marcus Hagerott75895e72016-12-12 17:21:57 -080094 public AccountsListAdapter(Context context, List<AccountInfo> accounts,
Chiao Cheng53d4bf62012-12-04 14:59:50 -080095 AccountWithDataSet currentAccount) {
96 mContext = context;
Marcus Hagerott75895e72016-12-12 17:21:57 -080097
98 final AccountInfo currentInfo = AccountInfo.getAccount(accounts, currentAccount);
99 if (currentInfo != null
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700100 && !accounts.isEmpty()
Marcus Hagerott75895e72016-12-12 17:21:57 -0800101 && !accounts.get(0).sameAccount(currentAccount)
102 && accounts.remove(currentInfo)) {
103 accounts.add(0, currentInfo);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700104 }
105
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800106 mInflater = LayoutInflater.from(context);
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800107
Marcus Hagerottc2093f32016-12-12 10:18:12 -0800108 mAccounts = accounts;
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800109 }
110
Wenyi Wang80e02582015-11-03 17:26:54 -0800111 public void setCustomLayout(int customLayout) {
112 mCustomLayout = customLayout;
113 }
114
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800115 @Override
116 public View getView(int position, View convertView, ViewGroup parent) {
Wenyi Wang80e02582015-11-03 17:26:54 -0800117 final View resultView = convertView != null ? convertView :
118 mInflater.inflate(mCustomLayout > 0 ? mCustomLayout :
guanxiongliu84a6e652016-07-29 15:43:13 -0700119 R.layout.account_selector_list_item_condensed, parent, false);
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800120
121 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
122 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
123 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
124
Marcus Hagerott75895e72016-12-12 17:21:57 -0800125 text1.setText(mAccounts.get(position).getTypeLabel());
126 text2.setText(mAccounts.get(position).getNameLabel());
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800127
Marcus Hagerott75895e72016-12-12 17:21:57 -0800128 icon.setImageDrawable(mAccounts.get(position).getIcon());
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800129
130 return resultView;
131 }
132
133 @Override
134 public int getCount() {
Marcus Hagerott75895e72016-12-12 17:21:57 -0800135 return mAccounts.size();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800136 }
137
138 @Override
139 public AccountWithDataSet getItem(int position) {
Marcus Hagerott75895e72016-12-12 17:21:57 -0800140 return mAccounts.get(position).getAccount();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800141 }
142
143 @Override
144 public long getItemId(int position) {
145 return position;
146 }
147}
148