summaryrefslogtreecommitdiffstats
path: root/go/quickstep/src/com/android/quickstep/ThumbnailDrawable.java
blob: a8cc0a1b834118d93e2d845671963544f968ad90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.quickstep;

import static android.graphics.Shader.TileMode.CLAMP;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;

import com.android.launcher3.R;
import com.android.systemui.shared.recents.model.ThumbnailData;

/**
 * Bitmap backed drawable that supports rotating the thumbnail bitmap depending on if the
 * orientation the thumbnail was taken in matches the desired orientation. In addition, the
 * thumbnail always fills into the containing bounds.
 */
public final class ThumbnailDrawable extends Drawable {

    private final Paint mPaint = new Paint();
    private final Matrix mMatrix = new Matrix();
    private final ThumbnailData mThumbnailData;
    private final BitmapShader mShader;
    private final RectF mDestRect = new RectF();
    private final int mCornerRadius;
    private int mRequestedOrientation;

    public ThumbnailDrawable(Resources res, @NonNull ThumbnailData thumbnailData,
            int requestedOrientation) {
        mThumbnailData = thumbnailData;
        mRequestedOrientation = requestedOrientation;
        mCornerRadius = (int) res.getDimension(R.dimen.task_thumbnail_corner_radius);
        mShader = new BitmapShader(mThumbnailData.thumbnail, CLAMP, CLAMP);
        mPaint.setShader(mShader);
        updateMatrix();
    }

    /**
     * Set the requested orientation.
     *
     * @param orientation the orientation we want the thumbnail to be in
     */
    public void setRequestedOrientation(int orientation) {
        if (mRequestedOrientation != orientation) {
            mRequestedOrientation = orientation;
            updateMatrix();
        }
    }

    @Override
    public void draw(Canvas canvas) {
        if (mThumbnailData.thumbnail == null) {
            return;
        }
        canvas.drawRoundRect(mDestRect, mCornerRadius, mCornerRadius, mPaint);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mDestRect.set(bounds);
        updateMatrix();
    }

    @Override
    public void setAlpha(int alpha) {
        final int oldAlpha = mPaint.getAlpha();
        if (alpha != oldAlpha) {
            mPaint.setAlpha(alpha);
            invalidateSelf();
        }
    }

    @Override
    public int getAlpha() {
        return mPaint.getAlpha();
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
        invalidateSelf();
    }

    @Override
    public ColorFilter getColorFilter() {
        return mPaint.getColorFilter();
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    private void updateMatrix() {
        if (mThumbnailData.thumbnail == null) {
            return;
        }
        mMatrix.reset();
        float scaleX;
        float scaleY;
        Rect bounds = getBounds();
        Bitmap thumbnail = mThumbnailData.thumbnail;
        if (mRequestedOrientation != mThumbnailData.orientation) {
            // Rotate and translate so that top left is the same.
            mMatrix.postRotate(90, 0, 0);
            mMatrix.postTranslate(thumbnail.getHeight(), 0);

            scaleX = (float) bounds.width() / thumbnail.getHeight();
            scaleY = (float) bounds.height() / thumbnail.getWidth();
        } else {
            scaleX = (float) bounds.width() / thumbnail.getWidth();
            scaleY = (float) bounds.height() / thumbnail.getHeight();
        }
        // Scale to fill.
        mMatrix.postScale(scaleX, scaleY);
        mShader.setLocalMatrix(mMatrix);
    }
}