1 /*
2  * Copyright (C) 2017 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.documentsui.inspector;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Point;
22 import android.graphics.drawable.Drawable;
23 import android.util.AttributeSet;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.ImageView;
27 import android.widget.ImageView.ScaleType;
28 import android.widget.RelativeLayout;
29 
30 import com.android.documentsui.IconUtils;
31 import com.android.documentsui.ProviderExecutor;
32 import com.android.documentsui.R;
33 import com.android.documentsui.ThumbnailLoader;
34 import com.android.documentsui.base.Display;
35 import com.android.documentsui.base.DocumentInfo;
36 import com.android.documentsui.inspector.InspectorController.HeaderDisplay;
37 
38 import java.util.function.Consumer;
39 
40 import javax.annotation.Nullable;
41 
42 /**
43  * Organizes and displays the title and thumbnail for a given document
44  */
45 public final class HeaderView extends RelativeLayout implements HeaderDisplay {
46 
47     private final Context mContext;
48     private final View mHeader;
49     private ImageView mThumbnail;
50     private Point mImageDimensions;
51 
HeaderView(Context context)52     public HeaderView(Context context) {
53         this(context, null);
54     }
55 
HeaderView(Context context, AttributeSet attrs)56     public HeaderView(Context context, AttributeSet attrs) {
57         this(context, attrs, 0);
58     }
59 
HeaderView(Context context, AttributeSet attrs, int defStyleAttr)60     public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
61         super(context, attrs, defStyleAttr);
62         LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
63                 Context.LAYOUT_INFLATER_SERVICE);
64         mContext = context;
65         mHeader = inflater.inflate(R.layout.inspector_header, null);
66         mThumbnail = (ImageView) mHeader.findViewById(R.id.inspector_thumbnail);
67 
68         int width = (int) Display.screenWidth((Activity)context);
69         int height = mContext.getResources().getDimensionPixelSize(R.dimen.inspector_header_height);
70         mImageDimensions = new Point(width, height);
71         addView(mHeader);
72     }
73 
74     @Override
accept(DocumentInfo info)75     public void accept(DocumentInfo info) {
76         loadHeaderImage(info);
77     }
78 
loadHeaderImage(DocumentInfo doc)79     private void loadHeaderImage(DocumentInfo doc) {
80         if (!doc.isThumbnailSupported()) {
81             showImage(doc, null);
82         } else {
83             Consumer<Bitmap> callback = new Consumer<Bitmap>() {
84                 @Override
85                 public void accept(Bitmap thumbnail) {
86                     showImage(doc, thumbnail);
87                 }
88             };
89             // load the thumbnail async.
90             final ThumbnailLoader task = new ThumbnailLoader(doc.derivedUri, doc.userId, mThumbnail,
91                     mImageDimensions, doc.lastModified, callback, false);
92             task.executeOnExecutor(ProviderExecutor.forAuthority(doc.derivedUri.getAuthority()),
93                     doc.derivedUri);
94         }
95     }
96 
97     /**
98      * Shows the supplied image, falling back to a mimetype icon if the image is null.
99      */
showImage(DocumentInfo info, @Nullable Bitmap thumbnail)100     private void showImage(DocumentInfo info, @Nullable Bitmap thumbnail) {
101         if (thumbnail != null) {
102             mThumbnail.setScaleType(ScaleType.CENTER_CROP);
103             mThumbnail.setImageBitmap(thumbnail);
104         } else {
105             Drawable mimeIcon = IconUtils.loadMimeIcon(mContext, info.mimeType);
106             mThumbnail.setScaleType(ScaleType.FIT_CENTER);
107             mThumbnail.setImageDrawable(mimeIcon);
108         }
109         mThumbnail.animate().alpha(1.0f).start();
110     }
111 }