Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.util; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 27 | import com.android.contacts.model.account.AccountDisplayInfo; |
| 28 | import com.android.contacts.model.account.AccountDisplayInfoFactory; |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame^] | 29 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 30 | import com.android.contacts.model.AccountTypeManager; |
| 31 | import com.android.contacts.model.account.AccountWithDataSet; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 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; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 41 | private final List<AccountDisplayInfo> mAccountDisplayInfoList; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 42 | private final Context mContext; |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 43 | private int mCustomLayout = -1; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 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 | |
| 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 Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 65 | final List<AccountWithDataSet> accounts = getAccounts(accountListFilter); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 66 | if (currentAccount != null |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 67 | && !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 Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 78 | } |
| 79 | mInflater = LayoutInflater.from(context); |
| 80 | } |
| 81 | |
| 82 | private List<AccountWithDataSet> getAccounts(AccountListFilter accountListFilter) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 83 | final AccountTypeManager typeManager = AccountTypeManager.getInstance(mContext); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 84 | if (accountListFilter == AccountListFilter.ACCOUNTS_GROUP_WRITABLE) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 85 | return new ArrayList<AccountWithDataSet>(typeManager.getGroupWritableAccounts()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 86 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 87 | return new ArrayList<AccountWithDataSet>(typeManager.getAccounts( |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 88 | accountListFilter == AccountListFilter.ACCOUNTS_CONTACT_WRITABLE)); |
| 89 | } |
| 90 | |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 91 | public void setCustomLayout(int customLayout) { |
| 92 | mCustomLayout = customLayout; |
| 93 | } |
| 94 | |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public View getView(int position, View convertView, ViewGroup parent) { |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 97 | final View resultView = convertView != null ? convertView : |
| 98 | mInflater.inflate(mCustomLayout > 0 ? mCustomLayout : |
guanxiongliu | 84a6e65 | 2016-07-29 15:43:13 -0700 | [diff] [blame] | 99 | R.layout.account_selector_list_item_condensed, parent, false); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 100 | |
| 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 Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 105 | text1.setText(mAccountDisplayInfoList.get(position).getTypeLabel()); |
| 106 | text2.setText(mAccountDisplayInfoList.get(position).getNameLabel()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 107 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 108 | icon.setImageDrawable(mAccountDisplayInfoList.get(position).getIcon()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 109 | |
| 110 | return resultView; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int getCount() { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 115 | return mAccountDisplayInfoList.size(); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public AccountWithDataSet getItem(int position) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 120 | return mAccountDisplayInfoList.get(position).getSource(); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public long getItemId(int position) { |
| 125 | return position; |
| 126 | } |
| 127 | } |
| 128 | |