1 /*
2  * Copyright (C) 2016 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.dialer.callcomposer;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.FrameLayout;
24 import android.widget.ImageView;
25 import android.widget.ImageView.ScaleType;
26 import com.bumptech.glide.Glide;
27 import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy;
28 import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
29 import com.bumptech.glide.request.RequestOptions;
30 import java.util.concurrent.TimeUnit;
31 
32 /** Shows an item in the gallery picker grid view. Hosts an FileImageView with a checkbox. */
33 public class GalleryGridItemView extends FrameLayout {
34 
35   private final GalleryGridItemData data = new GalleryGridItemData();
36 
37   private ImageView image;
38   private View checkbox;
39   private View gallery;
40   private String currentFilePath;
41   private boolean isGallery;
42 
GalleryGridItemView(Context context, AttributeSet attrs)43   public GalleryGridItemView(Context context, AttributeSet attrs) {
44     super(context, attrs);
45   }
46 
47   @Override
onFinishInflate()48   protected void onFinishInflate() {
49     super.onFinishInflate();
50     image = (ImageView) findViewById(R.id.image);
51     checkbox = findViewById(R.id.checkbox);
52     gallery = findViewById(R.id.gallery);
53 
54     image.setClipToOutline(true);
55     checkbox.setClipToOutline(true);
56     gallery.setClipToOutline(true);
57   }
58 
59   @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)60   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
61     // The grid view auto-fit the columns, so we want to let the height match the width
62     // to make the image square.
63     super.onMeasure(widthMeasureSpec, widthMeasureSpec);
64   }
65 
getData()66   public GalleryGridItemData getData() {
67     return data;
68   }
69 
70   @Override
setSelected(boolean selected)71   public void setSelected(boolean selected) {
72     if (selected) {
73       checkbox.setVisibility(VISIBLE);
74       int paddingPx = getResources().getDimensionPixelSize(R.dimen.gallery_item_selected_padding);
75       setPadding(paddingPx, paddingPx, paddingPx, paddingPx);
76     } else {
77       checkbox.setVisibility(GONE);
78       int paddingPx = getResources().getDimensionPixelOffset(R.dimen.gallery_item_padding);
79       setPadding(paddingPx, paddingPx, paddingPx, paddingPx);
80     }
81   }
82 
isGallery()83   public boolean isGallery() {
84     return isGallery;
85   }
86 
showGallery(boolean show)87   public void showGallery(boolean show) {
88     isGallery = show;
89     gallery.setVisibility(show ? VISIBLE : INVISIBLE);
90   }
91 
bind(Cursor cursor)92   public void bind(Cursor cursor) {
93     data.bind(cursor);
94     showGallery(false);
95     updateImageView();
96   }
97 
updateImageView()98   private void updateImageView() {
99     image.setScaleType(ScaleType.CENTER_CROP);
100 
101     if (currentFilePath == null || !currentFilePath.equals(data.getFilePath())) {
102       currentFilePath = data.getFilePath();
103 
104       // Downloads/loads an image from the given URI so that the image's largest dimension is
105       // between 1/2 the given dimensions and the given dimensions, with no restrictions on the
106       // image's smallest dimension. We skip the memory cache, but glide still applies it's disk
107       // cache to optimize loads.
108       Glide.with(getContext())
109           .load(data.getFileUri())
110           .apply(RequestOptions.downsampleOf(DownsampleStrategy.AT_MOST).skipMemoryCache(true))
111           .transition(DrawableTransitionOptions.withCrossFade())
112           .into(image);
113     }
114     long dateModifiedSeconds = data.getDateModifiedSeconds();
115     if (dateModifiedSeconds > 0) {
116       image.setContentDescription(
117           getResources()
118               .getString(
119                   R.string.gallery_item_description,
120                   TimeUnit.SECONDS.toMillis(dateModifiedSeconds)));
121     } else {
122       image.setContentDescription(
123           getResources().getString(R.string.gallery_item_description_no_date));
124     }
125   }
126 }
127