Source code
package android.support.v7.internal.widget;
import android.content.Context;
import android.os.Build.VERSION;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.appcompat.R;
import android.support.v7.internal.view.menu.MenuBuilder;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.ActionMenuPresenter;
import android.support.v7.widget.ActionMenuView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.accessibility.AccessibilityEvent;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ActionBarContextView extends AbsActionBarView {
private static final String TAG = "ActionBarContextView";
private View mClose;
private int mCloseItemLayout;
private View mCustomView;
private CharSequence mSubtitle;
private int mSubtitleStyleRes;
private TextView mSubtitleView;
private CharSequence mTitle;
private LinearLayout mTitleLayout;
private boolean mTitleOptional;
private int mTitleStyleRes;
private TextView mTitleView;
public /* bridge */ /* synthetic */ void animateToVisibility(int x0) {
super.animateToVisibility(x0);
}
public /* bridge */ /* synthetic */ boolean canShowOverflowMenu() {
return super.canShowOverflowMenu();
}
public /* bridge */ /* synthetic */ void dismissPopupMenus() {
super.dismissPopupMenus();
}
public /* bridge */ /* synthetic */ int getAnimatedVisibility() {
return super.getAnimatedVisibility();
}
public /* bridge */ /* synthetic */ int getContentHeight() {
return super.getContentHeight();
}
public /* bridge */ /* synthetic */ boolean isOverflowMenuShowPending() {
return super.isOverflowMenuShowPending();
}
public /* bridge */ /* synthetic */ boolean isOverflowReserved() {
return super.isOverflowReserved();
}
public /* bridge */ /* synthetic */ boolean onHoverEvent(MotionEvent x0) {
return super.onHoverEvent(x0);
}
public /* bridge */ /* synthetic */ boolean onTouchEvent(MotionEvent x0) {
return super.onTouchEvent(x0);
}
public /* bridge */ /* synthetic */ void postShowOverflowMenu() {
super.postShowOverflowMenu();
}
public /* bridge */ /* synthetic */ void setVisibility(int x0) {
super.setVisibility(x0);
}
public /* bridge */ /* synthetic */ ViewPropertyAnimatorCompat setupAnimatorToVisibility(int x0, long x1) {
return super.setupAnimatorToVisibility(x0, x1);
}
public ActionBarContextView(Context context) {
this(context, null);
}
public ActionBarContextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.actionModeStyle);
}
public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.ActionMode, defStyle, 0);
setBackgroundDrawable(a.getDrawable(R.styleable.ActionMode_background));
this.mTitleStyleRes = a.getResourceId(R.styleable.ActionMode_titleTextStyle, 0);
this.mSubtitleStyleRes = a.getResourceId(R.styleable.ActionMode_subtitleTextStyle, 0);
this.mContentHeight = a.getLayoutDimension(R.styleable.ActionMode_height, 0);
this.mCloseItemLayout = a.getResourceId(R.styleable.ActionMode_closeItemLayout, R.layout.abc_action_mode_close_item_material);
a.recycle();
}
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (this.mActionMenuPresenter != null) {
this.mActionMenuPresenter.hideOverflowMenu();
this.mActionMenuPresenter.hideSubMenus();
}
}
public void setContentHeight(int height) {
this.mContentHeight = height;
}
public void setCustomView(View view) {
if (this.mCustomView != null) {
removeView(this.mCustomView);
}
this.mCustomView = view;
if (!(view == null || this.mTitleLayout == null)) {
removeView(this.mTitleLayout);
this.mTitleLayout = null;
}
if (view != null) {
addView(view);
}
requestLayout();
}
public void setTitle(CharSequence title) {
this.mTitle = title;
initTitle();
}
public void setSubtitle(CharSequence subtitle) {
this.mSubtitle = subtitle;
initTitle();
}
public CharSequence getTitle() {
return this.mTitle;
}
public CharSequence getSubtitle() {
return this.mSubtitle;
}
private void initTitle() {
boolean hasTitle;
boolean hasSubtitle;
int i;
int i2 = 8;
if (this.mTitleLayout == null) {
LayoutInflater.from(getContext()).inflate(R.layout.abc_action_bar_title_item, this);
this.mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
this.mTitleView = (TextView) this.mTitleLayout.findViewById(R.id.action_bar_title);
this.mSubtitleView = (TextView) this.mTitleLayout.findViewById(R.id.action_bar_subtitle);
if (this.mTitleStyleRes != 0) {
this.mTitleView.setTextAppearance(getContext(), this.mTitleStyleRes);
}
if (this.mSubtitleStyleRes != 0) {
this.mSubtitleView.setTextAppearance(getContext(), this.mSubtitleStyleRes);
}
}
this.mTitleView.setText(this.mTitle);
this.mSubtitleView.setText(this.mSubtitle);
if (TextUtils.isEmpty(this.mTitle)) {
hasTitle = false;
} else {
hasTitle = true;
}
if (TextUtils.isEmpty(this.mSubtitle)) {
hasSubtitle = false;
} else {
hasSubtitle = true;
}
TextView textView = this.mSubtitleView;
if (hasSubtitle) {
i = 0;
} else {
i = 8;
}
textView.setVisibility(i);
LinearLayout linearLayout = this.mTitleLayout;
if (hasTitle || hasSubtitle) {
i2 = 0;
}
linearLayout.setVisibility(i2);
if (this.mTitleLayout.getParent() == null) {
addView(this.mTitleLayout);
}
}
public void initForMode(final ActionMode mode) {
if (this.mClose == null) {
this.mClose = LayoutInflater.from(getContext()).inflate(this.mCloseItemLayout, this, false);
addView(this.mClose);
} else if (this.mClose.getParent() == null) {
addView(this.mClose);
}
this.mClose.findViewById(R.id.action_mode_close_button).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mode.finish();
}
});
MenuBuilder menu = (MenuBuilder) mode.getMenu();
if (this.mActionMenuPresenter != null) {
this.mActionMenuPresenter.dismissPopupMenus();
}
this.mActionMenuPresenter = new ActionMenuPresenter(getContext());
this.mActionMenuPresenter.setReserveOverflow(true);
LayoutParams layoutParams = new LayoutParams(-2, -1);
menu.addMenuPresenter(this.mActionMenuPresenter, this.mPopupContext);
this.mMenuView = (ActionMenuView) this.mActionMenuPresenter.getMenuView(this);
this.mMenuView.setBackgroundDrawable(null);
addView(this.mMenuView, layoutParams);
}
public void closeMode() {
if (this.mClose == null) {
killMode();
}
}
public void killMode() {
removeAllViews();
this.mCustomView = null;
this.mMenuView = null;
}
public boolean showOverflowMenu() {
if (this.mActionMenuPresenter != null) {
return this.mActionMenuPresenter.showOverflowMenu();
}
return false;
}
public boolean hideOverflowMenu() {
if (this.mActionMenuPresenter != null) {
return this.mActionMenuPresenter.hideOverflowMenu();
}
return false;
}
public boolean isOverflowMenuShowing() {
if (this.mActionMenuPresenter != null) {
return this.mActionMenuPresenter.isOverflowMenuShowing();
}
return false;
}
protected LayoutParams generateDefaultLayoutParams() {
return new MarginLayoutParams(-1, -2);
}
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (MeasureSpec.getMode(widthMeasureSpec) != 1073741824) {
throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + "with android:layout_width=\"match_parent\" (or fill_parent)");
} else if (MeasureSpec.getMode(heightMeasureSpec) == 0) {
throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + "with android:layout_height=\"wrap_content\"");
} else {
int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
int maxHeight = this.mContentHeight > 0 ? this.mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
int verticalPadding = getPaddingTop() + getPaddingBottom();
int availableWidth = (contentWidth - getPaddingLeft()) - getPaddingRight();
int height = maxHeight - verticalPadding;
int childSpecHeight = MeasureSpec.makeMeasureSpec(height, Integer.MIN_VALUE);
if (this.mClose != null) {
MarginLayoutParams lp = (MarginLayoutParams) this.mClose.getLayoutParams();
availableWidth = measureChildView(this.mClose, availableWidth, childSpecHeight, 0) - (lp.leftMargin + lp.rightMargin);
}
if (this.mMenuView != null && this.mMenuView.getParent() == this) {
availableWidth = measureChildView(this.mMenuView, availableWidth, childSpecHeight, 0);
}
if (this.mTitleLayout != null && this.mCustomView == null) {
if (this.mTitleOptional) {
this.mTitleLayout.measure(MeasureSpec.makeMeasureSpec(0, 0), childSpecHeight);
int titleWidth = this.mTitleLayout.getMeasuredWidth();
boolean titleFits = titleWidth <= availableWidth;
if (titleFits) {
availableWidth -= titleWidth;
}
this.mTitleLayout.setVisibility(titleFits ? 0 : 8);
} else {
availableWidth = measureChildView(this.mTitleLayout, availableWidth, childSpecHeight, 0);
}
}
if (this.mCustomView != null) {
int customWidth;
int customHeight;
LayoutParams lp2 = this.mCustomView.getLayoutParams();
int customWidthMode = lp2.width != -2 ? 1073741824 : Integer.MIN_VALUE;
if (lp2.width >= 0) {
customWidth = Math.min(lp2.width, availableWidth);
} else {
customWidth = availableWidth;
}
int customHeightMode = lp2.height != -2 ? 1073741824 : Integer.MIN_VALUE;
if (lp2.height >= 0) {
customHeight = Math.min(lp2.height, height);
} else {
customHeight = height;
}
this.mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
}
if (this.mContentHeight <= 0) {
int measuredHeight = 0;
int count = getChildCount();
for (int i = 0; i < count; i++) {
int paddedViewHeight = getChildAt(i).getMeasuredHeight() + verticalPadding;
if (paddedViewHeight > measuredHeight) {
measuredHeight = paddedViewHeight;
}
}
setMeasuredDimension(contentWidth, measuredHeight);
return;
}
setMeasuredDimension(contentWidth, maxHeight);
}
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
boolean isLayoutRtl = ViewUtils.isLayoutRtl(this);
int x = isLayoutRtl ? (r - l) - getPaddingRight() : getPaddingLeft();
int y = getPaddingTop();
int contentHeight = ((b - t) - getPaddingTop()) - getPaddingBottom();
if (!(this.mClose == null || this.mClose.getVisibility() == 8)) {
MarginLayoutParams lp = (MarginLayoutParams) this.mClose.getLayoutParams();
int startMargin = isLayoutRtl ? lp.rightMargin : lp.leftMargin;
int endMargin = isLayoutRtl ? lp.leftMargin : lp.rightMargin;
x = AbsActionBarView.next(x, startMargin, isLayoutRtl);
x = AbsActionBarView.next(x + positionChild(this.mClose, x, y, contentHeight, isLayoutRtl), endMargin, isLayoutRtl);
}
if (!(this.mTitleLayout == null || this.mCustomView != null || this.mTitleLayout.getVisibility() == 8)) {
x += positionChild(this.mTitleLayout, x, y, contentHeight, isLayoutRtl);
}
if (this.mCustomView != null) {
x += positionChild(this.mCustomView, x, y, contentHeight, isLayoutRtl);
}
x = isLayoutRtl ? getPaddingLeft() : (r - l) - getPaddingRight();
if (this.mMenuView != null) {
x += positionChild(this.mMenuView, x, y, contentHeight, !isLayoutRtl);
}
}
public boolean shouldDelayChildPressedState() {
return false;
}
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
if (VERSION.SDK_INT < 14) {
return;
}
if (event.getEventType() == 32) {
event.setSource(this);
event.setClassName(getClass().getName());
event.setPackageName(getContext().getPackageName());
event.setContentDescription(this.mTitle);
return;
}
super.onInitializeAccessibilityEvent(event);
}
public void setTitleOptional(boolean titleOptional) {
if (titleOptional != this.mTitleOptional) {
requestLayout();
}
this.mTitleOptional = titleOptional;
}
public boolean isTitleOptional() {
return this.mTitleOptional;
}
}