Home | History | Annotate | Download | only in dirlist
      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 
     17 package com.android.documentsui.dirlist;
     18 
     19 import static com.android.documentsui.base.DocumentInfo.getCursorLong;
     20 import static com.android.documentsui.base.DocumentInfo.getCursorString;
     21 
     22 import android.annotation.ColorInt;
     23 import android.content.Context;
     24 import android.database.Cursor;
     25 import android.graphics.Rect;
     26 import android.provider.DocumentsContract.Document;
     27 import android.text.format.Formatter;
     28 import android.view.MotionEvent;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.ImageView;
     32 import android.widget.TextView;
     33 
     34 import com.android.documentsui.R;
     35 import com.android.documentsui.base.DocumentInfo;
     36 import com.android.documentsui.base.Shared;
     37 import com.android.documentsui.roots.RootCursorWrapper;
     38 
     39 final class GridDocumentHolder extends DocumentHolder {
     40 
     41     final TextView mTitle;
     42     final TextView mDate;
     43     final TextView mDetails;
     44     final ImageView mIconMimeLg;
     45     final ImageView mIconMimeSm;
     46     final ImageView mIconThumb;
     47     final ImageView mIconCheck;
     48     final IconHelper mIconHelper;
     49 
     50     private final @ColorInt int mDisabledBgColor;
     51     private final @ColorInt int mDefaultBgColor;
     52     // This is used in as a convenience in our bind method.
     53     private final DocumentInfo mDoc = new DocumentInfo();
     54 
     55     public GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
     56         super(context, parent, R.layout.item_doc_grid);
     57 
     58         mDisabledBgColor = context.getColor(R.color.item_doc_background_disabled);
     59         mDefaultBgColor = context.getColor(R.color.item_doc_background);
     60 
     61         mTitle = (TextView) itemView.findViewById(android.R.id.title);
     62         mDate = (TextView) itemView.findViewById(R.id.date);
     63         mDetails = (TextView) itemView.findViewById(R.id.details);
     64         mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg);
     65         mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm);
     66         mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
     67         mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
     68 
     69         mIconHelper = iconHelper;
     70     }
     71 
     72     @Override
     73     public void setSelected(boolean selected, boolean animate) {
     74         // We always want to make sure our check box disappears if we're not selected,
     75         // even if the item is disabled. This is because this object can be reused
     76         // and this method will be called to setup initial state.
     77         float checkAlpha = selected ? 1f : 0f;
     78         if (animate) {
     79             fade(mIconMimeSm, checkAlpha).start();
     80             fade(mIconCheck, checkAlpha).start();
     81         } else {
     82             mIconCheck.setAlpha(checkAlpha);
     83         }
     84 
     85         // But it should be an error to be set to selected && be disabled.
     86         if (!itemView.isEnabled()) {
     87             assert(!selected);
     88             return;
     89         }
     90 
     91         super.setSelected(selected, animate);
     92 
     93         if (animate) {
     94             fade(mIconMimeSm, 1f - checkAlpha).start();
     95         } else {
     96             mIconMimeSm.setAlpha(1f - checkAlpha);
     97         }
     98     }
     99 
    100     @Override
    101     public void setEnabled(boolean enabled) {
    102         super.setEnabled(enabled);
    103 
    104         // Text colors enabled/disabled is handle via a color set.
    105         itemView.setBackgroundColor(enabled ? mDefaultBgColor : mDisabledBgColor);
    106         float imgAlpha = enabled ? 1f : DISABLED_ALPHA;
    107 
    108         mIconMimeLg.setAlpha(imgAlpha);
    109         mIconMimeSm.setAlpha(imgAlpha);
    110         mIconThumb.setAlpha(imgAlpha);
    111     }
    112 
    113     @Override
    114     public boolean inDragRegion(MotionEvent event) {
    115      // Entire grid box should be draggable
    116         return true;
    117     }
    118 
    119     @Override
    120     public boolean inSelectRegion(MotionEvent event) {
    121         Rect iconRect = new Rect();
    122         mIconMimeSm.getGlobalVisibleRect(iconRect);
    123 
    124         return iconRect.contains((int) event.getRawX(), (int) event.getRawY());
    125     }
    126 
    127     /**
    128      * Bind this view to the given document for display.
    129      * @param cursor Pointing to the item to be bound.
    130      * @param modelId The model ID of the item.
    131      */
    132     @Override
    133     public void bind(Cursor cursor, String modelId) {
    134         assert(cursor != null);
    135 
    136         mModelId = modelId;
    137 
    138         mDoc.updateFromCursor(cursor, getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY));
    139 
    140         mIconHelper.stopLoading(mIconThumb);
    141 
    142         mIconMimeLg.animate().cancel();
    143         mIconMimeLg.setAlpha(1f);
    144         mIconThumb.animate().cancel();
    145         mIconThumb.setAlpha(0f);
    146 
    147         mIconHelper.load(mDoc, mIconThumb, mIconMimeLg, mIconMimeSm);
    148 
    149         mTitle.setText(mDoc.displayName, TextView.BufferType.SPANNABLE);
    150         mTitle.setVisibility(View.VISIBLE);
    151 
    152         // If file is partial, we want to show summary field as that's more relevant than fileSize
    153         // and date
    154         if (mDoc.isPartial()) {
    155             final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
    156             mDetails.setVisibility(View.VISIBLE);
    157             mDate.setText(null);
    158             mDetails.setText(docSummary);
    159         } else {
    160             if (mDoc.lastModified == -1) {
    161                 mDate.setText(null);
    162             } else {
    163                 mDate.setText(Shared.formatTime(mContext, mDoc.lastModified));
    164             }
    165 
    166             final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
    167             if (mDoc.isDirectory() || docSize == -1) {
    168                 mDetails.setVisibility(View.GONE);
    169             } else {
    170                 mDetails.setVisibility(View.VISIBLE);
    171                 mDetails.setText(Formatter.formatFileSize(mContext, docSize));
    172             }
    173         }
    174     }
    175 }
    176