Debian Codegraph @

core/java/com/android/internal/widget/ConversationHeaderLinearLayout.java

raw symbols SCIP
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 com.android.internal.widget;
18
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.LinearLayout;
24 import android.widget.RemoteViews;
25
26 import java.util.LinkedList;
27 import java.util.List;
28
29 /**
30 * This is a subclass of LinearLayout meant to be used in the Conversation header, to fix a bug
31 * when multiple user-provided strings are shown in the same conversation header. b/189723284
32 *
33 * This works around a deficiency in LinearLayout when shrinking views that it can't fully reduce
34 * all contents if any of the oversized views reaches zero.
35 */
36 @RemoteViews.RemoteView
37 public class ConversationHeaderLinearLayout extends LinearLayout {
38
39 public ConversationHeaderLinearLayout(Context context) {
40 super(context);
41 }
42
43 public ConversationHeaderLinearLayout(Context context,
44 @Nullable AttributeSet attrs) {
45 super(context, attrs);
46 }
47
48 public ConversationHeaderLinearLayout(Context context, @Nullable AttributeSet attrs,
49 int defStyleAttr) {
50 super(context, attrs, defStyleAttr);
51 }
52
53 private int calculateTotalChildLength() {
54 final int count = getChildCount();
55 int totalLength = 0;
56
57 for (int i = 0; i < count; ++i) {
58 final View child = getChildAt(i);
59 if (child == null || child.getVisibility() == GONE) {
60 continue;
61 }
62 final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)
63 child.getLayoutParams();
64 totalLength += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
65 }
66 return totalLength + getPaddingLeft() + getPaddingRight();
67 }
68
69 @Override
70 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
71 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
72
73 final int containerWidth = getMeasuredWidth();
74 final int contentsWidth = calculateTotalChildLength();
75
76 int excessContents = contentsWidth - containerWidth;
77 if (excessContents <= 0) {
78 return;
79 }
80 final int count = getChildCount();
81
82 float remainingWeight = 0;
83 List<ViewInfo> visibleChildrenToShorten = null;
84
85 // Find children which need to be shortened in order to ensure the contents fit.
86 for (int i = 0; i < count; ++i) {
87 final View child = getChildAt(i);
88 if (child == null || child.getVisibility() == View.GONE) {
89 continue;
90 }
91 final float weight = ((LayoutParams) child.getLayoutParams()).weight;
92 if (weight == 0) {
93 continue;
94 }
95 if (child.getMeasuredWidth() == 0) {
96 continue;
97 }
98 if (visibleChildrenToShorten == null) {
99 visibleChildrenToShorten = new LinkedList<>();
100 }
101 visibleChildrenToShorten.add(new ViewInfo(child));
102 remainingWeight += Math.max(0, weight);
103 }
104 if (visibleChildrenToShorten == null || visibleChildrenToShorten.isEmpty()) {
105 return;
106 }
107 balanceViewWidths(visibleChildrenToShorten, remainingWeight, excessContents);
108 remeasureChangedChildren(visibleChildrenToShorten);
109 }
110
111 /**
112 * Measure any child with a width that has changed.
113 */
114 private void remeasureChangedChildren(List<ViewInfo> childrenInfo) {
115 for (ViewInfo info : childrenInfo) {
116 if (info.mWidth != info.mStartWidth) {
117 final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
118 Math.max(0, info.mWidth), MeasureSpec.EXACTLY);
119 final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
120 info.mView.getMeasuredHeight(), MeasureSpec.EXACTLY);
121 info.mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
122 }
123 }
124 }
125
126 /**
127 * Given a list of view, use the weights to remove width from each view proportionally to the
128 * weight (and ignoring the view's actual width), but do this iteratively whenever a view is
129 * reduced to zero width, because in that case other views need reduction.
130 */
131 void balanceViewWidths(List<ViewInfo> viewInfos, float weightSum, int excessContents) {
132 boolean performAnotherPass = true;
133 // Loops only when all of the following are true:
134 // * `performAnotherPass` -- a view clamped to 0 width (or the first iteration)
135 // * `excessContents > 0` -- there is still horizontal space to allocate
136 // * `weightSum > 0` -- at least 1 view with nonzero width AND nonzero weight left
137 while (performAnotherPass && excessContents > 0 && weightSum > 0) {
138 int excessRemovedDuringThisPass = 0;
139 float weightSumForNextPass = 0;
140 performAnotherPass = false;
141 for (ViewInfo info : viewInfos) {
142 if (info.mWeight <= 0) {
143 continue;
144 }
145 if (info.mWidth <= 0) {
146 continue;
147 }
148 int newWidth = (int) (info.mWidth - (excessContents * (info.mWeight / weightSum)));
149 if (newWidth < 0) {
150 newWidth = 0;
151 performAnotherPass = true;
152 }
153 excessRemovedDuringThisPass += info.mWidth - newWidth;
154 info.mWidth = newWidth;
155 if (info.mWidth > 0) {
156 weightSumForNextPass += info.mWeight;
157 }
158 }
159 excessContents -= excessRemovedDuringThisPass;
160 weightSum = weightSumForNextPass;
161 }
162 }
163
164 /**
165 * A helper class for measuring children.
166 */
167 static class ViewInfo {
168 final View mView;
169 final float mWeight;
170 final int mStartWidth;
171 int mWidth;
172
173 ViewInfo(View view) {
174 this.mView = view;
175 this.mWeight = ((LayoutParams) view.getLayoutParams()).weight;
176 this.mStartWidth = this.mWidth = view.getMeasuredWidth();
177 }
178 }
179 }