content: package androidx.appcompat.view.menu;
  
  import android.content.Context;
  import android.content.res.Resources;
  import android.os.Parcelable;
  import android.util.AttributeSet;
  import android.view.Gravity;
  import android.view.KeyEvent;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.ViewTreeObserver;
  import android.widget.AdapterView;
  import android.widget.FrameLayout;
  import android.widget.ListView;
  import android.widget.PopupWindow;
  import android.widget.TextView;
  import androidx.appcompat.R;
  import androidx.appcompat.view.menu.MenuPresenter;
  import androidx.appcompat.widget.MenuPopupWindow;
  import androidx.core.view.ViewCompat;
  
  final class StandardMenuPopup extends MenuPopup implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener, MenuPresenter, View.OnKeyListener {
      private static final int ITEM_LAYOUT = R.layout.abc_popup_menu_item_layout;
      private final MenuAdapter mAdapter;
      private View mAnchorView;
      private final View.OnAttachStateChangeListener mAttachStateChangeListener = new View.OnAttachStateChangeListener() {
          public void onViewAttachedToWindow(View v) {
          }
  
          public void onViewDetachedFromWindow(View v) {
              if (StandardMenuPopup.this.mTreeObserver != null) {
                  if (!StandardMenuPopup.this.mTreeObserver.isAlive()) {
                      StandardMenuPopup.this.mTreeObserver = v.getViewTreeObserver();
                  }
                  StandardMenuPopup.this.mTreeObserver.removeGlobalOnLayoutListener(StandardMenuPopup.this.mGlobalLayoutListener);
              }
              v.removeOnAttachStateChangeListener(this);
          }
      };
      private int mContentWidth;
      private final Context mContext;
      private int mDropDownGravity = 0;
      final ViewTreeObserver.OnGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
          public void onGlobalLayout() {
              if (StandardMenuPopup.this.isShowing() && !StandardMenuPopup.this.mPopup.isModal()) {
                  View view = StandardMenuPopup.this.mShownAnchorView;
                  if (view == null || !view.isShown()) {
                      StandardMenuPopup.this.dismiss();
                  } else {
                      StandardMenuPopup.this.mPopup.show();
                  }
              }
          }
      };
      private boolean mHasContentWidth;
      private final MenuBuilder mMenu;
      private PopupWindow.OnDismissListener mOnDismissListener;
      private final boolean mOverflowOnly;
      final MenuPopupWindow mPopup;
      private final int mPopupMaxWidth;
      private final int mPopupStyleAttr;
      private final int mPopupStyleRes;
      private MenuPresenter.Callback mPresenterCallback;
      private boolean mShowTitle;
      View mShownAnchorView;
      ViewTreeObserver mTreeObserver;
      private boolean mWasDismissed;
  
      public StandardMenuPopup(Context context, MenuBuilder menu, View anchorView, int popupStyleAttr, int popupStyleRes, boolean overflowOnly) {
          this.mContext = context;
          this.mMenu = menu;
          this.mOverflowOnly = overflowOnly;
          this.mAdapter = new MenuAdapter(menu, LayoutInflater.from(context), overflowOnly, ITEM_LAYOUT);
          this.mPopupStyleAttr = popupStyleAttr;
          this.mPopupStyleRes = popupStyleRes;
          Resources resources = context.getResources();
          this.mPopupMaxWidth = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
          this.mAnchorView = anchorView;
          this.mPopup = new MenuPopupWindow(context, (AttributeSet) null, popupStyleAttr, popupStyleRes);
          menu.addMenuPresenter(this, context);
      }
  
      private boolean tryShow() {
          View view;
          if (isShowing()) {
              return true;
          }
          if (this.mWasDismissed || (view = this.mAnchorView) == null) {
              return false;
          }
          this.mShownAnchorView = view;
          this.mPopup.setOnDismissListener(this);
          this.mPopup.setOnItemClickListener(this);
          this.mPopup.setModal(true);
          View view2 = this.mShownAnchorView;
          boolean z = this.mTreeObserver == null;
          ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
          this.mTreeObserver = viewTreeObserver;
          if (z) {
              viewTreeObserver.addOnGlobalLayoutListener(this.mGlobalLayoutListener);
          }
          view2.addOnAttachStateChangeListener(this.mAttachStateChangeListener);
          this.mPopup.setAnchorView(view2);
          this.mPopup.setDropDownGravity(this.mDropDownGravity);
          if (!this.mHasContentWidth) {
              this.mContentWidth = measureIndividualMenuWidth(this.mAdapter, (ViewGroup) null, this.mContext, this.mPopupMaxWidth);
              this.mHasContentWidth = true;
          }
          this.mPopup.setContentWidth(this.mContentWidth);
          this.mPopup.setInputMethodMode(2);
          this.mPopup.setEpicenterBounds(getEpicenterBounds());
          this.mPopup.show();
          ListView listView = this.mPopup.getListView();
          listView.setOnKeyListener(this);
          if (this.mShowTitle && this.mMenu.getHeaderTitle() != null) {
              FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.mContext).inflate(R.layout.abc_popup_menu_header_item_layout, listView, false);
              TextView textView = (TextView) frameLayout.findViewById(16908310);
              if (textView != null) {
                  textView.setText(this.mMenu.getHeaderTitle());
              }
              frameLayout.setEnabled(false);
              listView.addHeaderView(frameLayout, (Object) null, false);
          }
          this.mPopup.setAdapter(this.mAdapter);
          this.mPopup.show();
          return true;
      }
  
      public void addMenu(MenuBuilder menu) {
      }
  
      public void dismiss() {
          if (isShowing()) {
              this.mPopup.dismiss();
          }
      }
  
      public boolean flagActionItems() {
          return false;
      }
  
      public ListView getListView() {
          return this.mPopup.getListView();
      }
  
      public boolean isShowing() {
          return !this.mWasDismissed && this.mPopup.isShowing();
      }
  
      public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
          if (menu == this.mMenu) {
              dismiss();
              MenuPresenter.Callback callback = this.mPresenterCallback;
              if (callback != null) {
                  callback.onCloseMenu(menu, allMenusAreClosing);
              }
          }
      }
  
      public void onDismiss() {
          this.mWasDismissed = true;
          this.mMenu.close();
          ViewTreeObserver viewTreeObserver = this.mTreeObserver;
          if (viewTreeObserver != null) {
              if (!viewTreeObserver.isAlive()) {
                  this.mTreeObserver = this.mShownAnchorView.getViewTreeObserver();
              }
              this.mTreeObserver.removeGlobalOnLayoutListener(this.mGlobalLayoutListener);
              this.mTreeObserver = null;
          }
          this.mShownAnchorView.removeOnAttachStateChangeListener(this.mAttachStateChangeListener);
          PopupWindow.OnDismissListener onDismissListener = this.mOnDismissListener;
          if (onDismissListener != null) {
              onDismissListener.onDismiss();
          }
      }
  
      public boolean onKey(View v, int keyCode, KeyEvent event) {
          if (event.getAction() != 1 || keyCode != 82) {
              return false;
          }
          dismiss();
          return true;
      }
  
      public void onRestoreInstanceState(Parcelable state) {
      }
  
      public Parcelable onSaveInstanceState() {
          return null;
      }
  
      public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
          if (subMenu.hasVisibleItems()) {
              MenuPopupHelper menuPopupHelper = new MenuPopupHelper(this.mContext, subMenu, this.mShownAnchorView, this.mOverflowOnly, this.mPopupStyleAttr, this.mPopupStyleRes);
              menuPopupHelper.setPresenterCallback(this.mPresenterCallback);
              menuPopupHelper.setForceShowIcon(MenuPopup.shouldPreserveIconSpacing(subMenu));
              menuPopupHelper.setOnDismissListener(this.mOnDismissListener);
              this.mOnDismissListener = null;
              this.mMenu.close(false);
              int horizontalOffset = this.mPopup.getHorizontalOffset();
              int verticalOffset = this.mPopup.getVerticalOffset();
              if ((Gravity.getAbsoluteGravity(this.mDropDownGravity, ViewCompat.getLayoutDirection(this.mAnchorView)) & 7) == 5) {
                  horizontalOffset += this.mAnchorView.getWidth();
              }
              if (menuPopupHelper.tryShow(horizontalOffset, verticalOffset)) {
                  MenuPresenter.Callback callback = this.mPresenterCallback;
                  if (callback == null) {
                      return true;
                  }
                  callback.onOpenSubMenu(subMenu);
                  return true;
              }
          }
          return false;
      }
  
      public void setAnchorView(View anchor) {
          this.mAnchorView = anchor;
      }
  
      public void setCallback(MenuPresenter.Callback cb) {
          this.mPresenterCallback = cb;
      }
  
      public void setForceShowIcon(boolean forceShow) {
          this.mAdapter.setForceShowIcon(forceShow);
      }
  
      public void setGravity(int gravity) {
          this.mDropDownGravity = gravity;
      }
  
      public void setHorizontalOffset(int x) {
          this.mPopup.setHorizontalOffset(x);
      }
  
      public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
          this.mOnDismissListener = listener;
      }
  
      public void setShowTitle(boolean showTitle) {
          this.mShowTitle = showTitle;
      }
  
      public void setVerticalOffset(int y) {
          this.mPopup.setVerticalOffset(y);
      }
  
      public void show() {
          if (!tryShow()) {
              throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
          }
      }
  
      public void updateMenuView(boolean cleared) {
          this.mHasContentWidth = false;
          MenuAdapter menuAdapter = this.mAdapter;
          if (menuAdapter != null) {
              menuAdapter.notifyDataSetChanged();
          }
      }
  }
  