1b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah/**
2b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * Copyright (c) 2013, Google Inc.
3b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah *
4b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * Licensed under the Apache License, Version 2.0 (the "License");
5b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * you may not use this file except in compliance with the License.
6b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * You may obtain a copy of the License at
7b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah *
8b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah *     http://www.apache.org/licenses/LICENSE-2.0
9b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah *
10b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * Unless required by applicable law or agreed to in writing, software
11b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * distributed under the License is distributed on an "AS IS" BASIS,
12b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * See the License for the specific language governing permissions and
14b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah * limitations under the License.
15b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah */
16b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shahpackage com.android.mail.ui;
17b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
1827abcb2d58204bf9fbb0f72cdd103ccce6fc90c3Andrew Sappersteinimport android.content.Context;
19c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdonimport android.graphics.Typeface;
20c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdonimport android.text.TextUtils;
2127abcb2d58204bf9fbb0f72cdd103ccce6fc90c3Andrew Sappersteinimport android.util.AttributeSet;
2227abcb2d58204bf9fbb0f72cdd103ccce6fc90c3Andrew Sappersteinimport android.view.View;
23b905f0ea18ee0dc5774adb29c57cd7c122058c71Rohan Shahimport android.widget.ImageView;
24c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdonimport android.widget.LinearLayout;
25b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shahimport android.widget.TextView;
26b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
272b806edc62eb8e83c77edc471fda4652281a15c4James Lemieuximport com.android.bitmap.BitmapCache;
2827abcb2d58204bf9fbb0f72cdd103ccce6fc90c3Andrew Sappersteinimport com.android.mail.R;
29c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdonimport com.android.mail.bitmap.AccountAvatarDrawable;
30c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdonimport com.android.mail.bitmap.ContactResolver;
31b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shahimport com.android.mail.providers.Account;
32b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
33b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah/**
34671133b7412ea98fc325fd88944e0d670d6992c6Rohan Shah * The view for each account in the folder list/drawer.
35b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah */
36c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdonpublic class AccountItemView extends LinearLayout {
37c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon    private TextView mAccountDisplayName;
38c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon    private TextView mAccountAddress;
39c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon    private ImageView mAvatar;
40c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon    private ImageView mCheckmark;
41b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
42b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    public AccountItemView(Context context) {
43b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah        super(context);
44b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    }
45b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
46b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    public AccountItemView(Context context, AttributeSet attrs) {
47b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah        super(context, attrs);
48b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    }
49b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
50b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    public AccountItemView(Context context, AttributeSet attrs, int defStyle) {
51b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah        super(context, attrs, defStyle);
52b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    }
53b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
54b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    @Override
55b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    protected void onFinishInflate() {
56b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah        super.onFinishInflate();
57c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        mAccountDisplayName = (TextView)findViewById(R.id.account_display_name);
58c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        mAccountAddress = (TextView)findViewById(R.id.account_address);
59c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        mAvatar = (ImageView)findViewById(R.id.avatar);
60c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        mCheckmark = (ImageView)findViewById(R.id.checkmark);
61b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    }
62b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah
63671133b7412ea98fc325fd88944e0d670d6992c6Rohan Shah    /**
64c1e4506e27921c69e8b4441e6ff4c1b105a37b06Rohan Shah     * Sets the account name and draws the unread count. Depending on the account state (current or
65c1e4506e27921c69e8b4441e6ff4c1b105a37b06Rohan Shah     * unused), the colors and drawables will change through the call to setSelected for each
66c1e4506e27921c69e8b4441e6ff4c1b105a37b06Rohan Shah     * necessary element.
67671133b7412ea98fc325fd88944e0d670d6992c6Rohan Shah     *
68671133b7412ea98fc325fd88944e0d670d6992c6Rohan Shah     * @param account account whose name will be displayed
69074a5a1c102176f2d370a568df48eea67bcaf02dRohan Shah     * @param isCurrentAccount true if the account is the one in use, false otherwise
70671133b7412ea98fc325fd88944e0d670d6992c6Rohan Shah     */
71c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon    public void bind(final Context context, final Account account, final boolean isCurrentAccount,
72c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            final BitmapCache imagesCache, final ContactResolver contactResolver) {
73c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        if (!TextUtils.isEmpty(account.getSenderName())) {
74c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountDisplayName.setText(account.getSenderName());
75c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountAddress.setText(account.getEmailAddress());
76c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountAddress.setVisibility(View.VISIBLE);
77c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        } else if (!TextUtils.isEmpty(account.getDisplayName()) &&
78c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon                !TextUtils.equals(account.getDisplayName(), account.getEmailAddress())) {
79c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountDisplayName.setText(account.getDisplayName());
80c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountAddress.setText(account.getEmailAddress());
81c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountAddress.setVisibility(View.VISIBLE);
82c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        } else {
83c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountDisplayName.setText(account.getEmailAddress());
84c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountAddress.setVisibility(View.GONE);
85b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah        }
86c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        if (isCurrentAccount) {
87c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mCheckmark.setVisibility(View.VISIBLE);
88c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountDisplayName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
89fe6c6a7b8efa95d8efe80209d2668aca3ce11448Tony Mantler            final int blackColor = getResources().getColor(R.color.text_color_black);
90fe6c6a7b8efa95d8efe80209d2668aca3ce11448Tony Mantler            mAccountDisplayName.setTextColor(blackColor);
91fe6c6a7b8efa95d8efe80209d2668aca3ce11448Tony Mantler            mAccountAddress.setTextColor(blackColor);
92c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        } else {
93c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mCheckmark.setVisibility(View.GONE);
94c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon            mAccountDisplayName.setTypeface(Typeface.DEFAULT);
95fe6c6a7b8efa95d8efe80209d2668aca3ce11448Tony Mantler            final int greyColor = getResources().getColor(R.color.text_color_grey);
96fe6c6a7b8efa95d8efe80209d2668aca3ce11448Tony Mantler            mAccountDisplayName.setTextColor(greyColor);
97fe6c6a7b8efa95d8efe80209d2668aca3ce11448Tony Mantler            mAccountAddress.setTextColor(greyColor);
98c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        }
99c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon
100c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        ImageView v = (ImageView) mAvatar.findViewById(R.id.avatar);
101c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        AccountAvatarDrawable drawable = new AccountAvatarDrawable(
102c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon                context.getResources(), imagesCache, contactResolver);
103c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        final int size = context.getResources().getDimensionPixelSize(
104c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon                R.dimen.account_avatar_dimension);
105c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        drawable.setDecodeDimensions(size, size);
106c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        drawable.bind(account.getSenderName(), account.getEmailAddress());
107c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon        v.setImageDrawable(drawable);
108c7849b23a73d699b5e7f199f0a3afce5b9dee7a6Martin Hibdon
109b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah    }
110b958701ff6a98a7d95930e61d91104bb5db3299aRohan Shah}
111