1 /*
2  * Copyright (C) 2023 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.preview.ui.util
17 
18 import android.graphics.Point
19 import android.graphics.PointF
20 import android.graphics.Rect
21 import com.android.wallpaper.picker.preview.ui.util.CropSizeUtil.fitCropRectToLayoutDirection
22 import com.android.wallpaper.util.WallpaperCropUtils
23 import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
24 
25 object FullResImageViewUtil {
26 
27     private const val DEFAULT_WALLPAPER_MAX_ZOOM = 8f
28 
getScaleAndCenternull29     fun getScaleAndCenter(
30         viewSize: Point,
31         rawWallpaperSize: Point,
32         displaySize: Point,
33         cropRect: Rect?,
34         isRtl: Boolean,
35         systemScale: Float,
36     ): ScaleAndCenter {
37         // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
38         // defaultRawWallpaperRect represents a brand new wallpaper preview with no crop hints.
39         // For full screen, the preview image container size has already been adjusted
40         // to preserve a boundary beyond the visible crop per comment at
41         // FullWallpaperPreviewBinder#adjustSizesForCropping.
42         // For small screen preview, we need to apply additional scaling since the
43         // container is the same size as the preview.
44         viewSize.apply {
45             // Preserve precision by not converting scale to int but the result
46             x = (x * systemScale).toInt()
47             y = (y * systemScale).toInt()
48         }
49         val defaultRawWallpaperRect =
50             WallpaperCropUtils.calculateVisibleRect(rawWallpaperSize, viewSize)
51         val visibleRawWallpaperRect =
52             cropRect?.let { fitCropRectToLayoutDirection(it, displaySize, isRtl) }
53                 ?: defaultRawWallpaperRect
54 
55         val centerPosition =
56             PointF(
57                 visibleRawWallpaperRect.centerX().toFloat(),
58                 visibleRawWallpaperRect.centerY().toFloat()
59             )
60         val defaultWallpaperZoom =
61             WallpaperCropUtils.calculateMinZoom(
62                 Point(defaultRawWallpaperRect.width(), defaultRawWallpaperRect.height()),
63                 viewSize
64             )
65         val visibleWallpaperZoom =
66             WallpaperCropUtils.calculateMinZoom(
67                 Point(visibleRawWallpaperRect.width(), visibleRawWallpaperRect.height()),
68                 viewSize
69             )
70 
71         return ScaleAndCenter(
72             defaultWallpaperZoom,
73             defaultWallpaperZoom.coerceAtLeast(DEFAULT_WALLPAPER_MAX_ZOOM),
74             visibleWallpaperZoom,
75             centerPosition,
76         )
77     }
78 
<lambda>null79     fun SubsamplingScaleImageView.getCropRect() = Rect().apply { visibleFileRect(this) }
80 
81     data class ScaleAndCenter(
82         val minScale: Float,
83         val maxScale: Float,
84         val defaultScale: Float,
85         val center: PointF
86     )
87 }
88