114bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta/*
214bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project
314bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta *
414bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * you may not use this file except in compliance with the License.
614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * You may obtain a copy of the License at
714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta *
814bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
914bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta *
1014bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
1114bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
1214bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * See the License for the specific language governing permissions and
1414bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta * limitations under the License.
1514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta */
1614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
1714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptapackage com.android.layoutlib.bridge.bars;
1814bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
19796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Guptaimport com.android.ide.common.rendering.api.RenderResources;
2014bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.ide.common.rendering.api.ResourceValue;
2114bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.ide.common.rendering.api.SessionParams;
22929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.R;
2314bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.internal.view.menu.MenuBuilder;
24929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.view.menu.MenuItemImpl;
2514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.layoutlib.bridge.android.BridgeContext;
2614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.layoutlib.bridge.impl.ResourceHelper;
2714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
28476e582d2ffdf25102d4c55f8c242baa3d21d37fDeepanshu Guptaimport android.annotation.NonNull;
29476e582d2ffdf25102d4c55f8c242baa3d21d37fDeepanshu Guptaimport android.annotation.Nullable;
3014bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport android.content.Context;
31929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.content.res.TypedArray;
32929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.util.DisplayMetrics;
33929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.util.TypedValue;
34224e931fed1220f175805fdec71a882649270aa7Deepanshu Guptaimport android.view.InflateException;
35929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.view.View;
360bffc736e1f260724764d31f7499cbc09471a535Deepanshu Guptaimport android.view.View.MeasureSpec;
37929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.view.ViewGroup;
380bffc736e1f260724764d31f7499cbc09471a535Deepanshu Guptaimport android.view.ViewGroup.LayoutParams;
390bffc736e1f260724764d31f7499cbc09471a535Deepanshu Guptaimport android.widget.ActionMenuPresenter;
40929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.widget.FrameLayout;
41929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.widget.ListAdapter;
42929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.widget.ListView;
43929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.widget.RelativeLayout;
4414bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
4514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport java.util.ArrayList;
4614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
47224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta/**
48224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta * Creates the ActionBar as done by the framework.
49224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta */
50224e931fed1220f175805fdec71a882649270aa7Deepanshu Guptapublic class FrameworkActionBar extends BridgeActionBar {
510bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta
520bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    private static final String LAYOUT_ATTR_NAME = "windowActionBarFullscreenDecorLayout";
530bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta
540bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    // The Action Bar
55224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @NonNull private FrameworkActionBarWrapper mActionBar;
5614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
57929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    // A fake parent for measuring views.
58224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Nullable private ViewGroup mMeasureParent;
5914bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
60929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    /**
610bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta     * Inflate the action bar and attach it to {@code parentView}
62929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     */
63b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta    public FrameworkActionBar(@NonNull BridgeContext context, @NonNull SessionParams params) {
64b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta        super(context, params);
65929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
66224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        View decorContent = getDecorContent();
6714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
68224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar = FrameworkActionBarWrapper.getActionBarWrapper(context, getCallBack(),
69224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                decorContent);
7014bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
71b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta        FrameLayout contentRoot = (FrameLayout) decorContent.findViewById(android.R.id.content);
720bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta
730bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        // If something went wrong and we were not able to initialize the content root,
740bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        // just add a frame layout inside this and return.
750bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (contentRoot == null) {
760bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            contentRoot = new FrameLayout(context);
77796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            setMatchParent(contentRoot);
78b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta            if (mEnclosingLayout != null) {
79b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta                mEnclosingLayout.addView(contentRoot);
80b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta            }
81224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            setContentRoot(contentRoot);
820bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        } else {
83224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            setContentRoot(contentRoot);
84224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            setupActionBar();
850bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            mActionBar.inflateMenus();
8614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta        }
8714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta    }
88929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
89224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
90224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected ResourceValue getLayoutResource(BridgeContext context) {
91224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        ResourceValue layoutName =
92224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                context.getRenderResources().findItemInTheme(LAYOUT_ATTR_NAME, true);
93224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        if (layoutName != null) {
94224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            // We may need to resolve the reference obtained.
95224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            layoutName = context.getRenderResources().findResValue(layoutName.getValue(),
96224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                    layoutName.isFramework());
97224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        }
98224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        if (layoutName == null) {
99224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta             throw new InflateException("Unable to find action bar layout (" + LAYOUT_ATTR_NAME
100224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                    + ") in the current theme.");
101224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        }
102224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        return layoutName;
103224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
104224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
105224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
106224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setupActionBar() {
107224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        super.setupActionBar();
108224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setupActionBar();
109224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
110224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
111224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
112224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setHomeAsUp(boolean homeAsUp) {
113224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setHomeAsUp(homeAsUp);
114224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
115224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
116224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
117224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setTitle(CharSequence title) {
118224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setTitle(title);
119224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
120224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
121224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
122224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setSubtitle(CharSequence subtitle) {
123224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setSubTitle(subtitle);
124224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
125224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
126224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
127224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setIcon(String icon) {
128224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setIcon(icon);
129796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    }
130796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
131929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    /**
132929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     * Creates a Popup and adds it to the content frame. It also adds another {@link FrameLayout} to
133929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     * the content frame which shall serve as the new content root.
134929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     */
135224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
136929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public void createMenuPopup() {
137929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (!isOverflowPopupNeeded()) {
138929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            return;
139929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
140929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
141929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        DisplayMetrics metrics = mBridgeContext.getMetrics();
1420bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        MenuBuilder menu = mActionBar.getMenuBuilder();
1430bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        OverflowMenuAdapter adapter = new OverflowMenuAdapter(menu, mActionBar.getPopupContext());
144929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
145796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        ListView listView = new ListView(mActionBar.getPopupContext(), null,
146796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                R.attr.dropDownListViewStyle);
147929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
148929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                measureContentWidth(adapter), LayoutParams.WRAP_CONTENT);
149929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
1500bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (mActionBar.isSplit()) {
151929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
152796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            layoutParams.bottomMargin = getActionBarHeight() + mActionBar.getMenuPopupMargin();
153929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        } else {
154796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
155796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            layoutParams.topMargin = getActionBarHeight() + mActionBar.getMenuPopupMargin();
156929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
157796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        layoutParams.setMarginEnd(getPixelValue("5dp", metrics));
158796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setLayoutParams(layoutParams);
159796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setAdapter(adapter);
1600bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        final TypedArray a = mActionBar.getPopupContext().obtainStyledAttributes(null,
161929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                R.styleable.PopupWindow, R.attr.popupMenuStyle, 0);
162796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setBackground(a.getDrawable(R.styleable.PopupWindow_popupBackground));
163796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setDivider(a.getDrawable(R.attr.actionBarDivider));
164929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        a.recycle();
165796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setElevation(mActionBar.getMenuPopupElevation());
166b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta        assert mEnclosingLayout != null : "Unable to find view to attach ActionMenuPopup.";
167796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        mEnclosingLayout.addView(listView);
168929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
169929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
170929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private boolean isOverflowPopupNeeded() {
1710bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        boolean needed = mActionBar.isOverflowPopupNeeded();
172929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (!needed) {
173929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            return false;
174929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
175929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        // Copied from android.widget.ActionMenuPresenter.updateMenuView()
1760bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        ArrayList<MenuItemImpl> menus = mActionBar.getMenuBuilder().getNonActionItems();
1770bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        ActionMenuPresenter presenter = mActionBar.getActionMenuPresenter();
1780bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (presenter == null) {
1790bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            throw new RuntimeException("Failed to create a Presenter for Action Bar Menus.");
1800bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        }
1810bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (presenter.isOverflowReserved() &&
182929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                menus != null) {
183929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int count = menus.size();
184929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            if (count == 1) {
185929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                needed = !menus.get(0).isActionViewExpanded();
186929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            } else {
187929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                needed = count > 0;
188929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
189929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
190929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return needed;
191929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
192929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
193929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    // Copied from com.android.internal.view.menu.MenuPopHelper.measureContentWidth()
194929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private int measureContentWidth(@NonNull ListAdapter adapter) {
195929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        // Menus don't tend to be long, so this is more sane than it looks.
196929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        int maxWidth = 0;
197929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        View itemView = null;
198929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        int itemType = 0;
199929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
2000bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        Context context = mActionBar.getPopupContext();
201929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
202929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
203929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final int count = adapter.getCount();
204929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        for (int i = 0; i < count; i++) {
205929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int positionType = adapter.getItemViewType(i);
206929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            if (positionType != itemType) {
207929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                itemType = positionType;
208929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                itemView = null;
209929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
210929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
211929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            if (mMeasureParent == null) {
2120bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta                mMeasureParent = new FrameLayout(context);
213929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
214929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
215929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            itemView = adapter.getView(i, itemView, mMeasureParent);
216929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            itemView.measure(widthMeasureSpec, heightMeasureSpec);
217929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
218929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int itemWidth = itemView.getMeasuredWidth();
2190bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            int popupMaxWidth = Math.max(mBridgeContext.getMetrics().widthPixels / 2,
2200bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta                    context.getResources().getDimensionPixelSize(R.dimen.config_prefDialogWidth));
2210bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            if (itemWidth >= popupMaxWidth) {
2220bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta                return popupMaxWidth;
223929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            } else if (itemWidth > maxWidth) {
224929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                maxWidth = itemWidth;
225929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
226929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
227929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
228929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return maxWidth;
229929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
230929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
231796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    static int getPixelValue(@NonNull String value, @NonNull DisplayMetrics metrics) {
232929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        TypedValue typedValue = ResourceHelper.getValue(null, value, false /*requireUnit*/);
233929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return (int) typedValue.getDimension(metrics);
234929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
235929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
236796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    // TODO: This is duplicated from RenderSessionImpl.
237796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    private int getActionBarHeight() {
238796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        RenderResources resources = mBridgeContext.getRenderResources();
239796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        DisplayMetrics metrics = mBridgeContext.getMetrics();
240796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        ResourceValue value = resources.findItemInTheme("actionBarSize", true);
241796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
242796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        // resolve it
243796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        value = resources.resolveResValue(value);
244796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
245796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        if (value != null) {
246796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            // get the numerical value, if available
247796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            TypedValue typedValue = ResourceHelper.getValue("actionBarSize", value.getValue(),
248796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                    true);
249796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            if (typedValue != null) {
250796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                // compute the pixel value based on the display metrics
251796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                return (int) typedValue.getDimension(metrics);
252796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
253796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            }
254796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        }
255796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        return 0;
256796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    }
25714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta}
258