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