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; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 28 | import com.android.contacts.model.AccountTypeManager; |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 29 | import com.android.contacts.model.account.AccountInfo; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 30 | import com.android.contacts.model.account.AccountWithDataSet; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
| 36 | * List-Adapter for Account selection |
| 37 | */ |
| 38 | public final class AccountsListAdapter extends BaseAdapter { |
| 39 | private final LayoutInflater mInflater; |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 40 | private final List<AccountInfo> mAccounts; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 41 | private final Context mContext; |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 42 | private int mCustomLayout = -1; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 43 | |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 44 | public enum AccountListFilter { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 45 | ALL_ACCOUNTS { |
| 46 | @Override |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 47 | public List<AccountWithDataSet> getSourceAccounts(Context context) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 48 | return AccountTypeManager.getInstance(context).getAccounts(false); |
| 49 | } |
| 50 | }, |
| 51 | ACCOUNTS_CONTACT_WRITABLE { |
| 52 | @Override |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 53 | public List<AccountWithDataSet> getSourceAccounts(Context context) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 54 | return AccountTypeManager.getInstance(context).getAccounts(true); |
| 55 | } |
| 56 | }, |
| 57 | ACCOUNTS_GROUP_WRITABLE { |
| 58 | @Override |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 59 | public List<AccountWithDataSet> getSourceAccounts(Context context) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 60 | return AccountTypeManager.getInstance(context).getGroupWritableAccounts(); |
| 61 | } |
| 62 | }; |
| 63 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 64 | 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 Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 77 | 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 Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 86 | public AccountsListAdapter(Context context, List<AccountInfo> accounts) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 87 | this(context, accounts, null); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 88 | } |
| 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 Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 94 | public AccountsListAdapter(Context context, List<AccountInfo> accounts, |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 95 | AccountWithDataSet currentAccount) { |
| 96 | mContext = context; |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 97 | |
| 98 | final AccountInfo currentInfo = AccountInfo.getAccount(accounts, currentAccount); |
| 99 | if (currentInfo != null |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 100 | && !accounts.isEmpty() |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 101 | && !accounts.get(0).sameAccount(currentAccount) |
| 102 | && accounts.remove(currentInfo)) { |
| 103 | accounts.add(0, currentInfo); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 106 | mInflater = LayoutInflater.from(context); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 107 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 108 | mAccounts = accounts; |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 111 | public void setCustomLayout(int customLayout) { |
| 112 | mCustomLayout = customLayout; |
| 113 | } |
| 114 | |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 115 | @Override |
| 116 | public View getView(int position, View convertView, ViewGroup parent) { |
Wenyi Wang | 80e0258 | 2015-11-03 17:26:54 -0800 | [diff] [blame] | 117 | final View resultView = convertView != null ? convertView : |
| 118 | mInflater.inflate(mCustomLayout > 0 ? mCustomLayout : |
guanxiongliu | 84a6e65 | 2016-07-29 15:43:13 -0700 | [diff] [blame] | 119 | R.layout.account_selector_list_item_condensed, parent, false); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 120 | |
| 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 Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 125 | text1.setText(mAccounts.get(position).getTypeLabel()); |
| 126 | text2.setText(mAccounts.get(position).getNameLabel()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 127 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 128 | icon.setImageDrawable(mAccounts.get(position).getIcon()); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 129 | |
| 130 | return resultView; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int getCount() { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 135 | return mAccounts.size(); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public AccountWithDataSet getItem(int position) { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 140 | return mAccounts.get(position).getAccount(); |
Chiao Cheng | 53d4bf6 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public long getItemId(int position) { |
| 145 | return position; |
| 146 | } |
| 147 | } |
| 148 | |