Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright (C) 2010 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 com.actionbarsherlock.internal.app;
  
  import android.os.Build;
  import android.view.View;
  import static com.actionbarsherlock.internal.ResourcesCompat.getResources_getBoolean;

ActionBarImpl is the ActionBar implementation used by devices of all screen sizes. If it detects a compatible decor, it will split contextual modes across both the ActionBarView at the top of the screen and a horizontal LinearLayout at the bottom which is normally hidden.
  
  public class ActionBarImpl extends ActionBar {
      //UNUSED private static final String TAG = "ActionBarImpl";
  
      private Context mContext;
      private Context mThemedContext;
      private Activity mActivity;
      //UNUSED private Dialog mDialog;
  
      private ActionBarContainer mContainerView;
      private ActionBarView mActionView;
      private ActionBarContextView mContextView;
      private ActionBarContainer mSplitView;
      private NineFrameLayout mContentView;
  
      private ArrayList<TabImplmTabs = new ArrayList<TabImpl>();
  
      private TabImpl mSelectedTab;
      private int mSavedTabPosition = ;
  
  
      private boolean mLastMenuVisibility;
              new ArrayList<OnMenuVisibilityListener>();
  
      private static final int CONTEXT_DISPLAY_NORMAL = 0;
      private static final int CONTEXT_DISPLAY_SPLIT = 1;
  
      private static final int INVALID_POSITION = -1;
  
      private int mContextDisplayMode;
     private boolean mHasEmbeddedTabs;
 
     final Handler mHandler = new Handler();
 
     private Animator mCurrentShowAnim;
     private Animator mCurrentModeAnim;
     private boolean mShowHideAnimationEnabled;
     boolean mWasHiddenBeforeMode;
 
         @Override
         public void onAnimationEnd(Animator animation) {
             if ( != null) {
                 .setTranslationY(0);
                 .setTranslationY(0);
             }
             if ( != null &&  == ) {
                 .setVisibility(.);
             }
             .setVisibility(.);
             .setTransitioning(false);
              = null;
             completeDeferredDestroyActionMode();
         }
     };
 
         @Override
         public void onAnimationEnd(Animator animation) {
              = null;
             .requestLayout();
         }
     };
 
     public ActionBarImpl(Activity activityint features) {
          = activity;
         Window window = activity.getWindow();
         View decor = window.getDecorView();
         init(decor);
 
         //window.hasFeature() workaround for pre-3.0
         if ((features & (1 << .)) == 0) {
              = (NineFrameLayout)decor.findViewById(...);
         }
     }
 
     public ActionBarImpl(Dialog dialog) {
         //UNUSED mDialog = dialog;
         init(dialog.getWindow().getDecorView());
     }
 
     private void init(View decor) {
          = decor.getContext();
          = (ActionBarViewdecor.findViewById(..);
          = (ActionBarContextViewdecor.findViewById(
                 ..);
          = (ActionBarContainerdecor.findViewById(
                 ..);
          = (ActionBarContainerdecor.findViewById(
                 ..);
 
         if ( == null ||  == null ||  == null) {
             throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
                     "with a compatible window decor layout");
         }
 
                  : ;
 
         // Older apps get the home button interaction enabled by default.
         // Newer apps need to enable it explicitly.
 
                 ..));
     }
 
     public void onConfigurationChanged(Configuration newConfig) {
                 ..));
 
         //Manually dispatch a configuration change to the action bar view on pre-2.2
         if (.. < ..) {
             .onConfigurationChanged(newConfig);
             if ( != null) {
                 .onConfigurationChanged(newConfig);
             }
         }
     }
 
     private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
          = hasEmbeddedTabs;
         // Switch tab layout configuration if needed
         if (!) {
             .setEmbeddedTabView(null);
         } else {
             .setTabContainer(null);
         }
         final boolean isInTabMode = getNavigationMode() == ;
         if ( != null) {
             .setVisibility(isInTabMode ? . : .);
         }
         .setCollapsable(! && isInTabMode);
     }
 
     private void ensureTabsExist() {
         if ( != null) {
             return;
         }
 
         ScrollingTabContainerView tabScroller = new ScrollingTabContainerView();
 
         if () {
             tabScroller.setVisibility(.);
             .setEmbeddedTabView(tabScroller);
         } else {
             tabScroller.setVisibility(getNavigationMode() ==  ?
                     . : .);
             .setTabContainer(tabScroller);
         }
          = tabScroller;
     }
 
         if ( != null) {
              = null;
              = null;
         }
     }

    
Enables or disables animation between show/hide states. If animation is disabled using this method, animations in progress will be finished.

Parameters:
enabled true to animate, false to not animate.
 
     public void setShowHideAnimationEnabled(boolean enabled) {
          = enabled;
         if (!enabled &&  != null) {
             .end();
         }
     }
 
     public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
         .add(listener);
     }
 
     public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
         .remove(listener);
     }
 
     public void dispatchMenuVisibilityChanged(boolean isVisible) {
         if (isVisible == ) {
             return;
         }
          = isVisible;
 
         final int count = .size();
         for (int i = 0; i < counti++) {
             .get(i).onMenuVisibilityChanged(isVisible);
         }
     }
 
     @Override
     public void setCustomView(int resId) {
         setCustomView(LayoutInflater.from(getThemedContext()).inflate(resIdfalse));
     }
 
     @Override
     public void setDisplayUseLogoEnabled(boolean useLogo) {
         setDisplayOptions(useLogo ?  : 0, );
     }
 
     @Override
     public void setDisplayShowHomeEnabled(boolean showHome) {
         setDisplayOptions(showHome ?  : 0, );
     }
 
     @Override
     public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
         setDisplayOptions(showHomeAsUp ?  : 0, );
     }
 
     @Override
     public void setDisplayShowTitleEnabled(boolean showTitle) {
         setDisplayOptions(showTitle ?  : 0, );
     }
 
     @Override
     public void setDisplayShowCustomEnabled(boolean showCustom) {
         setDisplayOptions(showCustom ?  : 0, );
     }
 
     @Override
     public void setHomeButtonEnabled(boolean enable) {
         .setHomeButtonEnabled(enable);
     }
 
     @Override
     public void setTitle(int resId) {
         setTitle(.getString(resId));
     }
 
     @Override
     public void setSubtitle(int resId) {
         setSubtitle(.getString(resId));
     }
 
     public void setSelectedNavigationItem(int position) {
         switch (.getNavigationMode()) {
         case :
             selectTab(.get(position));
             break;
         case :
             .setDropdownSelectedPosition(position);
             break;
         default:
             throw new IllegalStateException(
                     "setSelectedNavigationIndex not valid for current navigation mode");
         }
     }
 
     public void removeAllTabs() {
         cleanupTabs();
     }
 
     private void cleanupTabs() {
         if ( != null) {
             selectTab(null);
         }
         .clear();
         if ( != null) {
             .removeAllTabs();
         }
          = ;
     }
 
     public void setTitle(CharSequence title) {
         .setTitle(title);
     }
 
     public void setSubtitle(CharSequence subtitle) {
         .setSubtitle(subtitle);
     }
 
     public void setDisplayOptions(int options) {
         .setDisplayOptions(options);
     }
 
     public void setDisplayOptions(int optionsint mask) {
         final int current = .getDisplayOptions();
         .setDisplayOptions((options & mask) | (current & ~mask));
     }
 
     public void setBackgroundDrawable(Drawable d) {
     }
 
     public void setStackedBackgroundDrawable(Drawable d) {
     }
 
     public void setSplitBackgroundDrawable(Drawable d) {
         if ( != null) {
             .setSplitBackground(d);
         }
     }
 
     public View getCustomView() {
         return .getCustomNavigationView();
     }
 
     public CharSequence getTitle() {
         return .getTitle();
     }
 
     public CharSequence getSubtitle() {
         return .getSubtitle();
     }
 
     public int getNavigationMode() {
         return .getNavigationMode();
     }
 
     public int getDisplayOptions() {
         return .getDisplayOptions();
     }
 
     public ActionMode startActionMode(ActionMode.Callback callback) {
         boolean wasHidden = false;
         if ( != null) {
             wasHidden = ;
             .finish();
         }
 
         .killMode();
         ActionModeImpl mode = new ActionModeImpl(callback);
         if (mode.dispatchOnCreate()) {
              = !isShowing() || wasHidden;
             mode.invalidate();
             .initForMode(mode);
             animateToMode(true);
             if ( != null &&  == ) {
                 // TODO animate this
                 .setVisibility(.);
             }
              = mode;
             return mode;
         }
         return null;
     }
 
     private void configureTab(Tab tabint position) {
         final TabImpl tabi = (TabImpltab;
         final ActionBar.TabListener callback = tabi.getCallback();
 
         if (callback == null) {
             throw new IllegalStateException("Action Bar Tab must have a Callback");
         }
 
         tabi.setPosition(position);
         .add(positiontabi);
 
         final int count = .size();
         for (int i = position + 1; i < counti++) {
             .get(i).setPosition(i);
         }
     }
 
     @Override
     public void addTab(Tab tab) {
         addTab(tab.isEmpty());
     }
 
     @Override
     public void addTab(Tab tabint position) {
         addTab(tabposition.isEmpty());
     }
 
     @Override
     public void addTab(Tab tabboolean setSelected) {
         ensureTabsExist();
         .addTab(tabsetSelected);
         configureTab(tab.size());
         if (setSelected) {
             selectTab(tab);
         }
     }
 
     @Override
     public void addTab(Tab tabint positionboolean setSelected) {
         ensureTabsExist();
         .addTab(tabpositionsetSelected);
         configureTab(tabposition);
         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 ( == null) {
             // No tabs around to remove
             return;
         }
 
         int selectedTabPosition =  != null
                 ? .getPosition() : ;
         .removeTabAt(position);
         TabImpl removedTab = .remove(position);
         if (removedTab != null) {
             removedTab.setPosition(-1);
         }
 
         final int newTabCount = .size();
         for (int i = positioni < newTabCounti++) {
             .get(i).setPosition(i);
         }
 
         if (selectedTabPosition == position) {
             selectTab(.isEmpty() ? null : .get(Math.max(0, position - 1)));
         }
     }
 
     @Override
     public void selectTab(Tab tab) {
         if (getNavigationMode() != ) {
              = tab != null ? tab.getPosition() : ;
             return;
         }
 
         FragmentTransaction trans = null;
         if ( instanceof FragmentActivity) {
             trans = ((FragmentActivity)).getSupportFragmentManager().beginTransaction()
                     .disallowAddToBackStack();
         }
 
         if ( == tab) {
             if ( != null) {
                 .getCallback().onTabReselected(trans);
                 .animateToTab(tab.getPosition());
             }
         } else {
             .setTabSelected(tab != null ? tab.getPosition() : .);
             if ( != null) {
                 .getCallback().onTabUnselected(trans);
             }
              = (TabImpltab;
             if ( != null) {
                 .getCallback().onTabSelected(trans);
             }
         }
 
         if (trans != null && !trans.isEmpty()) {
             trans.commit();
         }
     }
 
     @Override
     public Tab getSelectedTab() {
         return ;
     }
 
     @Override
     public int getHeight() {
         return .getHeight();
     }
 
     @Override
     public void show() {
         show(true);
     }
 
     void show(boolean markHiddenBeforeMode) {
         if ( != null) {
             .end();
         }
         if (.getVisibility() == .) {
             if (markHiddenBeforeMode = false;
             return;
         }
 
         if () {
             .setAlpha(0);
             AnimatorSet anim = new AnimatorSet();
             AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat("alpha", 1));
             if ( != null) {
                 b.with(ObjectAnimator.ofFloat("translationY",
                         -.getHeight(), 0));
                 .setTranslationY(-.getHeight());
                 b.with(ObjectAnimator.ofFloat("translationY", 0));
             }
             if ( != null &&  == ) {
                 .setAlpha(0);
                 .setVisibility(.);
                 b.with(ObjectAnimator.ofFloat("alpha", 1));
             }
             anim.addListener();
              = anim;
             anim.start();
         } else {
             .setAlpha(1);
             .setTranslationY(0);
             .onAnimationEnd(null);
         }
     }
 
     @Override
     public void hide() {
         if ( != null) {
             .end();
         }
         if (.getVisibility() == .) {
             return;
         }
 
         if () {
             .setAlpha(1);
             .setTransitioning(true);
             AnimatorSet anim = new AnimatorSet();
             AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat("alpha", 0));
             if ( != null) {
                 b.with(ObjectAnimator.ofFloat("translationY",
                         0, -.getHeight()));
                 b.with(ObjectAnimator.ofFloat("translationY",
                         -.getHeight()));
             }
             if ( != null && .getVisibility() == .) {
                 .setAlpha(1);
                 b.with(ObjectAnimator.ofFloat("alpha", 0));
             }
             anim.addListener();
              = anim;
             anim.start();
         } else {
             .onAnimationEnd(null);
         }
     }
 
     public boolean isShowing() {
         return .getVisibility() == .;
     }
 
     void animateToMode(boolean toActionMode) {
         if (toActionMode) {
             show(false);
         }
         if ( != null) {
             .end();
         }
 
         .animateToVisibility(toActionMode ? . : .);
         .animateToVisibility(toActionMode ? . : .);
         if ( != null && !.hasEmbeddedTabs() && .isCollapsed()) {
             .animateToVisibility(toActionMode ? . : .);
         }
     }
 
     public Context getThemedContext() {
         if ( == null) {
             TypedValue outValue = new TypedValue();
             Resources.Theme currentTheme = .getTheme();
             currentTheme.resolveAttribute(..,
                     outValuetrue);
             final int targetThemeRes = outValue.resourceId;
 
             if (targetThemeRes != 0) { //XXX && mContext.getThemeResId() != targetThemeRes) {
                  = new ContextThemeWrapper(targetThemeRes);
             } else {
                  = ;
             }
         }
         return ;
     }

    

Hide:
 
     public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
         private ActionMode.Callback mCallback;
         private MenuBuilder mMenu;
         private WeakReference<ViewmCustomView;
 
         public ActionModeImpl(ActionMode.Callback callback) {
              = callback;
              = new MenuBuilder(getThemedContext())
                     .setDefaultShowAsAction(.);
             .setCallback(this);
         }
 
         @Override
         public MenuInflater getMenuInflater() {
             return new MenuInflater(getThemedContext());
         }
 
         @Override
         public Menu getMenu() {
             return ;
         }
 
         @Override
         public void finish() {
             if ( != this) {
                 // Not the active action mode - no-op
                 return;
             }
 
             // If we were hidden before the mode was shown, defer the onDestroy
             // callback until the animation is finished and associated relayout
             // is about to happen. This lets apps better anticipate visibility
             // and layout behavior.
             if () {
                  = this;
                  = ;
             } else {
                 .onDestroyActionMode(this);
             }
              = null;
             animateToMode(false);
 
             // Clear out the context mode views after the animation finishes
             .closeMode();
 
              = null;
 
             if () {
                 hide();
             }
         }
 
         @Override
         public void invalidate() {
             .stopDispatchingItemsChanged();
             try {
                 .onPrepareActionMode(this);
             } finally {
                 .startDispatchingItemsChanged();
             }
         }
 
         public boolean dispatchOnCreate() {
             .stopDispatchingItemsChanged();
             try {
                 return .onCreateActionMode(this);
             } finally {
                 .startDispatchingItemsChanged();
             }
         }
 
         @Override
         public void setCustomView(View view) {
             .setCustomView(view);
              = new WeakReference<View>(view);
         }
 
         @Override
         public void setSubtitle(CharSequence subtitle) {
             .setSubtitle(subtitle);
         }
 
         @Override
         public void setTitle(CharSequence title) {
             .setTitle(title);
         }
 
         @Override
         public void setTitle(int resId) {
             setTitle(.getResources().getString(resId));
         }
 
         @Override
         public void setSubtitle(int resId) {
             setSubtitle(.getResources().getString(resId));
         }
 
         @Override
         public CharSequence getTitle() {
             return .getTitle();
         }
 
         @Override
         public CharSequence getSubtitle() {
             return .getSubtitle();
         }
 
         @Override
         public View getCustomView() {
             return  != null ? .get() : null;
         }
 
         public boolean onMenuItemSelected(MenuBuilder menuMenuItem item) {
             if ( != null) {
                 return .onActionItemClicked(thisitem);
             } else {
                 return false;
             }
         }
 
         public void onCloseMenu(MenuBuilder menuboolean allMenusAreClosing) {
         }
 
         public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
             if ( == null) {
                 return false;
             }
 
             if (!subMenu.hasVisibleItems()) {
                 return true;
             }
 
             new MenuPopupHelper(getThemedContext(), subMenu).show();
             return true;
         }
 
         public void onCloseSubMenu(SubMenuBuilder menu) {
         }
 
         public void onMenuModeChange(MenuBuilder menu) {
             if ( == null) {
                 return;
             }
             invalidate();
             .showOverflowMenu();
         }
     }

    

Hide:
 
     public class TabImpl extends ActionBar.Tab {
         private ActionBar.TabListener mCallback;
         private Object mTag;
         private Drawable mIcon;
         private CharSequence mText;
         private CharSequence mContentDesc;
         private int mPosition = -1;
         private View mCustomView;
 
         @Override
         public Object getTag() {
             return ;
         }
 
         @Override
         public Tab setTag(Object tag) {
              = tag;
             return this;
         }
 
         public ActionBar.TabListener getCallback() {
             return ;
         }
 
         @Override
         public Tab setTabListener(ActionBar.TabListener callback) {
              = callback;
             return this;
         }
 
         @Override
         public View getCustomView() {
             return ;
         }
 
         @Override
         public Tab setCustomView(View view) {
              = view;
             if ( >= 0) {
                 .updateTab();
             }
             return this;
         }
 
         @Override
         public Tab setCustomView(int layoutResId) {
             return setCustomView(LayoutInflater.from(getThemedContext())
                     .inflate(layoutResIdnull));
         }
 
         @Override
         public Drawable getIcon() {
             return ;
         }
 
         @Override
         public int getPosition() {
             return ;
         }
 
         public void setPosition(int position) {
              = position;
         }
 
         @Override
         public CharSequence getText() {
             return ;
         }
 
         @Override
         public Tab setIcon(Drawable icon) {
              = icon;
             if ( >= 0) {
                 .updateTab();
             }
             return this;
         }
 
         @Override
         public Tab setIcon(int resId) {
             return setIcon(.getResources().getDrawable(resId));
         }
 
         @Override
         public Tab setText(CharSequence text) {
              = text;
             if ( >= 0) {
                 .updateTab();
             }
             return this;
         }
 
         @Override
         public Tab setText(int resId) {
             return setText(.getResources().getText(resId));
         }
 
         @Override
         public void select() {
             selectTab(this);
         }
 
         @Override
         public Tab setContentDescription(int resId) {
             return setContentDescription(.getResources().getText(resId));
         }
 
         @Override
         public Tab setContentDescription(CharSequence contentDesc) {
              = contentDesc;
             if ( >= 0) {
                 .updateTab();
             }
             return this;
         }
 
         @Override
         public CharSequence getContentDescription() {
             return ;
         }
     }
 
     @Override
     public void setCustomView(View view) {
         .setCustomNavigationView(view);
     }
 
     @Override
     public void setCustomView(View viewLayoutParams layoutParams) {
         view.setLayoutParams(layoutParams);
         .setCustomNavigationView(view);
     }
 
     @Override
     public void setListNavigationCallbacks(SpinnerAdapter adapterOnNavigationListener callback) {
         .setDropdownAdapter(adapter);
         .setCallback(callback);
     }
 
     @Override
     public int getSelectedNavigationIndex() {
         switch (.getNavigationMode()) {
             case :
                 return  != null ? .getPosition() : -1;
             case :
                 return .getDropdownSelectedPosition();
             default:
                 return -1;
         }
     }
 
     @Override
     public int getNavigationItemCount() {
         switch (.getNavigationMode()) {
             case :
                 return .size();
             case :
                 SpinnerAdapter adapter = .getDropdownAdapter();
                 return adapter != null ? adapter.getCount() : 0;
             default:
                 return 0;
         }
     }