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.content.Context;
24import android.database.Cursor;
25import android.net.Uri;
26import android.provider.DocumentsContract;
27import android.provider.DocumentsContract.Document;
28import android.text.format.Formatter;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.ImageView;
32import android.widget.LinearLayout;
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 ListDocumentHolder extends DocumentHolder {
41    final TextView mTitle;
42    final LinearLayout mDetails;  // Container of date/size/summary
43    final TextView mDate;
44    final TextView mSize;
45    final TextView mSummary;
46    final ImageView mIconMime;
47    final ImageView mIconThumb;
48    final ImageView mIconCheck;
49    final IconHelper mIconHelper;
50
51    public ListDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
52        super(context, parent, R.layout.item_doc_list);
53
54        mTitle = (TextView) itemView.findViewById(android.R.id.title);
55        mDate = (TextView) itemView.findViewById(R.id.date);
56        mSize = (TextView) itemView.findViewById(R.id.size);
57        mSummary = (TextView) itemView.findViewById(android.R.id.summary);
58        mIconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
59        mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
60        mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
61        // Warning: mDetails view doesn't exists in layout-sw720dp-land layout
62        mDetails = (LinearLayout) itemView.findViewById(R.id.line2);
63
64        mIconHelper = iconHelper;
65    }
66
67    @Override
68    public void setSelected(boolean selected, boolean animate) {
69        // We always want to make sure our check box disappears if we're not selected,
70        // even if the item is disabled. But it should be an error (see assert below)
71        // to be set to selected && be disabled.
72        float checkAlpha = selected ? 1f : 0f;
73        if (animate) {
74            mIconCheck.animate().alpha(checkAlpha).start();
75        } else {
76            mIconCheck.setAlpha(checkAlpha);
77        }
78
79        if (!itemView.isEnabled()) {
80            assert(!selected);
81            return;
82        }
83
84        super.setSelected(selected, animate);
85
86        if (animate) {
87            mIconMime.animate().alpha(1f - checkAlpha).start();
88            mIconThumb.animate().alpha(1f - checkAlpha).start();
89        } else {
90            mIconMime.setAlpha(1f - checkAlpha);
91            mIconThumb.setAlpha(1f - checkAlpha);
92        }
93    }
94
95    @Override
96    public void setEnabled(boolean enabled) {
97        super.setEnabled(enabled);
98
99        // Text colors enabled/disabled is handle via a color set.
100        final float imgAlpha = enabled ? 1f : DISABLED_ALPHA;
101        mIconMime.setAlpha(imgAlpha);
102        mIconThumb.setAlpha(imgAlpha);
103    }
104
105    /**
106     * Bind this view to the given document for display.
107     * @param cursor Pointing to the item to be bound.
108     * @param modelId The model ID of the item.
109     * @param state Current display state.
110     */
111    @Override
112    public void bind(Cursor cursor, String modelId, State state) {
113        assert(cursor != null);
114
115        this.modelId = modelId;
116
117        final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
118        final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
119        final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
120        final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
121        final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
122        final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
123        final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
124        final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
125        final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
126        final boolean isDirectory = Document.MIME_TYPE_DIR.equals(docMimeType);
127
128        mIconHelper.stopLoading(mIconThumb);
129
130        mIconMime.animate().cancel();
131        mIconMime.setAlpha(1f);
132        mIconThumb.animate().cancel();
133        mIconThumb.setAlpha(0f);
134
135        final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
136        mIconHelper.loadThumbnail(uri, docMimeType, docFlags, docIcon, mIconThumb, mIconMime, null);
137
138        mTitle.setText(docDisplayName, TextView.BufferType.SPANNABLE);
139        mTitle.setVisibility(View.VISIBLE);
140
141
142        boolean hasDetails = false;
143        if (isDirectory) {
144            // Note, we don't show any details for any directory...ever.
145            hasDetails = false;
146        } else {
147            if (docSummary != null) {
148                hasDetails = true;
149                mSummary.setText(docSummary);
150                mSummary.setVisibility(View.VISIBLE);
151            } else {
152                mSummary.setVisibility(View.INVISIBLE);
153            }
154
155            if (docLastModified > 0) {
156                hasDetails = true;
157                mDate.setText(Shared.formatTime(mContext, docLastModified));
158            } else {
159                mDate.setText(null);
160            }
161
162            if (state.showSize && docSize > -1) {
163                hasDetails = true;
164                mSize.setVisibility(View.VISIBLE);
165                mSize.setText(Formatter.formatFileSize(mContext, docSize));
166            } else {
167                mSize.setVisibility(View.GONE);
168            }
169        }
170
171        // mDetails view doesn't exists in layout-sw720dp-land layout
172        if (mDetails != null) {
173            mDetails.setVisibility(hasDetails ? View.VISIBLE : View.GONE);
174        }
175    }
176}
177