Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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.launcher3; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.graphics.Canvas; |
| 20 | import android.graphics.Rect; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | |
| 25 | import com.android.launcher3.celllayout.CellLayoutLayoutParams; |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 26 | import com.android.launcher3.celllayout.MulticellReorderAlgorithm; |
| 27 | import com.android.launcher3.celllayout.ReorderAlgorithm; |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 28 | import com.android.launcher3.util.CellAndSpan; |
| 29 | import com.android.launcher3.util.GridOccupancy; |
| 30 | |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 31 | /** |
| 32 | * CellLayout that simulates a split in the middle for use in foldable devices. |
| 33 | */ |
| 34 | public class MultipageCellLayout extends CellLayout { |
| 35 | |
| 36 | private final Drawable mLeftBackground; |
| 37 | private final Drawable mRightBackground; |
| 38 | |
Sebastian Franco | 317c4fa | 2023-02-21 17:01:24 -0800 | [diff] [blame] | 39 | private boolean mSeamWasAdded = false; |
| 40 | |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 41 | public MultipageCellLayout(Context context) { |
| 42 | this(context, null); |
| 43 | } |
| 44 | |
| 45 | public MultipageCellLayout(Context context, AttributeSet attrs) { |
| 46 | this(context, attrs, 0); |
| 47 | } |
| 48 | |
| 49 | public MultipageCellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 50 | super(context, attrs, defStyle); |
| 51 | mLeftBackground = getContext().getDrawable(R.drawable.bg_celllayout); |
| 52 | mLeftBackground.setCallback(this); |
| 53 | mLeftBackground.setAlpha(0); |
| 54 | |
| 55 | mRightBackground = getContext().getDrawable(R.drawable.bg_celllayout); |
| 56 | mRightBackground.setCallback(this); |
| 57 | mRightBackground.setAlpha(0); |
| 58 | |
| 59 | DeviceProfile deviceProfile = mActivity.getDeviceProfile(); |
| 60 | |
| 61 | mCountX = deviceProfile.inv.numColumns * 2; |
| 62 | mCountY = deviceProfile.inv.numRows; |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 63 | setGridSize(mCountX, mCountY); |
| 64 | } |
| 65 | |
| 66 | @Override |
Sebastian Franco | 317c4fa | 2023-02-21 17:01:24 -0800 | [diff] [blame] | 67 | boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY, View dragView, |
| 68 | int[] direction, boolean commit) { |
Sebastian Franco | ee1baba | 2023-02-22 11:23:41 -0800 | [diff] [blame] | 69 | // Add seam to x position |
Sebastian Franco | c515d02 | 2023-03-01 12:51:12 -0800 | [diff] [blame] | 70 | if (cellX >= mCountX / 2) { |
Sebastian Franco | ee1baba | 2023-02-22 11:23:41 -0800 | [diff] [blame] | 71 | cellX++; |
| 72 | } |
| 73 | int finalCellX = cellX; |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 74 | return ((MulticellReorderAlgorithm) createReorderAlgorithm()).simulateSeam( |
Sebastian Franco | ee1baba | 2023-02-22 11:23:41 -0800 | [diff] [blame] | 75 | () -> super.createAreaForResize(finalCellX, cellY, spanX, spanY, dragView, |
| 76 | direction, commit)); |
Sebastian Franco | 317c4fa | 2023-02-21 17:01:24 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 80 | public ReorderAlgorithm createReorderAlgorithm() { |
| 81 | return new MulticellReorderAlgorithm(this); |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 85 | public void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) { |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 86 | int childCount = mShortcutsAndWidgets.getChildCount(); |
| 87 | for (int i = 0; i < childCount; i++) { |
| 88 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 89 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
| 90 | int seamOffset = lp.getCellX() >= mCountX / 2 && lp.canReorder ? 1 : 0; |
| 91 | CellAndSpan c = new CellAndSpan(lp.getCellX() + seamOffset, lp.getCellY(), lp.cellHSpan, |
| 92 | lp.cellVSpan); |
| 93 | solution.add(child, c); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void onDraw(Canvas canvas) { |
| 99 | if (mLeftBackground.getAlpha() > 0) { |
| 100 | mLeftBackground.setState(mBackground.getState()); |
| 101 | mLeftBackground.draw(canvas); |
| 102 | } |
| 103 | if (mRightBackground.getAlpha() > 0) { |
| 104 | mRightBackground.setState(mBackground.getState()); |
| 105 | mRightBackground.draw(canvas); |
| 106 | } |
| 107 | |
| 108 | super.onDraw(canvas); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void updateBgAlpha() { |
| 113 | mLeftBackground.setAlpha((int) (mSpringLoadedProgress * 255)); |
| 114 | mRightBackground.setAlpha((int) (mSpringLoadedProgress * 255)); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 119 | super.onLayout(changed, l, t, r, b); |
| 120 | Rect rect = mBackground.getBounds(); |
| 121 | mLeftBackground.setBounds(rect.left, rect.top, rect.right / 2 - 20, rect.bottom); |
| 122 | mRightBackground.setBounds(rect.right / 2 + 20, rect.top, rect.right, rect.bottom); |
| 123 | } |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 124 | |
| 125 | public void setCountX(int countX) { |
| 126 | mCountX = countX; |
| 127 | } |
| 128 | |
| 129 | public void setCountY(int countY) { |
| 130 | mCountY = countY; |
| 131 | } |
| 132 | |
| 133 | public void setOccupied(GridOccupancy occupied) { |
| 134 | mOccupied = occupied; |
| 135 | } |
| 136 | |
| 137 | public boolean isSeamWasAdded() { |
| 138 | return mSeamWasAdded; |
| 139 | } |
| 140 | |
| 141 | public void setSeamWasAdded(boolean seamWasAdded) { |
| 142 | mSeamWasAdded = seamWasAdded; |
| 143 | } |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 144 | } |