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;
21 import android.graphics.Rect;
22 import android.view.WindowManager;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.android.wallpaper.util.BitmapProcessor;
27 import com.android.wallpaper.util.ScreenSizeCalculator;
28 import com.android.wallpaper.util.WallpaperCropUtils;
29 
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 
40     private final Context mContext;
41     private Point mScreenSize;
42     private boolean mIsRtl;
43 
WallpaperPreviewBitmapTransformation(Context appContext, boolean isRtl)44     public WallpaperPreviewBitmapTransformation(Context appContext, boolean isRtl) {
45         WindowManager windowManager = (WindowManager)
46                 appContext.getSystemService(Context.WINDOW_SERVICE);
47         mScreenSize =
48                 ScreenSizeCalculator.getInstance().getScreenSize(windowManager.getDefaultDisplay());
49         mIsRtl = isRtl;
50         mContext = appContext;
51     }
52 
53     @Override
transform(@onNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight)54     protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform,
55             int outWidth, int outHeight) {
56         // Transform the thumbnail bitmap to match the default MosaicView positioning.
57         float scale = WallpaperCropUtils.calculateMinZoom(
58                 new Point(toTransform.getWidth(), toTransform.getHeight()),
59                 mScreenSize);
60         Rect originalSize = new Rect(0, 0, toTransform.getWidth(),
61                 toTransform.getHeight());
62         Point scaledThumbnailSize = new Point(Math.round(toTransform.getWidth() * scale),
63                 Math.round(toTransform.getHeight() * scale));
64         Point scaledThumbnailToScreenSize = WallpaperCropUtils.calculateCenterPosition(
65                 scaledThumbnailSize, mScreenSize, false /* alignStart */, mIsRtl);
66 
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         }
79 
80         return BitmapProcessor.createLowResBitmap(cropped, cropped.getWidth(), cropped.getHeight());
81     }
82 
83     @Override
updateDiskCacheKey(MessageDigest messageDigest)84     public void updateDiskCacheKey(MessageDigest messageDigest) {
85         messageDigest.update(getId().getBytes());
86     }
87 
88     /**
89      * Returns a unique identifier for this transformation.
90      */
getId()91     private String getId() {
92         return "preview";
93     }
94 
95 }