| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
void balanceViewWidths(List<ViewInfo> viewInfos, float weightSum, int excessContents) { |
| 132 |
boolean performAnotherPass = true; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 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 |
|
| 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 |
} |