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

Commit 88b7f6a0 authored by Jon Miranda's avatar Jon Miranda
Browse files

Move color extraction utils to extractor class so each instance can

have its own set of temp variables.

- Pass in float[] param to not create new obj in each frame
  for dragging widget case.

Bug: 175329686
Bug: 187019711
Test: manual, verify
Change-Id: I4350fb7e66a80c73def5659fb41abc14d357e667
parent 21930da9
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -195,6 +195,7 @@ public class CellLayout extends ViewGroup {

    private final Rect mTempRect = new Rect();
    private final RectF mTempRectF = new RectF();
    private final float[] mTmpFloatArray = new float[4];

    private static final Paint sPaint = new Paint();

@@ -1080,7 +1081,7 @@ public class CellLayout extends ViewGroup {

            // Now get the rect in drag layer coordinates.
            getBoundsForViewInDragLayer(launcher.getDragLayer(), workspace, mTempRect, false,
                    mTempRectF);
                    mTmpFloatArray, mTempRectF);
            Utilities.setRect(mTempRectF, mTempRect);
            ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, pageId);
        }
+2 −2
Original line number Diff line number Diff line
@@ -242,8 +242,8 @@ public final class Utilities {
     * @param outRect The out rect where we return the bounds of {@param view} in drag layer coords.
     */
    public static void getBoundsForViewInDragLayer(BaseDragLayer dragLayer, View view,
            Rect viewBounds, boolean ignoreTransform, RectF outRect) {
        float[] points = sTmpFloatArray;
            Rect viewBounds, boolean ignoreTransform, float[] recycle, RectF outRect) {
        float[] points = recycle == null ? new float[4] : recycle;
        points[0] = viewBounds.left;
        points[1] = viewBounds.top;
        points[2] = viewBounds.right;
+1 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.launcher3.popup;

import static com.android.launcher3.anim.Interpolators.ACCEL_DEACCEL;
import static com.android.launcher3.popup.PopupPopulator.MAX_SHORTCUTS;
import static com.android.launcher3.util.ColorExtractionUtils.getColorExtractionRect;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
@@ -748,7 +747,7 @@ public abstract class ArrowPopup<T extends StatefulActivity<LauncherState>>
                    View view = getChildAt(i);
                    if (view.getVisibility() == VISIBLE) {
                        RectF rf = new RectF();
                        getColorExtractionRect(Launcher.getLauncher(getContext()),
                        mColorExtractor.getExtractedRectForView(Launcher.getLauncher(getContext()),
                                workspace.getCurrentPage(), view, rf);
                        if (rf.isEmpty()) {
                            numVisibleChild++;
+0 −110
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.util;

import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.Utilities;

/**
 * Utility class used to map launcher views to wallpaper rect.
 */
public class ColorExtractionUtils {

    public static final String TAG = "ColorExtractionUtils";

    private static final Rect sTempRect = new Rect();
    private static final RectF sTempRectF = new RectF();

    /**
     * Takes a view and returns its rect that can be used by the wallpaper local color extractor.
     *
     * @param launcher Launcher class class.
     * @param pageId The page the workspace item is on.
     * @param v The view.
     * @param colorExtractionRectOut The location rect, but converted to a format expected by the
     *                               wallpaper local color extractor.
     */
    public static void getColorExtractionRect(Launcher launcher, int pageId, View v,
            RectF colorExtractionRectOut) {
        Rect viewRect = sTempRect;
        viewRect.set(0, 0, v.getWidth(), v.getHeight());
        Utilities.getBoundsForViewInDragLayer(launcher.getDragLayer(), v, viewRect, false,
                sTempRectF);
        Utilities.setRect(sTempRectF, viewRect);
        getColorExtractionRect(launcher, pageId, viewRect, colorExtractionRectOut);
    }

    /**
     * Takes a rect in drag layer coordinates and returns the rect that can be used by the wallpaper
     * local color extractor.
     *
     * @param launcher Launcher class.
     * @param pageId The page the workspace item is on.
     * @param rectInDragLayer The relevant bounds of the view in drag layer coordinates.
     * @param colorExtractionRectOut The location rect, but converted to a format expected by the
     *                               wallpaper local color extractor.
     */
    public static void getColorExtractionRect(Launcher launcher, int pageId, Rect rectInDragLayer,
            RectF colorExtractionRectOut) {
        // If the view hasn't been measured and laid out, we cannot do this.
        if (rectInDragLayer.isEmpty()) {
            colorExtractionRectOut.setEmpty();
            return;
        }

        Resources res = launcher.getResources();
        DeviceProfile dp = launcher.getDeviceProfile().inv.getDeviceProfile(launcher);
        float screenWidth = dp.widthPx;
        float screenHeight = dp.heightPx;
        int numScreens = launcher.getWorkspace().getNumPagesForWallpaperParallax();
        pageId = Utilities.isRtl(res) ? numScreens - pageId - 1 : pageId;
        float relativeScreenWidth = 1f / numScreens;

        int[] dragLayerBounds = new int[2];
        launcher.getDragLayer().getLocationOnScreen(dragLayerBounds);
        // Translate from drag layer coordinates to screen coordinates.
        int screenLeft = rectInDragLayer.left + dragLayerBounds[0];
        int screenTop = rectInDragLayer.top + dragLayerBounds[1];
        int screenRight = rectInDragLayer.right + dragLayerBounds[0];
        int screenBottom = rectInDragLayer.bottom + dragLayerBounds[1];

        // This is the position of the view relative to the wallpaper, as expected by the
        // local color extraction of the WallpaperManager.
        // The coordinate system is such that, on the horizontal axis, each screen has a
        // distinct range on the [0,1] segment. So if there are 3 screens, they will have the
        // ranges [0, 1/3], [1/3, 2/3] and [2/3, 1]. The position on the subrange should be
        // the position of the view relative to the screen. For the vertical axis, this is
        // simply the location of the view relative to the screen.
        // Translate from drag layer coordinates to screen coordinates
        colorExtractionRectOut.left = (screenLeft / screenWidth + pageId) * relativeScreenWidth;
        colorExtractionRectOut.right = (screenRight / screenWidth + pageId) * relativeScreenWidth;
        colorExtractionRectOut.top = screenTop / screenHeight;
        colorExtractionRectOut.bottom = screenBottom / screenHeight;

        if (colorExtractionRectOut.left < 0
                || colorExtractionRectOut.right > 1
                || colorExtractionRectOut.top < 0
                || colorExtractionRectOut.bottom > 1) {
            colorExtractionRectOut.setEmpty();
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ public class FloatingIconView extends FrameLayout implements
        }

        Utilities.getBoundsForViewInDragLayer(launcher.getDragLayer(), v, outViewBounds,
                ignoreTransform, outRect);
                ignoreTransform, null /** recycle */, outRect);
    }

    /**
Loading