blob: 12cb35d18db290e3eaa9ee65333472b895350674 [file] [log] [blame]
Sebastian Franco09589322022-11-02 15:25:58 -07001/*
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 */
16package com.android.launcher3;
17
18import android.content.Context;
19import android.graphics.Canvas;
20import android.graphics.Rect;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.View;
24
25import com.android.launcher3.celllayout.CellLayoutLayoutParams;
Sebastian Francoe4c03452022-12-27 14:50:02 -060026import com.android.launcher3.celllayout.MulticellReorderAlgorithm;
27import com.android.launcher3.celllayout.ReorderAlgorithm;
Sebastian Franco09589322022-11-02 15:25:58 -070028import com.android.launcher3.util.CellAndSpan;
29import com.android.launcher3.util.GridOccupancy;
30
Sebastian Franco09589322022-11-02 15:25:58 -070031/**
32 * CellLayout that simulates a split in the middle for use in foldable devices.
33 */
34public class MultipageCellLayout extends CellLayout {
35
36 private final Drawable mLeftBackground;
37 private final Drawable mRightBackground;
38
Sebastian Franco317c4fa2023-02-21 17:01:24 -080039 private boolean mSeamWasAdded = false;
40
Sebastian Franco09589322022-11-02 15:25:58 -070041 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 Franco09589322022-11-02 15:25:58 -070063 setGridSize(mCountX, mCountY);
64 }
65
66 @Override
Sebastian Franco317c4fa2023-02-21 17:01:24 -080067 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY, View dragView,
68 int[] direction, boolean commit) {
Sebastian Francoee1baba2023-02-22 11:23:41 -080069 // Add seam to x position
Sebastian Francoc515d022023-03-01 12:51:12 -080070 if (cellX >= mCountX / 2) {
Sebastian Francoee1baba2023-02-22 11:23:41 -080071 cellX++;
72 }
73 int finalCellX = cellX;
Sebastian Francoe4c03452022-12-27 14:50:02 -060074 return ((MulticellReorderAlgorithm) createReorderAlgorithm()).simulateSeam(
Sebastian Francoee1baba2023-02-22 11:23:41 -080075 () -> super.createAreaForResize(finalCellX, cellY, spanX, spanY, dragView,
76 direction, commit));
Sebastian Franco317c4fa2023-02-21 17:01:24 -080077 }
78
79 @Override
Sebastian Francoe4c03452022-12-27 14:50:02 -060080 public ReorderAlgorithm createReorderAlgorithm() {
81 return new MulticellReorderAlgorithm(this);
Sebastian Franco09589322022-11-02 15:25:58 -070082 }
83
84 @Override
Sebastian Francoe4c03452022-12-27 14:50:02 -060085 public void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Sebastian Franco09589322022-11-02 15:25:58 -070086 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 Francoe4c03452022-12-27 14:50:02 -0600124
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 Franco09589322022-11-02 15:25:58 -0700144}