From c6ae88f904296754d866cafadcfc457ed8bbf6fe Mon Sep 17 00:00:00 2001 From: Fengjiang Li Date: Wed, 6 Mar 2024 17:02:02 -0800 Subject: [PATCH] Use Matrix.setRectToRect() replace RectUtils.letterBox() Bug: 284236964 Flag: aconfig launcher.enable_add_app_widget_via_config_activity_v2 DISABLED Test: manual Change-Id: Ia83f3415442c2359bffb1efcfd1ee96bf17ab9ff --- src/com/android/launcher3/RectUtils.kt | 60 -------- .../widget/PendingAppWidgetHostView.java | 14 +- .../com/android/launcher3/RectUtilsTest.kt | 138 ------------------ 3 files changed, 7 insertions(+), 205 deletions(-) delete mode 100644 src/com/android/launcher3/RectUtils.kt delete mode 100644 tests/src/com/android/launcher3/RectUtilsTest.kt diff --git a/src/com/android/launcher3/RectUtils.kt b/src/com/android/launcher3/RectUtils.kt deleted file mode 100644 index 68d2eaf9ab..0000000000 --- a/src/com/android/launcher3/RectUtils.kt +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.launcher3 - -import android.graphics.Rect - -/** - * Fit [this] into [targetRect] with letter boxing. After calling this method, [this] will be - * modified to be letter boxed. - * - * @param targetRect target [Rect] that [this] should be fitted into - */ -fun Rect.letterBox(targetRect: Rect) { - letterBox(targetRect, this) -} - -/** - * Fit [this] into [targetRect] with letter boxing. After calling this method, [resultRect] will be - * modified to be letter boxed. - * - * @param targetRect target [Rect] that [this] should be fitted into - * @param resultRect the letter boxed [Rect] - */ -fun Rect.letterBox(targetRect: Rect, resultRect: Rect) { - val widthRatio: Float = 1f * targetRect.width() / width() - val heightRatio: Float = 1f * targetRect.height() / height() - if (widthRatio < heightRatio) { - val scaledHeight: Int = (widthRatio * height()).toInt() - val verticalPadding: Int = (targetRect.height() - scaledHeight) / 2 - resultRect.set( - targetRect.left, - targetRect.top + verticalPadding, - targetRect.right, - targetRect.bottom - verticalPadding - ) - } else { - val scaledWidth: Int = (heightRatio * width()).toInt() - val horizontalPadding: Int = (targetRect.width() - scaledWidth) / 2 - resultRect.set( - targetRect.left + horizontalPadding, - targetRect.top, - targetRect.right - horizontalPadding, - targetRect.bottom - ) - } -} diff --git a/src/com/android/launcher3/widget/PendingAppWidgetHostView.java b/src/com/android/launcher3/widget/PendingAppWidgetHostView.java index 7f9a1fc4c2..9c9b80d80f 100644 --- a/src/com/android/launcher3/widget/PendingAppWidgetHostView.java +++ b/src/com/android/launcher3/widget/PendingAppWidgetHostView.java @@ -29,9 +29,11 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.Rect; +import android.graphics.RectF; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -53,7 +55,6 @@ import com.android.launcher3.DeviceProfile; import com.android.launcher3.Launcher; import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; -import com.android.launcher3.RectUtilsKt; import com.android.launcher3.icons.FastBitmapDrawable; import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver; import com.android.launcher3.model.data.ItemInfoWithIcon; @@ -77,9 +78,9 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView private final Rect mRect = new Rect(); - private final Rect mPreviewBitmapRect = new Rect(); - private final Rect mCanvasRect = new Rect(); - private final Rect mLetterBoxedPreviewBitmapRect = new Rect(); + private final Matrix mMatrix = new Matrix(); + private final RectF mPreviewBitmapRect = new RectF(); + private final RectF mCanvasRect = new RectF(); private final LauncherWidgetHolder mWidgetHolder; private final LauncherAppWidgetProviderInfo mAppwidget; @@ -458,9 +459,8 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView mPreviewBitmapRect.set(0, 0, mPreviewBitmap.getWidth(), mPreviewBitmap.getHeight()); mCanvasRect.set(0, 0, getWidth(), getHeight()); - RectUtilsKt.letterBox(mPreviewBitmapRect, mCanvasRect, mLetterBoxedPreviewBitmapRect); - canvas.drawBitmap(mPreviewBitmap, mPreviewBitmapRect, mLetterBoxedPreviewBitmapRect, - mPreviewPaint); + mMatrix.setRectToRect(mPreviewBitmapRect, mCanvasRect, Matrix.ScaleToFit.CENTER); + canvas.drawBitmap(mPreviewBitmap, mMatrix, mPreviewPaint); return; } if (mCenterDrawable == null) { diff --git a/tests/src/com/android/launcher3/RectUtilsTest.kt b/tests/src/com/android/launcher3/RectUtilsTest.kt deleted file mode 100644 index f0d22eb9d4..0000000000 --- a/tests/src/com/android/launcher3/RectUtilsTest.kt +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.launcher3 - -import android.graphics.Rect -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.filters.SmallTest -import com.google.common.truth.Truth.assertThat -import org.junit.Test -import org.junit.runner.RunWith - -@SmallTest -@RunWith(AndroidJUnit4::class) -class RectUtilsTest { - - private val srcRect = Rect() - private val destRect = Rect() - private val letterBoxedRect = Rect() - - @Test - fun letterBoxSelf_toSameRect_noScale() { - srcRect.set(0, 0, 100, 100) - destRect.set(0, 0, 100, 100) - - srcRect.letterBox(destRect) - - assertThat(srcRect).isEqualTo(Rect(0, 0, 100, 100)) - } - - @Test - fun letterBox_toSameRect_noScale() { - srcRect.set(0, 0, 100, 100) - destRect.set(0, 0, 100, 100) - - srcRect.letterBox(destRect, letterBoxedRect) - - assertThat(letterBoxedRect).isEqualTo(Rect(0, 0, 100, 100)) - assertThat(srcRect).isEqualTo(Rect(0, 0, 100, 100)) - } - - @Test - fun letterBoxSelf_toSmallHeight_scaleDownHorizontally() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(0, 0, 939, 520) - - srcRect.letterBox(destRect) - - assertThat(srcRect).isEqualTo(Rect(114, 0, 825, 520)) - } - - @Test - fun letterBoxRect_toSmallHeight_scaleDownHorizontally() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(0, 0, 939, 520) - - srcRect.letterBox(destRect, letterBoxedRect) - - assertThat(letterBoxedRect).isEqualTo(Rect(114, 0, 825, 520)) - assertThat(srcRect).isEqualTo(Rect(0, 0, 2893, 2114)) - } - - @Test - fun letterBoxSelf_toSmallHeightWithOffset_scaleDownHorizontally() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(10, 20, 949, 540) - - srcRect.letterBox(destRect) - - assertThat(srcRect).isEqualTo(Rect(124, 20, 835, 540)) - } - - @Test - fun letterBoxRect_toSmallHeightWithOffset_scaleDownHorizontally() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(10, 20, 949, 540) - - srcRect.letterBox(destRect, letterBoxedRect) - - assertThat(letterBoxedRect).isEqualTo(Rect(124, 20, 835, 540)) - assertThat(srcRect).isEqualTo(Rect(0, 0, 2893, 2114)) - } - - @Test - fun letterBoxSelf_toSmallWidth_scaleDownVertically() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(0, 0, 520, 939) - - srcRect.letterBox(destRect) - - assertThat(srcRect).isEqualTo(Rect(0, 280, 520, 659)) - } - - @Test - fun letterBoxRect_toSmallWidth_scaleDownVertically() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(0, 0, 520, 939) - - srcRect.letterBox(destRect, letterBoxedRect) - - assertThat(letterBoxedRect).isEqualTo(Rect(0, 280, 520, 659)) - assertThat(srcRect).isEqualTo(Rect(0, 0, 2893, 2114)) - } - - @Test - fun letterBoxSelf_toSmallWidthWithOffset_scaleDownVertically() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(40, 60, 560, 999) - - srcRect.letterBox(destRect) - - assertThat(srcRect).isEqualTo(Rect(40, 340, 560, 719)) - } - - @Test - fun letterBoxRect_toSmallWidthWithOffset_scaleDownVertically() { - srcRect.set(0, 0, 2893, 2114) - destRect.set(40, 60, 560, 999) - - srcRect.letterBox(destRect, letterBoxedRect) - - assertThat(letterBoxedRect).isEqualTo(Rect(40, 340, 560, 719)) - assertThat(srcRect).isEqualTo(Rect(0, 0, 2893, 2114)) - } -}