Android Open Source - android-menudrawer Action Bar Helper Compat






From Project

Back to project page android-menudrawer.

License

The source code is released under:

Apache License

If you think the Android project android-menudrawer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.simonvt.menudrawer.compat;
//from ww  w  . ja va 2  s  .com
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.lang.reflect.Method;

final class ActionBarHelperCompat {

    private static final String TAG = "ActionBarHelperCompat";

    private ActionBarHelperCompat() {
    }

    public static void setActionBarUpIndicator(Object info, Activity activity, Drawable drawable, int contentDescRes) {
        final SetIndicatorInfo sii = (SetIndicatorInfo) info;
        if (sii.mUpIndicatorView != null) {
            sii.mUpIndicatorView.setImageDrawable(drawable);
            final String contentDescription = contentDescRes == 0 ? null : activity.getString(contentDescRes);
            sii.mUpIndicatorView.setContentDescription(contentDescription);
        }
    }

    public static void setActionBarDescription(Object info, Activity activity, int contentDescRes) {
        final SetIndicatorInfo sii = (SetIndicatorInfo) info;
        if (sii.mUpIndicatorView != null) {
            final String contentDescription = contentDescRes == 0 ? null : activity.getString(contentDescRes);
            sii.mUpIndicatorView.setContentDescription(contentDescription);
        }
    }

    public static Drawable getThemeUpIndicator(Object info) {
        final SetIndicatorInfo sii = (SetIndicatorInfo) info;
        if (sii.mUpIndicatorView != null) {
            return sii.mUpIndicatorView.getDrawable();
        }
        return null;
    }

    public static Object getIndicatorInfo(Activity activity) {
        return new SetIndicatorInfo(activity);
    }

    public static void setDisplayHomeAsUpEnabled(Object info, boolean enabled) {
        final SetIndicatorInfo sii = (SetIndicatorInfo) info;
        if (sii.mHomeAsUpEnabled != null) {
            try {
                sii.mHomeAsUpEnabled.invoke(sii.mActionBar, enabled);
            } catch (Throwable t) {
                if (ActionBarHelper.DEBUG) {
                    Log.e(TAG, "Unable to call setHomeAsUpEnabled", t);
                }
            }
        }
    }

    private static class SetIndicatorInfo {

        public ImageView mUpIndicatorView;
        public Object mActionBar;
        public Method mHomeAsUpEnabled;

        SetIndicatorInfo(Activity activity) {
            try {
                String appPackage = activity.getPackageName();

                try {
                    // Attempt to find ActionBarSherlock up indicator
                    final int homeId = activity.getResources().getIdentifier("abs__home", "id", appPackage);
                    View v = activity.findViewById(homeId);
                    ViewGroup parent = (ViewGroup) v.getParent();
                    final int upId = activity.getResources().getIdentifier("abs__up", "id", appPackage);
                    mUpIndicatorView = (ImageView) parent.findViewById(upId);
                } catch (Throwable t) {
                    if (ActionBarHelper.DEBUG) {
                        Log.e(TAG, "ABS action bar not found", t);
                    }
                }

                if (mUpIndicatorView == null) {
                    // Attempt to find AppCompat up indicator
                    final int homeId = activity.getResources().getIdentifier("home", "id", appPackage);
                    View v = activity.findViewById(homeId);
                    ViewGroup parent = (ViewGroup) v.getParent();
                    final int upId = activity.getResources().getIdentifier("up", "id", appPackage);
                    mUpIndicatorView = (ImageView) parent.findViewById(upId);
                }

                Class supportActivity = activity.getClass();
                Method getActionBar = supportActivity.getMethod("getSupportActionBar");

                mActionBar = getActionBar.invoke(activity, null);
                Class supportActionBar = mActionBar.getClass();
                mHomeAsUpEnabled = supportActionBar.getMethod("setDisplayHomeAsUpEnabled", Boolean.TYPE);

            } catch (Throwable t) {
                if (ActionBarHelper.DEBUG) {
                    Log.e(TAG, "Unable to init SetIndicatorInfo for ABS", t);
                }
            }
        }
    }
}




Java Source Code List

net.simonvt.menudrawer.BuildLayerFrameLayout.java
net.simonvt.menudrawer.ColorDrawable.java
net.simonvt.menudrawer.DraggableDrawer.java
net.simonvt.menudrawer.FloatScroller.java
net.simonvt.menudrawer.MenuDrawer.java
net.simonvt.menudrawer.NoClickThroughFrameLayout.java
net.simonvt.menudrawer.OverlayDrawer.java
net.simonvt.menudrawer.PeekInterpolator.java
net.simonvt.menudrawer.Position.java
net.simonvt.menudrawer.Scroller.java
net.simonvt.menudrawer.SinusoidalInterpolator.java
net.simonvt.menudrawer.SlideDrawable.java
net.simonvt.menudrawer.SlidingDrawer.java
net.simonvt.menudrawer.SmoothInterpolator.java
net.simonvt.menudrawer.StaticDrawer.java
net.simonvt.menudrawer.ViewHelper.java
net.simonvt.menudrawer.compat.ActionBarHelperCompat.java
net.simonvt.menudrawer.compat.ActionBarHelperNative.java
net.simonvt.menudrawer.compat.ActionBarHelper.java
net.simonvt.menudrawer.samples.ActionBarSherlockSample.java
net.simonvt.menudrawer.samples.BaseListSample.java
net.simonvt.menudrawer.samples.BottomDrawerSample.java
net.simonvt.menudrawer.samples.BottomOverlaySample.java
net.simonvt.menudrawer.samples.Category.java
net.simonvt.menudrawer.samples.FragmentSample.java
net.simonvt.menudrawer.samples.Item.java
net.simonvt.menudrawer.samples.LayoutSample.java
net.simonvt.menudrawer.samples.LeftDrawerSample.java
net.simonvt.menudrawer.samples.LeftOverlaySample.java
net.simonvt.menudrawer.samples.ListActivitySample.java
net.simonvt.menudrawer.samples.MenuAdapter.java
net.simonvt.menudrawer.samples.RightDrawerSample.java
net.simonvt.menudrawer.samples.RightOverlaySample.java
net.simonvt.menudrawer.samples.SamplesActivity.java
net.simonvt.menudrawer.samples.StaticDrawerSample.java
net.simonvt.menudrawer.samples.TopDrawerSample.java
net.simonvt.menudrawer.samples.TopOverlaySample.java
net.simonvt.menudrawer.samples.ViewPagerSample.java
net.simonvt.menudrawer.samples.WindowSample.java