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; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 21 | import android.view.WindowManager; |
| 22 | |
| 23 | import com.android.wallpaper.util.ScreenSizeCalculator; |
| 24 | import com.android.wallpaper.util.WallpaperCropUtils; |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame^] | 25 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 26 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; |
| 27 | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; |
| 28 | |
| 29 | import java.security.MessageDigest; |
| 30 | |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame^] | 31 | import androidx.annotation.NonNull; |
| 32 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Glide bitmap transformation which emulates the default preview positioning of a wallpaper image. |
| 35 | */ |
| 36 | public class WallpaperPreviewBitmapTransformation extends BitmapTransformation { |
| 37 | |
| 38 | private Point mDefaultCropSurfaceSize; |
| 39 | private Point mScreenSize; |
| 40 | private boolean mIsRtl; |
| 41 | |
| 42 | public WallpaperPreviewBitmapTransformation(Context appContext, boolean isRtl) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 43 | WindowManager windowManager = (WindowManager) |
| 44 | appContext.getSystemService(Context.WINDOW_SERVICE); |
| 45 | mDefaultCropSurfaceSize = WallpaperCropUtils.getDefaultCropSurfaceSize( |
| 46 | appContext.getResources(), windowManager.getDefaultDisplay()); |
| 47 | mScreenSize = |
| 48 | ScreenSizeCalculator.getInstance().getScreenSize(windowManager.getDefaultDisplay()); |
| 49 | mIsRtl = isRtl; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, |
| 54 | int outWidth, int outHeight) { |
| 55 | |
| 56 | // Transform the thumbnail bitmap to match the default MosaicView positioning. |
| 57 | float scale = Math.max((float) mDefaultCropSurfaceSize.x / toTransform.getWidth(), |
| 58 | (float) mDefaultCropSurfaceSize.y / toTransform.getHeight()); |
| 59 | Point scaledThumbnailSize = new Point(Math.round(toTransform.getWidth() * scale), |
| 60 | Math.round(toTransform.getHeight() * scale)); |
| 61 | Point scaledThumbnailToCropSurface = WallpaperCropUtils.calculateCenterPosition( |
| 62 | scaledThumbnailSize, mDefaultCropSurfaceSize, false /* alignStart */, mIsRtl); |
| 63 | |
| 64 | Point cropSurfaceToScreenSize = WallpaperCropUtils.calculateCenterPosition( |
| 65 | mDefaultCropSurfaceSize, mScreenSize, true /* alignStart */, mIsRtl); |
| 66 | |
| 67 | Point scaledThumbnailToScreenSize = new Point( |
| 68 | scaledThumbnailToCropSurface.x + cropSurfaceToScreenSize.x, |
| 69 | scaledThumbnailToCropSurface.y + cropSurfaceToScreenSize.y); |
| 70 | |
| 71 | return Bitmap.createBitmap(toTransform, |
| 72 | Math.round(scaledThumbnailToScreenSize.x / scale), |
| 73 | Math.round(scaledThumbnailToScreenSize.y / scale), |
| 74 | Math.round(mScreenSize.x / scale), |
| 75 | Math.round(mScreenSize.y / scale)); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void updateDiskCacheKey(MessageDigest messageDigest) { |
| 80 | messageDigest.update(getId().getBytes()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns a unique identifier for this transformation. |
| 85 | */ |
| 86 | private String getId() { |
| 87 | return "preview"; |
| 88 | } |
| 89 | |
| 90 | } |