Program: 199008
Project: 199008
Buggy source:
@Override protected void onLayout(boolean changed,int l,int t,int r,int b){
  final boolean isRtl=getLayoutDirection() == LAYOUT_DIRECTION_RTL;
  final int width=getWidth();
  final int height=getHeight();
  final int paddingLeft=getPaddingLeft();
  final int paddingRight=getPaddingRight();
  final int paddingTop=getPaddingTop();
  final int paddingBottom=getPaddingBottom();
  int left=paddingLeft;
  int right=width - paddingRight;
  final int[] collapsingMargins=mTempMargins;
  collapsingMargins[0]=collapsingMargins[1]=0;
  if (shouldLayout(mNavButtonView)) {
    if (isRtl) {
      right=layoutChildRight(mNavButtonView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mNavButtonView,left,collapsingMargins);
    }
  }
  if (shouldLayout(mCollapseButtonView)) {
    if (isRtl) {
      right=layoutChildRight(mCollapseButtonView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mCollapseButtonView,left,collapsingMargins);
    }
  }
  if (shouldLayout(mMenuView)) {
    if (isRtl) {
      left=layoutChildLeft(mMenuView,left,collapsingMargins);
    }
 else {
      right=layoutChildRight(mMenuView,right,collapsingMargins);
    }
  }
  collapsingMargins[0]=Math.max(0,getContentInsetLeft() - left);
  collapsingMargins[1]=Math.max(0,getContentInsetRight() - (width - paddingRight - right));
  left=Math.max(left,getContentInsetLeft());
  right=Math.min(right,width - paddingRight - getContentInsetRight());
  if (shouldLayout(mExpandedActionView)) {
    if (isRtl) {
      right=layoutChildRight(mExpandedActionView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mExpandedActionView,left,collapsingMargins);
    }
  }
  if (shouldLayout(mLogoView)) {
    if (isRtl) {
      right=layoutChildRight(mLogoView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mLogoView,left,collapsingMargins);
    }
  }
  final boolean layoutTitle=shouldLayout(mTitleTextView);
  final boolean layoutSubtitle=shouldLayout(mSubtitleTextView);
  int titleHeight=0;
  if (layoutTitle) {
    final LayoutParams lp=(LayoutParams)mTitleTextView.getLayoutParams();
    titleHeight+=lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
  }
  if (layoutSubtitle) {
    final LayoutParams lp=(LayoutParams)mSubtitleTextView.getLayoutParams();
    titleHeight+=lp.bottomMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
  }
  if (layoutTitle || layoutSubtitle) {
    int titleTop;
    final View topChild=layoutTitle ? mTitleTextView : mSubtitleTextView;
    final View bottomChild=layoutSubtitle ? mSubtitleTextView : mTitleTextView;
    final LayoutParams toplp=(LayoutParams)topChild.getLayoutParams();
    final LayoutParams bottomlp=(LayoutParams)bottomChild.getLayoutParams();
switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP:
      titleTop=getPaddingTop() + toplp.topMargin + mTitleMarginTop;
    break;
default :
case Gravity.CENTER_VERTICAL:
  final int space=height - paddingTop - paddingBottom;
int spaceAbove=(space - titleHeight) / 2;
if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
spaceAbove=toplp.topMargin + mTitleMarginTop;
}
 else {
final int spaceBelow=height - paddingBottom - titleHeight- spaceAbove- paddingTop;
if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
  spaceAbove=Math.max(0,spaceAbove - (bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
}
}
titleTop=paddingTop + spaceAbove;
break;
case Gravity.BOTTOM:
titleTop=height - paddingBottom - bottomlp.bottomMargin- mTitleMarginBottom- titleHeight;
break;
}
if (isRtl) {
final int rd=mTitleMarginStart - collapsingMargins[1];
right-=Math.max(0,rd);
collapsingMargins[1]=Math.max(0,-rd);
int titleRight=right;
int subtitleRight=right;
if (layoutTitle) {
final LayoutParams lp=(LayoutParams)mTitleTextView.getLayoutParams();
final int titleLeft=titleRight - mTitleTextView.getMeasuredWidth();
final int titleBottom=titleTop + mTitleTextView.getMeasuredHeight();
mTitleTextView.layout(titleLeft,titleTop,titleRight,titleBottom);
titleRight=titleLeft - mTitleMarginEnd;
titleTop=titleBottom + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp=(LayoutParams)mSubtitleTextView.getLayoutParams();
titleTop+=lp.topMargin;
final int subtitleLeft=subtitleRight - mSubtitleTextView.getMeasuredWidth();
final int subtitleBottom=titleTop + mSubtitleTextView.getMeasuredHeight();
mSubtitleTextView.layout(subtitleLeft,titleTop,subtitleRight,subtitleBottom);
subtitleRight=subtitleRight - mTitleMarginEnd;
titleTop=subtitleBottom + lp.bottomMargin;
}
right=Math.max(titleRight,subtitleRight);
}
 else {
final int ld=mTitleMarginStart - collapsingMargins[0];
left+=Math.max(0,ld);
collapsingMargins[0]=Math.max(0,-ld);
int titleLeft=left;
int subtitleLeft=left;
if (layoutTitle) {
final LayoutParams lp=(LayoutParams)mTitleTextView.getLayoutParams();
final int titleRight=titleLeft + mTitleTextView.getMeasuredWidth();
final int titleBottom=titleTop + mTitleTextView.getMeasuredHeight();
mTitleTextView.layout(titleLeft,titleTop,titleRight,titleBottom);
titleLeft=titleRight + mTitleMarginEnd;
titleTop=titleBottom + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp=(LayoutParams)mSubtitleTextView.getLayoutParams();
titleTop+=lp.topMargin;
final int subtitleRight=subtitleLeft + mSubtitleTextView.getMeasuredWidth();
final int subtitleBottom=titleTop + mSubtitleTextView.getMeasuredHeight();
mSubtitleTextView.layout(subtitleLeft,titleTop,subtitleRight,subtitleBottom);
subtitleLeft=subtitleRight + mTitleMarginEnd;
titleTop=subtitleBottom + lp.bottomMargin;
}
left=Math.max(titleLeft,subtitleLeft);
}
}
addCustomViewsWithGravity(mTempViews,Gravity.LEFT);
final int leftViewsCount=mTempViews.size();
for (int i=0; i < leftViewsCount; i++) {
left=layoutChildLeft(mTempViews.get(i),left,collapsingMargins);
}
addCustomViewsWithGravity(mTempViews,Gravity.RIGHT);
final int rightViewsCount=mTempViews.size();
for (int i=0; i < rightViewsCount; i++) {
right=layoutChildRight(mTempViews.get(i),right,collapsingMargins);
}
addCustomViewsWithGravity(mTempViews,Gravity.CENTER_HORIZONTAL);
final int centerViewsWidth=getViewListMeasuredWidth(mTempViews,collapsingMargins);
final int parentCenter=paddingLeft + (width - paddingLeft - paddingRight) / 2;
final int halfCenterViewsWidth=centerViewsWidth / 2;
int centerLeft=parentCenter - halfCenterViewsWidth;
final int centerRight=centerLeft + centerViewsWidth;
if (centerLeft < left) {
centerLeft=left;
}
 else if (centerRight > right) {
centerLeft-=centerRight - right;
}
final int centerViewsCount=mTempViews.size();
for (int i=0; i < centerViewsCount; i++) {
centerLeft=layoutChildLeft(mTempViews.get(i),centerLeft,collapsingMargins);
}
mTempViews.clear();
}

Fixed version (ground truth):
@Override protected void onLayout(boolean changed,int l,int t,int r,int b){
  final boolean isRtl=getLayoutDirection() == LAYOUT_DIRECTION_RTL;
  final int width=getWidth();
  final int height=getHeight();
  final int paddingLeft=getPaddingLeft();
  final int paddingRight=getPaddingRight();
  final int paddingTop=getPaddingTop();
  final int paddingBottom=getPaddingBottom();
  int left=paddingLeft;
  int right=width - paddingRight;
  final int[] collapsingMargins=mTempMargins;
  collapsingMargins[0]=collapsingMargins[1]=0;
  if (shouldLayout(mNavButtonView)) {
    if (isRtl) {
      right=layoutChildRight(mNavButtonView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mNavButtonView,left,collapsingMargins);
    }
  }
  if (shouldLayout(mCollapseButtonView)) {
    if (isRtl) {
      right=layoutChildRight(mCollapseButtonView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mCollapseButtonView,left,collapsingMargins);
    }
  }
  if (shouldLayout(mMenuView)) {
    if (isRtl) {
      left=layoutChildLeft(mMenuView,left,collapsingMargins);
    }
 else {
      right=layoutChildRight(mMenuView,right,collapsingMargins);
    }
  }
  collapsingMargins[0]=Math.max(0,getContentInsetLeft() - left);
  collapsingMargins[1]=Math.max(0,getContentInsetRight() - (width - paddingRight - right));
  left=Math.max(left,getContentInsetLeft());
  right=Math.min(right,width - paddingRight - getContentInsetRight());
  if (shouldLayout(mExpandedActionView)) {
    if (isRtl) {
      right=layoutChildRight(mExpandedActionView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mExpandedActionView,left,collapsingMargins);
    }
  }
  if (shouldLayout(mLogoView)) {
    if (isRtl) {
      right=layoutChildRight(mLogoView,right,collapsingMargins);
    }
 else {
      left=layoutChildLeft(mLogoView,left,collapsingMargins);
    }
  }
  final boolean layoutTitle=shouldLayout(mTitleTextView);
  final boolean layoutSubtitle=shouldLayout(mSubtitleTextView);
  int titleHeight=0;
  if (layoutTitle) {
    final LayoutParams lp=(LayoutParams)mTitleTextView.getLayoutParams();
    titleHeight+=lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
  }
  if (layoutSubtitle) {
    final LayoutParams lp=(LayoutParams)mSubtitleTextView.getLayoutParams();
    titleHeight+=lp.topMargin + mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin;
  }
  if (layoutTitle || layoutSubtitle) {
    int titleTop;
    final View topChild=layoutTitle ? mTitleTextView : mSubtitleTextView;
    final View bottomChild=layoutSubtitle ? mSubtitleTextView : mTitleTextView;
    final LayoutParams toplp=(LayoutParams)topChild.getLayoutParams();
    final LayoutParams bottomlp=(LayoutParams)bottomChild.getLayoutParams();
switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP:
      titleTop=getPaddingTop() + toplp.topMargin + mTitleMarginTop;
    break;
default :
case Gravity.CENTER_VERTICAL:
  final int space=height - paddingTop - paddingBottom;
int spaceAbove=(space - titleHeight) / 2;
if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
spaceAbove=toplp.topMargin + mTitleMarginTop;
}
 else {
final int spaceBelow=height - paddingBottom - titleHeight- spaceAbove- paddingTop;
if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
  spaceAbove=Math.max(0,spaceAbove - (bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
}
}
titleTop=paddingTop + spaceAbove;
break;
case Gravity.BOTTOM:
titleTop=height - paddingBottom - bottomlp.bottomMargin- mTitleMarginBottom- titleHeight;
break;
}
if (isRtl) {
final int rd=mTitleMarginStart - collapsingMargins[1];
right-=Math.max(0,rd);
collapsingMargins[1]=Math.max(0,-rd);
int titleRight=right;
int subtitleRight=right;
if (layoutTitle) {
final LayoutParams lp=(LayoutParams)mTitleTextView.getLayoutParams();
final int titleLeft=titleRight - mTitleTextView.getMeasuredWidth();
final int titleBottom=titleTop + mTitleTextView.getMeasuredHeight();
mTitleTextView.layout(titleLeft,titleTop,titleRight,titleBottom);
titleRight=titleLeft - mTitleMarginEnd;
titleTop=titleBottom + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp=(LayoutParams)mSubtitleTextView.getLayoutParams();
titleTop+=lp.topMargin;
final int subtitleLeft=subtitleRight - mSubtitleTextView.getMeasuredWidth();
final int subtitleBottom=titleTop + mSubtitleTextView.getMeasuredHeight();
mSubtitleTextView.layout(subtitleLeft,titleTop,subtitleRight,subtitleBottom);
subtitleRight=subtitleRight - mTitleMarginEnd;
titleTop=subtitleBottom + lp.bottomMargin;
}
right=Math.max(titleRight,subtitleRight);
}
 else {
final int ld=mTitleMarginStart - collapsingMargins[0];
left+=Math.max(0,ld);
collapsingMargins[0]=Math.max(0,-ld);
int titleLeft=left;
int subtitleLeft=left;
if (layoutTitle) {
final LayoutParams lp=(LayoutParams)mTitleTextView.getLayoutParams();
final int titleRight=titleLeft + mTitleTextView.getMeasuredWidth();
final int titleBottom=titleTop + mTitleTextView.getMeasuredHeight();
mTitleTextView.layout(titleLeft,titleTop,titleRight,titleBottom);
titleLeft=titleRight + mTitleMarginEnd;
titleTop=titleBottom + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp=(LayoutParams)mSubtitleTextView.getLayoutParams();
titleTop+=lp.topMargin;
final int subtitleRight=subtitleLeft + mSubtitleTextView.getMeasuredWidth();
final int subtitleBottom=titleTop + mSubtitleTextView.getMeasuredHeight();
mSubtitleTextView.layout(subtitleLeft,titleTop,subtitleRight,subtitleBottom);
subtitleLeft=subtitleRight + mTitleMarginEnd;
titleTop=subtitleBottom + lp.bottomMargin;
}
left=Math.max(titleLeft,subtitleLeft);
}
}
addCustomViewsWithGravity(mTempViews,Gravity.LEFT);
final int leftViewsCount=mTempViews.size();
for (int i=0; i < leftViewsCount; i++) {
left=layoutChildLeft(mTempViews.get(i),left,collapsingMargins);
}
addCustomViewsWithGravity(mTempViews,Gravity.RIGHT);
final int rightViewsCount=mTempViews.size();
for (int i=0; i < rightViewsCount; i++) {
right=layoutChildRight(mTempViews.get(i),right,collapsingMargins);
}
addCustomViewsWithGravity(mTempViews,Gravity.CENTER_HORIZONTAL);
final int centerViewsWidth=getViewListMeasuredWidth(mTempViews,collapsingMargins);
final int parentCenter=paddingLeft + (width - paddingLeft - paddingRight) / 2;
final int halfCenterViewsWidth=centerViewsWidth / 2;
int centerLeft=parentCenter - halfCenterViewsWidth;
final int centerRight=centerLeft + centerViewsWidth;
if (centerLeft < left) {
centerLeft=left;
}
 else if (centerRight > right) {
centerLeft-=centerRight - right;
}
final int centerViewsCount=mTempViews.size();
for (int i=0; i < centerViewsCount; i++) {
centerLeft=layoutChildLeft(mTempViews.get(i),centerLeft,collapsingMargins);
}
mTempViews.clear();
}