blob: fa5dea5947574d8cb9f308cc472768e85ed9dd41 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.picker;
17
18import android.content.Context;
19import android.graphics.Bitmap;
20import android.graphics.Point;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070021import android.graphics.Rect;
Jon Miranda16ea1b12017-12-12 14:52:48 -080022import android.view.WindowManager;
23
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080024import androidx.annotation.NonNull;
25
26import com.android.wallpaper.util.BitmapProcessor;
Jon Miranda16ea1b12017-12-12 14:52:48 -080027import com.android.wallpaper.util.ScreenSizeCalculator;
28import com.android.wallpaper.util.WallpaperCropUtils;
Sunny Goyal8600a3f2018-08-15 12:48:01 -070029
Jon Miranda16ea1b12017-12-12 14:52:48 -080030import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
31import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
32
33import java.security.MessageDigest;
34
35/**
36 * Glide bitmap transformation which emulates the default preview positioning of a wallpaper image.
37 */
38public class WallpaperPreviewBitmapTransformation extends BitmapTransformation {
39
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080040 private final Context mContext;
Jon Miranda16ea1b12017-12-12 14:52:48 -080041 private Point mScreenSize;
42 private boolean mIsRtl;
43
44 public WallpaperPreviewBitmapTransformation(Context appContext, boolean isRtl) {
Jon Miranda16ea1b12017-12-12 14:52:48 -080045 WindowManager windowManager = (WindowManager)
46 appContext.getSystemService(Context.WINDOW_SERVICE);
Jon Miranda16ea1b12017-12-12 14:52:48 -080047 mScreenSize =
48 ScreenSizeCalculator.getInstance().getScreenSize(windowManager.getDefaultDisplay());
49 mIsRtl = isRtl;
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080050 mContext = appContext;
Jon Miranda16ea1b12017-12-12 14:52:48 -080051 }
52
53 @Override
54 protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform,
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080055 int outWidth, int outHeight) {
Jon Miranda16ea1b12017-12-12 14:52:48 -080056 // Transform the thumbnail bitmap to match the default MosaicView positioning.
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080057 float scale = WallpaperCropUtils.calculateMinZoom(
58 new Point(toTransform.getWidth(), toTransform.getHeight()),
59 mScreenSize);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070060 Rect originalSize = new Rect(0, 0, toTransform.getWidth(),
61 toTransform.getHeight());
Jon Miranda16ea1b12017-12-12 14:52:48 -080062 Point scaledThumbnailSize = new Point(Math.round(toTransform.getWidth() * scale),
63 Math.round(toTransform.getHeight() * scale));
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080064 Point scaledThumbnailToScreenSize = WallpaperCropUtils.calculateCenterPosition(
65 scaledThumbnailSize, mScreenSize, false /* alignStart */, mIsRtl);
Jon Miranda16ea1b12017-12-12 14:52:48 -080066
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070067 int x = Math.round(scaledThumbnailToScreenSize.x / scale);
68 int y = Math.round(scaledThumbnailToScreenSize.y / scale);
69 Rect cropSize = new Rect(x, y, x + Math.round(mScreenSize.x / scale),
70 y + Math.round(mScreenSize.y / scale));
71 Bitmap cropped;
72 if (!originalSize.contains(cropSize)) {
73 // If crop size is not smaller than original, then use the original bitmap
74 cropped = toTransform;
75 } else {
76 cropped = Bitmap.createBitmap(toTransform, cropSize.left, cropSize.top,
77 cropSize.width(), cropSize.height());
78 }
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080079
80 return BitmapProcessor.blur(mContext, cropped, cropped.getWidth(), cropped.getHeight());
Jon Miranda16ea1b12017-12-12 14:52:48 -080081 }
82
83 @Override
84 public void updateDiskCacheKey(MessageDigest messageDigest) {
85 messageDigest.update(getId().getBytes());
86 }
87
88 /**
89 * Returns a unique identifier for this transformation.
90 */
91 private String getId() {
92 return "preview";
93 }
94
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -080095}