1/**
2 * Copyright (c) 2013, Google Inc.
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 */
16package com.android.mail.ui;
17
18import com.android.mail.R;
19
20import android.widget.ImageView;
21import android.widget.TextView;
22
23import com.android.mail.providers.Account;
24import com.android.mail.utils.Utils;
25
26import android.content.Context;
27
28import android.util.AttributeSet;
29import android.view.View;
30import android.widget.RelativeLayout;
31
32/**
33 * The view for each account in the folder list/drawer.
34 */
35public class AccountItemView extends RelativeLayout {
36    private TextView mAccountTextView;
37    private TextView mUnreadCountTextView;
38    private ImageView mSelectedButton;
39
40    public AccountItemView(Context context) {
41        super(context);
42    }
43
44    public AccountItemView(Context context, AttributeSet attrs) {
45        super(context, attrs);
46    }
47
48    public AccountItemView(Context context, AttributeSet attrs, int defStyle) {
49        super(context, attrs, defStyle);
50    }
51
52    @Override
53    protected void onFinishInflate() {
54        super.onFinishInflate();
55        mAccountTextView = (TextView)findViewById(R.id.name);
56        mUnreadCountTextView = (TextView)findViewById(R.id.unread);
57        mSelectedButton = (ImageView)findViewById(R.id.account_radio_button);
58    }
59
60    /**
61     * Sets the account name and draws the unread count. Depending on the account state (current or
62     * unused), the colors and drawables will change through the call to setSelected for each
63     * necessary element.
64     *
65     * @param account account whose name will be displayed
66     * @param isCurrentAccount true if the account is the one in use, false otherwise
67     * @param count unread count
68     */
69    public void bind(final Account account, final boolean isCurrentAccount, final int count) {
70        mAccountTextView.setText(account.name);
71        setUnreadCount(count);
72        mUnreadCountTextView.setSelected(isCurrentAccount);
73        mAccountTextView.setSelected(isCurrentAccount);
74        mSelectedButton.setSelected(isCurrentAccount);
75    }
76
77    /**
78     * Sets the unread count, taking care to hide/show the textview if the count
79     * is zero/non-zero.
80     */
81    private void setUnreadCount(final int count) {
82        mUnreadCountTextView.setVisibility(count > 0 ? View.VISIBLE : View.GONE);
83        if (count > 0) {
84            mUnreadCountTextView.setText(Utils.getUnreadCountString(getContext(), count));
85        }
86    }
87}
88