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
17package com.android.documentsui.dirlist;
18
19import static com.android.documentsui.model.DocumentInfo.getCursorInt;
20import static com.android.documentsui.model.DocumentInfo.getCursorLong;
21import static com.android.documentsui.model.DocumentInfo.getCursorString;
22
23import android.annotation.ColorInt;
24import android.content.Context;
25import android.database.Cursor;
26import android.net.Uri;
27import android.provider.DocumentsContract;
28import android.provider.DocumentsContract.Document;
29import android.text.format.Formatter;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ImageView;
33import android.widget.TextView;
34
35import com.android.documentsui.R;
36import com.android.documentsui.RootCursorWrapper;
37import com.android.documentsui.Shared;
38import com.android.documentsui.State;
39
40final class GridDocumentHolder extends DocumentHolder {
41
42    private static boolean mHideTitles;
43
44    final TextView mTitle;
45    final TextView mDate;
46    final TextView mSize;
47    final ImageView mIconMimeLg;
48    final ImageView mIconMimeSm;
49    final ImageView mIconThumb;
50    final ImageView mIconCheck;
51    final IconHelper mIconHelper;
52
53    private final @ColorInt int mDisabledBgColor;
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
60        mTitle = (TextView) itemView.findViewById(android.R.id.title);
61        mDate = (TextView) itemView.findViewById(R.id.date);
62        mSize = (TextView) itemView.findViewById(R.id.size);
63        mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg);
64        mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm);
65        mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
66        mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
67
68        mIconHelper = iconHelper;
69    }
70
71    @Override
72    public void setSelected(boolean selected, boolean animate) {
73        // We always want to make sure our check box disappears if we're not selected,
74        // even if the item is disabled. This is because this object can be reused
75        // and this method will be called to setup initial state.
76        float checkAlpha = selected ? 1f : 0f;
77        if (animate) {
78            mIconCheck.animate().alpha(checkAlpha).start();
79        } else {
80            mIconCheck.setAlpha(checkAlpha);
81        }
82
83        // But it should be an error to be set to selected && be disabled.
84        if (!itemView.isEnabled()) {
85            assert(!selected);
86            return;
87        }
88
89        super.setSelected(selected, animate);
90
91        if (animate) {
92            mIconMimeSm.animate().alpha(1f - checkAlpha).start();
93        } else {
94            mIconMimeSm.setAlpha(1f - checkAlpha);
95        }
96    }
97
98    public void setEnabled(boolean enabled) {
99        super.setEnabled(enabled);
100
101        // Text colors enabled/disabled is handle via a color set.
102        itemView.setBackgroundColor(enabled ? mDefaultBgColor : mDisabledBgColor);
103        float imgAlpha = enabled ? 1f : DISABLED_ALPHA;
104
105        mIconMimeLg.setAlpha(imgAlpha);
106        mIconMimeSm.setAlpha(imgAlpha);
107        mIconThumb.setAlpha(imgAlpha);
108    }
109
110    /**
111     * Bind this view to the given document for display.
112     * @param cursor Pointing to the item to be bound.
113     * @param modelId The model ID of the item.
114     * @param state Current display state.
115     */
116    public void bind(Cursor cursor, String modelId, State state) {
117        assert(cursor != null);
118
119        this.modelId = modelId;
120
121        final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
122        final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
123        final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
124        final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
125        final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
126        final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
127        final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
128        final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
129
130        mIconHelper.stopLoading(mIconThumb);
131
132        mIconMimeLg.animate().cancel();
133        mIconMimeLg.setAlpha(1f);
134        mIconThumb.animate().cancel();
135        mIconThumb.setAlpha(0f);
136
137        final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
138        mIconHelper.loadThumbnail(uri, docMimeType, docFlags, docIcon, mIconThumb, mIconMimeLg,
139                mIconMimeSm);
140
141        if (mHideTitles) {
142            mTitle.setVisibility(View.GONE);
143        } else {
144            mTitle.setText(docDisplayName, TextView.BufferType.SPANNABLE);
145            mTitle.setVisibility(View.VISIBLE);
146        }
147
148        if (docLastModified == -1) {
149            mDate.setText(null);
150        } else {
151            mDate.setText(Shared.formatTime(mContext, docLastModified));
152        }
153
154        if (!state.showSize || Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
155            mSize.setVisibility(View.GONE);
156        } else {
157            mSize.setVisibility(View.VISIBLE);
158            mSize.setText(Formatter.formatFileSize(mContext, docSize));
159        }
160    }
161}
162