gt:         if (!mHiddenByApp) {
context: /*
   * Copyright (C) 2014 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package android.support.v7.internal.app;
  
  import android.app.Activity;
  import android.app.Dialog;
  import android.content.Context;
  import android.content.res.Configuration;
  import android.content.res.Resources;
  import android.content.res.TypedArray;
  import android.graphics.drawable.Drawable;
  import android.os.Build;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.view.ViewCompat;
  import android.support.v4.view.ViewPropertyAnimatorCompat;
  import android.support.v4.view.ViewPropertyAnimatorListener;
  import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
  import android.support.v4.view.ViewPropertyAnimatorUpdateListener;
  import android.support.v7.app.ActionBar;
  import android.support.v7.appcompat.R;
  import android.support.v7.internal.view.ActionBarPolicy;
  import android.support.v7.internal.view.SupportMenuInflater;
  import android.support.v7.internal.view.ViewPropertyAnimatorCompatSet;
  import android.support.v7.internal.view.menu.MenuBuilder;
  import android.support.v7.internal.view.menu.MenuPopupHelper;
  import android.support.v7.internal.view.menu.SubMenuBuilder;
  import android.support.v7.internal.widget.ActionBarContainer;
  import android.support.v7.internal.widget.ActionBarContextView;
  import android.support.v7.internal.widget.ActionBarOverlayLayout;
  import android.support.v7.internal.widget.DecorToolbar;
  import android.support.v7.internal.widget.ScrollingTabContainerView;
  import android.support.v7.internal.widget.TintManager;
  import android.support.v7.view.ActionMode;
  import android.support.v7.widget.Toolbar;
  import android.util.TypedValue;
  import android.view.ContextThemeWrapper;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewParent;
  import android.view.Window;
  import android.view.accessibility.AccessibilityEvent;
  import android.view.animation.AccelerateInterpolator;
  import android.view.animation.AnimationUtils;
  import android.view.animation.DecelerateInterpolator;
  import android.view.animation.Interpolator;
  import android.widget.SpinnerAdapter;
  
  import java.lang.ref.WeakReference;
  import java.util.ArrayList;
  
  /**
   * WindowDecorActionBar is the ActionBar implementation used
   * by devices of all screen sizes as part of the window decor layout.
   *
   * @hide
   */
  public class WindowDecorActionBar extends ActionBar implements
          ActionBarOverlayLayout.ActionBarVisibilityCallback {
      private static final String TAG = "WindowDecorActionBar";
  
      private static final Interpolator sHideInterpolator = new AccelerateInterpolator();
      private static final Interpolator sShowInterpolator = new DecelerateInterpolator();
  
      /**
       * Only allow show/hide animations on ICS+, as that is what ViewPropertyAnimatorCompat supports
       */
      private static final boolean ALLOW_SHOW_HIDE_ANIMATIONS = Build.VERSION.SDK_INT >= 14;
  
      private Context mContext;
      private Context mThemedContext;
      private Activity mActivity;
      private Dialog mDialog;
  
      private ActionBarOverlayLayout mOverlayLayout;
      private ActionBarContainer mContainerView;
      private DecorToolbar mDecorToolbar;
      private ActionBarContextView mContextView;
      private View mContentView;
      private ScrollingTabContainerView mTabScrollView;
  
      private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
  
      private TabImpl mSelectedTab;
      private int mSavedTabPosition = INVALID_POSITION;
  
      private boolean mDisplayHomeAsUpSet;
  
      ActionModeImpl mActionMode;
      ActionMode mDeferredDestroyActionMode;
      ActionMode.Callback mDeferredModeDestroyCallback;
  
      private boolean mLastMenuVisibility;
      private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
              new ArrayList<OnMenuVisibilityListener>();
  
      private static final int INVALID_POSITION = -1;
  
      // The fade duration for toolbar and action bar when entering/exiting action mode.
      private static final long FADE_OUT_DURATION_MS = 100;
      private static final long FADE_IN_DURATION_MS = 200;
  
      private boolean mHasEmbeddedTabs;
  
      private int mCurWindowVisibility = View.VISIBLE;
  
      private boolean mContentAnimations = true;
      private boolean mHiddenByApp;
      private boolean mHiddenBySystem;
      private boolean mShowingForMode;
  
      private boolean mNowShowing = true;
  
      private ViewPropertyAnimatorCompatSet mCurrentShowAnim;
      private boolean mShowHideAnimationEnabled;
      boolean mHideOnContentScroll;
  
      private TintManager mTintManager;
  
      final ViewPropertyAnimatorListener mHideListener = new ViewPropertyAnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(View view) {
              if (mContentAnimations && mContentView != null) {
                  ViewCompat.setTranslationY(mContentView, 0f);
                  ViewCompat.setTranslationY(mContainerView, 0f);
              }
              mContainerView.setVisibility(View.GONE);
              mContainerView.setTransitioning(false);
              mCurrentShowAnim = null;
              completeDeferredDestroyActionMode();
              if (mOverlayLayout != null) {
                  ViewCompat.requestApplyInsets(mOverlayLayout);
              }
          }
      };
  
      final ViewPropertyAnimatorListener mShowListener = new ViewPropertyAnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(View view) {
              mCurrentShowAnim = null;
              mContainerView.requestLayout();
          }
      };
  
      final ViewPropertyAnimatorUpdateListener mUpdateListener =
              new ViewPropertyAnimatorUpdateListener() {
                  @Override
                  public void onAnimationUpdate(View view) {
                      final ViewParent parent = mContainerView.getParent();
                      ((View) parent).invalidate();
                  }
              };
  
      public WindowDecorActionBar(Activity activity, boolean overlayMode) {
          mActivity = activity;
          Window window = activity.getWindow();
          View decor = window.getDecorView();
          init(decor);
          if (!overlayMode) {
              mContentView = decor.findViewById(android.R.id.content);
          }
      }
  
      public WindowDecorActionBar(Dialog dialog) {
          mDialog = dialog;
          init(dialog.getWindow().getDecorView());
      }
  
      /**
       * Only for edit mode.
       * @hide
       */
      public WindowDecorActionBar(View layout) {
          assert layout.isInEditMode();
          init(layout);
      }
  
      private void init(View decor) {
          mOverlayLayout = (ActionBarOverlayLayout) decor.findViewById(R.id.decor_content_parent);
          if (mOverlayLayout != null) {
              mOverlayLayout.setActionBarVisibilityCallback(this);
          }
          mDecorToolbar = getDecorToolbar(decor.findViewById(R.id.action_bar));
          mContextView = (ActionBarContextView) decor.findViewById(
                  R.id.action_context_bar);
          mContainerView = (ActionBarContainer) decor.findViewById(
                  R.id.action_bar_container);
  
          if (mDecorToolbar == null || mContextView == null || mContainerView == null) {
              throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
                      "with a compatible window decor layout");
          }
  
          mContext = mDecorToolbar.getContext();
  
          // This was initially read from the action bar style
          final int current = mDecorToolbar.getDisplayOptions();
          final boolean homeAsUp = (current & DISPLAY_HOME_AS_UP) != 0;
          if (homeAsUp) {
              mDisplayHomeAsUpSet = true;
          }
  
          ActionBarPolicy abp = ActionBarPolicy.get(mContext);
          setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);
          setHasEmbeddedTabs(abp.hasEmbeddedTabs());
  
          final TypedArray a = mContext.obtainStyledAttributes(null,
                  R.styleable.ActionBar,
                  R.attr.actionBarStyle, 0);
          if (a.getBoolean(R.styleable.ActionBar_hideOnContentScroll, false)) {
              setHideOnContentScrollEnabled(true);
          }
          final int elevation = a.getDimensionPixelSize(R.styleable.ActionBar_elevation, 0);
          if (elevation != 0) {
              setElevation(elevation);
          }
          a.recycle();
      }
  
      private DecorToolbar getDecorToolbar(View view) {
          if (view instanceof DecorToolbar) {
              return (DecorToolbar) view;
          } else if (view instanceof Toolbar) {
              return ((Toolbar) view).getWrapper();
          } else {
              throw new IllegalStateException("Can't make a decor toolbar out of " +
                      view != null ? view.getClass().getSimpleName() : "null");
          }
      }
  
      @Override
      public void setElevation(float elevation) {
          ViewCompat.setElevation(mContainerView, elevation);
      }
  
      @Override
      public float getElevation() {
          return ViewCompat.getElevation(mContainerView);
      }
  
      public void onConfigurationChanged(Configuration newConfig) {
          setHasEmbeddedTabs(ActionBarPolicy.get(mContext).hasEmbeddedTabs());
      }
  
      private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
          mHasEmbeddedTabs = hasEmbeddedTabs;
          // Switch tab layout configuration if needed
          if (!mHasEmbeddedTabs) {
              mDecorToolbar.setEmbeddedTabView(null);
              mContainerView.setTabContainer(mTabScrollView);
          } else {
              mContainerView.setTabContainer(null);
              mDecorToolbar.setEmbeddedTabView(mTabScrollView);
          }
          final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
          if (mTabScrollView != null) {
              if (isInTabMode) {
                  mTabScrollView.setVisibility(View.VISIBLE);
                  if (mOverlayLayout != null) {
                      ViewCompat.requestApplyInsets(mOverlayLayout);
                  }
              } else {
                  mTabScrollView.setVisibility(View.GONE);
              }
          }
          mDecorToolbar.setCollapsible(!mHasEmbeddedTabs && isInTabMode);
          mOverlayLayout.setHasNonEmbeddedTabs(!mHasEmbeddedTabs && isInTabMode);
      }
  
      private void ensureTabsExist() {
          if (mTabScrollView != null) {
              return;
          }
  
          ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
  
          if (mHasEmbeddedTabs) {
              tabScroller.setVisibility(View.VISIBLE);
              mDecorToolbar.setEmbeddedTabView(tabScroller);
          } else {
              if (getNavigationMode() == NAVIGATION_MODE_TABS) {
                  tabScroller.setVisibility(View.VISIBLE);
                  if (mOverlayLayout != null) {
                      ViewCompat.requestApplyInsets(mOverlayLayout);
                  }
              } else {
                  tabScroller.setVisibility(View.GONE);
              }
              mContainerView.setTabContainer(tabScroller);
          }
          mTabScrollView = tabScroller;
      }
  
      void completeDeferredDestroyActionMode() {
          if (mDeferredModeDestroyCallback != null) {
              mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
              mDeferredDestroyActionMode = null;
              mDeferredModeDestroyCallback = null;
          }
      }
  
      public void onWindowVisibilityChanged(int visibility) {
          mCurWindowVisibility = visibility;
      }
  
      /**
       * Enables or disables animation between show/hide states.
       * If animation is disabled using this method, animations in progress
       * will be finished.
       *
       * @param enabled true to animate, false to not animate.
       */
      public void setShowHideAnimationEnabled(boolean enabled) {
          mShowHideAnimationEnabled = enabled;
          if (!enabled && mCurrentShowAnim != null) {
              mCurrentShowAnim.cancel();
          }
      }
  
      public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
          mMenuVisibilityListeners.add(listener);
      }
  
      public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
          mMenuVisibilityListeners.remove(listener);
      }
  
      public void dispatchMenuVisibilityChanged(boolean isVisible) {
          if (isVisible == mLastMenuVisibility) {
              return;
          }
          mLastMenuVisibility = isVisible;
  
          final int count = mMenuVisibilityListeners.size();
          for (int i = 0; i < count; i++) {
              mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
          }
      }
  
      @Override
      public void setCustomView(int resId) {
          setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId,
                  mDecorToolbar.getViewGroup(), false));
      }
  
      @Override
      public void setDisplayUseLogoEnabled(boolean useLogo) {
          setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
      }
  
      @Override
      public void setDisplayShowHomeEnabled(boolean showHome) {
          setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
      }
  
      @Override
      public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
          setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
      }
  
      @Override
      public void setDisplayShowTitleEnabled(boolean showTitle) {
          setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
      }
  
      @Override
      public void setDisplayShowCustomEnabled(boolean showCustom) {
          setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
      }
  
      @Override
      public void setHomeButtonEnabled(boolean enable) {
          mDecorToolbar.setHomeButtonEnabled(enable);
      }
  
      @Override
      public void setTitle(int resId) {
          setTitle(mContext.getString(resId));
      }
  
      @Override
      public void setSubtitle(int resId) {
          setSubtitle(mContext.getString(resId));
      }
  
      public void setSelectedNavigationItem(int position) {
          switch (mDecorToolbar.getNavigationMode()) {
              case NAVIGATION_MODE_TABS:
                  selectTab(mTabs.get(position));
                  break;
              case NAVIGATION_MODE_LIST:
                  mDecorToolbar.setDropdownSelectedPosition(position);
                  break;
              default:
                  throw new IllegalStateException(
                          "setSelectedNavigationIndex not valid for current navigation mode");
          }
      }
  
      public void removeAllTabs() {
          cleanupTabs();
      }
  
      private void cleanupTabs() {
          if (mSelectedTab != null) {
              selectTab(null);
          }
          mTabs.clear();
          if (mTabScrollView != null) {
              mTabScrollView.removeAllTabs();
          }
          mSavedTabPosition = INVALID_POSITION;
      }
  
      public void setTitle(CharSequence title) {
          mDecorToolbar.setTitle(title);
      }
  
      @Override
      public void setWindowTitle(CharSequence title) {
          mDecorToolbar.setWindowTitle(title);
      }
  
      public void setSubtitle(CharSequence subtitle) {
          mDecorToolbar.setSubtitle(subtitle);
      }
  
      public void setDisplayOptions(int options) {
          if ((options & DISPLAY_HOME_AS_UP) != 0) {
              mDisplayHomeAsUpSet = true;
          }
          mDecorToolbar.setDisplayOptions(options);
      }
  
      public void setDisplayOptions(int options, int mask) {
          final int current = mDecorToolbar.getDisplayOptions();
          if ((mask & DISPLAY_HOME_AS_UP) != 0) {
              mDisplayHomeAsUpSet = true;
          }
          mDecorToolbar.setDisplayOptions((options & mask) | (current & ~mask));
      }
  
      public void setBackgroundDrawable(Drawable d) {
          mContainerView.setPrimaryBackground(d);
      }
  
      public void setStackedBackgroundDrawable(Drawable d) {
          mContainerView.setStackedBackground(d);
      }
  
      public void setSplitBackgroundDrawable(Drawable d) {
          // no-op. We don't support split action bars
      }
  
      public View getCustomView() {
          return mDecorToolbar.getCustomView();
      }
  
      public CharSequence getTitle() {
          return mDecorToolbar.getTitle();
      }
  
      public CharSequence getSubtitle() {
          return mDecorToolbar.getSubtitle();
      }
  
      public int getNavigationMode() {
          return mDecorToolbar.getNavigationMode();
      }
  
      public int getDisplayOptions() {
          return mDecorToolbar.getDisplayOptions();
      }
  
      public ActionMode startActionMode(ActionMode.Callback callback) {
          if (mActionMode != null) {
              mActionMode.finish();
          }
  
          mOverlayLayout.setHideOnContentScrollEnabled(false);
          mContextView.killMode();
          ActionModeImpl mode = new ActionModeImpl(mContextView.getContext(), callback);
          if (mode.dispatchOnCreate()) {
              mode.invalidate();
              mContextView.initForMode(mode);
              animateToMode(true);
              mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
              mActionMode = mode;
              return mode;
          }
          return null;
      }
  
      private void configureTab(Tab tab, int position) {
          final TabImpl tabi = (TabImpl) tab;
          final ActionBar.TabListener callback = tabi.getCallback();
  
          if (callback == null) {
              throw new IllegalStateException("Action Bar Tab must have a Callback");
          }
  
          tabi.setPosition(position);
          mTabs.add(position, tabi);
  
          final int count = mTabs.size();
          for (int i = position + 1; i < count; i++) {
              mTabs.get(i).setPosition(i);
          }
      }
  
      @Override
      public void addTab(Tab tab) {
          addTab(tab, mTabs.isEmpty());
      }
  
      @Override
      public void addTab(Tab tab, int position) {
          addTab(tab, position, mTabs.isEmpty());
      }
  
      @Override
      public void addTab(Tab tab, boolean setSelected) {
          ensureTabsExist();
          mTabScrollView.addTab(tab, setSelected);
          configureTab(tab, mTabs.size());
          if (setSelected) {
              selectTab(tab);
          }
      }
  
      @Override
      public void addTab(Tab tab, int position, boolean setSelected) {
          ensureTabsExist();
          mTabScrollView.addTab(tab, position, setSelected);
          configureTab(tab, position);
          if (setSelected) {
              selectTab(tab);
          }
      }
  
      @Override
      public Tab newTab() {
          return new TabImpl();
      }
  
      @Override
      public void removeTab(Tab tab) {
          removeTabAt(tab.getPosition());
      }
  
      @Override
      public void removeTabAt(int position) {
          if (mTabScrollView == null) {
              // No tabs around to remove
              return;
          }
  
          int selectedTabPosition = mSelectedTab != null
                  ? mSelectedTab.getPosition() : mSavedTabPosition;
          mTabScrollView.removeTabAt(position);
          TabImpl removedTab = mTabs.remove(position);
          if (removedTab != null) {
              removedTab.setPosition(-1);
          }
  
          final int newTabCount = mTabs.size();
          for (int i = position; i < newTabCount; i++) {
              mTabs.get(i).setPosition(i);
          }
  
          if (selectedTabPosition == position) {
              selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
          }
      }
  
      @Override
      public void selectTab(Tab tab) {
          if (getNavigationMode() != NAVIGATION_MODE_TABS) {
              mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
              return;
          }
  
          final FragmentTransaction trans;
          if (mActivity instanceof FragmentActivity && !mDecorToolbar.getViewGroup().isInEditMode()) {
              // If we're not in edit mode and our Activity is a FragmentActivity, start a tx
              trans = ((FragmentActivity) mActivity).getSupportFragmentManager()
                      .beginTransaction().disallowAddToBackStack();
          } else {
              trans = null;
          }
  
          if (mSelectedTab == tab) {
              if (mSelectedTab != null) {
                  mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
                  mTabScrollView.animateToTab(tab.getPosition());
              }
          } else {
              mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
              if (mSelectedTab != null) {
                  mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
              }
              mSelectedTab = (TabImpl) tab;
              if (mSelectedTab != null) {
                  mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
              }
          }
  
          if (trans != null && !trans.isEmpty()) {
              trans.commit();
          }
      }
  
      @Override
      public Tab getSelectedTab() {
          return mSelectedTab;
      }
  
      @Override
      public int getHeight() {
          return mContainerView.getHeight();
      }
  
      public void enableContentAnimations(boolean enabled) {
          mContentAnimations = enabled;
      }
  
      @Override
      public void show() {
          if (mHiddenByApp) {
              mHiddenByApp = false;
              updateVisibility(false);
          }
      }
  
      private void showForActionMode() {
          if (!mShowingForMode) {
              mShowingForMode = true;
              if (mOverlayLayout != null) {
                  mOverlayLayout.setShowingForActionMode(true);
              }
              updateVisibility(false);
          }
      }
  
      public void showForSystem() {
          if (mHiddenBySystem) {
              mHiddenBySystem = false;
              updateVisibility(true);
          }
      }
  
      @Override
      public void hide() {
  