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
17package com.android.contacts.list;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.util.Log;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.LinearLayout;
25import android.widget.RadioButton;
26import android.widget.TextView;
27
28import com.android.contacts.R;
29import com.android.contacts.model.AccountTypeManager;
30import com.android.contacts.model.account.AccountType;
31
32/**
33 * Contact list filter parameters.
34 */
35public class ContactListFilterView extends LinearLayout {
36
37    private static final String TAG = ContactListFilterView.class.getSimpleName();
38
39    private ImageView mIcon;
40    private TextView mAccountType;
41    private TextView mAccountUserName;
42    private RadioButton mRadioButton;
43    private ContactListFilter mFilter;
44    private boolean mSingleAccount;
45
46    public ContactListFilterView(Context context) {
47        super(context);
48    }
49
50    public ContactListFilterView(Context context, AttributeSet attrs) {
51        super(context, attrs);
52    }
53
54    public void setContactListFilter(ContactListFilter filter) {
55        mFilter = filter;
56    }
57
58    public ContactListFilter getContactListFilter() {
59        return mFilter;
60    }
61
62    public void setSingleAccount(boolean flag) {
63        this.mSingleAccount = flag;
64    }
65
66    @Override
67    public void setActivated(boolean activated) {
68        super.setActivated(activated);
69        if (mRadioButton != null) {
70            mRadioButton.setChecked(activated);
71        } else {
72            // We're guarding against null-pointer exceptions,
73            // but otherwise this code is not expected to work
74            // properly if the button hasn't been initialized.
75            Log.wtf(TAG, "radio-button cannot be activated because it is null");
76        }
77    }
78
79    public void bindView(AccountTypeManager accountTypes) {
80        if (mAccountType == null) {
81            mIcon = (ImageView) findViewById(R.id.icon);
82            mAccountType = (TextView) findViewById(R.id.accountType);
83            mAccountUserName = (TextView) findViewById(R.id.accountUserName);
84            mRadioButton = (RadioButton) findViewById(R.id.radioButton);
85            mRadioButton.setChecked(isActivated());
86        }
87
88        if (mFilter == null) {
89            mAccountType.setText(R.string.contactsList);
90            return;
91        }
92
93        mAccountUserName.setVisibility(View.GONE);
94        switch (mFilter.filterType) {
95            case ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS: {
96                bindView(0, R.string.list_filter_all_accounts);
97                break;
98            }
99            case ContactListFilter.FILTER_TYPE_STARRED: {
100                bindView(R.drawable.ic_menu_star_holo_light, R.string.list_filter_all_starred);
101                break;
102            }
103            case ContactListFilter.FILTER_TYPE_CUSTOM: {
104                bindView(R.drawable.ic_menu_settings_holo_light, R.string.list_filter_customize);
105                break;
106            }
107            case ContactListFilter.FILTER_TYPE_WITH_PHONE_NUMBERS_ONLY: {
108                bindView(0, R.string.list_filter_phones);
109                break;
110            }
111            case ContactListFilter.FILTER_TYPE_SINGLE_CONTACT: {
112                bindView(0, R.string.list_filter_single);
113                break;
114            }
115            case ContactListFilter.FILTER_TYPE_ACCOUNT: {
116                mAccountUserName.setVisibility(View.VISIBLE);
117                mIcon.setVisibility(View.VISIBLE);
118                if (mFilter.icon != null) {
119                    mIcon.setImageDrawable(mFilter.icon);
120                } else {
121                    mIcon.setImageResource(R.drawable.unknown_source);
122                }
123                final AccountType accountType =
124                        accountTypes.getAccountType(mFilter.accountType, mFilter.dataSet);
125                mAccountUserName.setText(mFilter.accountName);
126                mAccountType.setText(accountType.getDisplayLabel(getContext()));
127                break;
128            }
129        }
130    }
131
132    private void bindView(int iconResource, int textResource) {
133        if (iconResource != 0) {
134            mIcon.setVisibility(View.VISIBLE);
135            mIcon.setImageResource(iconResource);
136        } else {
137            mIcon.setVisibility(View.GONE);
138        }
139
140        mAccountType.setText(textResource);
141    }
142}
143