/** Sets the current content to be the main view panel. */
 private void setMainPanelAsContent() {
   // This should never be called if the main panel has not been initialized.
   Preconditions.checkNotNull(mMainPanel);
   mContentContainer.removeAllViews();
   Size mainPanelSize = mMainPanel.measure();
   ViewGroup.LayoutParams params = mContentContainer.getLayoutParams();
   params.width = mainPanelSize.getWidth();
   params.height = mainPanelSize.getHeight();
   mContentContainer.setLayoutParams(params);
   mContentContainer.addView(mMainPanel.getView());
   setContentAreaAsTouchableSurface();
 }
    /** Places the main view panel at the appropriate resting coordinates. */
    private void positionMainPanel() {
      Preconditions.checkNotNull(mMainPanel);
      float x =
          mPopupWindow.getWidth() - (mMainPanel.getView().getMeasuredWidth() + mMarginHorizontal);
      mContentContainer.setX(x);

      float y = mMarginVertical;
      if (mOverflowDirection == OVERFLOW_DIRECTION_UP) {
        y = getHeight() - (mMainPanel.getView().getMeasuredHeight() + mMarginVertical);
      }
      mContentContainer.setY(y);
      setContentAreaAsTouchableSurface();
    }
    /** Prepares the content container for show and update calls. */
    private void preparePopupContent() {
      // Reset visibility.
      if (mMainPanel != null) {
        mMainPanel.fadeIn(false);
      }
      if (mOverflowPanel != null) {
        mOverflowPanel.fadeIn(false);
      }

      // Reset position.
      if (mMainPanel != null && mContentContainer.getChildAt(0) == mMainPanel.getView()) {
        positionMainPanel();
      }
      if (mOverflowPanel != null && mContentContainer.getChildAt(0) == mOverflowPanel.getView()) {
        positionOverflowPanel();
      }
    }
 /** Lays out buttons for the specified menu items. */
 public void layoutMenuItems(
     List<MenuItem> menuItems,
     MenuItem.OnMenuItemClickListener menuItemClickListener,
     int suggestedWidth) {
   mContentContainer.removeAllViews();
   if (mMainPanel == null) {
     mMainPanel = new FloatingToolbarMainPanel(mParent.getContext(), mOpenOverflow);
   }
   List<MenuItem> overflowMenuItems = mMainPanel.layoutMenuItems(menuItems, suggestedWidth);
   mMainPanel.setOnMenuItemClickListener(menuItemClickListener);
   if (!overflowMenuItems.isEmpty()) {
     if (mOverflowPanel == null) {
       mOverflowPanel = new FloatingToolbarOverflowPanel(mParent.getContext(), mCloseOverflow);
     }
     mOverflowPanel.setMenuItems(overflowMenuItems);
     mOverflowPanel.setOnMenuItemClickListener(menuItemClickListener);
   }
   updatePopupSize();
 }
 private void updatePopupSize() {
   int width = 0;
   int height = 0;
   if (mMainPanel != null) {
     Size mainPanelSize = mMainPanel.measure();
     width = mainPanelSize.getWidth();
     height = mainPanelSize.getHeight();
   }
   if (mOverflowPanel != null) {
     Size overflowPanelSize = mOverflowPanel.measure();
     width = Math.max(width, overflowPanelSize.getWidth());
     height = Math.max(height, overflowPanelSize.getHeight());
   }
   mPopupWindow.setWidth(width + mMarginHorizontal * 2);
   mPopupWindow.setHeight(height + mMarginVertical * 2);
 }
    /**
     * Opens the floating toolbar overflow. This method should not be called if menu items have not
     * been laid out with {@link #layoutMenuItems(java.util.List, MenuItem.OnMenuItemClickListener,
     * int)}.
     *
     * @throws IllegalStateException if called when menu items have not been laid out.
     */
    private void openOverflow() {
      Preconditions.checkState(mMainPanel != null);
      Preconditions.checkState(mOverflowPanel != null);

      mMainPanel.fadeOut(true);
      Size overflowPanelSize = mOverflowPanel.measure();
      final int targetWidth = overflowPanelSize.getWidth();
      final int targetHeight = overflowPanelSize.getHeight();
      final boolean morphUpwards = (mOverflowDirection == OVERFLOW_DIRECTION_UP);
      final int startWidth = mContentContainer.getWidth();
      final int startHeight = mContentContainer.getHeight();
      final float startY = mContentContainer.getY();
      final float right = mContentContainer.getX() + mContentContainer.getWidth();
      Animation widthAnimation =
          new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
              ViewGroup.LayoutParams params = mContentContainer.getLayoutParams();
              int deltaWidth = (int) (interpolatedTime * (targetWidth - startWidth));
              params.width = startWidth + deltaWidth;
              mContentContainer.setLayoutParams(params);
              mContentContainer.setX(right - mContentContainer.getWidth());
            }
          };
      Animation heightAnimation =
          new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
              ViewGroup.LayoutParams params = mContentContainer.getLayoutParams();
              int deltaHeight = (int) (interpolatedTime * (targetHeight - startHeight));
              params.height = startHeight + deltaHeight;
              mContentContainer.setLayoutParams(params);
              if (morphUpwards) {
                float y = startY - (mContentContainer.getHeight() - startHeight);
                mContentContainer.setY(y);
              }
            }
          };
      widthAnimation.setDuration(240);
      heightAnimation.setDuration(180);
      heightAnimation.setStartOffset(60);
      mOpenOverflowAnimation.getAnimations().clear();
      mOpenOverflowAnimation.setAnimationListener(mOnOverflowOpened);
      mOpenOverflowAnimation.addAnimation(widthAnimation);
      mOpenOverflowAnimation.addAnimation(heightAnimation);
      mContentContainer.startAnimation(mOpenOverflowAnimation);
    }