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
19929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.annotations.NonNull;
20929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.annotations.Nullable;
21796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Guptaimport com.android.ide.common.rendering.api.RenderResources;
2214bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.ide.common.rendering.api.ResourceValue;
2314bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.ide.common.rendering.api.SessionParams;
24929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.R;
2514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.internal.view.menu.MenuBuilder;
26929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.view.menu.MenuItemImpl;
2714bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.layoutlib.bridge.android.BridgeContext;
2814bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Guptaimport com.android.layoutlib.bridge.impl.ResourceHelper;
2914bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
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     */
63224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    public FrameworkActionBar(@NonNull BridgeContext context, @NonNull SessionParams params,
640bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            @NonNull ViewGroup parentView) {
65224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        super(context, params, parentView);
66929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
67224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        View decorContent = getDecorContent();
6814bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
69224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar = FrameworkActionBarWrapper.getActionBarWrapper(context, getCallBack(),
70224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                decorContent);
7114bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta
72796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        FrameLayout contentRoot = (FrameLayout) mEnclosingLayout.findViewById(android.R.id.content);
730bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta
740bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        // If something went wrong and we were not able to initialize the content root,
750bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        // just add a frame layout inside this and return.
760bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (contentRoot == null) {
770bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            contentRoot = new FrameLayout(context);
78796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            setMatchParent(contentRoot);
79796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            mEnclosingLayout.addView(contentRoot);
80224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            setContentRoot(contentRoot);
810bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        } else {
82224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            setContentRoot(contentRoot);
83224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            setupActionBar();
840bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            mActionBar.inflateMenus();
8514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta        }
8614bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta    }
87929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
88224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
89224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected ResourceValue getLayoutResource(BridgeContext context) {
90224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        ResourceValue layoutName =
91224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                context.getRenderResources().findItemInTheme(LAYOUT_ATTR_NAME, true);
92224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        if (layoutName != null) {
93224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            // We may need to resolve the reference obtained.
94224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta            layoutName = context.getRenderResources().findResValue(layoutName.getValue(),
95224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                    layoutName.isFramework());
96224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        }
97224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        if (layoutName == null) {
98224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta             throw new InflateException("Unable to find action bar layout (" + LAYOUT_ATTR_NAME
99224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta                    + ") in the current theme.");
100224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        }
101224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        return layoutName;
102224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
103224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
104224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
105224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setupActionBar() {
106224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        super.setupActionBar();
107224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setupActionBar();
108224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
109224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
110224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
111224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setHomeAsUp(boolean homeAsUp) {
112224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setHomeAsUp(homeAsUp);
113224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
114224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
115224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
116224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setTitle(CharSequence title) {
117224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setTitle(title);
118224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
119224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
120224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
121224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setSubtitle(CharSequence subtitle) {
122224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setSubTitle(subtitle);
123224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    }
124224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta
125224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
126224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    protected void setIcon(String icon) {
127224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta        mActionBar.setIcon(icon);
128796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    }
129796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
130929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    /**
131929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     * Creates a Popup and adds it to the content frame. It also adds another {@link FrameLayout} to
132929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     * the content frame which shall serve as the new content root.
133929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta     */
134224e931fed1220f175805fdec71a882649270aa7Deepanshu Gupta    @Override
135929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public void createMenuPopup() {
136929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (!isOverflowPopupNeeded()) {
137929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            return;
138929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
139929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
140929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        DisplayMetrics metrics = mBridgeContext.getMetrics();
1410bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        MenuBuilder menu = mActionBar.getMenuBuilder();
1420bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        OverflowMenuAdapter adapter = new OverflowMenuAdapter(menu, mActionBar.getPopupContext());
143929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
144796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        ListView listView = new ListView(mActionBar.getPopupContext(), null,
145796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                R.attr.dropDownListViewStyle);
146929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
147929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                measureContentWidth(adapter), LayoutParams.WRAP_CONTENT);
148929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
1490bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (mActionBar.isSplit()) {
150929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
151796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            layoutParams.bottomMargin = getActionBarHeight() + mActionBar.getMenuPopupMargin();
152929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        } else {
153796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
154796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            layoutParams.topMargin = getActionBarHeight() + mActionBar.getMenuPopupMargin();
155929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
156796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        layoutParams.setMarginEnd(getPixelValue("5dp", metrics));
157796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setLayoutParams(layoutParams);
158796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setAdapter(adapter);
1590bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        final TypedArray a = mActionBar.getPopupContext().obtainStyledAttributes(null,
160929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                R.styleable.PopupWindow, R.attr.popupMenuStyle, 0);
161796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setBackground(a.getDrawable(R.styleable.PopupWindow_popupBackground));
162796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setDivider(a.getDrawable(R.attr.actionBarDivider));
163929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        a.recycle();
164796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        listView.setElevation(mActionBar.getMenuPopupElevation());
165796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        mEnclosingLayout.addView(listView);
166929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
167929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
168929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private boolean isOverflowPopupNeeded() {
1690bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        boolean needed = mActionBar.isOverflowPopupNeeded();
170929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (!needed) {
171929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            return false;
172929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
173929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        // Copied from android.widget.ActionMenuPresenter.updateMenuView()
1740bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        ArrayList<MenuItemImpl> menus = mActionBar.getMenuBuilder().getNonActionItems();
1750bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        ActionMenuPresenter presenter = mActionBar.getActionMenuPresenter();
1760bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (presenter == null) {
1770bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            throw new RuntimeException("Failed to create a Presenter for Action Bar Menus.");
1780bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        }
1790bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        if (presenter.isOverflowReserved() &&
180929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                menus != null) {
181929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int count = menus.size();
182929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            if (count == 1) {
183929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                needed = !menus.get(0).isActionViewExpanded();
184929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            } else {
185929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                needed = count > 0;
186929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
187929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
188929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return needed;
189929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
190929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
191929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    // Copied from com.android.internal.view.menu.MenuPopHelper.measureContentWidth()
192929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private int measureContentWidth(@NonNull ListAdapter adapter) {
193929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        // Menus don't tend to be long, so this is more sane than it looks.
194929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        int maxWidth = 0;
195929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        View itemView = null;
196929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        int itemType = 0;
197929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
1980bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        Context context = mActionBar.getPopupContext();
199929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
200929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
201929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final int count = adapter.getCount();
202929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        for (int i = 0; i < count; i++) {
203929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int positionType = adapter.getItemViewType(i);
204929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            if (positionType != itemType) {
205929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                itemType = positionType;
206929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                itemView = null;
207929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
208929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
209929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            if (mMeasureParent == null) {
2100bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta                mMeasureParent = new FrameLayout(context);
211929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
212929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
213929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            itemView = adapter.getView(i, itemView, mMeasureParent);
214929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            itemView.measure(widthMeasureSpec, heightMeasureSpec);
215929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
216929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int itemWidth = itemView.getMeasuredWidth();
2170bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            int popupMaxWidth = Math.max(mBridgeContext.getMetrics().widthPixels / 2,
2180bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta                    context.getResources().getDimensionPixelSize(R.dimen.config_prefDialogWidth));
2190bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta            if (itemWidth >= popupMaxWidth) {
2200bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta                return popupMaxWidth;
221929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            } else if (itemWidth > maxWidth) {
222929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                maxWidth = itemWidth;
223929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
224929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
225929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
226929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return maxWidth;
227929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
228929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
229796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    static int getPixelValue(@NonNull String value, @NonNull DisplayMetrics metrics) {
230929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        TypedValue typedValue = ResourceHelper.getValue(null, value, false /*requireUnit*/);
231929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return (int) typedValue.getDimension(metrics);
232929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
233929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
234796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    // TODO: This is duplicated from RenderSessionImpl.
235796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    private int getActionBarHeight() {
236796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        RenderResources resources = mBridgeContext.getRenderResources();
237796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        DisplayMetrics metrics = mBridgeContext.getMetrics();
238796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        ResourceValue value = resources.findItemInTheme("actionBarSize", true);
239796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
240796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        // resolve it
241796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        value = resources.resolveResValue(value);
242796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
243796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        if (value != null) {
244796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            // get the numerical value, if available
245796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            TypedValue typedValue = ResourceHelper.getValue("actionBarSize", value.getValue(),
246796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                    true);
247796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            if (typedValue != null) {
248796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                // compute the pixel value based on the display metrics
249796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta                return (int) typedValue.getDimension(metrics);
250796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta
251796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta            }
252796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        }
253796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta        return 0;
254796e9b7f9910f2ba8133cdb6f137107585b2e5faDeepanshu Gupta    }
25514bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta}
256