blob: 0ee04a62197f4ceafa317efe248dce52a4578045 [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;
Jon Miranda16ea1b12017-12-12 14:52:48 -080021import android.view.WindowManager;
22
23import com.android.wallpaper.util.ScreenSizeCalculator;
24import com.android.wallpaper.util.WallpaperCropUtils;
Sunny Goyal8600a3f2018-08-15 12:48:01 -070025
Jon Miranda16ea1b12017-12-12 14:52:48 -080026import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
27import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
28
29import java.security.MessageDigest;
30
Sunny Goyal8600a3f2018-08-15 12:48:01 -070031import androidx.annotation.NonNull;
32
Jon Miranda16ea1b12017-12-12 14:52:48 -080033/**
34 * Glide bitmap transformation which emulates the default preview positioning of a wallpaper image.
35 */
36public 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 Miranda16ea1b12017-12-12 14:52:48 -080043 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}