Home | History | Annotate | Download | only in activity
      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 
     17 package com.android.email.activity;
     18 
     19 import com.android.email.R;
     20 
     21 import android.content.Context;
     22 import android.util.AttributeSet;
     23 import android.view.MotionEvent;
     24 import 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  */
     31 public 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     public boolean mSelected;
     39 
     40     private boolean mAllowBatch;
     41     private MessageList.MessageListAdapter mAdapter;
     42 
     43     private boolean mDownEvent;
     44     private boolean mCachedViewPositions;
     45     private int mCheckRight;
     46     private int mStarLeft;
     47 
     48     // Padding to increase clickable areas on left & right of each list item
     49     private final static float CHECKMARK_PAD = 10.0F;
     50     private final static float STAR_PAD = 10.0F;
     51 
     52     public MessageListItem(Context context) {
     53         super(context);
     54     }
     55 
     56     public MessageListItem(Context context, AttributeSet attrs) {
     57         super(context, attrs);
     58     }
     59 
     60     public MessageListItem(Context context, AttributeSet attrs, int defStyle) {
     61         super(context, attrs, defStyle);
     62     }
     63 
     64     /**
     65      * Called by the adapter at bindView() time
     66      *
     67      * @param adapter the adapter that creates this view
     68      * @param allowBatch true if multi-select is enabled for this list
     69      */
     70     public void bindViewInit(MessageList.MessageListAdapter adapter, boolean allowBatch) {
     71         mAdapter = adapter;
     72         mAllowBatch = allowBatch;
     73         mCachedViewPositions = false;
     74     }
     75 
     76     /**
     77      * Overriding this method allows us to "catch" clicks in the checkbox or star
     78      * and process them accordingly.
     79      */
     80     @Override
     81     public boolean onTouchEvent(MotionEvent event) {
     82         boolean handled = false;
     83         int touchX = (int) event.getX();
     84 
     85         if (!mCachedViewPositions) {
     86             float paddingScale = getContext().getResources().getDisplayMetrics().density;
     87             int checkPadding = (int) ((CHECKMARK_PAD * paddingScale) + 0.5);
     88             int starPadding = (int) ((STAR_PAD * paddingScale) + 0.5);
     89             mCheckRight = findViewById(R.id.selected).getRight() + checkPadding;
     90             mStarLeft = findViewById(R.id.favorite).getLeft() - starPadding;
     91             mCachedViewPositions = true;
     92         }
     93 
     94         switch (event.getAction()) {
     95             case MotionEvent.ACTION_DOWN:
     96                 mDownEvent = true;
     97                 if ((mAllowBatch && touchX < mCheckRight) || touchX > mStarLeft) {
     98                     handled = true;
     99                 }
    100                 break;
    101 
    102             case MotionEvent.ACTION_CANCEL:
    103                 mDownEvent = false;
    104                 break;
    105 
    106             case MotionEvent.ACTION_UP:
    107                 if (mDownEvent) {
    108                     if (mAllowBatch && touchX < mCheckRight) {
    109                         mSelected = !mSelected;
    110                         mAdapter.updateSelected(this, mSelected);
    111                         handled = true;
    112                     } else if (touchX > mStarLeft) {
    113                         mFavorite = !mFavorite;
    114                         mAdapter.updateFavorite(this, mFavorite);
    115                         handled = true;
    116                     }
    117                 }
    118                 break;
    119         }
    120 
    121         if (handled) {
    122             postInvalidate();
    123         } else {
    124             handled = super.onTouchEvent(event);
    125         }
    126 
    127         return handled;
    128     }
    129 }
    130