From 609df338cc6038d520fd6e264e44415a09761647 Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Tue, 27 May 2025 11:10:05 -0700 Subject: [PATCH] Moving dropTarget variables in DeviceProfile to their own class Bug: 419264653 Flag: EXEMPT refactor Test: DeviceProfileDumpTest Test: All Image tests Change-Id: I8fc519ec8adcda6495ac7c9a9c747b6a80615547 --- .../android/launcher3/ButtonDropTarget.java | 3 +- src/com/android/launcher3/DeviceProfile.java | 55 +++++++---------- src/com/android/launcher3/DropTargetBar.java | 36 ++++++----- .../deviceprofile/DropTargetProfile.kt | 61 +++++++++++++++++++ 4 files changed, 106 insertions(+), 49 deletions(-) create mode 100644 src/com/android/launcher3/deviceprofile/DropTargetProfile.kt diff --git a/src/com/android/launcher3/ButtonDropTarget.java b/src/com/android/launcher3/ButtonDropTarget.java index 50111111fe..312980713f 100644 --- a/src/com/android/launcher3/ButtonDropTarget.java +++ b/src/com/android/launcher3/ButtonDropTarget.java @@ -287,7 +287,8 @@ public abstract class ButtonDropTarget extends TextView @Override public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { super.getHitRect(outRect); - outRect.bottom += mActivityContext.getDeviceProfile().dropTargetDragPaddingPx; + outRect.bottom += + mActivityContext.getDeviceProfile().getDropTargetProfile().getDragPaddingPx(); sTempCords[0] = sTempCords[1] = 0; mActivityContext.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords); diff --git a/src/com/android/launcher3/DeviceProfile.java b/src/com/android/launcher3/DeviceProfile.java index 2c08dfeb13..eddbce41b7 100644 --- a/src/com/android/launcher3/DeviceProfile.java +++ b/src/com/android/launcher3/DeviceProfile.java @@ -56,6 +56,7 @@ import com.android.launcher3.CellLayout.ContainerType; import com.android.launcher3.DevicePaddings.DevicePadding; import com.android.launcher3.InvariantDeviceProfile.DisplayOptionSpec; import com.android.launcher3.deviceprofile.DeviceProperties; +import com.android.launcher3.deviceprofile.DropTargetProfile; import com.android.launcher3.deviceprofile.OverviewProfile; import com.android.launcher3.graphics.ThemeManager; import com.android.launcher3.icons.DotRenderer; @@ -262,16 +263,7 @@ public class DeviceProfile { // Widgets private final ViewScaleProvider mViewScaleProvider; - // Drop Target - public int dropTargetBarSizePx; - public int dropTargetBarTopMarginPx; - public int dropTargetBarBottomMarginPx; - public int dropTargetDragPaddingPx; - public int dropTargetTextSizePx; - public int dropTargetHorizontalPaddingPx; - public int dropTargetVerticalPaddingPx; - public int dropTargetGapPx; - public int dropTargetButtonWorkspaceEdgeGapPx; + private final DropTargetProfile mDropTargetProfile; // Insets private final Rect mInsets = new Rect(); @@ -349,6 +341,7 @@ public class DeviceProfile { mWorkspacePageIndicatorOverlapWorkspace = 0; numFolderRows = 0; numFolderColumns = 0; + mDropTargetProfile = new DropTargetProfile(0, 0, 0, 0, 0, 0, 0, 0, 0); folderLabelTextScale = 0; areNavButtonsInline = false; mHotseatBarEdgePaddingPx = 0; @@ -558,27 +551,14 @@ public class DeviceProfile { cellStyle.recycle(); } - dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size); // Some foldable portrait modes are too wide in terms of aspect ratio so we need to tweak // the dimensions for edit state. final boolean shouldApplyWidePortraitDimens = mDeviceProperties.isTablet() && !mDeviceProperties.isLandscape() && mDeviceProperties.getAspectRatio() < MAX_ASPECT_RATIO_FOR_ALTERNATE_EDIT_STATE; - dropTargetBarTopMarginPx = shouldApplyWidePortraitDimens - ? 0 - : res.getDimensionPixelSize(R.dimen.drop_target_top_margin); - dropTargetBarBottomMarginPx = shouldApplyWidePortraitDimens - ? res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin_wide_portrait) - : res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin); - dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding); - dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size); - dropTargetHorizontalPaddingPx = res.getDimensionPixelSize( - R.dimen.drop_target_button_drawable_horizontal_padding); - dropTargetVerticalPaddingPx = res.getDimensionPixelSize( - R.dimen.drop_target_button_drawable_vertical_padding); - dropTargetGapPx = res.getDimensionPixelSize(R.dimen.drop_target_button_gap); - dropTargetButtonWorkspaceEdgeGapPx = res.getDimensionPixelSize( - R.dimen.drop_target_button_workspace_edge_gap); + mDropTargetProfile = DropTargetProfile + .Factory + .createDropTargetProfile(res, shouldApplyWidePortraitDimens); workspaceSpringLoadedMinNextPageVisiblePx = res.getDimensionPixelSize( R.dimen.dynamic_grid_spring_loaded_min_next_space_visible); @@ -1716,8 +1696,9 @@ public class DeviceProfile { * Gets the scaled top of the workspace in px for the spring-loaded edit state. */ public float getCellLayoutSpringLoadShrunkTop() { - return mInsets.top + dropTargetBarTopMarginPx + dropTargetBarSizePx - + dropTargetBarBottomMarginPx; + return mInsets.top + getDropTargetProfile().getBarTopMarginPx() + + getDropTargetProfile().getBarSizePx() + + getDropTargetProfile().getBarBottomMarginPx(); } /** @@ -2141,7 +2122,8 @@ public class DeviceProfile { public Rect getAbsoluteOpenFolderBounds() { if (isVerticalBarLayout()) { // Folders should only appear right of the drop target bar and left of the hotseat - return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx, + return new Rect( + mInsets.left + getDropTargetProfile().getBarSizePx() + edgeMarginPx, mInsets.top, mInsets.left + mDeviceProperties.getAvailableWidthPx() - hotseatBarSizePx - edgeMarginPx, mInsets.top + mDeviceProperties.getAvailableHeightPx()); @@ -2149,7 +2131,7 @@ public class DeviceProfile { // Folders should only appear below the drop target bar and above the hotseat int hotseatTop = isTaskbarPresent ? taskbarHeight : hotseatBarSizePx; return new Rect(mInsets.left + edgeMarginPx, - mInsets.top + dropTargetBarSizePx + edgeMarginPx, + mInsets.top + getDropTargetProfile().getBarSizePx() + edgeMarginPx, mInsets.left + mDeviceProperties.getAvailableWidthPx() - edgeMarginPx, mInsets.top + mDeviceProperties.getAvailableHeightPx() - hotseatTop - workspacePageIndicatorHeight - edgeMarginPx); @@ -2386,10 +2368,13 @@ public class DeviceProfile { writer.println(prefix + pxToDpStr("overviewGridSideMargin", getOverviewProfile().getGridSideMargin())); - writer.println(prefix + pxToDpStr("dropTargetBarTopMarginPx", dropTargetBarTopMarginPx)); - writer.println(prefix + pxToDpStr("dropTargetBarSizePx", dropTargetBarSizePx)); + writer.println(prefix + pxToDpStr("dropTargetBarTopMarginPx", + getDropTargetProfile().getBarTopMarginPx())); + writer.println(prefix + pxToDpStr("dropTargetBarSizePx", + getDropTargetProfile().getBarSizePx())); writer.println( - prefix + pxToDpStr("dropTargetBarBottomMarginPx", dropTargetBarBottomMarginPx)); + prefix + pxToDpStr("dropTargetBarBottomMarginPx", + getDropTargetProfile().getBarBottomMarginPx())); writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkTop()", getCellLayoutSpringLoadShrunkTop())); @@ -2459,6 +2444,10 @@ public class DeviceProfile { } } + public DropTargetProfile getDropTargetProfile() { + return mDropTargetProfile; + } + /** * Callback when a component changes the DeviceProfile associated with it, as a result of * configuration change diff --git a/src/com/android/launcher3/DropTargetBar.java b/src/com/android/launcher3/DropTargetBar.java index 57a924faae..58ab4bada2 100644 --- a/src/com/android/launcher3/DropTargetBar.java +++ b/src/com/android/launcher3/DropTargetBar.java @@ -107,22 +107,23 @@ public class DropTargetBar extends FrameLayout horizontalMargin = getContext().getResources() .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal); } - lp.topMargin += deviceProfile.dropTargetBarTopMarginPx; - lp.bottomMargin += deviceProfile.dropTargetBarBottomMarginPx; + lp.topMargin += deviceProfile.getDropTargetProfile().getBarTopMarginPx(); + lp.bottomMargin += deviceProfile.getDropTargetProfile().getBarBottomMarginPx(); lp.width = deviceProfile.getDeviceProperties().getAvailableWidthPx() - 2 * horizontalMargin; if (mIsVertical) { lp.leftMargin = (widthPx - lp.width) / 2; lp.rightMargin = (widthPx - lp.width) / 2; } - lp.height = deviceProfile.dropTargetBarSizePx; + lp.height = deviceProfile.getDropTargetProfile().getBarSizePx(); lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; DeviceProfile dp = mLauncher.getDeviceProfile(); - int horizontalPadding = dp.dropTargetHorizontalPaddingPx; - int verticalPadding = dp.dropTargetVerticalPaddingPx; + int horizontalPadding = dp.getDropTargetProfile().getHorizontalPaddingPx(); + int verticalPadding = dp.getDropTargetProfile().getVerticalPaddingPx(); setLayoutParams(lp); for (ButtonDropTarget button : mDropTargets) { - button.setTextSize(TypedValue.COMPLEX_UNIT_PX, deviceProfile.dropTargetTextSizePx); + button.setTextSize(TypedValue.COMPLEX_UNIT_PX, + deviceProfile.getDropTargetProfile().getTextSizePx()); button.setToolTipLocation(tooltipLocation); button.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding); @@ -149,18 +150,19 @@ public class DropTargetBar extends FrameLayout ButtonDropTarget firstButton = mTempTargets[0]; firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, - mLauncher.getDeviceProfile().dropTargetTextSizePx); + mLauncher.getDeviceProfile().getDropTargetProfile().getTextSizePx()); firstButton.setTextVisible(true); firstButton.setIconVisible(true); firstButton.measure(widthSpec, heightSpec); firstButton.resizeTextToFit(); } else if (visibleCount == 2) { DeviceProfile dp = mLauncher.getDeviceProfile(); - int verticalPadding = dp.dropTargetVerticalPaddingPx; - int horizontalPadding = dp.dropTargetHorizontalPaddingPx; + int verticalPadding = dp.getDropTargetProfile().getVerticalPaddingPx(); + int horizontalPadding = dp.getDropTargetProfile().getHorizontalPaddingPx(); ButtonDropTarget firstButton = mTempTargets[0]; - firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx); + firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, + dp.getDropTargetProfile().getTextSizePx()); firstButton.setTextVisible(true); firstButton.setIconVisible(true); firstButton.setTextMultiLine(false); @@ -169,7 +171,8 @@ public class DropTargetBar extends FrameLayout verticalPadding); ButtonDropTarget secondButton = mTempTargets[1]; - secondButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx); + secondButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, + dp.getDropTargetProfile().getTextSizePx()); secondButton.setTextVisible(true); secondButton.setIconVisible(true); secondButton.setTextMultiLine(false); @@ -181,10 +184,12 @@ public class DropTargetBar extends FrameLayout if (dp.getDeviceProperties().isTwoPanels()) { // Each button for two panel fits to half the width of the screen excluding the // center gap between the buttons. - availableWidth = (dp.getDeviceProperties().getAvailableWidthPx() - dp.dropTargetGapPx) / 2; + availableWidth = (dp.getDeviceProperties().getAvailableWidthPx() + - dp.getDropTargetProfile().getGapPx()) / 2; } else { // Both buttons plus the button gap do not display past the edge of the screen. - availableWidth = dp.getDeviceProperties().getAvailableWidthPx() - dp.dropTargetGapPx; + availableWidth = dp.getDeviceProperties().getAvailableWidthPx() + - dp.getDropTargetProfile().getGapPx(); } int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); @@ -201,7 +206,8 @@ public class DropTargetBar extends FrameLayout } if (!dp.getDeviceProperties().isTwoPanels()) { - availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx; + availableWidth -= firstButton.getMeasuredWidth() + + dp.getDropTargetProfile().getGapPx(); widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); } secondButton.measure(widthSpec, heightSpec); @@ -255,7 +261,7 @@ public class DropTargetBar extends FrameLayout button.layout(barCenter - (button.getMeasuredWidth() / 2), 0, barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight()); } else if (visibleCount == 2) { - int buttonGap = dp.dropTargetGapPx; + int buttonGap = dp.getDropTargetProfile().getGapPx(); ButtonDropTarget leftButton = mTempTargets[0]; ButtonDropTarget rightButton = mTempTargets[1]; diff --git a/src/com/android/launcher3/deviceprofile/DropTargetProfile.kt b/src/com/android/launcher3/deviceprofile/DropTargetProfile.kt new file mode 100644 index 0000000000..b74d323471 --- /dev/null +++ b/src/com/android/launcher3/deviceprofile/DropTargetProfile.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2025 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.deviceprofile + +import android.content.res.Resources +import com.android.launcher3.R + +data class DropTargetProfile( + val barSizePx: Int, + val barTopMarginPx: Int, + val barBottomMarginPx: Int, + val dragPaddingPx: Int, + val textSizePx: Int, + val horizontalPaddingPx: Int, + val verticalPaddingPx: Int, + val gapPx: Int, + val buttonWorkspaceEdgeGapPx: Int, +) { + companion object Factory { + fun createDropTargetProfile( + res: Resources, + shouldApplyWidePortraitDimens: Boolean, + ): DropTargetProfile { + return DropTargetProfile( + barSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size), + barTopMarginPx = + if (shouldApplyWidePortraitDimens) 0 + else res.getDimensionPixelSize(R.dimen.drop_target_top_margin), + barBottomMarginPx = + if (shouldApplyWidePortraitDimens) + res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin_wide_portrait) + else res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin), + dragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding), + textSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size), + horizontalPaddingPx = + res.getDimensionPixelSize( + R.dimen.drop_target_button_drawable_horizontal_padding + ), + verticalPaddingPx = + res.getDimensionPixelSize(R.dimen.drop_target_button_drawable_vertical_padding), + gapPx = res.getDimensionPixelSize(R.dimen.drop_target_button_gap), + buttonWorkspaceEdgeGapPx = + res.getDimensionPixelSize(R.dimen.drop_target_button_workspace_edge_gap), + ) + } + } +}