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 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 27 | import com.android.contacts.R; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 28 | import com.android.contacts.list.ContactListFilter; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 29 | import com.android.contacts.model.AccountTypeManager; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 30 | import com.android.contacts.model.account.AccountDisplayInfo; |
| 31 | import com.android.contacts.model.account.AccountDisplayInfoFactory; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 32 | import com.android.contacts.model.account.AccountType; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 33 | import com.android.contacts.model.account.AccountWithDataSet; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * List-Adapter for Account selection |
| 40 | */ |
| 41 | public final class AccountsListAdapter extends BaseAdapter { |
| 42 | private final LayoutInflater mInflater; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 43 | private final List<AccountDisplayInfo> mAccountDisplayInfoList; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 44 | private final List<AccountWithDataSet> mAccounts; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 45 | private final Context mContext; |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 46 | private int mCustomLayout = -1; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 47 | |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 48 | public enum AccountListFilter { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 49 | 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 Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 71 | 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 Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 82 | } |
| 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 Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 88 | public AccountsListAdapter(Context context, List<AccountWithDataSet> accounts, |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 89 | AccountWithDataSet currentAccount) { |
| 90 | mContext = context; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 91 | if (currentAccount != null |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 92 | && !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 Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 103 | } |
| 104 | mInflater = LayoutInflater.from(context); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 105 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 106 | mAccounts = accounts; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 109 | public void setCustomLayout(int customLayout) { |
| 110 | mCustomLayout = customLayout; |
| 111 | } |
| 112 | |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public View getView(int position, View convertView, ViewGroup parent) { |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 115 | final View resultView = convertView != null ? convertView : |
| 116 | mInflater.inflate(mCustomLayout > 0 ? mCustomLayout : |
guanxiongliu | 84a6e65 | 2016-07-29 15:43:13 -0700 | [diff] [blame] | 117 | R.layout.account_selector_list_item_condensed, parent, false); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 118 | |
| 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 Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 123 | text1.setText(mAccountDisplayInfoList.get(position).getTypeLabel()); |
| 124 | text2.setText(mAccountDisplayInfoList.get(position).getNameLabel()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 125 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 126 | icon.setImageDrawable(mAccountDisplayInfoList.get(position).getIcon()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 127 | |
| 128 | return resultView; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int getCount() { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 133 | return mAccountDisplayInfoList.size(); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public AccountWithDataSet getItem(int position) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 138 | return mAccountDisplayInfoList.get(position).getSource(); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public long getItemId(int position) { |
| 143 | return position; |
| 144 | } |
| 145 | } |
| 146 | |