content: package androidx.appcompat.widget;
  
  import android.content.Context;
  import android.graphics.drawable.Drawable;
  import android.os.Parcelable;
  import android.text.TextUtils;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.util.SparseArray;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.widget.AdapterView;
  import android.widget.Spinner;
  import android.widget.SpinnerAdapter;
  import androidx.appcompat.R;
  import androidx.appcompat.content.res.AppCompatResources;
  import androidx.appcompat.view.menu.ActionMenuItem;
  import androidx.appcompat.view.menu.MenuBuilder;
  import androidx.appcompat.view.menu.MenuPresenter;
  import androidx.appcompat.widget.Toolbar;
  import androidx.core.view.ViewCompat;
  import androidx.core.view.ViewPropertyAnimatorCompat;
  import androidx.core.view.ViewPropertyAnimatorListenerAdapter;
  
  public class ToolbarWidgetWrapper implements DecorToolbar {
      private static final int AFFECTS_LOGO_MASK = 3;
      private static final long DEFAULT_FADE_DURATION_MS = 200;
      private static final String TAG = "ToolbarWidgetWrapper";
      private ActionMenuPresenter mActionMenuPresenter;
      private View mCustomView;
      private int mDefaultNavigationContentDescription;
      private Drawable mDefaultNavigationIcon;
      private int mDisplayOpts;
      private CharSequence mHomeDescription;
      private Drawable mIcon;
      private Drawable mLogo;
      boolean mMenuPrepared;
      private Drawable mNavIcon;
      private int mNavigationMode;
      private Spinner mSpinner;
      private CharSequence mSubtitle;
      private View mTabView;
      CharSequence mTitle;
      private boolean mTitleSet;
      Toolbar mToolbar;
      Window.Callback mWindowCallback;
  
      public ToolbarWidgetWrapper(Toolbar toolbar, boolean style) {
          this(toolbar, style, R.string.abc_action_bar_up_description, R.drawable.abc_ic_ab_back_material);
      }
  
      public ToolbarWidgetWrapper(Toolbar toolbar, boolean style, int defaultNavigationContentDescription, int defaultNavigationIcon) {
          Drawable drawable;
          this.mNavigationMode = 0;
          this.mDefaultNavigationContentDescription = 0;
          this.mToolbar = toolbar;
          this.mTitle = toolbar.getTitle();
          this.mSubtitle = toolbar.getSubtitle();
          this.mTitleSet = this.mTitle != null;
          this.mNavIcon = toolbar.getNavigationIcon();
          TintTypedArray obtainStyledAttributes = TintTypedArray.obtainStyledAttributes(toolbar.getContext(), (AttributeSet) null, R.styleable.ActionBar, R.attr.actionBarStyle, 0);
          this.mDefaultNavigationIcon = obtainStyledAttributes.getDrawable(R.styleable.ActionBar_homeAsUpIndicator);
          if (style) {
              CharSequence text = obtainStyledAttributes.getText(R.styleable.ActionBar_title);
              if (!TextUtils.isEmpty(text)) {
                  setTitle(text);
              }
              CharSequence text2 = obtainStyledAttributes.getText(R.styleable.ActionBar_subtitle);
              if (!TextUtils.isEmpty(text2)) {
                  setSubtitle(text2);
              }
              Drawable drawable2 = obtainStyledAttributes.getDrawable(R.styleable.ActionBar_logo);
              if (drawable2 != null) {
                  setLogo(drawable2);
              }
              Drawable drawable3 = obtainStyledAttributes.getDrawable(R.styleable.ActionBar_icon);
              if (drawable3 != null) {
                  setIcon(drawable3);
              }
              if (this.mNavIcon == null && (drawable = this.mDefaultNavigationIcon) != null) {
                  setNavigationIcon(drawable);
              }
              setDisplayOptions(obtainStyledAttributes.getInt(R.styleable.ActionBar_displayOptions, 0));
              int resourceId = obtainStyledAttributes.getResourceId(R.styleable.ActionBar_customNavigationLayout, 0);
              if (resourceId != 0) {
                  setCustomView(LayoutInflater.from(this.mToolbar.getContext()).inflate(resourceId, this.mToolbar, false));
                  setDisplayOptions(this.mDisplayOpts | 16);
              }
              int layoutDimension = obtainStyledAttributes.getLayoutDimension(R.styleable.ActionBar_height, 0);
              if (layoutDimension > 0) {
                  ViewGroup.LayoutParams layoutParams = this.mToolbar.getLayoutParams();
                  layoutParams.height = layoutDimension;
                  this.mToolbar.setLayoutParams(layoutParams);
              }
              int dimensionPixelOffset = obtainStyledAttributes.getDimensionPixelOffset(R.styleable.ActionBar_contentInsetStart, -1);
              int dimensionPixelOffset2 = obtainStyledAttributes.getDimensionPixelOffset(R.styleable.ActionBar_contentInsetEnd, -1);
              if (dimensionPixelOffset >= 0 || dimensionPixelOffset2 >= 0) {
                  this.mToolbar.setContentInsetsRelative(Math.max(dimensionPixelOffset, 0), Math.max(dimensionPixelOffset2, 0));
              }
              int resourceId2 = obtainStyledAttributes.getResourceId(R.styleable.ActionBar_titleTextStyle, 0);
              if (resourceId2 != 0) {
                  Toolbar toolbar2 = this.mToolbar;
                  toolbar2.setTitleTextAppearance(toolbar2.getContext(), resourceId2);
              }
              int resourceId3 = obtainStyledAttributes.getResourceId(R.styleable.ActionBar_subtitleTextStyle, 0);
              if (resourceId3 != 0) {
                  Toolbar toolbar3 = this.mToolbar;
                  toolbar3.setSubtitleTextAppearance(toolbar3.getContext(), resourceId3);
              }
              int resourceId4 = obtainStyledAttributes.getResourceId(R.styleable.ActionBar_popupTheme, 0);
              if (resourceId4 != 0) {
                  this.mToolbar.setPopupTheme(resourceId4);
              }
          } else {
              this.mDisplayOpts = detectDisplayOptions();
          }
          obtainStyledAttributes.recycle();
          setDefaultNavigationContentDescription(defaultNavigationContentDescription);
          this.mHomeDescription = this.mToolbar.getNavigationContentDescription();
          this.mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
              final ActionMenuItem mNavItem;
  
              {
                  this.mNavItem = new ActionMenuItem(ToolbarWidgetWrapper.this.mToolbar.getContext(), 0, 16908332, 0, 0, ToolbarWidgetWrapper.this.mTitle);
              }
  
              public void onClick(View v) {
                  if (ToolbarWidgetWrapper.this.mWindowCallback != null && ToolbarWidgetWrapper.this.mMenuPrepared) {
                      ToolbarWidgetWrapper.this.mWindowCallback.onMenuItemSelected(0, this.mNavItem);
                  }
              }
          });
      }
  
      private int detectDisplayOptions() {
          if (this.mToolbar.getNavigationIcon() == null) {
              return 11;
          }
          int i = 11 | 4;
          this.mDefaultNavigationIcon = this.mToolbar.getNavigationIcon();
          return i;
      }
  
      private void ensureSpinner() {
          if (this.mSpinner == null) {
              this.mSpinner = new AppCompatSpinner(getContext(), (AttributeSet) null, R.attr.actionDropDownStyle);
              this.mSpinner.setLayoutParams(new Toolbar.LayoutParams(-2, -2, 8388627));
          }
      }
  
      private void setTitleInt(CharSequence title) {
          this.mTitle = title;
          if ((this.mDisplayOpts & 8) != 0) {
              this.mToolbar.setTitle(title);
              if (this.mTitleSet) {
                  ViewCompat.setAccessibilityPaneTitle(this.mToolbar.getRootView(), title);
              }
          }
      }
  
      private void updateHomeAccessibility() {
          if ((this.mDisplayOpts & 4) == 0) {
              return;
          }
          if (TextUtils.isEmpty(this.mHomeDescription)) {
              this.mToolbar.setNavigationContentDescription(this.mDefaultNavigationContentDescription);
          } else {
              this.mToolbar.setNavigationContentDescription(this.mHomeDescription);
          }
      }
  
      private void updateNavigationIcon() {
          if ((this.mDisplayOpts & 4) != 0) {
              Toolbar toolbar = this.mToolbar;
              Drawable drawable = this.mNavIcon;
              if (drawable == null) {
                  drawable = this.mDefaultNavigationIcon;
              }
              toolbar.setNavigationIcon(drawable);
              return;
          }
          this.mToolbar.setNavigationIcon((Drawable) null);
      }
  
      private void updateToolbarLogo() {
          Drawable drawable = null;
          int i = this.mDisplayOpts;
          if ((i & 2) != 0) {
              if ((i & 1) != 0) {
                  Drawable drawable2 = this.mLogo;
                  if (drawable2 == null) {
                      drawable2 = this.mIcon;
                  }
                  drawable = drawable2;
              } else {
                  drawable = this.mIcon;
              }
          }
          this.mToolbar.setLogo(drawable);
      }
  
      public void animateToVisibility(int visibility) {
          ViewPropertyAnimatorCompat viewPropertyAnimatorCompat = setupAnimatorToVisibility(visibility, DEFAULT_FADE_DURATION_MS);
          if (viewPropertyAnimatorCompat != null) {
              viewPropertyAnimatorCompat.start();
          }
      }
  
      public boolean canShowOverflowMenu() {
          return this.mToolbar.canShowOverflowMenu();
      }
  
      public void collapseActionView() {
          this.mToolbar.collapseActionView();
      }
  
      public void dismissPopupMenus() {
          this.mToolbar.dismissPopupMenus();
      }
  
      public Context getContext() {
          return this.mToolbar.getContext();
      }
  
      public View getCustomView() {
          return this.mCustomView;
      }
  
      public int getDisplayOptions() {
          return this.mDisplayOpts;
      }
  
      public int getDropdownItemCount() {
          Spinner spinner = this.mSpinner;
          if (spinner != null) {
              return spinner.getCount();
          }
          return 0;
      }
  
      public int getDropdownSelectedPosition() {
          Spinner spinner = this.mSpinner;
          if (spinner != null) {
              return spinner.getSelectedItemPosition();
          }
          return 0;
      }
  
      public int getHeight() {
          return this.mToolbar.getHeight();
      }
  
      public Menu getMenu() {
          return this.mToolbar.getMenu();
      }
  
      public int getNavigationMode() {
          return this.mNavigationMode;
      }
  
      public CharSequence getSubtitle() {
          return this.mToolbar.getSubtitle();
      }
  
      public CharSequence getTitle() {
          return this.mToolbar.getTitle();
      }
  
      public ViewGroup getViewGroup() {
          return this.mToolbar;
      }
  
      public int getVisibility() {
          return this.mToolbar.getVisibility();
      }
  
      public boolean hasEmbeddedTabs() {
          return this.mTabView != null;
      }
  
      public boolean hasExpandedActionView() {
          return this.mToolbar.hasExpandedActionView();
      }
  
      public boolean hasIcon() {
          return this.mIcon != null;
      }
  
      public boolean hasLogo() {
          return this.mLogo != null;
      }
  
      public boolean hideOverflowMenu() {
          return this.mToolbar.hideOverflowMenu();
      }
  
      public void initIndeterminateProgress() {
          Log.i(TAG, "Progress display unsupported");
      }
  
      public void initProgress() {
          Log.i(TAG, "Progress display unsupported");
      }
  
      public boolean isOverflowMenuShowPending() {
          return this.mToolbar.isOverflowMenuShowPending();
      }
  
      public boolean isOverflowMenuShowing() {
          return this.mToolbar.isOverflowMenuShowing();
      }
  
      public boolean isTitleTruncated() {
          return this.mToolbar.isTitleTruncated();
      }
  
      public void restoreHierarchyState(SparseArray<Parcelable> sparseArray) {
          this.mToolbar.restoreHierarchyState(sparseArray);
      }
  
      public void saveHierarchyState(SparseArray<Parcelable> sparseArray) {
          this.mToolbar.saveHierarchyState(sparseArray);
      }
  
      public void setBackgroundDrawable(Drawable d) {
          ViewCompat.setBackground(this.mToolbar, d);
      }
  
      public void setCollapsible(boolean collapsible) {
          this.mToolbar.setCollapsible(collapsible);
      }
  
      public void setCustomView(View view) {
          View view2 = this.mCustomView;
          if (!(view2 == null || (this.mDisplayOpts & 16) == 0)) {
              this.mToolbar.removeView(view2);
          }
          this.mCustomView = view;
          if (view != null && (this.mDisplayOpts & 16) != 0) {
              this.mToolbar.addView(view);
          }
      }
  
      public void setDefaultNavigationContentDescription(int defaultNavigationContentDescription) {
          if (defaultNavigationContentDescription != this.mDefaultNavigationContentDescription) {
              this.mDefaultNavigationContentDescription = defaultNavigationContentDescription;
              if (TextUtils.isEmpty(this.mToolbar.getNavigationContentDescription())) {
                  setNavigationContentDescription(this.mDefaultNavigationContentDescription);
              }
          }
      }
  
      public void setDefaultNavigationIcon(Drawable defaultNavigationIcon) {
          if (this.mDefaultNavigationIcon != defaultNavigationIcon) {
              this.mDefaultNavigationIcon = defaultNavigationIcon;
              updateNavigationIcon();
          }
      }
  
      public void setDisplayOptions(int newOpts) {
          View view;
          int i = this.mDisplayOpts ^ newOpts;
          this.mDisplayOpts = newOpts;
          if (i != 0) {
              if ((i & 4) != 0) {
                  if ((newOpts & 4) != 0) {
                      updateHomeAccessibility();
                  }
                  updateNavigationIcon();
              }
              if ((i & 3) != 0) {
                  updateToolbarLogo();
              }
              if ((i & 8) != 0) {
                  if ((newOpts & 8) != 0) {
                      this.mToolbar.setTitle(this.mTitle);
                      this.mToolbar.setSubtitle(this.mSubtitle);
                  } else {
                      this.mToolbar.setTitle((CharSequence) null);
                      this.mToolbar.setSubtitle((CharSequence) null);
                  }
              }
              if ((i & 16) != 0 && (view = this.mCustomView) != null) {
                  if ((newOpts & 16) != 0) {
                      this.mToolbar.addView(view);
                  } else {
                      this.mToolbar.removeView(view);
                  }
              }
          }
      }
  
      public void setDropdownParams(SpinnerAdapter adapter, AdapterView.OnItemSelectedListener listener) {
          ensureSpinner();
          this.mSpinner.setAdapter(adapter);
          this.mSpinner.setOnItemSelectedListener(listener);
      }
  
      public void setDropdownSelectedPosition(int position) {
          Spinner spinner = this.mSpinner;
          if (spinner != null) {
              spinner.setSelection(position);
              return;
          }
          throw new IllegalStateException("Can't set dropdown selected position without an adapter");
      }
  
      public void setEmbeddedTabView(ScrollingTabContainerView tabView) {
          Toolbar toolbar;
          View view = this.mTabView;
          if (view != null && view.getParent() == (toolbar = this.mToolbar)) {
              toolbar.removeView(this.mTabView);
          }
          this.mTabView = tabView;
          if (tabView != null && this.mNavigationMode == 2) {
              this.mToolbar.addView(tabView, 0);
              Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) this.mTabView.getLayoutParams();
              layoutParams.width = -2;
              layoutParams.height = -2;
              layoutParams.gravity = 8388691;
              tabView.setAllowCollapse(true);
          }
      }
  
      public void setHomeButtonEnabled(boolean enable) {
      }
  
      public void setIcon(int resId) {
          setIcon(resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
      }
  
      public void setIcon(Drawable d) {
          this.mIcon = d;
          updateToolbarLogo();
      }
  
      public void setLogo(int resId) {
          setLogo(resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
      }
  
      public void setLogo(Drawable d) {
          this.mLogo = d;
          updateToolbarLogo();
      }
  
      public void setMenu(Menu menu, MenuPresenter.Callback cb) {
          if (this.mActionMenuPresenter == null) {
              ActionMenuPresenter actionMenuPresenter = new ActionMenuPresenter(this.mToolbar.getContext());
              this.mActionMenuPresenter = actionMenuPresenter;
              actionMenuPresenter.setId(R.id.action_menu_presenter);
          }
          this.mActionMenuPresenter.setCallback(cb);
          this.mToolbar.setMenu((MenuBuilder) menu, this.mActionMenuPresenter);
      }
  
      public void setMenuCallbacks(MenuPresenter.Callback actionMenuPresenterCallback, MenuBuilder.Callback menuBuilderCallback) {
          this.mToolbar.setMenuCallbacks(actionMenuPresenterCallback, menuBuilderCallback);
      }
  
      public void setMenuPrepared() {
          this.mMenuPrepared = true;
      }
  
      public void setNavigationContentDescription(int resId) {
          setNavigationContentDescription((CharSequence) resId == 0 ? null : getContext().getString(resId));
      }
  
      public void setNavigationContentDescription(CharSequence description) {
          this.mHomeDescription = description;
          updateHomeAccessibility();
      }
  
      public void setNavigationIcon(int resId) {
          setNavigationIcon(resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
      }
  
      public void setNavigationIcon(Drawable icon) {
          this.mNavIcon = icon;
          updateNavigationIcon();
      }
  
      public void setNavigationMode(int mode) {
          Toolbar toolbar;
          Toolbar toolbar2;
          int i = this.mNavigationMode;
          if (mode != i) {
              switch (i) {
                  case 1:
                      Spinner spinner = this.mSpinner;
                      if (spinner != null && spinner.getParent() == (toolbar = this.mToolbar)) {
                          toolbar.removeView(this.mSpinner);
                          break;
                      }
                  case 2:
                      View view = this.mTabView;
                      if (view != null && view.getParent() == (toolbar2 = this.mToolbar)) {
                          toolbar2.removeView(this.mTabView);
                          break;
                      }
              }
              this.mNavigationMode = mode;
              switch (mode) {
                  case 0:
                      return;
                  case 1:
                      ensureSpinner();
                      this.mToolbar.addView(this.mSpinner, 0);
                      return;
                  case 2:
                      View view2 = this.mTabView;
                      if (view2 != null) {
                          this.mToolbar.addView(view2, 0);
                          Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) this.mTabView.getLayoutParams();
                          layoutParams.width = -2;
                          layoutParams.height = -2;
                          layoutParams.gravity = 8388691;
                          return;
                      }
                      return;
                  default:
                      throw new IllegalArgumentException("Invalid navigation mode " + mode);
              }
          }
      }
  
      public void setSubtitle(CharSequence subtitle) {
          this.mSubtitle = subtitle;
          if ((this.mDisplayOpts & 8) != 0) {
              this.mToolbar.setSubtitle(subtitle);
          }
      }
  
      public void setTitle(CharSequence title) {
          this.mTitleSet = true;
          setTitleInt(title);
      }
  
      public void setVisibility(int visible) {
          this.mToolbar.setVisibility(visible);
      }
  
      public void setWindowCallback(Window.Callback cb) {
          this.mWindowCallback = cb;
      }
  
      public void setWindowTitle(CharSequence title) {
          if (!this.mTitleSet) {
              setTitleInt(title);
          }
      }
  
      public ViewPropertyAnimatorCompat setupAnimatorToVisibility(final int visibility, long duration) {
          return ViewCompat.animate(this.mToolbar).alpha(visibility == 0 ? 1.0f : 0.0f).setDuration(duration).setListener(new ViewPropertyAnimatorListenerAdapter() {
              private boolean mCanceled = false;
  
              public void onAnimationCancel(View view) {
                  this.mCanceled = true;
              }
  
              public void onAnimationEnd(View view) {
                  if (!this.mCanceled) {
                      ToolbarWidgetWrapper.this.mToolbar.setVisibility(visibility);
                  }
              }
  
              public void onAnimationStart(View view) {
                  ToolbarWidgetWrapper.this.mToolbar.setVisibility(0);
              }
          });
      }
  
      public boolean showOverflowMenu() {
          return this.mToolbar.showOverflowMenu();
      }
  }
  