MailboxListItem.java revision 6c3cd4e2477eb1c7d1e107b3ea352639f1102933
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.email.activity;
18
19import com.android.email.R;
20import com.android.emailcommon.provider.EmailContent.Mailbox;
21
22import android.content.Context;
23import android.content.res.Resources;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.widget.RelativeLayout;
27import android.widget.TextView;
28
29public class MailboxListItem extends RelativeLayout {
30    // Colors used for drop targets
31    private static Integer sDropUnavailableFgColor;
32    private static Integer sDropAvailableBgColor;
33    private static Integer sTextPrimaryColor;
34    private static Integer sTextSecondaryColor;
35
36    public long mMailboxId;
37    public Integer mMailboxType;
38    /** If {@code true} this item can be used as a drop target. Otherwise, drop is prohibited. */
39    public boolean mIsValidDropTarget;
40    /** If {@code true} this item can be navigated to. Otherwise, it can just be selected. */
41    public boolean mIsNavigable;
42    public MailboxesAdapter mAdapter;
43
44    private Drawable mBackground;
45    private TextView mLabelName;
46    private TextView mLabelCount;
47
48    public MailboxListItem(Context context) {
49        super(context);
50    }
51
52    public MailboxListItem(Context context, AttributeSet attrs) {
53        super(context, attrs);
54    }
55
56    public MailboxListItem(Context context, AttributeSet attrs, int defStyle) {
57        super(context, attrs, defStyle);
58    }
59
60    @Override
61    protected void onFinishInflate() {
62        super.onFinishInflate();
63        mBackground = getBackground();
64        if (sDropAvailableBgColor == null) {
65            Resources res = getResources();
66            sDropAvailableBgColor = res.getColor(R.color.mailbox_drop_available_bg_color);
67            sDropUnavailableFgColor = res.getColor(R.color.mailbox_drop_unavailable_fg_color);
68            sTextPrimaryColor = res.getColor(R.color.text_primary_color);
69            sTextSecondaryColor = res.getColor(R.color.text_secondary_color);
70        }
71        mLabelName = (TextView)findViewById(R.id.mailbox_name);
72        mLabelCount = (TextView)findViewById(R.id.message_count);
73    }
74
75    /**
76     * Whether or not this mailbox item is a drop target. Only valid mailboxes or those
77     * not forbidden by the system (see {@link Mailbox#INVALID_DROP_TARGETS}) will return
78     * {@code true}.
79     */
80    public boolean isDropTarget(long itemMailbox) {
81        return mIsValidDropTarget && (itemMailbox != mMailboxId);
82    }
83
84    /**
85     * Returns whether or not this item can be navigated to.
86     */
87    public boolean isNavigable() {
88        return mIsNavigable;
89    }
90
91    public void setDropTargetBackground(boolean dragInProgress, long itemMailbox) {
92        int labelNameColor = sTextPrimaryColor;
93        int labelCountColor = sTextSecondaryColor;
94        if (dragInProgress) {
95            if (isDropTarget(itemMailbox)) {
96                setBackgroundColor(sDropAvailableBgColor);
97            } else {
98                labelNameColor = sDropUnavailableFgColor;
99                labelCountColor = sDropUnavailableFgColor;
100            }
101        } else {
102            setBackgroundDrawable(mBackground);
103        }
104        mLabelName.setTextColor(labelNameColor);
105        mLabelCount.setTextColor(labelCountColor);
106    }
107}
108