Home | History | Annotate | Download | only in attachmentchooser
      1 /*
      2  * Copyright (C) 2015 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 package com.android.messaging.ui.attachmentchooser;
     17 
     18 import android.content.Context;
     19 import android.graphics.Rect;
     20 import android.util.AttributeSet;
     21 import android.view.LayoutInflater;
     22 import android.view.TouchDelegate;
     23 import android.view.View;
     24 import android.widget.CheckBox;
     25 import android.widget.FrameLayout;
     26 
     27 import com.android.messaging.R;
     28 import com.android.messaging.datamodel.data.MessagePartData;
     29 import com.android.messaging.ui.AttachmentPreviewFactory;
     30 import com.android.messaging.util.Assert;
     31 import com.google.common.annotations.VisibleForTesting;
     32 
     33 /**
     34  * Shows an item in the attachment picker grid.
     35  */
     36 public class AttachmentGridItemView extends FrameLayout {
     37     public interface HostInterface {
     38         boolean isItemSelected(MessagePartData attachment);
     39         void onItemCheckedChanged(AttachmentGridItemView view, MessagePartData attachment);
     40         void onItemClicked(AttachmentGridItemView view, MessagePartData attachment);
     41     }
     42 
     43     @VisibleForTesting
     44     MessagePartData mAttachmentData;
     45     private FrameLayout mAttachmentViewContainer;
     46     private CheckBox mCheckBox;
     47     private HostInterface mHostInterface;
     48 
     49     public AttachmentGridItemView(final Context context, final AttributeSet attrs) {
     50         super(context, attrs);
     51     }
     52 
     53     @Override
     54     protected void onFinishInflate() {
     55         super.onFinishInflate();
     56         mAttachmentViewContainer = (FrameLayout) findViewById(R.id.attachment_container);
     57         mCheckBox = (CheckBox) findViewById(R.id.checkbox);
     58         mCheckBox.setOnClickListener(new OnClickListener() {
     59             @Override
     60             public void onClick(final View v) {
     61                 mHostInterface.onItemCheckedChanged(AttachmentGridItemView.this, mAttachmentData);
     62             }
     63         });
     64         setOnClickListener(new OnClickListener() {
     65             @Override
     66             public void onClick(final View v) {
     67                 mHostInterface.onItemClicked(AttachmentGridItemView.this, mAttachmentData);
     68             }
     69         });
     70         addOnLayoutChangeListener(new OnLayoutChangeListener() {
     71             @Override
     72             public void onLayoutChange(View v, int left, int top, int right, int bottom,
     73                     int oldLeft, int oldTop, int oldRight, int oldBottom) {
     74                 // Enlarge the clickable region for the checkbox.
     75                 final int touchAreaIncrease = getResources().getDimensionPixelOffset(
     76                         R.dimen.attachment_grid_checkbox_area_increase);
     77                 final Rect region = new Rect();
     78                 mCheckBox.getHitRect(region);
     79                 region.inset(-touchAreaIncrease, -touchAreaIncrease);
     80                 setTouchDelegate(new TouchDelegate(region, mCheckBox));
     81             }
     82         });
     83     }
     84 
     85     @Override
     86     protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
     87         // The grid view auto-fits the columns, so we want to let the height match the width
     88         // to make the attachment preview square.
     89         super.onMeasure(widthMeasureSpec, widthMeasureSpec);
     90     }
     91 
     92     public void bind(final MessagePartData attachment, final HostInterface hostInterface) {
     93         Assert.isTrue(attachment.isAttachment());
     94         mHostInterface = hostInterface;
     95         updateSelectedState();
     96         if (mAttachmentData == null || !mAttachmentData.equals(attachment)) {
     97             mAttachmentData = attachment;
     98             updateAttachmentView();
     99         }
    100     }
    101 
    102     @VisibleForTesting
    103     HostInterface testGetHostInterface() {
    104         return mHostInterface;
    105     }
    106 
    107     public void updateSelectedState() {
    108         mCheckBox.setChecked(mHostInterface.isItemSelected(mAttachmentData));
    109     }
    110 
    111     private void updateAttachmentView() {
    112         mAttachmentViewContainer.removeAllViews();
    113         final LayoutInflater inflater = LayoutInflater.from(getContext());
    114         final View attachmentView = AttachmentPreviewFactory.createAttachmentPreview(inflater,
    115                 mAttachmentData, mAttachmentViewContainer,
    116                 AttachmentPreviewFactory.TYPE_CHOOSER_GRID, true /* startImageRequest */, null);
    117         mAttachmentViewContainer.addView(attachmentView);
    118     }
    119 }
    120