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 android.content.Context;
19import android.graphics.Typeface;
20import android.text.TextUtils;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.LinearLayout;
25import android.widget.TextView;
26
27import com.android.bitmap.BitmapCache;
28import com.android.mail.R;
29import com.android.mail.bitmap.AccountAvatarDrawable;
30import com.android.mail.bitmap.ContactResolver;
31import com.android.mail.providers.Account;
32
33/**
34 * The view for each account in the folder list/drawer.
35 */
36public class AccountItemView extends LinearLayout {
37    private TextView mAccountDisplayName;
38    private TextView mAccountAddress;
39    private ImageView mAvatar;
40    private ImageView mCheckmark;
41
42    public AccountItemView(Context context) {
43        super(context);
44    }
45
46    public AccountItemView(Context context, AttributeSet attrs) {
47        super(context, attrs);
48    }
49
50    public AccountItemView(Context context, AttributeSet attrs, int defStyle) {
51        super(context, attrs, defStyle);
52    }
53
54    @Override
55    protected void onFinishInflate() {
56        super.onFinishInflate();
57        mAccountDisplayName = (TextView)findViewById(R.id.account_display_name);
58        mAccountAddress = (TextView)findViewById(R.id.account_address);
59        mAvatar = (ImageView)findViewById(R.id.avatar);
60        mCheckmark = (ImageView)findViewById(R.id.checkmark);
61    }
62
63    /**
64     * Sets the account name and draws the unread count. Depending on the account state (current or
65     * unused), the colors and drawables will change through the call to setSelected for each
66     * necessary element.
67     *
68     * @param account account whose name will be displayed
69     * @param isCurrentAccount true if the account is the one in use, false otherwise
70     */
71    public void bind(final Context context, final Account account, final boolean isCurrentAccount,
72            final BitmapCache imagesCache, final ContactResolver contactResolver) {
73        if (!TextUtils.isEmpty(account.getSenderName())) {
74            mAccountDisplayName.setText(account.getSenderName());
75            mAccountAddress.setText(account.getEmailAddress());
76            mAccountAddress.setVisibility(View.VISIBLE);
77        } else if (!TextUtils.isEmpty(account.getDisplayName()) &&
78                !TextUtils.equals(account.getDisplayName(), account.getEmailAddress())) {
79            mAccountDisplayName.setText(account.getDisplayName());
80            mAccountAddress.setText(account.getEmailAddress());
81            mAccountAddress.setVisibility(View.VISIBLE);
82        } else {
83            mAccountDisplayName.setText(account.getEmailAddress());
84            mAccountAddress.setVisibility(View.GONE);
85        }
86        if (isCurrentAccount) {
87            mCheckmark.setVisibility(View.VISIBLE);
88            mAccountDisplayName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
89        } else {
90            mCheckmark.setVisibility(View.GONE);
91            mAccountDisplayName.setTypeface(Typeface.DEFAULT);
92        }
93
94        ImageView v = (ImageView) mAvatar.findViewById(R.id.avatar);
95        AccountAvatarDrawable drawable = new AccountAvatarDrawable(
96                context.getResources(), imagesCache, contactResolver);
97        final int size = context.getResources().getDimensionPixelSize(
98                R.dimen.account_avatar_dimension);
99        drawable.setDecodeDimensions(size, size);
100        drawable.bind(account.getSenderName(), account.getEmailAddress());
101        v.setImageDrawable(drawable);
102
103    }
104}
105