1 /* 2 * Copyright (C) 2021 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 androidx.constraintlayout.core.widgets; 18 19 import androidx.constraintlayout.core.widgets.analyzer.Grouping; 20 import androidx.constraintlayout.core.widgets.analyzer.WidgetGroup; 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.HashMap; 25 26 /** 27 * HelperWidget class 28 */ 29 public class HelperWidget extends ConstraintWidget implements Helper { 30 public ConstraintWidget[] mWidgets = new ConstraintWidget[4]; 31 public int mWidgetsCount = 0; 32 33 @Override updateConstraints(ConstraintWidgetContainer container)34 public void updateConstraints(ConstraintWidgetContainer container) { 35 // nothing here 36 } 37 38 /** 39 * Add a widget to the helper 40 * 41 * @param widget a widget 42 */ 43 @Override add(ConstraintWidget widget)44 public void add(ConstraintWidget widget) { 45 if (widget == this || widget == null) { 46 return; 47 } 48 if (mWidgetsCount + 1 > mWidgets.length) { 49 mWidgets = Arrays.copyOf(mWidgets, mWidgets.length * 2); 50 } 51 mWidgets[mWidgetsCount] = widget; 52 mWidgetsCount++; 53 } 54 55 @Override copy(ConstraintWidget src, HashMap<ConstraintWidget, ConstraintWidget> map)56 public void copy(ConstraintWidget src, HashMap<ConstraintWidget, ConstraintWidget> map) { 57 super.copy(src, map); 58 HelperWidget srcHelper = (HelperWidget) src; 59 mWidgetsCount = 0; 60 final int count = srcHelper.mWidgetsCount; 61 for (int i = 0; i < count; i++) { 62 add(map.get(srcHelper.mWidgets[i])); 63 } 64 } 65 66 /** 67 * Reset the widgets list contained by this helper 68 */ 69 @Override removeAllIds()70 public void removeAllIds() { 71 mWidgetsCount = 0; 72 Arrays.fill(mWidgets, null); 73 } 74 75 // @TODO: add description addDependents(ArrayList<WidgetGroup> dependencyLists, int orientation, WidgetGroup group)76 public void addDependents(ArrayList<WidgetGroup> dependencyLists, 77 int orientation, 78 WidgetGroup group) { 79 for (int i = 0; i < mWidgetsCount; i++) { 80 ConstraintWidget widget = mWidgets[i]; 81 group.add(widget); 82 } 83 for (int i = 0; i < mWidgetsCount; i++) { 84 ConstraintWidget widget = mWidgets[i]; 85 Grouping.findDependents(widget, orientation, dependencyLists, group); 86 } 87 } 88 89 // @TODO: add description findGroupInDependents(int orientation)90 public int findGroupInDependents(int orientation) { 91 for (int i = 0; i < mWidgetsCount; i++) { 92 ConstraintWidget widget = mWidgets[i]; 93 if (orientation == HORIZONTAL && widget.horizontalGroup != -1) { 94 return widget.horizontalGroup; 95 } 96 if (orientation == VERTICAL && widget.verticalGroup != -1) { 97 return widget.verticalGroup; 98 } 99 } 100 return -1; 101 } 102 } 103