1e420de7fca9005d7084be275f268962a14094a22Chiao Cheng/*
2e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Copyright (C) 2010 The Android Open Source Project
3e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
4e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
5e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * you may not use this file except in compliance with the License.
6e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * You may obtain a copy of the License at
7e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
8e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
9e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
10e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Unless required by applicable law or agreed to in writing, software
11e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
12e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * See the License for the specific language governing permissions and
14e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * limitations under the License.
15e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */
16e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
17e420de7fca9005d7084be275f268962a14094a22Chiao Chengpackage com.android.contacts.common.util;
18e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
19e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.content.Context;
20e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.text.TextUtils.TruncateAt;
21e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.view.LayoutInflater;
22e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.view.View;
23e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.view.ViewGroup;
24e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.widget.BaseAdapter;
25e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.widget.ImageView;
26e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.widget.TextView;
27e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
28e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.R;
29e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
30e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.account.AccountType;
31e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.account.AccountWithDataSet;
32e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
33e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport java.util.ArrayList;
34e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport java.util.List;
35e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
36e420de7fca9005d7084be275f268962a14094a22Chiao Cheng/**
37e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * List-Adapter for Account selection
38e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */
39e420de7fca9005d7084be275f268962a14094a22Chiao Chengpublic final class AccountsListAdapter extends BaseAdapter {
40e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private final LayoutInflater mInflater;
41e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private final List<AccountWithDataSet> mAccounts;
42e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private final AccountTypeManager mAccountTypes;
43e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private final Context mContext;
44e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
45e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    /**
46e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * Filters that affect the list of accounts that is displayed by this adapter.
47e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     */
48e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public enum AccountListFilter {
49e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        ALL_ACCOUNTS,                   // All read-only and writable accounts
50e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        ACCOUNTS_CONTACT_WRITABLE,      // Only where the account type is contact writable
51e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        ACCOUNTS_GROUP_WRITABLE         // Only accounts where the account type is group writable
52e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
53e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
54e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public AccountsListAdapter(Context context, AccountListFilter accountListFilter) {
55e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        this(context, accountListFilter, null);
56e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
57e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
58e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    /**
59e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * @param currentAccount the Account currently selected by the user, which should come
60e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * first in the list. Can be null.
61e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     */
62e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public AccountsListAdapter(Context context, AccountListFilter accountListFilter,
63e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            AccountWithDataSet currentAccount) {
64e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        mContext = context;
65e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        mAccountTypes = AccountTypeManager.getInstance(context);
66e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        mAccounts = getAccounts(accountListFilter);
67e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        if (currentAccount != null
68e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                && !mAccounts.isEmpty()
69e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                && !mAccounts.get(0).equals(currentAccount)
70e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                && mAccounts.remove(currentAccount)) {
71e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            mAccounts.add(0, currentAccount);
72e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        }
73e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        mInflater = LayoutInflater.from(context);
74e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
75e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
76e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private List<AccountWithDataSet> getAccounts(AccountListFilter accountListFilter) {
77e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        if (accountListFilter == AccountListFilter.ACCOUNTS_GROUP_WRITABLE) {
78e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            return new ArrayList<AccountWithDataSet>(mAccountTypes.getGroupWritableAccounts());
79e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        }
80e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        return new ArrayList<AccountWithDataSet>(mAccountTypes.getAccounts(
81e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                accountListFilter == AccountListFilter.ACCOUNTS_CONTACT_WRITABLE));
82e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
83e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
84e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    @Override
85e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public View getView(int position, View convertView, ViewGroup parent) {
86e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final View resultView = convertView != null ? convertView
87e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
88e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
89e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
90e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
91e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
92e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
93e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final AccountWithDataSet account = mAccounts.get(position);
94e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final AccountType accountType = mAccountTypes.getAccountType(account.type, account.dataSet);
95e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
96e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        text1.setText(accountType.getDisplayLabel(mContext));
97e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
98e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        // For email addresses, we don't want to truncate at end, which might cut off the domain
99e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        // name.
100e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        text2.setText(account.name);
101e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        text2.setEllipsize(TruncateAt.MIDDLE);
102e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
103e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        icon.setImageDrawable(accountType.getDisplayIcon(mContext));
104e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
105e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        return resultView;
106e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
107e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
108e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    @Override
109e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public int getCount() {
110e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        return mAccounts.size();
111e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
112e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
113e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    @Override
114e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public AccountWithDataSet getItem(int position) {
115e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        return mAccounts.get(position);
116e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
117e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
118e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    @Override
119e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public long getItemId(int position) {
120e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        return position;
121e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
122e420de7fca9005d7084be275f268962a14094a22Chiao Cheng}
123e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
124