ActionBarContextView.java revision f16888f1e849b0bc0b9c17e5f833c4e2cd54c382
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.util.AttributeSet;
256e34636749217654f43221885afb7a29bb5ca96aAdam Powellimport android.view.ActionMode;
2689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.LayoutInflater;
2789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.View;
2889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.ViewGroup;
29f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powellimport android.widget.Button;
30f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powellimport android.widget.ButtonGroup;
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 mContentHeight;
4089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
4189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private CharSequence mTitle;
4289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private CharSequence mSubtitle;
43f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell
44f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell    private View mClose;
4589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private View mCustomView;
460e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private LinearLayout mTitleLayout;
4789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private TextView mTitleView;
480e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private TextView mSubtitleView;
49bc234a19f4c74116454e2c77f4739290e761995aAdam Powell    private int mTitleStyleRes;
50bc234a19f4c74116454e2c77f4739290e761995aAdam Powell    private int mSubtitleStyleRes;
51b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell    private ActionMenuView mMenuView;
5289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
5389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context) {
54bc234a19f4c74116454e2c77f4739290e761995aAdam Powell        this(context, null);
5589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
5689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
5789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context, AttributeSet attrs) {
58bc234a19f4c74116454e2c77f4739290e761995aAdam Powell        this(context, attrs, com.android.internal.R.attr.actionModeStyle);
5989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
6089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
6189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
6289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        super(context, attrs, defStyle);
6389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
64bc234a19f4c74116454e2c77f4739290e761995aAdam Powell        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionMode, defStyle, 0);
6589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        setBackgroundDrawable(a.getDrawable(
66bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                com.android.internal.R.styleable.ActionMode_background));
67bc234a19f4c74116454e2c77f4739290e761995aAdam Powell        mTitleStyleRes = a.getResourceId(
68bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                com.android.internal.R.styleable.ActionMode_titleTextStyle, 0);
69bc234a19f4c74116454e2c77f4739290e761995aAdam Powell        mSubtitleStyleRes = a.getResourceId(
70bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0);
71dcdefbbff2bfecbfeb7b6459de130f376595c590Adam Powell
72e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        mContentHeight = a.getLayoutDimension(
73bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                com.android.internal.R.styleable.ActionMode_height, 0);
7489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        a.recycle();
7589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
7689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
779146ac706265cd8cce66907e617bc8572152eb97Adam Powell    @Override
789146ac706265cd8cce66907e617bc8572152eb97Adam Powell    public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) {
799146ac706265cd8cce66907e617bc8572152eb97Adam Powell        // No starting an action mode for an existing action mode UI child! (Where would it go?)
809146ac706265cd8cce66907e617bc8572152eb97Adam Powell        return null;
819146ac706265cd8cce66907e617bc8572152eb97Adam Powell    }
829146ac706265cd8cce66907e617bc8572152eb97Adam Powell
83e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell    public void setHeight(int height) {
84e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        mContentHeight = height;
85e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell    }
86e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell
8789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void setCustomView(View view) {
8889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
8989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            removeView(mCustomView);
9089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
9189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCustomView = view;
920e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null) {
930e94b5151d817e600a888448a662208b29b5ef46Adam Powell            removeView(mTitleLayout);
940e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleLayout = null;
9589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
9689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (view != null) {
9789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            addView(view);
9889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
9989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        requestLayout();
10089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1010e94b5151d817e600a888448a662208b29b5ef46Adam Powell
10289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void setTitle(CharSequence title) {
10389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mTitle = title;
1040e94b5151d817e600a888448a662208b29b5ef46Adam Powell        initTitle();
1050e94b5151d817e600a888448a662208b29b5ef46Adam Powell    }
1060e94b5151d817e600a888448a662208b29b5ef46Adam Powell
1070e94b5151d817e600a888448a662208b29b5ef46Adam Powell    public void setSubtitle(CharSequence subtitle) {
1080e94b5151d817e600a888448a662208b29b5ef46Adam Powell        mSubtitle = subtitle;
1090e94b5151d817e600a888448a662208b29b5ef46Adam Powell        initTitle();
1100e94b5151d817e600a888448a662208b29b5ef46Adam Powell    }
1110e94b5151d817e600a888448a662208b29b5ef46Adam Powell
11229ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    public CharSequence getTitle() {
11329ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell        return mTitle;
11429ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    }
11529ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell
11629ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    public CharSequence getSubtitle() {
11729ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell        return mSubtitle;
11829ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    }
11929ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell
1200e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private void initTitle() {
1210e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout == null) {
12289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            LayoutInflater inflater = LayoutInflater.from(getContext());
123f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            inflater.inflate(R.layout.action_bar_title_item, this);
124f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
1250e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
1260e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
1270e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mTitle != null) {
1280e94b5151d817e600a888448a662208b29b5ef46Adam Powell                mTitleView.setText(mTitle);
129bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                if (mTitleStyleRes != 0) {
130bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                    mTitleView.setTextAppearance(mContext, mTitleStyleRes);
131bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                }
1320e94b5151d817e600a888448a662208b29b5ef46Adam Powell            }
1330e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mSubtitle != null) {
1340e94b5151d817e600a888448a662208b29b5ef46Adam Powell                mSubtitleView.setText(mSubtitle);
135bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                if (mSubtitleStyleRes != 0) {
136bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                    mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
137bc234a19f4c74116454e2c77f4739290e761995aAdam Powell                }
1388350f7dbc3a62211b2891f35911e4073d24c4cc5Adam Powell                mSubtitleView.setVisibility(VISIBLE);
13989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
14089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        } else {
1410e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleView.setText(mTitle);
1420e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mSubtitleView.setText(mSubtitle);
1438350f7dbc3a62211b2891f35911e4073d24c4cc5Adam Powell            mSubtitleView.setVisibility(mSubtitle != null ? VISIBLE : GONE);
1440e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mTitleLayout.getParent() == null) {
1450e94b5151d817e600a888448a662208b29b5ef46Adam Powell                addView(mTitleLayout);
14689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
14789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
14889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1490e94b5151d817e600a888448a662208b29b5ef46Adam Powell
1506e34636749217654f43221885afb7a29bb5ca96aAdam Powell    public void initForMode(final ActionMode mode) {
151f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell        if (mClose == null) {
152f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            LayoutInflater inflater = LayoutInflater.from(mContext);
153f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            inflater.inflate(R.layout.action_mode_close_item, this);
154f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            mClose = getChildAt(getChildCount() - 1);
155f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell        } else {
156f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            addView(mClose);
15789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
158f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell
159f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell        View closeButton = mClose.findViewById(R.id.action_mode_close_button);
160f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell        closeButton.setOnClickListener(new OnClickListener() {
161269b8bb0cbd78969450ea9ab255232d047580210Adam Powell            public void onClick(View v) {
162269b8bb0cbd78969450ea9ab255232d047580210Adam Powell                mode.finish();
163269b8bb0cbd78969450ea9ab255232d047580210Adam Powell            }
164269b8bb0cbd78969450ea9ab255232d047580210Adam Powell        });
16589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
1662c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        final MenuBuilder menu = (MenuBuilder) mode.getMenu();
167b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView = (ActionMenuView) menu.getMenuView(MenuBuilder.TYPE_ACTION_BUTTON, this);
168b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView.setOverflowReserved(true);
169b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView.updateChildren(false);
1702c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        addView(mMenuView);
17189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1720e94b5151d817e600a888448a662208b29b5ef46Adam Powell
17389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void closeMode() {
17489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        removeAllViews();
17589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCustomView = null;
1762c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        mMenuView = null;
17789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1780e94b5151d817e600a888448a662208b29b5ef46Adam Powell
179f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    public boolean showOverflowMenu() {
180f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        if (mMenuView != null) {
181f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell            return mMenuView.showOverflowMenu();
182f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        }
183f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        return false;
184f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    }
185f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell
186f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    public boolean hideOverflowMenu() {
187f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        if (mMenuView != null) {
188f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell            return mMenuView.hideOverflowMenu();
189f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        }
190f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        return false;
191f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    }
192f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell
193f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    public boolean isOverflowMenuShowing() {
194f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        if (mMenuView != null) {
195f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell            return mMenuView.isOverflowMenuShowing();
196f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        }
197f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        return false;
198f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell    }
199f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell
20089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    @Override
201a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    protected LayoutParams generateDefaultLayoutParams() {
202a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        // Used by custom views if they don't supply layout params. Everything else
203a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        // added to an ActionBarContextView should have them already.
204a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
205a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    }
206a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell
207a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    @Override
20889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
20989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
21089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (widthMode != MeasureSpec.EXACTLY) {
21189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
21289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    "with android:layout_width=\"match_parent\" (or fill_parent)");
21389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
21489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
21589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
21689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (heightMode != MeasureSpec.AT_MOST) {
21789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
21889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    "with android:layout_height=\"wrap_content\"");
21989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
22089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
22189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
22289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
223e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        int maxHeight = mContentHeight > 0 ?
224e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
225e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell
226e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        final int verticalPadding = getPaddingTop() + getPaddingBottom();
22789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
228e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        final int height = maxHeight - verticalPadding;
22989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
23089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
231f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell        if (mClose != null) {
232f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
23389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
23489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2350e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null && mCustomView == null) {
236be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell            availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
23789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
23889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
23989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int childCount = getChildCount();
24089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        for (int i = 0; i < childCount; i++) {
24189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            final View child = getChildAt(i);
242f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            if (child == mClose || child == mTitleLayout || child == mCustomView) {
24389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                continue;
24489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
24589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
246be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell            availableWidth = measureChildView(child, availableWidth, childSpecHeight, 0);
24789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
24889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
24989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
250a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            LayoutParams lp = mCustomView.getLayoutParams();
251a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
252a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
253a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customWidth = lp.width >= 0 ?
254a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    Math.min(lp.width, availableWidth) : availableWidth;
255a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
256a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
257a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customHeight = lp.height >= 0 ?
258a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    Math.min(lp.height, height) : height;
259a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
260a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
26189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
26289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
263e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        if (mContentHeight <= 0) {
264e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            int measuredHeight = 0;
265e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            final int count = getChildCount();
266e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            for (int i = 0; i < count; i++) {
267e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                View v = getChildAt(i);
268e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
269e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                if (paddedViewHeight > measuredHeight) {
270e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                    measuredHeight = paddedViewHeight;
271e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell                }
272e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            }
273e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            setMeasuredDimension(contentWidth, measuredHeight);
274e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        } else {
275e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell            setMeasuredDimension(contentWidth, maxHeight);
276e2194445b078932733a2d1a02fc084ea2f3c7360Adam Powell        }
27789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
27889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
27989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    @Override
28089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
28189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int x = getPaddingLeft();
28289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int y = getPaddingTop();
28389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
28489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
285f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell        if (mClose != null && mClose.getVisibility() != GONE) {
286f16888f1e849b0bc0b9c17e5f833c4e2cd54c382Adam Powell            x += positionChild(mClose, x, y, contentHeight);
28789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
28889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2890e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null && mCustomView == null) {
290be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell            x += positionChild(mTitleLayout, x, y, contentHeight);
29189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
29289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
29389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
294be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell            x += positionChild(mCustomView, x, y, contentHeight);
29589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
29689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
29789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        x = r - l - getPaddingRight();
29889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2992c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        if (mMenuView != null) {
300be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell            x -= positionChildInverse(mMenuView, x, y, contentHeight);
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