145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell/*
245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * Copyright (C) 2010 The Android Open Source Project
345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *
445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * you may not use this file except in compliance with the License.
645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * You may obtain a copy of the License at
745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *
845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *
1045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * Unless required by applicable law or agreed to in writing, software
1145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * See the License for the specific language governing permissions and
1445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * limitations under the License.
1545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell */
1645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
1745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellpackage com.android.internal.widget;
1845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
1977b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craikimport android.annotation.NonNull;
2045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.content.Context;
2145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.content.res.TypedArray;
22a72ef62e5c329a19eab9935acba816fc1369c637Adam Powellimport android.graphics.Canvas;
237f610fed107b158c144dca1b20a44ee91eb8c934Alan Viveretteimport android.graphics.ColorFilter;
2477b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craikimport android.graphics.Outline;
25f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powellimport android.graphics.PixelFormat;
26a72ef62e5c329a19eab9935acba816fc1369c637Adam Powellimport android.graphics.drawable.Drawable;
2745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.util.AttributeSet;
28640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powellimport android.view.ActionMode;
296ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powellimport android.view.MotionEvent;
30dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powellimport android.view.View;
31af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powellimport android.view.ViewGroup;
3245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.widget.FrameLayout;
3345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
3445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell/**
3545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * This class acts as a container for the action bar view and action mode context views.
3645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * It applies special styles as needed to help handle animated transitions between them.
3745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * @hide
3845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell */
3945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellpublic class ActionBarContainer extends FrameLayout {
4001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    private boolean mIsTransitioning;
41dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    private View mTabContainer;
42e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private View mActionBarView;
43ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase    private View mActionContextView;
4401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
45a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell    private Drawable mBackground;
46a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell    private Drawable mStackedBackground;
47a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell    private Drawable mSplitBackground;
48a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell    private boolean mIsSplit;
49a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell    private boolean mIsStacked;
50e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mHeight;
51a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
5245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    public ActionBarContainer(Context context) {
5345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        this(context, null);
5445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    }
5545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
5645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    public ActionBarContainer(Context context, AttributeSet attrs) {
5745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        super(context, attrs);
5845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
597f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        // Set a transparent background so that we project appropriately.
607f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        setBackground(new ActionBarBackgroundDrawable());
61a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
6245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
6345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell                com.android.internal.R.styleable.ActionBar);
64a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        mBackground = a.getDrawable(com.android.internal.R.styleable.ActionBar_background);
65a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        mStackedBackground = a.getDrawable(
66a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                com.android.internal.R.styleable.ActionBar_backgroundStacked);
67e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mHeight = a.getDimensionPixelSize(com.android.internal.R.styleable.ActionBar_height, -1);
68a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
69a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        if (getId() == com.android.internal.R.id.split_action_bar) {
70a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            mIsSplit = true;
71a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            mSplitBackground = a.getDrawable(
72a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                    com.android.internal.R.styleable.ActionBar_backgroundSplit);
73a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        }
7445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        a.recycle();
75a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
76a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        setWillNotDraw(mIsSplit ? mSplitBackground == null :
77a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                mBackground == null && mStackedBackground == null);
7845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    }
796ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell
80310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell    @Override
81310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell    public void onFinishInflate() {
82310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell        super.onFinishInflate();
83e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mActionBarView = findViewById(com.android.internal.R.id.action_bar);
84ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase        mActionContextView = findViewById(com.android.internal.R.id.action_context_bar);
85310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell    }
86310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell
87f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell    public void setPrimaryBackground(Drawable bg) {
88a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null) {
89a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mBackground.setCallback(null);
90a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            unscheduleDrawable(mBackground);
91a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
92f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell        mBackground = bg;
93a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (bg != null) {
94a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            bg.setCallback(this);
95e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell            if (mActionBarView != null) {
96e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell                mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(),
97e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell                        mActionBarView.getRight(), mActionBarView.getBottom());
98e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell            }
99a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
100a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        setWillNotDraw(mIsSplit ? mSplitBackground == null :
101a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell                mBackground == null && mStackedBackground == null);
102f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell        invalidate();
103f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell    }
104f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell
105f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell    public void setStackedBackground(Drawable bg) {
106a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null) {
107a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mStackedBackground.setCallback(null);
108a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            unscheduleDrawable(mStackedBackground);
109a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
110f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell        mStackedBackground = bg;
111a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (bg != null) {
112a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            bg.setCallback(this);
113e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell            if ((mIsStacked && mStackedBackground != null)) {
114e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell                mStackedBackground.setBounds(mTabContainer.getLeft(), mTabContainer.getTop(),
115e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell                        mTabContainer.getRight(), mTabContainer.getBottom());
116e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell            }
117a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
118a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        setWillNotDraw(mIsSplit ? mSplitBackground == null :
119a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell                mBackground == null && mStackedBackground == null);
120f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell        invalidate();
121f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell    }
122f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell
123f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell    public void setSplitBackground(Drawable bg) {
124a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null) {
125a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mSplitBackground.setCallback(null);
126a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            unscheduleDrawable(mSplitBackground);
127a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
128f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell        mSplitBackground = bg;
129a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (bg != null) {
130a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            bg.setCallback(this);
131e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell            if (mIsSplit && mSplitBackground != null) {
132e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell                mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
133e8c8ae401b6f0d894a766a1601f55f0fe567df02Adam Powell            }
134a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
135a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        setWillNotDraw(mIsSplit ? mSplitBackground == null :
136a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell                mBackground == null && mStackedBackground == null);
137f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell        invalidate();
138f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell    }
139f88b915567a37c481c4c50a6cc57e1ec0e7cf50dAdam Powell
140a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
141a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    public void setVisibility(int visibility) {
142a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        super.setVisibility(visibility);
143a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        final boolean isVisible = visibility == VISIBLE;
144a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null) mBackground.setVisible(isVisible, false);
145a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null) mStackedBackground.setVisible(isVisible, false);
146a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null) mSplitBackground.setVisible(isVisible, false);
147a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
148a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
149a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
150a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    protected boolean verifyDrawable(Drawable who) {
151a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        return (who == mBackground && !mIsSplit) || (who == mStackedBackground && mIsStacked) ||
152a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell                (who == mSplitBackground && mIsSplit) || super.verifyDrawable(who);
153a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
154a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
155a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
156a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    protected void drawableStateChanged() {
157a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        super.drawableStateChanged();
158a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null && mBackground.isStateful()) {
159a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mBackground.setState(getDrawableState());
160a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
161a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null && mStackedBackground.isStateful()) {
162a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mStackedBackground.setState(getDrawableState());
163a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
164a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null && mSplitBackground.isStateful()) {
165a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mSplitBackground.setState(getDrawableState());
166a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
167a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
168a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
169a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
170a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    public void jumpDrawablesToCurrentState() {
171a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        super.jumpDrawablesToCurrentState();
172a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null) {
173a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mBackground.jumpToCurrentState();
174a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
175a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null) {
176a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mStackedBackground.jumpToCurrentState();
177a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
178a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null) {
179a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mSplitBackground.jumpToCurrentState();
180a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
181a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
182a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
1834457e85a7090ad51726d50a4daf981d917cceeddFabrice Di Meglio    /**
1844457e85a7090ad51726d50a4daf981d917cceeddFabrice Di Meglio     * @hide
1854457e85a7090ad51726d50a4daf981d917cceeddFabrice Di Meglio     */
186a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
187a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    public void onResolveDrawables(int layoutDirection) {
188a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        super.onResolveDrawables(layoutDirection);
189a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null) {
190a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mBackground.setLayoutDirection(layoutDirection);
191a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
192a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null) {
193a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mStackedBackground.setLayoutDirection(layoutDirection);
194a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
195a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null) {
196a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mSplitBackground.setLayoutDirection(layoutDirection);
197a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
198a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
199a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
20001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    /**
20101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * Set the action bar into a "transitioning" state. While transitioning
20201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * the bar will block focus and touch from all of its descendants. This
20301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * prevents the user from interacting with the bar while it is animating
20401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * in or out.
20501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     *
20601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * @param isTransitioning true if the bar is currently transitioning, false otherwise.
20701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     */
20801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public void setTransitioning(boolean isTransitioning) {
20901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        mIsTransitioning = isTransitioning;
21001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS
21101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell                : FOCUS_AFTER_DESCENDANTS);
21201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
21301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
21401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    @Override
21501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public boolean onInterceptTouchEvent(MotionEvent ev) {
21601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        return mIsTransitioning || super.onInterceptTouchEvent(ev);
21701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
21801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
2196ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    @Override
2206ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    public boolean onTouchEvent(MotionEvent ev) {
2216ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        super.onTouchEvent(ev);
222dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
223dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        // An action bar always eats touch events.
2246ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        return true;
2256ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    }
226dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
2277d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell    @Override
2287d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell    public boolean onHoverEvent(MotionEvent ev) {
2297d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell        super.onHoverEvent(ev);
2307d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell
2317d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell        // An action bar always eats hover events.
2327d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell        return true;
2337d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell    }
2347d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell
235f5645cbafe7eed33452d888f16726bee8a0cd9feAdam Powell    public void setTabContainer(ScrollingTabContainerView tabView) {
236dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null) {
237dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            removeView(mTabContainer);
238dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
239dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        mTabContainer = tabView;
24025151e4792e9be8dbb892b7eba7349773b8127afAdam Powell        if (tabView != null) {
24125151e4792e9be8dbb892b7eba7349773b8127afAdam Powell            addView(tabView);
242af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell            final ViewGroup.LayoutParams lp = tabView.getLayoutParams();
243af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell            lp.width = LayoutParams.MATCH_PARENT;
244af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell            lp.height = LayoutParams.WRAP_CONTENT;
245f5645cbafe7eed33452d888f16726bee8a0cd9feAdam Powell            tabView.setAllowCollapse(false);
24625151e4792e9be8dbb892b7eba7349773b8127afAdam Powell        }
247dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
248dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
249dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public View getTabContainer() {
250dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        return mTabContainer;
251dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
252dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
253dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
2544423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri    public ActionMode startActionModeForChild(
2554423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri            View child, ActionMode.Callback callback, int type) {
2564423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        if (type != ActionMode.TYPE_PRIMARY) {
2574423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri            return super.startActionModeForChild(child, callback, type);
2584423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        }
2594423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        return null;
2604423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri    }
2614423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri
2621e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik    private static boolean isCollapsed(View view) {
263e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return view == null || view.getVisibility() == GONE || view.getMeasuredHeight() == 0;
264e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
265e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2662bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar    private int getMeasuredHeightWithMargins(View view) {
2672bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar        final LayoutParams lp = (LayoutParams) view.getLayoutParams();
2682bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar        return view.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
2692bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar    }
2702bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar
271640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell    @Override
272dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
273e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mActionBarView == null &&
274e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST && mHeight >= 0) {
275e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
276e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    Math.min(mHeight, MeasureSpec.getSize(heightMeasureSpec)), MeasureSpec.AT_MOST);
277e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
278dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
279d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell
280f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell        if (mActionBarView == null) return;
281310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell
282dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
2832bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            int nonTabMaxHeight = 0;
2842bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final int childCount = getChildCount();
2852bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            for (int i = 0; i < childCount; i++) {
2862bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                final View child = getChildAt(i);
2872bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                if (child == mTabContainer) {
2882bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    continue;
2892bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                }
2902bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                nonTabMaxHeight = Math.max(nonTabMaxHeight, isCollapsed(child) ? 0 :
2912bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                        getMeasuredHeightWithMargins(child));
292dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            }
2932bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final int mode = MeasureSpec.getMode(heightMeasureSpec);
2942bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final int maxHeight = mode == MeasureSpec.AT_MOST ?
2952bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    MeasureSpec.getSize(heightMeasureSpec) : Integer.MAX_VALUE;
2962bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            setMeasuredDimension(getMeasuredWidth(),
2972bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    Math.min(nonTabMaxHeight + getMeasuredHeightWithMargins(mTabContainer),
2982bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                            maxHeight));
299dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
300dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
301dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
302dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
303dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onLayout(boolean changed, int l, int t, int r, int b) {
304dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onLayout(changed, l, t, r, b);
305a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
3067f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        final View tabContainer = mTabContainer;
3077f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        final boolean hasTabs = tabContainer != null && tabContainer.getVisibility() != GONE;
308a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
3097f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        if (tabContainer != null && tabContainer.getVisibility() != GONE) {
310dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            final int containerHeight = getMeasuredHeight();
3112bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final LayoutParams lp = (LayoutParams) tabContainer.getLayoutParams();
3127f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            final int tabHeight = tabContainer.getMeasuredHeight();
3132bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            tabContainer.layout(l, containerHeight - tabHeight - lp.bottomMargin, r,
3142bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    containerHeight - lp.bottomMargin);
315dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
316a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
317a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        boolean needsInvalidate = false;
318a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        if (mIsSplit) {
319a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            if (mSplitBackground != null) {
320a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
321a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                needsInvalidate = true;
322a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            }
323a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        } else {
324a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            if (mBackground != null) {
325ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                if (mActionBarView.getVisibility() == View.VISIBLE) {
326ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                    mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(),
327ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                            mActionBarView.getRight(), mActionBarView.getBottom());
328ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                } else if (mActionContextView != null &&
329ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                        mActionContextView.getVisibility() == View.VISIBLE) {
330ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                    mBackground.setBounds(mActionContextView.getLeft(), mActionContextView.getTop(),
331ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                            mActionContextView.getRight(), mActionContextView.getBottom());
332ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                } else {
333ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                    mBackground.setBounds(0, 0, 0, 0);
334ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                }
335a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                needsInvalidate = true;
336a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            }
3377f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            mIsStacked = hasTabs;
3387f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            if (hasTabs && mStackedBackground != null) {
3397f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                mStackedBackground.setBounds(tabContainer.getLeft(), tabContainer.getTop(),
3407f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                        tabContainer.getRight(), tabContainer.getBottom());
341a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                needsInvalidate = true;
342a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            }
343a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        }
344a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
345a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        if (needsInvalidate) {
346a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            invalidate();
347a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        }
348dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
3497f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
3507f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette    /**
3517f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette     * Dummy drawable so that we don't break background display lists and
3527f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette     * projection surfaces.
3537f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette     */
3547f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette    private class ActionBarBackgroundDrawable extends Drawable {
3551e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik        @Override
3561e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik        public void draw(Canvas canvas) {
3577f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            if (mIsSplit) {
35877b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik                if (mSplitBackground != null) {
3591e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mSplitBackground.draw(canvas);
36077b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik                }
3617f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            } else {
3627f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                if (mBackground != null) {
3631e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mBackground.draw(canvas);
3647f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                }
3657f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                if (mStackedBackground != null && mIsStacked) {
3661e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mStackedBackground.draw(canvas);
3677f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                }
3687f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            }
36977b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        }
37077b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik
37177b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        @Override
37277b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        public void getOutline(@NonNull Outline outline) {
3731e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik            if (mIsSplit) {
3741e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                if (mSplitBackground != null) {
3751e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mSplitBackground.getOutline(outline);
3761e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                }
3771e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik            } else {
3781e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                // ignore the stacked background for shadow casting
3791e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                if (mBackground != null) {
3801e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mBackground.getOutline(outline);
3811e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                }
38277b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik            }
3837f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
3847f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
3857f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        @Override
3867f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        public void setAlpha(int alpha) {
3877f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
3887f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
3897f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        @Override
390bd3bfc5285dcacff0a69fecf3baeeeb90d887a58Chris Craik        public void setColorFilter(ColorFilter colorFilter) {
3917f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
3927f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
3937f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        @Override
3947f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        public int getOpacity() {
395f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            if (mIsSplit) {
396f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                if (mSplitBackground != null
397f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                        && mSplitBackground.getOpacity() == PixelFormat.OPAQUE) {
398f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                    return PixelFormat.OPAQUE;
399f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                }
400f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            } else {
401f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                if (mIsStacked && (mStackedBackground == null
402f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                        || mStackedBackground.getOpacity() != PixelFormat.OPAQUE)) {
403f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                    return PixelFormat.UNKNOWN;
404f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                }
405f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                if (!isCollapsed(mActionBarView) && mBackground != null
406f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                        && mBackground.getOpacity() == PixelFormat.OPAQUE) {
407f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                    return PixelFormat.OPAQUE;
408f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                }
409f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            }
410f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell
411f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            return PixelFormat.UNKNOWN;
4127f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
4137f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette    }
41445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell}
415