1 /*
2  * Copyright (C) 2024 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 
17 package com.android.launcher3.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.TableRow;
22 
23 /**
24  * A row of {@link WidgetCell}s that can be displayed in a table.
25  */
26 public class WidgetTableRow extends TableRow implements WidgetCell.PreviewReadyListener {
27     private int mNumOfReadyCells;
28     private int mNumOfCells;
29     private int mResizeDelay;
30 
WidgetTableRow(Context context)31     public WidgetTableRow(Context context) {
32         super(context);
33     }
WidgetTableRow(Context context, AttributeSet attrs)34     public WidgetTableRow(Context context, AttributeSet attrs) {
35         super(context, attrs);
36     }
37 
38     @Override
onPreviewAvailable()39     public void onPreviewAvailable() {
40         mNumOfReadyCells++;
41 
42         // Once all previews are loaded, find max visible height and adjust the preview containers.
43         if (mNumOfReadyCells == mNumOfCells) {
44             resize();
45         }
46     }
47 
resize()48     private void resize() {
49         int previewHeight = 0;
50         // get the maximum height of each widget preview
51         for (int i = 0; i < getChildCount(); i++) {
52             WidgetCell widgetCell = (WidgetCell) getChildAt(i);
53             previewHeight = Math.max(widgetCell.getPreviewContentHeight(), previewHeight);
54         }
55         if (mResizeDelay > 0) {
56             postDelayed(() -> setAlpha(1f), mResizeDelay);
57         }
58         if (previewHeight > 0) {
59             for (int i = 0; i < getChildCount(); i++) {
60                 WidgetCell widgetCell = (WidgetCell) getChildAt(i);
61                 widgetCell.setParentAlignedPreviewHeight(previewHeight);
62                 widgetCell.postDelayed(widgetCell::requestLayout, mResizeDelay);
63             }
64         }
65     }
66 
67     @Override
onLayout(boolean changed, int l, int t, int r, int b)68     protected void onLayout(boolean changed, int l, int t, int r, int b) {
69         super.onLayout(changed, l, t, r, b);
70     }
71 
72     /**
73      * Sets up the row to display the provided number of numOfCells.
74      *
75      * @param numOfCells    number of numOfCells in the row
76      * @param resizeDelayMs time to wait in millis before making any layout size adjustments e.g. we
77      *                      want to wait for list expand collapse animation before resizing the
78      *                      cell previews.
79      */
setupRow(int numOfCells, int resizeDelayMs)80     public void setupRow(int numOfCells, int resizeDelayMs) {
81         mNumOfCells = numOfCells;
82         mNumOfReadyCells = 0;
83 
84         mResizeDelay = resizeDelayMs;
85         // For delayed resize, reveal contents only after resize is done.
86         if (mResizeDelay > 0) {
87             setAlpha(0);
88         }
89     }
90 }
91