ActionBarContextView.java revision 9146ac706265cd8cce66907e617bc8572152eb97
189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell/*
289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * Copyright (C) 2010 The Android Open Source Project
389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *
489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * you may not use this file except in compliance with the License.
689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * You may obtain a copy of the License at
789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *
889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *
1089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * Unless required by applicable law or agreed to in writing, software
1189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * See the License for the specific language governing permissions and
1489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * limitations under the License.
1589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell */
1689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellpackage com.android.internal.widget;
1789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
1889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport com.android.internal.R;
192c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powellimport com.android.internal.view.menu.ActionMenuView;
202c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powellimport com.android.internal.view.menu.MenuBuilder;
2189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.content.Context;
2389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.content.res.TypedArray;
2489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.graphics.drawable.Drawable;
2589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.util.AttributeSet;
266e34636749217654f43221885afb7a29bb5ca96aAdam Powellimport android.view.ActionMode;
2789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.LayoutInflater;
2889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.View;
2989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.ViewGroup;
30e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powellimport android.view.View.MeasureSpec;
3189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.widget.ImageButton;
320e94b5151d817e600a888448a662208b29b5ef46Adam Powellimport android.widget.LinearLayout;
3389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.widget.TextView;
3489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
3589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell/**
3689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * @hide
3789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell */
3889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellpublic class ActionBarContextView extends ViewGroup {
3989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int mItemPadding;
4089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int mItemMargin;
412c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell    private int mActionSpacing;
4289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int mContentHeight;
4389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
4489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private CharSequence mTitle;
4589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private CharSequence mSubtitle;
4689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
4789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private ImageButton mCloseButton;
4889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private View mCustomView;
490e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private LinearLayout mTitleLayout;
5089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private TextView mTitleView;
510e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private TextView mSubtitleView;
5289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private Drawable mCloseDrawable;
53b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell    private ActionMenuView mMenuView;
5489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
5589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context) {
5689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        this(context, null, 0);
5789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
5889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
5989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context, AttributeSet attrs) {
6089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        this(context, attrs, 0);
6189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
6289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
6389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
6489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        super(context, attrs, defStyle);
6589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
6689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
6789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                com.android.internal.R.styleable.Theme);
6889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mItemPadding = a.getDimensionPixelOffset(
6989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                com.android.internal.R.styleable.Theme_actionButtonPadding, 0);
7089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        setBackgroundDrawable(a.getDrawable(
715d27977f9da482627ceb19317a2cd70467aff046Adam Powell                com.android.internal.R.styleable.Theme_actionModeBackground));
7289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCloseDrawable = a.getDrawable(
735d27977f9da482627ceb19317a2cd70467aff046Adam Powell                com.android.internal.R.styleable.Theme_actionModeCloseDrawable);
7489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mItemMargin = mItemPadding / 2;
75dcdefbbff2bfecbfeb7b6459de130f376595c590Adam Powell
76e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        mContentHeight = a.getLayoutDimension(
77e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                com.android.internal.R.styleable.Theme_windowActionBarSize, 0);
7889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        a.recycle();
7989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
8089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
819146ac706265cd8cce66907e617bc8572152eb97Adam Powell    @Override
829146ac706265cd8cce66907e617bc8572152eb97Adam Powell    public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) {
839146ac706265cd8cce66907e617bc8572152eb97Adam Powell        // No starting an action mode for an existing action mode UI child! (Where would it go?)
849146ac706265cd8cce66907e617bc8572152eb97Adam Powell        return null;
859146ac706265cd8cce66907e617bc8572152eb97Adam Powell    }
869146ac706265cd8cce66907e617bc8572152eb97Adam Powell
87e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell    public void setHeight(int height) {
88e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        mContentHeight = height;
89e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell    }
90e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell
9189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void setCustomView(View view) {
9289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
9389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            removeView(mCustomView);
9489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
9589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCustomView = view;
960e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null) {
970e94b5151d817e600a888448a662208b29b5ef46Adam Powell            removeView(mTitleLayout);
980e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleLayout = null;
9989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
10089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (view != null) {
10189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            addView(view);
10289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
10389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        requestLayout();
10489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1050e94b5151d817e600a888448a662208b29b5ef46Adam Powell
10689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void setTitle(CharSequence title) {
10789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mTitle = title;
1080e94b5151d817e600a888448a662208b29b5ef46Adam Powell        initTitle();
1090e94b5151d817e600a888448a662208b29b5ef46Adam Powell    }
1100e94b5151d817e600a888448a662208b29b5ef46Adam Powell
1110e94b5151d817e600a888448a662208b29b5ef46Adam Powell    public void setSubtitle(CharSequence subtitle) {
1120e94b5151d817e600a888448a662208b29b5ef46Adam Powell        mSubtitle = subtitle;
1130e94b5151d817e600a888448a662208b29b5ef46Adam Powell        initTitle();
1140e94b5151d817e600a888448a662208b29b5ef46Adam Powell    }
1150e94b5151d817e600a888448a662208b29b5ef46Adam Powell
11629ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    public CharSequence getTitle() {
11729ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell        return mTitle;
11829ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    }
11929ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell
12029ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    public CharSequence getSubtitle() {
12129ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell        return mSubtitle;
12229ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    }
12329ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell
1240e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private void initTitle() {
1250e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout == null) {
12689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            LayoutInflater inflater = LayoutInflater.from(getContext());
1270e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleLayout = (LinearLayout) inflater.inflate(R.layout.action_bar_title_item, null);
1280e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
1290e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
1300e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mTitle != null) {
1310e94b5151d817e600a888448a662208b29b5ef46Adam Powell                mTitleView.setText(mTitle);
1320e94b5151d817e600a888448a662208b29b5ef46Adam Powell            }
1330e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mSubtitle != null) {
1340e94b5151d817e600a888448a662208b29b5ef46Adam Powell                mSubtitleView.setText(mSubtitle);
1358350f7dbc3a62211b2891f35911e4073d24c4cc5Adam Powell                mSubtitleView.setVisibility(VISIBLE);
13689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
1370e94b5151d817e600a888448a662208b29b5ef46Adam Powell            addView(mTitleLayout);
13889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        } else {
1390e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleView.setText(mTitle);
1400e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mSubtitleView.setText(mSubtitle);
1418350f7dbc3a62211b2891f35911e4073d24c4cc5Adam Powell            mSubtitleView.setVisibility(mSubtitle != null ? VISIBLE : GONE);
1420e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mTitleLayout.getParent() == null) {
1430e94b5151d817e600a888448a662208b29b5ef46Adam Powell                addView(mTitleLayout);
14489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
14589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
14689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1470e94b5151d817e600a888448a662208b29b5ef46Adam Powell
1486e34636749217654f43221885afb7a29bb5ca96aAdam Powell    public void initForMode(final ActionMode mode) {
14989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCloseButton == null) {
15089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            mCloseButton = new ImageButton(getContext());
15189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            mCloseButton.setImageDrawable(mCloseDrawable);
15289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            mCloseButton.setBackgroundDrawable(null);
15389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
154269b8bb0cbd78969450ea9ab255232d047580210Adam Powell        mCloseButton.setOnClickListener(new OnClickListener() {
155269b8bb0cbd78969450ea9ab255232d047580210Adam Powell            public void onClick(View v) {
156269b8bb0cbd78969450ea9ab255232d047580210Adam Powell                mode.finish();
157269b8bb0cbd78969450ea9ab255232d047580210Adam Powell            }
158269b8bb0cbd78969450ea9ab255232d047580210Adam Powell        });
15989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        addView(mCloseButton);
16089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
1612c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        final MenuBuilder menu = (MenuBuilder) mode.getMenu();
162b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView = (ActionMenuView) menu.getMenuView(MenuBuilder.TYPE_ACTION_BUTTON, this);
163b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView.setOverflowReserved(true);
164b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView.updateChildren(false);
1652c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        addView(mMenuView);
16689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1670e94b5151d817e600a888448a662208b29b5ef46Adam Powell
16889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void closeMode() {
16989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        removeAllViews();
17089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCustomView = null;
1712c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        mMenuView = null;
17289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1730e94b5151d817e600a888448a662208b29b5ef46Adam Powell
174f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    public boolean showOverflowMenu() {
175f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        if (mMenuView != null) {
176f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell            return mMenuView.showOverflowMenu();
177f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        }
178f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        return false;
179f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    }
180f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell
181f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    public boolean hideOverflowMenu() {
182f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        if (mMenuView != null) {
183f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell            return mMenuView.hideOverflowMenu();
184f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        }
185f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        return false;
186f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    }
187f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell
188f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    public boolean isOverflowMenuShowing() {
189f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        if (mMenuView != null) {
190f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell            return mMenuView.isOverflowMenuShowing();
191f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        }
192f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        return false;
193f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    }
194f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell
19589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    @Override
196a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    protected LayoutParams generateDefaultLayoutParams() {
197a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        // Used by custom views if they don't supply layout params. Everything else
198a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        // added to an ActionBarContextView should have them already.
199a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
200a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    }
201a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell
202a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    @Override
20389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
20489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
20589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (widthMode != MeasureSpec.EXACTLY) {
20689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
20789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    "with android:layout_width=\"match_parent\" (or fill_parent)");
20889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
20989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
21089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
21189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (heightMode != MeasureSpec.AT_MOST) {
21289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
21389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    "with android:layout_height=\"wrap_content\"");
21489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
21589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
21689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
21789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int itemMargin = mItemPadding;
21889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
219e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        int maxHeight = mContentHeight > 0 ?
220e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
221e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell
222e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        final int verticalPadding = getPaddingTop() + getPaddingBottom();
22389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
224e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        final int height = maxHeight - verticalPadding;
22589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
22689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
22789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCloseButton != null) {
22889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            availableWidth = measureChildView(mCloseButton, availableWidth,
22989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    childSpecHeight, itemMargin);
23089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
23189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2320e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null && mCustomView == null) {
2330e94b5151d817e600a888448a662208b29b5ef46Adam Powell            availableWidth = measureChildView(mTitleLayout, availableWidth,
23489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    childSpecHeight, itemMargin);
23589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
23689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
23789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int childCount = getChildCount();
23889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        for (int i = 0; i < childCount; i++) {
23989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            final View child = getChildAt(i);
2400e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (child == mCloseButton || child == mTitleLayout || child == mCustomView) {
24189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                continue;
24289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
24389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
24489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            availableWidth = measureChildView(child, availableWidth, childSpecHeight, itemMargin);
24589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
24689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
24789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
248a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            LayoutParams lp = mCustomView.getLayoutParams();
249a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
250a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
251a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customWidth = lp.width >= 0 ?
252a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    Math.min(lp.width, availableWidth) : availableWidth;
253a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
254a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
255a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customHeight = lp.height >= 0 ?
256a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    Math.min(lp.height, height) : height;
257a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
258a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
25989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
26089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
261e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        if (mContentHeight <= 0) {
262e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            int measuredHeight = 0;
263e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            final int count = getChildCount();
264e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            for (int i = 0; i < count; i++) {
265e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                View v = getChildAt(i);
266e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
267e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                if (paddedViewHeight > measuredHeight) {
268e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                    measuredHeight = paddedViewHeight;
269e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                }
270e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            }
271e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            setMeasuredDimension(contentWidth, measuredHeight);
272e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        } else {
273e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            setMeasuredDimension(contentWidth, maxHeight);
274e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        }
27589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
27689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
27789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    @Override
27889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
27989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int x = getPaddingLeft();
28089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int y = getPaddingTop();
28189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
28289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int itemMargin = mItemPadding;
28389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
28489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCloseButton != null && mCloseButton.getVisibility() != GONE) {
28589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            x += positionChild(mCloseButton, x, y, contentHeight);
28689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
28789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2880e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null && mCustomView == null) {
2890e94b5151d817e600a888448a662208b29b5ef46Adam Powell            x += positionChild(mTitleLayout, x, y, contentHeight) + itemMargin;
29089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
29189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
29289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
29389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            x += positionChild(mCustomView, x, y, contentHeight) + itemMargin;
29489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
29589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
29689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        x = r - l - getPaddingRight();
29789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2982c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        if (mMenuView != null) {
2992c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell            x -= positionChildInverse(mMenuView, x + mActionSpacing, y, contentHeight)
3002c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell                    - mActionSpacing;
30189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
30289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
30389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
30489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int measureChildView(View child, int availableWidth, int childSpecHeight, int spacing) {
30589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        child.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
30689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                childSpecHeight);
30789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
30889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        availableWidth -= child.getMeasuredWidth();
30989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        availableWidth -= spacing;
31089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
31189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        return availableWidth;
31289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
31389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
31489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int positionChild(View child, int x, int y, int contentHeight) {
31589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childWidth = child.getMeasuredWidth();
31689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childHeight = child.getMeasuredHeight();
31789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childTop = y + (contentHeight - childHeight) / 2;
31889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
31989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        child.layout(x, childTop, x + childWidth, childTop + childHeight);
32089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
32189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        return childWidth;
32289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
32389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
32489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int positionChildInverse(View child, int x, int y, int contentHeight) {
32589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childWidth = child.getMeasuredWidth();
32689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childHeight = child.getMeasuredHeight();
32789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childTop = y + (contentHeight - childHeight) / 2;
32889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
32989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        child.layout(x - childWidth, childTop, x, childTop + childHeight);
33089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
33189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        return childWidth;
33289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
33389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell}
334