MessageListItem.java revision 91093ee6e8beb200c90e401c96a9c2a1d820bebc
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
33    public long mMessageId;
34    public long mMailboxId;
35    public long mAccountId;
36    public boolean mRead;
37    public boolean mFavorite;
38
39    private MessagesAdapter mAdapter;
40
41    private boolean mDownEvent;
42    private boolean mCachedViewPositions;
43    private int mStarLeft;
44
45    // Padding to increase clickable areas on right of each list item
46    private final static float STAR_PAD = 10.0F;
47
48    public MessageListItem(Context context) {
49        super(context);
50    }
51
52    public MessageListItem(Context context, AttributeSet attrs) {
53        super(context, attrs);
54    }
55
56    public MessageListItem(Context context, AttributeSet attrs, int defStyle) {
57        super(context, attrs, defStyle);
58    }
59
60    /**
61     * Called by the adapter at bindView() time
62     *
63     * @param adapter the adapter that creates this view
64     */
65    public void bindViewInit(MessagesAdapter adapter) {
66        mAdapter = adapter;
67        mCachedViewPositions = false;
68    }
69
70    /**
71     * Overriding this method allows us to "catch" clicks in the checkbox or star
72     * and process them accordingly.
73     */
74    @Override
75    public boolean onTouchEvent(MotionEvent event) {
76        boolean handled = false;
77        int touchX = (int) event.getX();
78
79        if (!mCachedViewPositions) {
80            float paddingScale = getContext().getResources().getDisplayMetrics().density;
81            int starPadding = (int) ((STAR_PAD * paddingScale) + 0.5);
82            mStarLeft = findViewById(R.id.favorite).getLeft() - starPadding;
83            mCachedViewPositions = true;
84        }
85
86        switch (event.getAction()) {
87            case MotionEvent.ACTION_DOWN:
88                mDownEvent = true;
89                if (touchX > mStarLeft) {
90                    handled = true;
91                }
92                break;
93
94            case MotionEvent.ACTION_CANCEL:
95                mDownEvent = false;
96                break;
97
98            case MotionEvent.ACTION_UP:
99                if (mDownEvent) {
100                    if (touchX > mStarLeft) {
101                        mFavorite = !mFavorite;
102                        mAdapter.updateFavorite(this, mFavorite);
103                        handled = true;
104                    }
105                }
106                break;
107        }
108
109        if (handled) {
110            postInvalidate();
111        } else {
112            handled = super.onTouchEvent(event);
113        }
114
115        return handled;
116    }
117}
118