MailboxListItem.java revision 3a8a1b451dc21eb0e1b05e04b1d4df8971187c84
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.Account;
21import com.android.emailcommon.provider.Mailbox;
22
23import android.content.Context;
24import android.content.res.Resources;
25import android.graphics.drawable.Drawable;
26import android.util.AttributeSet;
27import android.widget.RelativeLayout;
28import android.widget.TextView;
29
30public class MailboxListItem extends RelativeLayout {
31    // Colors used for drop targets
32    private static Integer sDropUnavailableFgColor;
33    private static Integer sDropAvailableBgColor;
34    private static Integer sTextPrimaryColor;
35    private static Integer sTextSecondaryColor;
36    private static Integer sDropTrashBgColor;
37
38    /**
39     * Owner account ID for the mailbox, {@link Account#ACCOUNT_ID_COMBINED_VIEW} for a combined
40     * mailbox, or the ID for the current account, if it's an account row.
41     */
42    public long mAccountId;
43
44    /**
45     * ID for the current mailbox, or {@link Mailbox#NO_MAILBOX} if it's an account row.
46     */
47    public long mMailboxId;
48    public Integer mMailboxType;
49    /** If {@code true} this item can be used as a drop target. Otherwise, drop is prohibited. */
50    public boolean mIsValidDropTarget;
51    /** If {@code true} this item can be navigated to. Otherwise, it can just be selected. */
52    public boolean mIsNavigable;
53    public MailboxesAdapter mAdapter;
54
55    private Drawable mBackground;
56    private TextView mLabelName;
57    private TextView mLabelCount;
58
59    /**
60     * Drawable for an active item for D&D.  Note the drawable has state, so we can't share it
61     * between items.
62     * DO NOT use this directly; use {@link #getDropActiveBgDrawable()} instead, as it's lazily-
63     * initialized.
64     */
65    private Drawable mDropActiveBgDrawable;
66
67    public MailboxListItem(Context context) {
68        super(context);
69    }
70
71    public MailboxListItem(Context context, AttributeSet attrs) {
72        super(context, attrs);
73    }
74
75    public MailboxListItem(Context context, AttributeSet attrs, int defStyle) {
76        super(context, attrs, defStyle);
77    }
78
79    @Override
80    protected void onFinishInflate() {
81        super.onFinishInflate();
82        mBackground = getBackground();
83        if (sDropAvailableBgColor == null) {
84            Resources res = getResources();
85            sDropAvailableBgColor = res.getColor(R.color.mailbox_drop_available_bg_color);
86            sDropUnavailableFgColor = res.getColor(R.color.mailbox_drop_unavailable_fg_color);
87            sDropTrashBgColor = res.getColor(R.color.mailbox_drop_destructive_bg_color);
88            sTextPrimaryColor = res.getColor(R.color.text_primary_color);
89            sTextSecondaryColor = res.getColor(R.color.text_secondary_color);
90        }
91        mLabelName = (TextView)findViewById(R.id.mailbox_name);
92        mLabelCount = (TextView)findViewById(R.id.message_count);
93    }
94
95    /**
96     * Whether or not this mailbox item is a drop target. Only valid mailboxes or those
97     * not forbidden by the system (see {@link Mailbox#INVALID_DROP_TARGETS}) will return
98     * {@code true}.
99     */
100    public boolean isDropTarget(long itemMailboxId) {
101        return mIsValidDropTarget && (itemMailboxId != mMailboxId);
102    }
103
104    /**
105     * Returns whether or not this item can be navigated to.
106     */
107    public boolean isNavigable() {
108        return mIsNavigable;
109    }
110
111    private Drawable getDropActiveBgDrawable() {
112        if (mDropActiveBgDrawable == null) {
113            mDropActiveBgDrawable =
114                getContext().getResources().getDrawable(R.drawable.list_activated_holo);
115        }
116        return mDropActiveBgDrawable;
117    }
118
119    @Override
120    public void setBackgroundDrawable(Drawable d) {
121        // Don't override with the same instance.
122        // If we don't do the check, something bad will happen to the fade-out animation for
123        // the selected to non-selected transition.  (Looks like if you re-set the same
124        // StateListDrawable instance, it'll get confused.)
125        if (d != getBackground()) {
126            super.setBackgroundDrawable(d);
127        }
128    }
129
130    /**
131     * Set the "trash" drop target background.
132     */
133    public void setDropTrashBackground() {
134        setBackgroundColor(sDropTrashBgColor);
135    }
136
137    /**
138     * Set the "active" drop target background.  (Used for the items that the user is hovering over)
139     */
140    public void setDropActiveBackground() {
141        setBackgroundDrawable(getDropActiveBgDrawable());
142    }
143
144    public void setDropTargetBackground(boolean dragInProgress, long itemMailbox) {
145        int labelNameColor = sTextPrimaryColor;
146        int labelCountColor = sTextSecondaryColor;
147
148        boolean isBackgroundSet = false;
149        if (dragInProgress) {
150            if (isDropTarget(itemMailbox)) {
151                setBackgroundColor(sDropAvailableBgColor);
152                isBackgroundSet = true;
153            } else {
154                labelNameColor = sDropUnavailableFgColor;
155                labelCountColor = sDropUnavailableFgColor;
156            }
157        }
158        if (!isBackgroundSet) {
159            // Drag not in progress, or it's not a drop target.
160            setBackgroundDrawable(mBackground);
161        }
162        mLabelName.setTextColor(labelNameColor);
163        mLabelCount.setTextColor(labelCountColor);
164    }
165}
166