Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2014
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 53.30 KB | None | 0 0
  1. package android.support.v7.widget;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Build.VERSION;
  7. import android.os.Parcel;
  8. import android.os.Parcelable;
  9. import android.os.Parcelable.Creator;
  10. import android.support.annotation.Nullable;
  11. import android.support.v4.view.GravityCompat;
  12. import android.support.v4.view.MarginLayoutParamsCompat;
  13. import android.support.v4.view.MenuItemCompat;
  14. import android.support.v4.view.MotionEventCompat;
  15. import android.support.v4.view.ViewCompat;
  16. import android.support.v7.app.ActionBar.LayoutParams;
  17. import android.support.v7.appcompat.R.attr;
  18. import android.support.v7.appcompat.R.styleable;
  19. import android.support.v7.internal.view.SupportMenuInflater;
  20. import android.support.v7.internal.view.menu.MenuBuilder;
  21. import android.support.v7.internal.view.menu.MenuBuilder.Callback;
  22. import android.support.v7.internal.view.menu.MenuItemImpl;
  23. import android.support.v7.internal.view.menu.MenuPresenter;
  24. import android.support.v7.internal.view.menu.MenuPresenter.Callback;
  25. import android.support.v7.internal.view.menu.MenuView;
  26. import android.support.v7.internal.view.menu.SubMenuBuilder;
  27. import android.support.v7.internal.widget.DecorToolbar;
  28. import android.support.v7.internal.widget.RtlSpacingHelper;
  29. import android.support.v7.internal.widget.TintManager;
  30. import android.support.v7.internal.widget.TintTypedArray;
  31. import android.support.v7.internal.widget.ToolbarWidgetWrapper;
  32. import android.support.v7.internal.widget.ViewUtils;
  33. import android.support.v7.view.CollapsibleActionView;
  34. import android.text.Layout;
  35. import android.text.TextUtils;
  36. import android.text.TextUtils.TruncateAt;
  37. import android.util.AttributeSet;
  38. import android.view.ContextThemeWrapper;
  39. import android.view.Menu;
  40. import android.view.MenuInflater;
  41. import android.view.MenuItem;
  42. import android.view.MotionEvent;
  43. import android.view.View;
  44. import android.view.View.BaseSavedState;
  45. import android.view.View.MeasureSpec;
  46. import android.view.View.OnClickListener;
  47. import android.view.ViewGroup;
  48. import android.view.ViewGroup.LayoutParams;
  49. import android.view.ViewGroup.MarginLayoutParams;
  50. import android.widget.ImageButton;
  51. import android.widget.ImageView;
  52. import android.widget.TextView;
  53. import java.util.ArrayList;
  54. import java.util.List;
  55.  
  56. public class Toolbar
  57.   extends ViewGroup
  58. {
  59.   private static final String TAG = "Toolbar";
  60.   private ActionMenuView mMenuView;
  61.   private TextView mTitleTextView;
  62.   private TextView mSubtitleTextView;
  63.   private ImageButton mNavButtonView;
  64.   private ImageView mLogoView;
  65.   private Drawable mCollapseIcon;
  66.   private ImageButton mCollapseButtonView;
  67.   View mExpandedActionView;
  68.   private Context mPopupContext;
  69.   private int mPopupTheme;
  70.   private int mTitleTextAppearance;
  71.   private int mSubtitleTextAppearance;
  72.   private int mButtonGravity;
  73.   private int mMaxButtonHeight;
  74.   private int mTitleMarginStart;
  75.   private int mTitleMarginEnd;
  76.   private int mTitleMarginTop;
  77.   private int mTitleMarginBottom;
  78.   private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper();
  79.   private int mGravity = 8388627;
  80.   private CharSequence mTitleText;
  81.   private CharSequence mSubtitleText;
  82.   private int mTitleTextColor;
  83.   private int mSubtitleTextColor;
  84.   private boolean mEatingTouch;
  85.   private final ArrayList<View> mTempViews = new ArrayList();
  86.   private final int[] mTempMargins = new int[2];
  87.   private OnMenuItemClickListener mOnMenuItemClickListener;
  88.   private final ActionMenuView.OnMenuItemClickListener mMenuViewItemClickListener = new ActionMenuView.OnMenuItemClickListener()
  89.   {
  90.     public boolean onMenuItemClick(MenuItem item)
  91.     {
  92.       if (Toolbar.this.mOnMenuItemClickListener != null) {
  93.         return Toolbar.this.mOnMenuItemClickListener.onMenuItemClick(item);
  94.       }
  95.       return false;
  96.     }
  97.   };
  98.   private ToolbarWidgetWrapper mWrapper;
  99.   private ActionMenuPresenter mOuterActionMenuPresenter;
  100.   private ExpandedActionViewMenuPresenter mExpandedMenuPresenter;
  101.   private MenuPresenter.Callback mActionMenuPresenterCallback;
  102.   private MenuBuilder.Callback mMenuBuilderCallback;
  103.   private boolean mCollapsible;
  104.   private int mMinHeight;
  105.   private final Runnable mShowOverflowMenuRunnable = new Runnable()
  106.   {
  107.     public void run()
  108.     {
  109.       Toolbar.this.showOverflowMenu();
  110.     }
  111.   };
  112.   private final TintManager mTintManager;
  113.  
  114.   public Toolbar(Context context)
  115.   {
  116.     this(context, null);
  117.   }
  118.  
  119.   public Toolbar(Context context, AttributeSet attrs)
  120.   {
  121.     this(context, attrs, R.attr.toolbarStyle);
  122.   }
  123.  
  124.   public Toolbar(Context context, AttributeSet attrs, int defStyleAttr)
  125.   {
  126.     super(themifyContext(context, attrs, defStyleAttr), attrs, defStyleAttr);
  127.    
  128.  
  129.     TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, R.styleable.Toolbar, defStyleAttr, 0);
  130.    
  131.  
  132.     this.mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
  133.     this.mSubtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
  134.     this.mGravity = a.getInteger(R.styleable.Toolbar_android_gravity, this.mGravity);
  135.     this.mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, 48);
  136.     this.mTitleMarginStart = (this.mTitleMarginEnd = this.mTitleMarginTop = this.mTitleMarginBottom = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargins, 0));
  137.    
  138.  
  139.     int marginStart = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginStart, -1);
  140.     if (marginStart >= 0) {
  141.       this.mTitleMarginStart = marginStart;
  142.     }
  143.     int marginEnd = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginEnd, -1);
  144.     if (marginEnd >= 0) {
  145.       this.mTitleMarginEnd = marginEnd;
  146.     }
  147.     int marginTop = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginTop, -1);
  148.     if (marginTop >= 0) {
  149.       this.mTitleMarginTop = marginTop;
  150.     }
  151.     int marginBottom = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginBottom, -1);
  152.     if (marginBottom >= 0) {
  153.       this.mTitleMarginBottom = marginBottom;
  154.     }
  155.     this.mMaxButtonHeight = a.getDimensionPixelSize(R.styleable.Toolbar_maxButtonHeight, -1);
  156.    
  157.     int contentInsetStart = a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetStart, -2147483648);
  158.    
  159.  
  160.     int contentInsetEnd = a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetEnd, -2147483648);
  161.    
  162.  
  163.     int contentInsetLeft = a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetLeft, 0);
  164.    
  165.     int contentInsetRight = a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
  166.    
  167.  
  168.     this.mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
  169.     if ((contentInsetStart != -2147483648) || (contentInsetEnd != -2147483648)) {
  170.       this.mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
  171.     }
  172.     this.mCollapseIcon = a.getDrawable(R.styleable.Toolbar_collapseIcon);
  173.    
  174.     CharSequence title = a.getText(R.styleable.Toolbar_title);
  175.     if (!TextUtils.isEmpty(title)) {
  176.       setTitle(title);
  177.     }
  178.     CharSequence subtitle = a.getText(R.styleable.Toolbar_subtitle);
  179.     if (!TextUtils.isEmpty(subtitle)) {
  180.       setSubtitle(subtitle);
  181.     }
  182.     this.mPopupContext = getContext();
  183.     setPopupTheme(a.getResourceId(R.styleable.Toolbar_popupTheme, 0));
  184.    
  185.     Drawable navIcon = a.getDrawable(R.styleable.Toolbar_navigationIcon);
  186.     if (navIcon != null) {
  187.       setNavigationIcon(navIcon);
  188.     }
  189.     CharSequence navDesc = a.getText(R.styleable.Toolbar_navigationContentDescription);
  190.     if (!TextUtils.isEmpty(navDesc)) {
  191.       setNavigationContentDescription(navDesc);
  192.     }
  193.     this.mMinHeight = a.getDimensionPixelSize(R.styleable.Toolbar_android_minHeight, 0);
  194.    
  195.     a.recycle();
  196.    
  197.  
  198.     this.mTintManager = a.getTintManager();
  199.   }
  200.  
  201.   public void setPopupTheme(int resId)
  202.   {
  203.     if (this.mPopupTheme != resId)
  204.     {
  205.       this.mPopupTheme = resId;
  206.       if (resId == 0) {
  207.         this.mPopupContext = getContext();
  208.       } else {
  209.         this.mPopupContext = new ContextThemeWrapper(getContext(), resId);
  210.       }
  211.     }
  212.   }
  213.  
  214.   public int getPopupTheme()
  215.   {
  216.     return this.mPopupTheme;
  217.   }
  218.  
  219.   public void onRtlPropertiesChanged(int layoutDirection)
  220.   {
  221.     if (Build.VERSION.SDK_INT >= 17) {
  222.       super.onRtlPropertiesChanged(layoutDirection);
  223.     }
  224.     this.mContentInsets.setDirection(layoutDirection == 1);
  225.   }
  226.  
  227.   public void setLogo(int resId)
  228.   {
  229.     setLogo(this.mTintManager.getDrawable(resId));
  230.   }
  231.  
  232.   public boolean canShowOverflowMenu()
  233.   {
  234.     return (getVisibility() == 0) && (this.mMenuView != null) && (this.mMenuView.isOverflowReserved());
  235.   }
  236.  
  237.   public boolean isOverflowMenuShowing()
  238.   {
  239.     return (this.mMenuView != null) && (this.mMenuView.isOverflowMenuShowing());
  240.   }
  241.  
  242.   public boolean isOverflowMenuShowPending()
  243.   {
  244.     return (this.mMenuView != null) && (this.mMenuView.isOverflowMenuShowPending());
  245.   }
  246.  
  247.   public boolean showOverflowMenu()
  248.   {
  249.     return (this.mMenuView != null) && (this.mMenuView.showOverflowMenu());
  250.   }
  251.  
  252.   public boolean hideOverflowMenu()
  253.   {
  254.     return (this.mMenuView != null) && (this.mMenuView.hideOverflowMenu());
  255.   }
  256.  
  257.   public void setMenu(MenuBuilder menu, ActionMenuPresenter outerPresenter)
  258.   {
  259.     if ((menu == null) && (this.mMenuView == null)) {
  260.       return;
  261.     }
  262.     ensureMenuView();
  263.     MenuBuilder oldMenu = this.mMenuView.peekMenu();
  264.     if (oldMenu == menu) {
  265.       return;
  266.     }
  267.     if (oldMenu != null)
  268.     {
  269.       oldMenu.removeMenuPresenter(this.mOuterActionMenuPresenter);
  270.       oldMenu.removeMenuPresenter(this.mExpandedMenuPresenter);
  271.     }
  272.     if (this.mExpandedMenuPresenter == null) {
  273.       this.mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter(null);
  274.     }
  275.     outerPresenter.setExpandedActionViewsExclusive(true);
  276.     if (menu != null)
  277.     {
  278.       menu.addMenuPresenter(outerPresenter, this.mPopupContext);
  279.       menu.addMenuPresenter(this.mExpandedMenuPresenter, this.mPopupContext);
  280.     }
  281.     else
  282.     {
  283.       outerPresenter.initForMenu(this.mPopupContext, null);
  284.       this.mExpandedMenuPresenter.initForMenu(this.mPopupContext, null);
  285.       outerPresenter.updateMenuView(true);
  286.       this.mExpandedMenuPresenter.updateMenuView(true);
  287.     }
  288.     this.mMenuView.setPopupTheme(this.mPopupTheme);
  289.     this.mMenuView.setPresenter(outerPresenter);
  290.     this.mOuterActionMenuPresenter = outerPresenter;
  291.   }
  292.  
  293.   public void dismissPopupMenus()
  294.   {
  295.     if (this.mMenuView != null) {
  296.       this.mMenuView.dismissPopupMenus();
  297.     }
  298.   }
  299.  
  300.   public boolean isTitleTruncated()
  301.   {
  302.     if (this.mTitleTextView == null) {
  303.       return false;
  304.     }
  305.     Layout titleLayout = this.mTitleTextView.getLayout();
  306.     if (titleLayout == null) {
  307.       return false;
  308.     }
  309.     int lineCount = titleLayout.getLineCount();
  310.     for (int i = 0; i < lineCount; i++) {
  311.       if (titleLayout.getEllipsisCount(i) > 0) {
  312.         return true;
  313.       }
  314.     }
  315.     return false;
  316.   }
  317.  
  318.   public void setLogo(Drawable drawable)
  319.   {
  320.     if (drawable != null)
  321.     {
  322.       ensureLogoView();
  323.       if (this.mLogoView.getParent() == null)
  324.       {
  325.         addSystemView(this.mLogoView);
  326.         updateChildVisibilityForExpandedActionView(this.mLogoView);
  327.       }
  328.     }
  329.     else if ((this.mLogoView != null) && (this.mLogoView.getParent() != null))
  330.     {
  331.       removeView(this.mLogoView);
  332.     }
  333.     if (this.mLogoView != null) {
  334.       this.mLogoView.setImageDrawable(drawable);
  335.     }
  336.   }
  337.  
  338.   public Drawable getLogo()
  339.   {
  340.     return this.mLogoView != null ? this.mLogoView.getDrawable() : null;
  341.   }
  342.  
  343.   public void setLogoDescription(int resId)
  344.   {
  345.     setLogoDescription(getContext().getText(resId));
  346.   }
  347.  
  348.   public void setLogoDescription(CharSequence description)
  349.   {
  350.     if (!TextUtils.isEmpty(description)) {
  351.       ensureLogoView();
  352.     }
  353.     if (this.mLogoView != null) {
  354.       this.mLogoView.setContentDescription(description);
  355.     }
  356.   }
  357.  
  358.   public CharSequence getLogoDescription()
  359.   {
  360.     return this.mLogoView != null ? this.mLogoView.getContentDescription() : null;
  361.   }
  362.  
  363.   private void ensureLogoView()
  364.   {
  365.     if (this.mLogoView == null) {
  366.       this.mLogoView = new ImageView(getContext());
  367.     }
  368.   }
  369.  
  370.   public boolean hasExpandedActionView()
  371.   {
  372.     return (this.mExpandedMenuPresenter != null) && (this.mExpandedMenuPresenter.mCurrentExpandedItem != null);
  373.   }
  374.  
  375.   public void collapseActionView()
  376.   {
  377.     MenuItemImpl item = this.mExpandedMenuPresenter == null ? null : this.mExpandedMenuPresenter.mCurrentExpandedItem;
  378.     if (item != null) {
  379.       item.collapseActionView();
  380.     }
  381.   }
  382.  
  383.   public CharSequence getTitle()
  384.   {
  385.     return this.mTitleText;
  386.   }
  387.  
  388.   public void setTitle(int resId)
  389.   {
  390.     setTitle(getContext().getText(resId));
  391.   }
  392.  
  393.   public void setTitle(CharSequence title)
  394.   {
  395.     if (!TextUtils.isEmpty(title))
  396.     {
  397.       if (this.mTitleTextView == null)
  398.       {
  399.         Context context = getContext();
  400.         this.mTitleTextView = new TextView(context);
  401.         this.mTitleTextView.setSingleLine();
  402.         this.mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
  403.         if (this.mTitleTextAppearance != 0) {
  404.           this.mTitleTextView.setTextAppearance(context, this.mTitleTextAppearance);
  405.         }
  406.         if (this.mTitleTextColor != 0) {
  407.           this.mTitleTextView.setTextColor(this.mTitleTextColor);
  408.         }
  409.       }
  410.       if (this.mTitleTextView.getParent() == null)
  411.       {
  412.         addSystemView(this.mTitleTextView);
  413.         updateChildVisibilityForExpandedActionView(this.mTitleTextView);
  414.       }
  415.     }
  416.     else if ((this.mTitleTextView != null) && (this.mTitleTextView.getParent() != null))
  417.     {
  418.       removeView(this.mTitleTextView);
  419.     }
  420.     if (this.mTitleTextView != null) {
  421.       this.mTitleTextView.setText(title);
  422.     }
  423.     this.mTitleText = title;
  424.   }
  425.  
  426.   public CharSequence getSubtitle()
  427.   {
  428.     return this.mSubtitleText;
  429.   }
  430.  
  431.   public void setSubtitle(int resId)
  432.   {
  433.     setSubtitle(getContext().getText(resId));
  434.   }
  435.  
  436.   public void setSubtitle(CharSequence subtitle)
  437.   {
  438.     if (!TextUtils.isEmpty(subtitle))
  439.     {
  440.       if (this.mSubtitleTextView == null)
  441.       {
  442.         Context context = getContext();
  443.         this.mSubtitleTextView = new TextView(context);
  444.         this.mSubtitleTextView.setSingleLine();
  445.         this.mSubtitleTextView.setEllipsize(TextUtils.TruncateAt.END);
  446.         if (this.mSubtitleTextAppearance != 0) {
  447.           this.mSubtitleTextView.setTextAppearance(context, this.mSubtitleTextAppearance);
  448.         }
  449.         if (this.mSubtitleTextColor != 0) {
  450.           this.mSubtitleTextView.setTextColor(this.mSubtitleTextColor);
  451.         }
  452.       }
  453.       if (this.mSubtitleTextView.getParent() == null)
  454.       {
  455.         addSystemView(this.mSubtitleTextView);
  456.         updateChildVisibilityForExpandedActionView(this.mSubtitleTextView);
  457.       }
  458.     }
  459.     else if ((this.mSubtitleTextView != null) && (this.mSubtitleTextView.getParent() != null))
  460.     {
  461.       removeView(this.mSubtitleTextView);
  462.     }
  463.     if (this.mSubtitleTextView != null) {
  464.       this.mSubtitleTextView.setText(subtitle);
  465.     }
  466.     this.mSubtitleText = subtitle;
  467.   }
  468.  
  469.   public void setTitleTextAppearance(Context context, int resId)
  470.   {
  471.     this.mTitleTextAppearance = resId;
  472.     if (this.mTitleTextView != null) {
  473.       this.mTitleTextView.setTextAppearance(context, resId);
  474.     }
  475.   }
  476.  
  477.   public void setSubtitleTextAppearance(Context context, int resId)
  478.   {
  479.     this.mSubtitleTextAppearance = resId;
  480.     if (this.mSubtitleTextView != null) {
  481.       this.mSubtitleTextView.setTextAppearance(context, resId);
  482.     }
  483.   }
  484.  
  485.   public void setTitleTextColor(int color)
  486.   {
  487.     this.mTitleTextColor = color;
  488.     if (this.mTitleTextView != null) {
  489.       this.mTitleTextView.setTextColor(color);
  490.     }
  491.   }
  492.  
  493.   public void setSubtitleTextColor(int color)
  494.   {
  495.     this.mSubtitleTextColor = color;
  496.     if (this.mSubtitleTextView != null) {
  497.       this.mSubtitleTextView.setTextColor(color);
  498.     }
  499.   }
  500.  
  501.   @Nullable
  502.   public CharSequence getNavigationContentDescription()
  503.   {
  504.     return this.mNavButtonView != null ? this.mNavButtonView.getContentDescription() : null;
  505.   }
  506.  
  507.   public void setNavigationContentDescription(int resId)
  508.   {
  509.     setNavigationContentDescription(resId != 0 ? getContext().getText(resId) : null);
  510.   }
  511.  
  512.   public void setNavigationContentDescription(@Nullable CharSequence description)
  513.   {
  514.     if (!TextUtils.isEmpty(description)) {
  515.       ensureNavButtonView();
  516.     }
  517.     if (this.mNavButtonView != null) {
  518.       this.mNavButtonView.setContentDescription(description);
  519.     }
  520.   }
  521.  
  522.   public void setNavigationIcon(int resId)
  523.   {
  524.     setNavigationIcon(this.mTintManager.getDrawable(resId));
  525.   }
  526.  
  527.   public void setNavigationIcon(@Nullable Drawable icon)
  528.   {
  529.     if (icon != null)
  530.     {
  531.       ensureNavButtonView();
  532.       if (this.mNavButtonView.getParent() == null)
  533.       {
  534.         addSystemView(this.mNavButtonView);
  535.         updateChildVisibilityForExpandedActionView(this.mNavButtonView);
  536.       }
  537.     }
  538.     else if ((this.mNavButtonView != null) && (this.mNavButtonView.getParent() != null))
  539.     {
  540.       removeView(this.mNavButtonView);
  541.     }
  542.     if (this.mNavButtonView != null) {
  543.       this.mNavButtonView.setImageDrawable(icon);
  544.     }
  545.   }
  546.  
  547.   @Nullable
  548.   public Drawable getNavigationIcon()
  549.   {
  550.     return this.mNavButtonView != null ? this.mNavButtonView.getDrawable() : null;
  551.   }
  552.  
  553.   public void setNavigationOnClickListener(View.OnClickListener listener)
  554.   {
  555.     ensureNavButtonView();
  556.     this.mNavButtonView.setOnClickListener(listener);
  557.   }
  558.  
  559.   public Menu getMenu()
  560.   {
  561.     ensureMenu();
  562.     return this.mMenuView.getMenu();
  563.   }
  564.  
  565.   private void ensureMenu()
  566.   {
  567.     ensureMenuView();
  568.     if (this.mMenuView.peekMenu() == null)
  569.     {
  570.       MenuBuilder menu = (MenuBuilder)this.mMenuView.getMenu();
  571.       if (this.mExpandedMenuPresenter == null) {
  572.         this.mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter(null);
  573.       }
  574.       this.mMenuView.setExpandedActionViewsExclusive(true);
  575.       menu.addMenuPresenter(this.mExpandedMenuPresenter, this.mPopupContext);
  576.     }
  577.   }
  578.  
  579.   private void ensureMenuView()
  580.   {
  581.     if (this.mMenuView == null)
  582.     {
  583.       this.mMenuView = new ActionMenuView(getContext());
  584.       this.mMenuView.setPopupTheme(this.mPopupTheme);
  585.       this.mMenuView.setOnMenuItemClickListener(this.mMenuViewItemClickListener);
  586.       this.mMenuView.setMenuCallbacks(this.mActionMenuPresenterCallback, this.mMenuBuilderCallback);
  587.       LayoutParams lp = generateDefaultLayoutParams();
  588.       lp.gravity = (0x800005 | this.mButtonGravity & 0x70);
  589.       this.mMenuView.setLayoutParams(lp);
  590.       addSystemView(this.mMenuView);
  591.     }
  592.   }
  593.  
  594.   private MenuInflater getMenuInflater()
  595.   {
  596.     return new SupportMenuInflater(getContext());
  597.   }
  598.  
  599.   public void inflateMenu(int resId)
  600.   {
  601.     getMenuInflater().inflate(resId, getMenu());
  602.   }
  603.  
  604.   public void setOnMenuItemClickListener(OnMenuItemClickListener listener)
  605.   {
  606.     this.mOnMenuItemClickListener = listener;
  607.   }
  608.  
  609.   public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd)
  610.   {
  611.     this.mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
  612.   }
  613.  
  614.   public int getContentInsetStart()
  615.   {
  616.     return this.mContentInsets.getStart();
  617.   }
  618.  
  619.   public int getContentInsetEnd()
  620.   {
  621.     return this.mContentInsets.getEnd();
  622.   }
  623.  
  624.   public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight)
  625.   {
  626.     this.mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
  627.   }
  628.  
  629.   public int getContentInsetLeft()
  630.   {
  631.     return this.mContentInsets.getLeft();
  632.   }
  633.  
  634.   public int getContentInsetRight()
  635.   {
  636.     return this.mContentInsets.getRight();
  637.   }
  638.  
  639.   private void ensureNavButtonView()
  640.   {
  641.     if (this.mNavButtonView == null)
  642.     {
  643.       this.mNavButtonView = new ImageButton(getContext(), null, R.attr.toolbarNavigationButtonStyle);
  644.      
  645.       LayoutParams lp = generateDefaultLayoutParams();
  646.       lp.gravity = (0x800003 | this.mButtonGravity & 0x70);
  647.       this.mNavButtonView.setLayoutParams(lp);
  648.     }
  649.   }
  650.  
  651.   private void ensureCollapseButtonView()
  652.   {
  653.     if (this.mCollapseButtonView == null)
  654.     {
  655.       this.mCollapseButtonView = new ImageButton(getContext(), null, R.attr.toolbarNavigationButtonStyle);
  656.      
  657.       this.mCollapseButtonView.setImageDrawable(this.mCollapseIcon);
  658.       LayoutParams lp = generateDefaultLayoutParams();
  659.       lp.gravity = (0x800003 | this.mButtonGravity & 0x70);
  660.       lp.mViewType = 2;
  661.       this.mCollapseButtonView.setLayoutParams(lp);
  662.       this.mCollapseButtonView.setOnClickListener(new View.OnClickListener()
  663.       {
  664.         public void onClick(View v)
  665.         {
  666.           Toolbar.this.collapseActionView();
  667.         }
  668.       });
  669.     }
  670.   }
  671.  
  672.   private void addSystemView(View v)
  673.   {
  674.     ViewGroup.LayoutParams vlp = v.getLayoutParams();
  675.     LayoutParams lp;
  676.     LayoutParams lp;
  677.     if (vlp == null)
  678.     {
  679.       lp = generateDefaultLayoutParams();
  680.     }
  681.     else
  682.     {
  683.       LayoutParams lp;
  684.       if (!checkLayoutParams(vlp)) {
  685.         lp = generateLayoutParams(vlp);
  686.       } else {
  687.         lp = (LayoutParams)vlp;
  688.       }
  689.     }
  690.     lp.mViewType = 1;
  691.     addView(v, lp);
  692.   }
  693.  
  694.   protected Parcelable onSaveInstanceState()
  695.   {
  696.     SavedState state = new SavedState(super.onSaveInstanceState());
  697.     if ((this.mExpandedMenuPresenter != null) && (this.mExpandedMenuPresenter.mCurrentExpandedItem != null)) {
  698.       state.expandedMenuItemId = this.mExpandedMenuPresenter.mCurrentExpandedItem.getItemId();
  699.     }
  700.     state.isOverflowOpen = isOverflowMenuShowing();
  701.     return state;
  702.   }
  703.  
  704.   protected void onRestoreInstanceState(Parcelable state)
  705.   {
  706.     SavedState ss = (SavedState)state;
  707.     super.onRestoreInstanceState(ss.getSuperState());
  708.    
  709.     Menu menu = this.mMenuView != null ? this.mMenuView.peekMenu() : null;
  710.     if ((ss.expandedMenuItemId != 0) && (this.mExpandedMenuPresenter != null) && (menu != null))
  711.     {
  712.       MenuItem item = menu.findItem(ss.expandedMenuItemId);
  713.       if (item != null) {
  714.         MenuItemCompat.expandActionView(item);
  715.       }
  716.     }
  717.     if (ss.isOverflowOpen) {
  718.       postShowOverflowMenu();
  719.     }
  720.   }
  721.  
  722.   private void postShowOverflowMenu()
  723.   {
  724.     removeCallbacks(this.mShowOverflowMenuRunnable);
  725.     post(this.mShowOverflowMenuRunnable);
  726.   }
  727.  
  728.   protected void onDetachedFromWindow()
  729.   {
  730.     super.onDetachedFromWindow();
  731.     removeCallbacks(this.mShowOverflowMenuRunnable);
  732.   }
  733.  
  734.   public boolean onTouchEvent(MotionEvent ev)
  735.   {
  736.     int action = MotionEventCompat.getActionMasked(ev);
  737.     if (action == 0) {
  738.       this.mEatingTouch = false;
  739.     }
  740.     if (!this.mEatingTouch)
  741.     {
  742.       boolean handled = super.onTouchEvent(ev);
  743.       if ((action == 0) && (!handled)) {
  744.         this.mEatingTouch = true;
  745.       }
  746.     }
  747.     if ((action == 1) || (action == 3)) {
  748.       this.mEatingTouch = false;
  749.     }
  750.     return true;
  751.   }
  752.  
  753.   private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed, int parentHeightSpec, int heightUsed, int heightConstraint)
  754.   {
  755.     ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)child.getLayoutParams();
  756.    
  757.     int childWidthSpec = getChildMeasureSpec(parentWidthSpec, getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin + widthUsed, lp.width);
  758.    
  759.  
  760.     int childHeightSpec = getChildMeasureSpec(parentHeightSpec, getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin + heightUsed, lp.height);
  761.    
  762.  
  763.  
  764.     int childHeightMode = View.MeasureSpec.getMode(childHeightSpec);
  765.     if ((childHeightMode != 1073741824) && (heightConstraint >= 0))
  766.     {
  767.       int size = childHeightMode != 0 ? Math.min(View.MeasureSpec.getSize(childHeightSpec), heightConstraint) : heightConstraint;
  768.      
  769.  
  770.       childHeightSpec = View.MeasureSpec.makeMeasureSpec(size, 1073741824);
  771.     }
  772.     child.measure(childWidthSpec, childHeightSpec);
  773.   }
  774.  
  775.   private int measureChildCollapseMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed, int[] collapsingMargins)
  776.   {
  777.     ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)child.getLayoutParams();
  778.    
  779.     int leftDiff = lp.leftMargin - collapsingMargins[0];
  780.     int rightDiff = lp.rightMargin - collapsingMargins[1];
  781.     int leftMargin = Math.max(0, leftDiff);
  782.     int rightMargin = Math.max(0, rightDiff);
  783.     int hMargins = leftMargin + rightMargin;
  784.     collapsingMargins[0] = Math.max(0, -leftDiff);
  785.     collapsingMargins[1] = Math.max(0, -rightDiff);
  786.    
  787.     int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft() + getPaddingRight() + hMargins + widthUsed, lp.width);
  788.    
  789.     int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin + heightUsed, lp.height);
  790.    
  791.  
  792.  
  793.     child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
  794.     return child.getMeasuredWidth() + hMargins;
  795.   }
  796.  
  797.   private boolean shouldCollapse()
  798.   {
  799.     if (!this.mCollapsible) {
  800.       return false;
  801.     }
  802.     int childCount = getChildCount();
  803.     for (int i = 0; i < childCount; i++)
  804.     {
  805.       View child = getChildAt(i);
  806.       if ((shouldLayout(child)) && (child.getMeasuredWidth() > 0) && (child.getMeasuredHeight() > 0)) {
  807.         return false;
  808.       }
  809.     }
  810.     return true;
  811.   }
  812.  
  813.   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  814.   {
  815.     int width = 0;
  816.     int height = 0;
  817.     int childState = 0;
  818.    
  819.     int[] collapsingMargins = this.mTempMargins;
  820.     int marginEndIndex;
  821.     int marginStartIndex;
  822.     int marginEndIndex;
  823.     if (ViewUtils.isLayoutRtl(this))
  824.     {
  825.       int marginStartIndex = 1;
  826.       marginEndIndex = 0;
  827.     }
  828.     else
  829.     {
  830.       marginStartIndex = 0;
  831.       marginEndIndex = 1;
  832.     }
  833.     int navWidth = 0;
  834.     if (shouldLayout(this.mNavButtonView))
  835.     {
  836.       measureChildConstrained(this.mNavButtonView, widthMeasureSpec, width, heightMeasureSpec, 0, this.mMaxButtonHeight);
  837.      
  838.       navWidth = this.mNavButtonView.getMeasuredWidth() + getHorizontalMargins(this.mNavButtonView);
  839.       height = Math.max(height, this.mNavButtonView.getMeasuredHeight() + getVerticalMargins(this.mNavButtonView));
  840.      
  841.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mNavButtonView));
  842.     }
  843.     if (shouldLayout(this.mCollapseButtonView))
  844.     {
  845.       measureChildConstrained(this.mCollapseButtonView, widthMeasureSpec, width, heightMeasureSpec, 0, this.mMaxButtonHeight);
  846.      
  847.       navWidth = this.mCollapseButtonView.getMeasuredWidth() + getHorizontalMargins(this.mCollapseButtonView);
  848.      
  849.       height = Math.max(height, this.mCollapseButtonView.getMeasuredHeight() + getVerticalMargins(this.mCollapseButtonView));
  850.      
  851.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mCollapseButtonView));
  852.     }
  853.     int contentInsetStart = getContentInsetStart();
  854.     width += Math.max(contentInsetStart, navWidth);
  855.     collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth);
  856.    
  857.     int menuWidth = 0;
  858.     if (shouldLayout(this.mMenuView))
  859.     {
  860.       measureChildConstrained(this.mMenuView, widthMeasureSpec, width, heightMeasureSpec, 0, this.mMaxButtonHeight);
  861.      
  862.       menuWidth = this.mMenuView.getMeasuredWidth() + getHorizontalMargins(this.mMenuView);
  863.       height = Math.max(height, this.mMenuView.getMeasuredHeight() + getVerticalMargins(this.mMenuView));
  864.      
  865.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mMenuView));
  866.     }
  867.     int contentInsetEnd = getContentInsetEnd();
  868.     width += Math.max(contentInsetEnd, menuWidth);
  869.     collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth);
  870.     if (shouldLayout(this.mExpandedActionView))
  871.     {
  872.       width += measureChildCollapseMargins(this.mExpandedActionView, widthMeasureSpec, width, heightMeasureSpec, 0, collapsingMargins);
  873.      
  874.       height = Math.max(height, this.mExpandedActionView.getMeasuredHeight() + getVerticalMargins(this.mExpandedActionView));
  875.      
  876.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mExpandedActionView));
  877.     }
  878.     if (shouldLayout(this.mLogoView))
  879.     {
  880.       width += measureChildCollapseMargins(this.mLogoView, widthMeasureSpec, width, heightMeasureSpec, 0, collapsingMargins);
  881.      
  882.       height = Math.max(height, this.mLogoView.getMeasuredHeight() + getVerticalMargins(this.mLogoView));
  883.      
  884.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mLogoView));
  885.     }
  886.     int childCount = getChildCount();
  887.     for (int i = 0; i < childCount; i++)
  888.     {
  889.       View child = getChildAt(i);
  890.       LayoutParams lp = (LayoutParams)child.getLayoutParams();
  891.       if ((lp.mViewType == 0) && (shouldLayout(child)))
  892.       {
  893.         width += measureChildCollapseMargins(child, widthMeasureSpec, width, heightMeasureSpec, 0, collapsingMargins);
  894.        
  895.         height = Math.max(height, child.getMeasuredHeight() + getVerticalMargins(child));
  896.         childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(child));
  897.       }
  898.     }
  899.     int titleWidth = 0;
  900.     int titleHeight = 0;
  901.     int titleVertMargins = this.mTitleMarginTop + this.mTitleMarginBottom;
  902.     int titleHorizMargins = this.mTitleMarginStart + this.mTitleMarginEnd;
  903.     if (shouldLayout(this.mTitleTextView))
  904.     {
  905.       titleWidth = measureChildCollapseMargins(this.mTitleTextView, widthMeasureSpec, width + titleHorizMargins, heightMeasureSpec, titleVertMargins, collapsingMargins);
  906.      
  907.  
  908.       titleWidth = this.mTitleTextView.getMeasuredWidth() + getHorizontalMargins(this.mTitleTextView);
  909.       titleHeight = this.mTitleTextView.getMeasuredHeight() + getVerticalMargins(this.mTitleTextView);
  910.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mTitleTextView));
  911.     }
  912.     if (shouldLayout(this.mSubtitleTextView))
  913.     {
  914.       titleWidth = Math.max(titleWidth, measureChildCollapseMargins(this.mSubtitleTextView, widthMeasureSpec, width + titleHorizMargins, heightMeasureSpec, titleHeight + titleVertMargins, collapsingMargins));
  915.      
  916.  
  917.  
  918.       titleHeight += this.mSubtitleTextView.getMeasuredHeight() + getVerticalMargins(this.mSubtitleTextView);
  919.      
  920.       childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(this.mSubtitleTextView));
  921.     }
  922.     width += titleWidth;
  923.     height = Math.max(height, titleHeight);
  924.    
  925.  
  926.  
  927.     width += getPaddingLeft() + getPaddingRight();
  928.     height += getPaddingTop() + getPaddingBottom();
  929.    
  930.     int measuredWidth = ViewCompat.resolveSizeAndState(Math.max(width, getSuggestedMinimumWidth()), widthMeasureSpec, childState & 0xFF000000);
  931.    
  932.  
  933.     int measuredHeight = ViewCompat.resolveSizeAndState(Math.max(height, getSuggestedMinimumHeight()), heightMeasureSpec, childState << 16);
  934.    
  935.  
  936.  
  937.     setMeasuredDimension(measuredWidth, shouldCollapse() ? 0 : measuredHeight);
  938.   }
  939.  
  940.   protected void onLayout(boolean changed, int l, int t, int r, int b)
  941.   {
  942.     boolean isRtl = ViewCompat.getLayoutDirection(this) == 1;
  943.     int width = getWidth();
  944.     int height = getHeight();
  945.     int paddingLeft = getPaddingLeft();
  946.     int paddingRight = getPaddingRight();
  947.     int paddingTop = getPaddingTop();
  948.     int paddingBottom = getPaddingBottom();
  949.     int left = paddingLeft;
  950.     int right = width - paddingRight;
  951.    
  952.     int[] collapsingMargins = this.mTempMargins; int
  953.       tmp75_74 = 0;collapsingMargins[1] = tmp75_74;collapsingMargins[0] = tmp75_74;
  954.    
  955.  
  956.     int alignmentHeight = getMinimumHeightCompat();
  957.     if (shouldLayout(this.mNavButtonView)) {
  958.       if (isRtl) {
  959.         right = layoutChildRight(this.mNavButtonView, right, collapsingMargins, alignmentHeight);
  960.       } else {
  961.         left = layoutChildLeft(this.mNavButtonView, left, collapsingMargins, alignmentHeight);
  962.       }
  963.     }
  964.     if (shouldLayout(this.mCollapseButtonView)) {
  965.       if (isRtl) {
  966.         right = layoutChildRight(this.mCollapseButtonView, right, collapsingMargins, alignmentHeight);
  967.       } else {
  968.         left = layoutChildLeft(this.mCollapseButtonView, left, collapsingMargins, alignmentHeight);
  969.       }
  970.     }
  971.     if (shouldLayout(this.mMenuView)) {
  972.       if (isRtl) {
  973.         left = layoutChildLeft(this.mMenuView, left, collapsingMargins, alignmentHeight);
  974.       } else {
  975.         right = layoutChildRight(this.mMenuView, right, collapsingMargins, alignmentHeight);
  976.       }
  977.     }
  978.     collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left);
  979.     collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right));
  980.     left = Math.max(left, getContentInsetLeft());
  981.     right = Math.min(right, width - paddingRight - getContentInsetRight());
  982.     if (shouldLayout(this.mExpandedActionView)) {
  983.       if (isRtl) {
  984.         right = layoutChildRight(this.mExpandedActionView, right, collapsingMargins, alignmentHeight);
  985.       } else {
  986.         left = layoutChildLeft(this.mExpandedActionView, left, collapsingMargins, alignmentHeight);
  987.       }
  988.     }
  989.     if (shouldLayout(this.mLogoView)) {
  990.       if (isRtl) {
  991.         right = layoutChildRight(this.mLogoView, right, collapsingMargins, alignmentHeight);
  992.       } else {
  993.         left = layoutChildLeft(this.mLogoView, left, collapsingMargins, alignmentHeight);
  994.       }
  995.     }
  996.     boolean layoutTitle = shouldLayout(this.mTitleTextView);
  997.     boolean layoutSubtitle = shouldLayout(this.mSubtitleTextView);
  998.     int titleHeight = 0;
  999.     if (layoutTitle)
  1000.     {
  1001.       LayoutParams lp = (LayoutParams)this.mTitleTextView.getLayoutParams();
  1002.       titleHeight += lp.topMargin + this.mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
  1003.     }
  1004.     if (layoutSubtitle)
  1005.     {
  1006.       LayoutParams lp = (LayoutParams)this.mSubtitleTextView.getLayoutParams();
  1007.       titleHeight += lp.topMargin + this.mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin;
  1008.     }
  1009.     if ((layoutTitle) || (layoutSubtitle))
  1010.     {
  1011.       View topChild = layoutTitle ? this.mTitleTextView : this.mSubtitleTextView;
  1012.       View bottomChild = layoutSubtitle ? this.mSubtitleTextView : this.mTitleTextView;
  1013.       LayoutParams toplp = (LayoutParams)topChild.getLayoutParams();
  1014.       LayoutParams bottomlp = (LayoutParams)bottomChild.getLayoutParams();
  1015.       boolean titleHasWidth = ((layoutTitle) && (this.mTitleTextView.getMeasuredWidth() > 0)) || ((layoutSubtitle) && (this.mSubtitleTextView.getMeasuredWidth() > 0));
  1016.       int titleTop;
  1017.       switch (this.mGravity & 0x70)
  1018.       {
  1019.       case 48:
  1020.         titleTop = getPaddingTop() + toplp.topMargin + this.mTitleMarginTop;
  1021.         break;
  1022.       case 16:
  1023.       default:
  1024.         int space = height - paddingTop - paddingBottom;
  1025.         int spaceAbove = (space - titleHeight) / 2;
  1026.         if (spaceAbove < toplp.topMargin + this.mTitleMarginTop)
  1027.         {
  1028.           spaceAbove = toplp.topMargin + this.mTitleMarginTop;
  1029.         }
  1030.         else
  1031.         {
  1032.           int spaceBelow = height - paddingBottom - titleHeight - spaceAbove - paddingTop;
  1033.           if (spaceBelow < toplp.bottomMargin + this.mTitleMarginBottom) {
  1034.             spaceAbove = Math.max(0, spaceAbove - (bottomlp.bottomMargin + this.mTitleMarginBottom - spaceBelow));
  1035.           }
  1036.         }
  1037.         titleTop = paddingTop + spaceAbove;
  1038.         break;
  1039.       case 80:
  1040.         titleTop = height - paddingBottom - bottomlp.bottomMargin - this.mTitleMarginBottom - titleHeight;
  1041.       }
  1042.       if (isRtl)
  1043.       {
  1044.         int rd = (titleHasWidth ? this.mTitleMarginStart : 0) - collapsingMargins[1];
  1045.         right -= Math.max(0, rd);
  1046.         collapsingMargins[1] = Math.max(0, -rd);
  1047.         int titleRight = right;
  1048.         int subtitleRight = right;
  1049.         if (layoutTitle)
  1050.         {
  1051.           LayoutParams lp = (LayoutParams)this.mTitleTextView.getLayoutParams();
  1052.           int titleLeft = titleRight - this.mTitleTextView.getMeasuredWidth();
  1053.           int titleBottom = titleTop + this.mTitleTextView.getMeasuredHeight();
  1054.           this.mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
  1055.           titleRight = titleLeft - this.mTitleMarginEnd;
  1056.           titleTop = titleBottom + lp.bottomMargin;
  1057.         }
  1058.         if (layoutSubtitle)
  1059.         {
  1060.           LayoutParams lp = (LayoutParams)this.mSubtitleTextView.getLayoutParams();
  1061.           titleTop += lp.topMargin;
  1062.           int subtitleLeft = subtitleRight - this.mSubtitleTextView.getMeasuredWidth();
  1063.           int subtitleBottom = titleTop + this.mSubtitleTextView.getMeasuredHeight();
  1064.           this.mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
  1065.           subtitleRight -= this.mTitleMarginEnd;
  1066.           titleTop = subtitleBottom + lp.bottomMargin;
  1067.         }
  1068.         if (titleHasWidth) {
  1069.           right = Math.min(titleRight, subtitleRight);
  1070.         }
  1071.       }
  1072.       else
  1073.       {
  1074.         int ld = (titleHasWidth ? this.mTitleMarginStart : 0) - collapsingMargins[0];
  1075.         left += Math.max(0, ld);
  1076.         collapsingMargins[0] = Math.max(0, -ld);
  1077.         int titleLeft = left;
  1078.         int subtitleLeft = left;
  1079.         if (layoutTitle)
  1080.         {
  1081.           LayoutParams lp = (LayoutParams)this.mTitleTextView.getLayoutParams();
  1082.           int titleRight = titleLeft + this.mTitleTextView.getMeasuredWidth();
  1083.           int titleBottom = titleTop + this.mTitleTextView.getMeasuredHeight();
  1084.           this.mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
  1085.           titleLeft = titleRight + this.mTitleMarginEnd;
  1086.           titleTop = titleBottom + lp.bottomMargin;
  1087.         }
  1088.         if (layoutSubtitle)
  1089.         {
  1090.           LayoutParams lp = (LayoutParams)this.mSubtitleTextView.getLayoutParams();
  1091.           titleTop += lp.topMargin;
  1092.           int subtitleRight = subtitleLeft + this.mSubtitleTextView.getMeasuredWidth();
  1093.           int subtitleBottom = titleTop + this.mSubtitleTextView.getMeasuredHeight();
  1094.           this.mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
  1095.           subtitleLeft = subtitleRight + this.mTitleMarginEnd;
  1096.           titleTop = subtitleBottom + lp.bottomMargin;
  1097.         }
  1098.         if (titleHasWidth) {
  1099.           left = Math.max(titleLeft, subtitleLeft);
  1100.         }
  1101.       }
  1102.     }
  1103.     addCustomViewsWithGravity(this.mTempViews, 3);
  1104.     int leftViewsCount = this.mTempViews.size();
  1105.     for (int i = 0; i < leftViewsCount; i++) {
  1106.       left = layoutChildLeft((View)this.mTempViews.get(i), left, collapsingMargins, alignmentHeight);
  1107.     }
  1108.     addCustomViewsWithGravity(this.mTempViews, 5);
  1109.     int rightViewsCount = this.mTempViews.size();
  1110.     for (int i = 0; i < rightViewsCount; i++) {
  1111.       right = layoutChildRight((View)this.mTempViews.get(i), right, collapsingMargins, alignmentHeight);
  1112.     }
  1113.     addCustomViewsWithGravity(this.mTempViews, 1);
  1114.     int centerViewsWidth = getViewListMeasuredWidth(this.mTempViews, collapsingMargins);
  1115.     int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
  1116.     int halfCenterViewsWidth = centerViewsWidth / 2;
  1117.     int centerLeft = parentCenter - halfCenterViewsWidth;
  1118.     int centerRight = centerLeft + centerViewsWidth;
  1119.     if (centerLeft < left) {
  1120.       centerLeft = left;
  1121.     } else if (centerRight > right) {
  1122.       centerLeft -= centerRight - right;
  1123.     }
  1124.     int centerViewsCount = this.mTempViews.size();
  1125.     for (int i = 0; i < centerViewsCount; i++) {
  1126.       centerLeft = layoutChildLeft((View)this.mTempViews.get(i), centerLeft, collapsingMargins, alignmentHeight);
  1127.     }
  1128.     this.mTempViews.clear();
  1129.   }
  1130.  
  1131.   private int getViewListMeasuredWidth(List<View> views, int[] collapsingMargins)
  1132.   {
  1133.     int collapseLeft = collapsingMargins[0];
  1134.     int collapseRight = collapsingMargins[1];
  1135.     int width = 0;
  1136.     int count = views.size();
  1137.     for (int i = 0; i < count; i++)
  1138.     {
  1139.       View v = (View)views.get(i);
  1140.       LayoutParams lp = (LayoutParams)v.getLayoutParams();
  1141.       int l = lp.leftMargin - collapseLeft;
  1142.       int r = lp.rightMargin - collapseRight;
  1143.       int leftMargin = Math.max(0, l);
  1144.       int rightMargin = Math.max(0, r);
  1145.       collapseLeft = Math.max(0, -l);
  1146.       collapseRight = Math.max(0, -r);
  1147.       width += leftMargin + v.getMeasuredWidth() + rightMargin;
  1148.     }
  1149.     return width;
  1150.   }
  1151.  
  1152.   private int layoutChildLeft(View child, int left, int[] collapsingMargins, int alignmentHeight)
  1153.   {
  1154.     LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1155.     int l = lp.leftMargin - collapsingMargins[0];
  1156.     left += Math.max(0, l);
  1157.     collapsingMargins[0] = Math.max(0, -l);
  1158.     int top = getChildTop(child, alignmentHeight);
  1159.     int childWidth = child.getMeasuredWidth();
  1160.     child.layout(left, top, left + childWidth, top + child.getMeasuredHeight());
  1161.     left += childWidth + lp.rightMargin;
  1162.     return left;
  1163.   }
  1164.  
  1165.   private int layoutChildRight(View child, int right, int[] collapsingMargins, int alignmentHeight)
  1166.   {
  1167.     LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1168.     int r = lp.rightMargin - collapsingMargins[1];
  1169.     right -= Math.max(0, r);
  1170.     collapsingMargins[1] = Math.max(0, -r);
  1171.     int top = getChildTop(child, alignmentHeight);
  1172.     int childWidth = child.getMeasuredWidth();
  1173.     child.layout(right - childWidth, top, right, top + child.getMeasuredHeight());
  1174.     right -= childWidth + lp.leftMargin;
  1175.     return right;
  1176.   }
  1177.  
  1178.   private int getChildTop(View child, int alignmentHeight)
  1179.   {
  1180.     LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1181.     int childHeight = child.getMeasuredHeight();
  1182.     int alignmentOffset = alignmentHeight > 0 ? (childHeight - alignmentHeight) / 2 : 0;
  1183.     switch (getChildVerticalGravity(lp.gravity))
  1184.     {
  1185.     case 48:
  1186.       return getPaddingTop() - alignmentOffset;
  1187.     case 80:
  1188.       return getHeight() - getPaddingBottom() - childHeight - lp.bottomMargin - alignmentOffset;
  1189.     }
  1190.     int paddingTop = getPaddingTop();
  1191.     int paddingBottom = getPaddingBottom();
  1192.     int height = getHeight();
  1193.     int space = height - paddingTop - paddingBottom;
  1194.     int spaceAbove = (space - childHeight) / 2;
  1195.     if (spaceAbove < lp.topMargin)
  1196.     {
  1197.       spaceAbove = lp.topMargin;
  1198.     }
  1199.     else
  1200.     {
  1201.       int spaceBelow = height - paddingBottom - childHeight - spaceAbove - paddingTop;
  1202.       if (spaceBelow < lp.bottomMargin) {
  1203.         spaceAbove = Math.max(0, spaceAbove - (lp.bottomMargin - spaceBelow));
  1204.       }
  1205.     }
  1206.     return paddingTop + spaceAbove;
  1207.   }
  1208.  
  1209.   private int getChildVerticalGravity(int gravity)
  1210.   {
  1211.     int vgrav = gravity & 0x70;
  1212.     switch (vgrav)
  1213.     {
  1214.     case 16:
  1215.     case 48:
  1216.     case 80:
  1217.       return vgrav;
  1218.     }
  1219.     return this.mGravity & 0x70;
  1220.   }
  1221.  
  1222.   private void addCustomViewsWithGravity(List<View> views, int gravity)
  1223.   {
  1224.     boolean isRtl = ViewCompat.getLayoutDirection(this) == 1;
  1225.     int childCount = getChildCount();
  1226.     int absGrav = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
  1227.    
  1228.  
  1229.     views.clear();
  1230.     if (isRtl) {
  1231.       for (int i = childCount - 1; i >= 0; i--)
  1232.       {
  1233.         View child = getChildAt(i);
  1234.         LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1235.         if ((lp.mViewType == 0) && (shouldLayout(child)) && (getChildHorizontalGravity(lp.gravity) == absGrav)) {
  1236.           views.add(child);
  1237.         }
  1238.       }
  1239.     } else {
  1240.       for (int i = 0; i < childCount; i++)
  1241.       {
  1242.         View child = getChildAt(i);
  1243.         LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1244.         if ((lp.mViewType == 0) && (shouldLayout(child)) && (getChildHorizontalGravity(lp.gravity) == absGrav)) {
  1245.           views.add(child);
  1246.         }
  1247.       }
  1248.     }
  1249.   }
  1250.  
  1251.   private int getChildHorizontalGravity(int gravity)
  1252.   {
  1253.     int ld = ViewCompat.getLayoutDirection(this);
  1254.     int absGrav = GravityCompat.getAbsoluteGravity(gravity, ld);
  1255.     int hGrav = absGrav & 0x7;
  1256.     switch (hGrav)
  1257.     {
  1258.     case 1:
  1259.     case 3:
  1260.     case 5:
  1261.       return hGrav;
  1262.     }
  1263.     return ld == 1 ? 5 : 3;
  1264.   }
  1265.  
  1266.   private boolean shouldLayout(View view)
  1267.   {
  1268.     return (view != null) && (view.getParent() == this) && (view.getVisibility() != 8);
  1269.   }
  1270.  
  1271.   private int getHorizontalMargins(View v)
  1272.   {
  1273.     ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
  1274.     return MarginLayoutParamsCompat.getMarginStart(mlp) + MarginLayoutParamsCompat.getMarginEnd(mlp);
  1275.   }
  1276.  
  1277.   private int getVerticalMargins(View v)
  1278.   {
  1279.     ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
  1280.     return mlp.topMargin + mlp.bottomMargin;
  1281.   }
  1282.  
  1283.   public LayoutParams generateLayoutParams(AttributeSet attrs)
  1284.   {
  1285.     return new LayoutParams(getContext(), attrs);
  1286.   }
  1287.  
  1288.   protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
  1289.   {
  1290.     if ((p instanceof LayoutParams)) {
  1291.       return new LayoutParams((LayoutParams)p);
  1292.     }
  1293.     if ((p instanceof ActionBar.LayoutParams)) {
  1294.       return new LayoutParams((ActionBar.LayoutParams)p);
  1295.     }
  1296.     if ((p instanceof ViewGroup.MarginLayoutParams)) {
  1297.       return new LayoutParams((ViewGroup.MarginLayoutParams)p);
  1298.     }
  1299.     return new LayoutParams(p);
  1300.   }
  1301.  
  1302.   protected LayoutParams generateDefaultLayoutParams()
  1303.   {
  1304.     return new LayoutParams(-2, -2);
  1305.   }
  1306.  
  1307.   protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
  1308.   {
  1309.     return (super.checkLayoutParams(p)) && ((p instanceof LayoutParams));
  1310.   }
  1311.  
  1312.   private static boolean isCustomView(View child)
  1313.   {
  1314.     return ((LayoutParams)child.getLayoutParams()).mViewType == 0;
  1315.   }
  1316.  
  1317.   public DecorToolbar getWrapper()
  1318.   {
  1319.     if (this.mWrapper == null) {
  1320.       this.mWrapper = new ToolbarWidgetWrapper(this, true);
  1321.     }
  1322.     return this.mWrapper;
  1323.   }
  1324.  
  1325.   private void setChildVisibilityForExpandedActionView(boolean expand)
  1326.   {
  1327.     int childCount = getChildCount();
  1328.     for (int i = 0; i < childCount; i++)
  1329.     {
  1330.       View child = getChildAt(i);
  1331.       LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1332.       if ((lp.mViewType != 2) && (child != this.mMenuView)) {
  1333.         child.setVisibility(expand ? 8 : 0);
  1334.       }
  1335.     }
  1336.   }
  1337.  
  1338.   private void updateChildVisibilityForExpandedActionView(View child)
  1339.   {
  1340.     LayoutParams lp = (LayoutParams)child.getLayoutParams();
  1341.     if ((lp.mViewType != 2) && (child != this.mMenuView)) {
  1342.       child.setVisibility(this.mExpandedActionView != null ? 8 : 0);
  1343.     }
  1344.   }
  1345.  
  1346.   public void setCollapsible(boolean collapsible)
  1347.   {
  1348.     this.mCollapsible = collapsible;
  1349.     requestLayout();
  1350.   }
  1351.  
  1352.   public void setMenuCallbacks(MenuPresenter.Callback pcb, MenuBuilder.Callback mcb)
  1353.   {
  1354.     this.mActionMenuPresenterCallback = pcb;
  1355.     this.mMenuBuilderCallback = mcb;
  1356.   }
  1357.  
  1358.   public void setMinimumHeight(int minHeight)
  1359.   {
  1360.     this.mMinHeight = minHeight;
  1361.    
  1362.     super.setMinimumHeight(minHeight);
  1363.   }
  1364.  
  1365.   private int getMinimumHeightCompat()
  1366.   {
  1367.     if (Build.VERSION.SDK_INT >= 16) {
  1368.       return ViewCompat.getMinimumHeight(this);
  1369.     }
  1370.     return this.mMinHeight;
  1371.   }
  1372.  
  1373.   public static abstract interface OnMenuItemClickListener
  1374.   {
  1375.     public abstract boolean onMenuItemClick(MenuItem paramMenuItem);
  1376.   }
  1377.  
  1378.   public static class LayoutParams
  1379.     extends ActionBar.LayoutParams
  1380.   {
  1381.     static final int CUSTOM = 0;
  1382.     static final int SYSTEM = 1;
  1383.     static final int EXPANDED = 2;
  1384.     int mViewType = 0;
  1385.    
  1386.     public LayoutParams(Context c, AttributeSet attrs)
  1387.     {
  1388.       super(attrs);
  1389.     }
  1390.    
  1391.     public LayoutParams(int width, int height)
  1392.     {
  1393.       super(height);
  1394.       this.gravity = 8388627;
  1395.     }
  1396.    
  1397.     public LayoutParams(int width, int height, int gravity)
  1398.     {
  1399.       super(height);
  1400.       this.gravity = gravity;
  1401.     }
  1402.    
  1403.     public LayoutParams(int gravity)
  1404.     {
  1405.       this(-2, -1, gravity);
  1406.     }
  1407.    
  1408.     public LayoutParams(LayoutParams source)
  1409.     {
  1410.       super();
  1411.      
  1412.       this.mViewType = source.mViewType;
  1413.     }
  1414.    
  1415.     public LayoutParams(ActionBar.LayoutParams source)
  1416.     {
  1417.       super();
  1418.     }
  1419.    
  1420.     public LayoutParams(ViewGroup.MarginLayoutParams source)
  1421.     {
  1422.       super();
  1423.      
  1424.  
  1425.       copyMarginsFromCompat(source);
  1426.     }
  1427.    
  1428.     public LayoutParams(ViewGroup.LayoutParams source)
  1429.     {
  1430.       super();
  1431.     }
  1432.    
  1433.     void copyMarginsFromCompat(ViewGroup.MarginLayoutParams source)
  1434.     {
  1435.       this.leftMargin = source.leftMargin;
  1436.       this.topMargin = source.topMargin;
  1437.       this.rightMargin = source.rightMargin;
  1438.       this.bottomMargin = source.bottomMargin;
  1439.     }
  1440.   }
  1441.  
  1442.   static class SavedState
  1443.     extends View.BaseSavedState
  1444.   {
  1445.     public int expandedMenuItemId;
  1446.     public boolean isOverflowOpen;
  1447.    
  1448.     public SavedState(Parcel source)
  1449.     {
  1450.       super();
  1451.       this.expandedMenuItemId = source.readInt();
  1452.       this.isOverflowOpen = (source.readInt() != 0);
  1453.     }
  1454.    
  1455.     public SavedState(Parcelable superState)
  1456.     {
  1457.       super();
  1458.     }
  1459.    
  1460.     public void writeToParcel(Parcel out, int flags)
  1461.     {
  1462.       super.writeToParcel(out, flags);
  1463.       out.writeInt(this.expandedMenuItemId);
  1464.       out.writeInt(this.isOverflowOpen ? 1 : 0);
  1465.     }
  1466.    
  1467.     public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator()
  1468.     {
  1469.       public Toolbar.SavedState createFromParcel(Parcel source)
  1470.       {
  1471.         return new Toolbar.SavedState(source);
  1472.       }
  1473.      
  1474.       public Toolbar.SavedState[] newArray(int size)
  1475.       {
  1476.         return new Toolbar.SavedState[size];
  1477.       }
  1478.     };
  1479.   }
  1480.  
  1481.   private class ExpandedActionViewMenuPresenter
  1482.     implements MenuPresenter
  1483.   {
  1484.     MenuBuilder mMenu;
  1485.     MenuItemImpl mCurrentExpandedItem;
  1486.    
  1487.     private ExpandedActionViewMenuPresenter() {}
  1488.    
  1489.     public void initForMenu(Context context, MenuBuilder menu)
  1490.     {
  1491.       if ((this.mMenu != null) && (this.mCurrentExpandedItem != null)) {
  1492.         this.mMenu.collapseItemActionView(this.mCurrentExpandedItem);
  1493.       }
  1494.       this.mMenu = menu;
  1495.     }
  1496.    
  1497.     public MenuView getMenuView(ViewGroup root)
  1498.     {
  1499.       return null;
  1500.     }
  1501.    
  1502.     public void updateMenuView(boolean cleared)
  1503.     {
  1504.       if (this.mCurrentExpandedItem != null)
  1505.       {
  1506.         boolean found = false;
  1507.         if (this.mMenu != null)
  1508.         {
  1509.           int count = this.mMenu.size();
  1510.           for (int i = 0; i < count; i++)
  1511.           {
  1512.             MenuItem item = this.mMenu.getItem(i);
  1513.             if (item == this.mCurrentExpandedItem)
  1514.             {
  1515.               found = true;
  1516.               break;
  1517.             }
  1518.           }
  1519.         }
  1520.         if (!found) {
  1521.           collapseItemActionView(this.mMenu, this.mCurrentExpandedItem);
  1522.         }
  1523.       }
  1524.     }
  1525.    
  1526.     public void setCallback(MenuPresenter.Callback cb) {}
  1527.    
  1528.     public boolean onSubMenuSelected(SubMenuBuilder subMenu)
  1529.     {
  1530.       return false;
  1531.     }
  1532.    
  1533.     public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {}
  1534.    
  1535.     public boolean flagActionItems()
  1536.     {
  1537.       return false;
  1538.     }
  1539.    
  1540.     public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item)
  1541.     {
  1542.       Toolbar.this.ensureCollapseButtonView();
  1543.       if (Toolbar.this.mCollapseButtonView.getParent() != Toolbar.this) {
  1544.         Toolbar.this.addView(Toolbar.this.mCollapseButtonView);
  1545.       }
  1546.       Toolbar.this.mExpandedActionView = item.getActionView();
  1547.       this.mCurrentExpandedItem = item;
  1548.       if (Toolbar.this.mExpandedActionView.getParent() != Toolbar.this)
  1549.       {
  1550.         Toolbar.LayoutParams lp = Toolbar.this.generateDefaultLayoutParams();
  1551.         lp.gravity = (0x800003 | Toolbar.this.mButtonGravity & 0x70);
  1552.         lp.mViewType = 2;
  1553.         Toolbar.this.mExpandedActionView.setLayoutParams(lp);
  1554.         Toolbar.this.addView(Toolbar.this.mExpandedActionView);
  1555.       }
  1556.       Toolbar.this.setChildVisibilityForExpandedActionView(true);
  1557.       Toolbar.this.requestLayout();
  1558.       item.setActionViewExpanded(true);
  1559.       if ((Toolbar.this.mExpandedActionView instanceof CollapsibleActionView)) {
  1560.         ((CollapsibleActionView)Toolbar.this.mExpandedActionView).onActionViewExpanded();
  1561.       }
  1562.       return true;
  1563.     }
  1564.    
  1565.     public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item)
  1566.     {
  1567.       if ((Toolbar.this.mExpandedActionView instanceof CollapsibleActionView)) {
  1568.         ((CollapsibleActionView)Toolbar.this.mExpandedActionView).onActionViewCollapsed();
  1569.       }
  1570.       Toolbar.this.removeView(Toolbar.this.mExpandedActionView);
  1571.       Toolbar.this.removeView(Toolbar.this.mCollapseButtonView);
  1572.       Toolbar.this.mExpandedActionView = null;
  1573.      
  1574.       Toolbar.this.setChildVisibilityForExpandedActionView(false);
  1575.       this.mCurrentExpandedItem = null;
  1576.       Toolbar.this.requestLayout();
  1577.       item.setActionViewExpanded(false);
  1578.      
  1579.       return true;
  1580.     }
  1581.    
  1582.     public int getId()
  1583.     {
  1584.       return 0;
  1585.     }
  1586.    
  1587.     public Parcelable onSaveInstanceState()
  1588.     {
  1589.       return null;
  1590.     }
  1591.    
  1592.     public void onRestoreInstanceState(Parcelable state) {}
  1593.   }
  1594.  
  1595.   private static Context themifyContext(Context context, AttributeSet attrs, int defStyleAttr)
  1596.   {
  1597.     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Toolbar, defStyleAttr, 0);
  1598.    
  1599.     int themeId = a.getResourceId(R.styleable.Toolbar_theme, 0);
  1600.     if (themeId != 0) {
  1601.       context = new ContextThemeWrapper(context, themeId);
  1602.     }
  1603.     a.recycle();
  1604.     return context;
  1605.   }
  1606. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement