MessageListItem.java revision bdf84d57e623499d9c250fcc6fa1ba1f947cd2b5
1/*
2 * Copyright (C) 2009 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;
20
21import android.content.Context;
22import android.util.AttributeSet;
23import android.view.MotionEvent;
24import android.widget.RelativeLayout;
25
26/**
27 * This custom View is the list item for the MessageList activity, and serves two purposes:
28 * 1.  It's a container to store message metadata (e.g. the ids of the message, mailbox, & account)
29 * 2.  It handles internal clicks such as the checkbox or the favorite star
30 */
31public class MessageListItem extends RelativeLayout {
32    // Note: messagesAdapter directly fiddles with these fields.
33    /* package */ long mMessageId;
34    /* package */ long mMailboxId;
35    /* package */ long mAccountId;
36    /* package */ boolean mRead;
37    /* package */ boolean mFavorite;
38
39    private MessagesAdapter mAdapter;
40
41    private boolean mDownEvent;
42    private boolean mCachedViewPositions;
43    private int mCheckRight;
44    private int mStarLeft;
45
46    // Padding to increase clickable areas on left & right of each list item
47    private final static float CHECKMARK_PAD = 10.0F;
48    private final static float STAR_PAD = 10.0F;
49
50    public MessageListItem(Context context) {
51        super(context);
52    }
53
54    public MessageListItem(Context context, AttributeSet attrs) {
55        super(context, attrs);
56    }
57
58    public MessageListItem(Context context, AttributeSet attrs, int defStyle) {
59        super(context, attrs, defStyle);
60    }
61
62    /**
63     * Called by the adapter at bindView() time
64     *
65     * @param adapter the adapter that creates this view
66     */
67    public void bindViewInit(MessagesAdapter adapter) {
68        mAdapter = adapter;
69        mCachedViewPositions = false;
70    }
71
72    /**
73     * Overriding this method allows us to "catch" clicks in the checkbox or star
74     * and process them accordingly.
75     */
76    @Override
77    public boolean onTouchEvent(MotionEvent event) {
78        boolean handled = false;
79        int touchX = (int) event.getX();
80
81        if (!mCachedViewPositions) {
82            final float paddingScale = getContext().getResources().getDisplayMetrics().density;
83            final int checkPadding = (int) ((CHECKMARK_PAD * paddingScale) + 0.5);
84            final int starPadding = (int) ((STAR_PAD * paddingScale) + 0.5);
85            mCheckRight = findViewById(R.id.selected).getRight() + checkPadding;
86            mStarLeft = findViewById(R.id.favorite).getLeft() - starPadding;
87            mCachedViewPositions = true;
88        }
89
90        switch (event.getAction()) {
91            case MotionEvent.ACTION_DOWN:
92                mDownEvent = true;
93                if ((touchX < mCheckRight) || (touchX > mStarLeft)) {
94                    handled = true;
95                }
96                break;
97
98            case MotionEvent.ACTION_CANCEL:
99                mDownEvent = false;
100                break;
101
102            case MotionEvent.ACTION_UP:
103                if (mDownEvent) {
104                    if (touchX < mCheckRight) {
105                        mAdapter.toggleSelected(this);
106                        handled = true;
107                    } else if (touchX > mStarLeft) {
108                        mFavorite = !mFavorite;
109                        mAdapter.updateFavorite(this, mFavorite);
110                        handled = true;
111                    }
112                }
113                break;
114        }
115
116        if (handled) {
117            postInvalidate();
118        } else {
119            handled = super.onTouchEvent(event);
120        }
121
122        return handled;
123    }
124}
125