Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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.wallpaper.picker; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.graphics.Bitmap; |
| 20 | import android.graphics.Point; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 21 | import android.graphics.Rect; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 22 | import android.view.WindowManager; |
| 23 | |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 24 | import androidx.annotation.NonNull; |
| 25 | |
| 26 | import com.android.wallpaper.util.BitmapProcessor; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 27 | import com.android.wallpaper.util.ScreenSizeCalculator; |
| 28 | import com.android.wallpaper.util.WallpaperCropUtils; |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame] | 29 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 30 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; |
| 31 | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; |
| 32 | |
| 33 | import java.security.MessageDigest; |
| 34 | |
| 35 | /** |
| 36 | * Glide bitmap transformation which emulates the default preview positioning of a wallpaper image. |
| 37 | */ |
| 38 | public class WallpaperPreviewBitmapTransformation extends BitmapTransformation { |
| 39 | |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 40 | private final Context mContext; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 41 | private Point mScreenSize; |
| 42 | private boolean mIsRtl; |
| 43 | |
| 44 | public WallpaperPreviewBitmapTransformation(Context appContext, boolean isRtl) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 45 | WindowManager windowManager = (WindowManager) |
| 46 | appContext.getSystemService(Context.WINDOW_SERVICE); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 47 | mScreenSize = |
| 48 | ScreenSizeCalculator.getInstance().getScreenSize(windowManager.getDefaultDisplay()); |
| 49 | mIsRtl = isRtl; |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 50 | mContext = appContext; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 55 | int outWidth, int outHeight) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 56 | // Transform the thumbnail bitmap to match the default MosaicView positioning. |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 57 | float scale = WallpaperCropUtils.calculateMinZoom( |
| 58 | new Point(toTransform.getWidth(), toTransform.getHeight()), |
| 59 | mScreenSize); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 60 | Rect originalSize = new Rect(0, 0, toTransform.getWidth(), |
| 61 | toTransform.getHeight()); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 62 | Point scaledThumbnailSize = new Point(Math.round(toTransform.getWidth() * scale), |
| 63 | Math.round(toTransform.getHeight() * scale)); |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 64 | Point scaledThumbnailToScreenSize = WallpaperCropUtils.calculateCenterPosition( |
| 65 | scaledThumbnailSize, mScreenSize, false /* alignStart */, mIsRtl); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 66 | |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 67 | 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 Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 79 | |
| 80 | return BitmapProcessor.blur(mContext, cropped, cropped.getWidth(), cropped.getHeight()); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 81 | } |
| 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 Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 95 | } |