Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c6ae88f9 authored by Fengjiang Li's avatar Fengjiang Li
Browse files

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
parent d632f222
Loading
Loading
Loading
Loading
+0 −60
Original line number Diff line number Diff line
/*
 * 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
        )
    }
}
+7 −7
Original line number Diff line number Diff line
@@ -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) {
+0 −138
Original line number Diff line number Diff line
/*
 * 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))
    }
}