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
150f6d87ec193f17e8dad82c9994ba7a58e975d364bAlan Viverette    protected boolean verifyDrawable(@NonNull 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();
158ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette
159ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        final int[] state = getDrawableState();
160ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        boolean changed = false;
161ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette
162ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        final Drawable background = mBackground;
163ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        if (background != null && background.isStateful()) {
164ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette            changed |= background.setState(state);
165ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        }
166ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette
167ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        final Drawable stackedBackground = mStackedBackground;
168ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        if (stackedBackground != null && stackedBackground.isStateful()) {
169ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette            changed |= stackedBackground.setState(state);
170a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
171ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette
172ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        final Drawable splitBackground = mSplitBackground;
173ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        if (splitBackground != null && splitBackground.isStateful()) {
174ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette            changed |= splitBackground.setState(state);
175a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
176ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette
177ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette        if (changed) {
178ad0020f8075ecf768cb610d60dbb167d41f0fbe5Alan Viverette            invalidate();
179a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
180a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
181a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
182a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
183a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    public void jumpDrawablesToCurrentState() {
184a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        super.jumpDrawablesToCurrentState();
185a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null) {
186a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mBackground.jumpToCurrentState();
187a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
188a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null) {
189a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mStackedBackground.jumpToCurrentState();
190a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
191a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null) {
192a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mSplitBackground.jumpToCurrentState();
193a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
194a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
195a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
1964457e85a7090ad51726d50a4daf981d917cceeddFabrice Di Meglio    /**
1974457e85a7090ad51726d50a4daf981d917cceeddFabrice Di Meglio     * @hide
1984457e85a7090ad51726d50a4daf981d917cceeddFabrice Di Meglio     */
199a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    @Override
200a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    public void onResolveDrawables(int layoutDirection) {
201a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        super.onResolveDrawables(layoutDirection);
202a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mBackground != null) {
203a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mBackground.setLayoutDirection(layoutDirection);
204a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
205a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mStackedBackground != null) {
206a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mStackedBackground.setLayoutDirection(layoutDirection);
207a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
208a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        if (mSplitBackground != null) {
209a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell            mSplitBackground.setLayoutDirection(layoutDirection);
210a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell        }
211a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell    }
212a7cc06d82e45918c37429a59b14545c6a57db4e4Adam Powell
21301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    /**
21401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * Set the action bar into a "transitioning" state. While transitioning
21501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * the bar will block focus and touch from all of its descendants. This
21601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * prevents the user from interacting with the bar while it is animating
21701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * in or out.
21801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     *
21901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * @param isTransitioning true if the bar is currently transitioning, false otherwise.
22001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     */
22101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public void setTransitioning(boolean isTransitioning) {
22201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        mIsTransitioning = isTransitioning;
22301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS
22401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell                : FOCUS_AFTER_DESCENDANTS);
22501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
22601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
22701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    @Override
22801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public boolean onInterceptTouchEvent(MotionEvent ev) {
22901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        return mIsTransitioning || super.onInterceptTouchEvent(ev);
23001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
23101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
2326ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    @Override
2336ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    public boolean onTouchEvent(MotionEvent ev) {
2346ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        super.onTouchEvent(ev);
235dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
236dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        // An action bar always eats touch events.
2376ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        return true;
2386ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    }
239dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
2407d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell    @Override
2417d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell    public boolean onHoverEvent(MotionEvent ev) {
2427d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell        super.onHoverEvent(ev);
2437d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell
2447d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell        // An action bar always eats hover events.
2457d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell        return true;
2467d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell    }
2477d09f04363cdda6a3cf8b2ad1b67c9a07fa8975fAdam Powell
248f5645cbafe7eed33452d888f16726bee8a0cd9feAdam Powell    public void setTabContainer(ScrollingTabContainerView tabView) {
249dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null) {
250dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            removeView(mTabContainer);
251dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
252dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        mTabContainer = tabView;
25325151e4792e9be8dbb892b7eba7349773b8127afAdam Powell        if (tabView != null) {
25425151e4792e9be8dbb892b7eba7349773b8127afAdam Powell            addView(tabView);
255af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell            final ViewGroup.LayoutParams lp = tabView.getLayoutParams();
256af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell            lp.width = LayoutParams.MATCH_PARENT;
257af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell            lp.height = LayoutParams.WRAP_CONTENT;
258f5645cbafe7eed33452d888f16726bee8a0cd9feAdam Powell            tabView.setAllowCollapse(false);
25925151e4792e9be8dbb892b7eba7349773b8127afAdam Powell        }
260dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
261dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
262dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public View getTabContainer() {
263dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        return mTabContainer;
264dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
265dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
266dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
2674423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri    public ActionMode startActionModeForChild(
2684423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri            View child, ActionMode.Callback callback, int type) {
2694423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        if (type != ActionMode.TYPE_PRIMARY) {
2704423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri            return super.startActionModeForChild(child, callback, type);
2714423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        }
2724423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        return null;
2734423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri    }
2744423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri
2751e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik    private static boolean isCollapsed(View view) {
276e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return view == null || view.getVisibility() == GONE || view.getMeasuredHeight() == 0;
277e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
278e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2792bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar    private int getMeasuredHeightWithMargins(View view) {
2802bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar        final LayoutParams lp = (LayoutParams) view.getLayoutParams();
2812bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar        return view.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
2822bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar    }
2832bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar
284640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell    @Override
285dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
286e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mActionBarView == null &&
287e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST && mHeight >= 0) {
288e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
289e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    Math.min(mHeight, MeasureSpec.getSize(heightMeasureSpec)), MeasureSpec.AT_MOST);
290e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
291dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
292d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell
293f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell        if (mActionBarView == null) return;
294310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell
295dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
2962bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            int nonTabMaxHeight = 0;
2972bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final int childCount = getChildCount();
2982bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            for (int i = 0; i < childCount; i++) {
2992bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                final View child = getChildAt(i);
3002bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                if (child == mTabContainer) {
3012bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    continue;
3022bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                }
3032bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                nonTabMaxHeight = Math.max(nonTabMaxHeight, isCollapsed(child) ? 0 :
3042bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                        getMeasuredHeightWithMargins(child));
305dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            }
3062bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final int mode = MeasureSpec.getMode(heightMeasureSpec);
3072bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final int maxHeight = mode == MeasureSpec.AT_MOST ?
3082bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    MeasureSpec.getSize(heightMeasureSpec) : Integer.MAX_VALUE;
3092bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            setMeasuredDimension(getMeasuredWidth(),
3102bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    Math.min(nonTabMaxHeight + getMeasuredHeightWithMargins(mTabContainer),
3112bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                            maxHeight));
312dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
313dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
314dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
315dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
316dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onLayout(boolean changed, int l, int t, int r, int b) {
317dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onLayout(changed, l, t, r, b);
318a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
3197f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        final View tabContainer = mTabContainer;
3207f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        final boolean hasTabs = tabContainer != null && tabContainer.getVisibility() != GONE;
321a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
3227f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        if (tabContainer != null && tabContainer.getVisibility() != GONE) {
323dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            final int containerHeight = getMeasuredHeight();
3242bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            final LayoutParams lp = (LayoutParams) tabContainer.getLayoutParams();
3257f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            final int tabHeight = tabContainer.getMeasuredHeight();
3262bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar            tabContainer.layout(l, containerHeight - tabHeight - lp.bottomMargin, r,
3272bbad0cddce213ffd3d1e0909f2dc74dd0038ba7Yigit Boyar                    containerHeight - lp.bottomMargin);
328dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
329a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
330a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        boolean needsInvalidate = false;
331a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        if (mIsSplit) {
332a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            if (mSplitBackground != null) {
333a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
334a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                needsInvalidate = true;
335a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            }
336a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        } else {
337a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            if (mBackground != null) {
338ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                if (mActionBarView.getVisibility() == View.VISIBLE) {
339ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                    mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(),
340ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                            mActionBarView.getRight(), mActionBarView.getBottom());
341ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                } else if (mActionContextView != null &&
342ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                        mActionContextView.getVisibility() == View.VISIBLE) {
343ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                    mBackground.setBounds(mActionContextView.getLeft(), mActionContextView.getTop(),
344ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                            mActionContextView.getRight(), mActionContextView.getBottom());
345ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                } else {
346ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                    mBackground.setBounds(0, 0, 0, 0);
347ccd3da23c6dbdda2af5c5d746652e4b240cb4f50Chet Haase                }
348a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                needsInvalidate = true;
349a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            }
3507f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            mIsStacked = hasTabs;
3517f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            if (hasTabs && mStackedBackground != null) {
3527f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                mStackedBackground.setBounds(tabContainer.getLeft(), tabContainer.getTop(),
3537f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                        tabContainer.getRight(), tabContainer.getBottom());
354a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell                needsInvalidate = true;
355a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            }
356a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        }
357a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell
358a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        if (needsInvalidate) {
359a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell            invalidate();
360a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell        }
361dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
3627f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
3637f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette    /**
3647f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette     * Dummy drawable so that we don't break background display lists and
3657f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette     * projection surfaces.
3667f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette     */
3677f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette    private class ActionBarBackgroundDrawable extends Drawable {
3681e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik        @Override
3691e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik        public void draw(Canvas canvas) {
3707f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            if (mIsSplit) {
37177b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik                if (mSplitBackground != null) {
3721e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mSplitBackground.draw(canvas);
37377b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik                }
3747f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            } else {
3757f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                if (mBackground != null) {
3761e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mBackground.draw(canvas);
3777f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                }
3787f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                if (mStackedBackground != null && mIsStacked) {
3791e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mStackedBackground.draw(canvas);
3807f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette                }
3817f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette            }
38277b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        }
38377b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik
38477b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        @Override
38577b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        public void getOutline(@NonNull Outline outline) {
3861e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik            if (mIsSplit) {
3871e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                if (mSplitBackground != null) {
3881e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mSplitBackground.getOutline(outline);
3891e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                }
3901e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik            } else {
3911e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                // ignore the stacked background for shadow casting
3921e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                if (mBackground != null) {
3931e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                    mBackground.getOutline(outline);
3941e610d479f091f1b09f2a7e5201711bf4d311f6fChris Craik                }
39577b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik            }
3967f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
3977f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
3987f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        @Override
3997f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        public void setAlpha(int alpha) {
4007f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
4017f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
4027f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        @Override
403bd3bfc5285dcacff0a69fecf3baeeeb90d887a58Chris Craik        public void setColorFilter(ColorFilter colorFilter) {
4047f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
4057f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette
4067f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        @Override
4077f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        public int getOpacity() {
408f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            if (mIsSplit) {
409f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                if (mSplitBackground != null
410f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                        && mSplitBackground.getOpacity() == PixelFormat.OPAQUE) {
411f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                    return PixelFormat.OPAQUE;
412f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                }
413f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            } else {
414f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                if (mIsStacked && (mStackedBackground == null
415f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                        || mStackedBackground.getOpacity() != PixelFormat.OPAQUE)) {
416f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                    return PixelFormat.UNKNOWN;
417f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                }
418f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                if (!isCollapsed(mActionBarView) && mBackground != null
419f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                        && mBackground.getOpacity() == PixelFormat.OPAQUE) {
420f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                    return PixelFormat.OPAQUE;
421f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell                }
422f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            }
423f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell
424f849a5e16d016fb6ae081a4575ce67f4ce688e3aAdam Powell            return PixelFormat.UNKNOWN;
4257f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette        }
4267f610fed107b158c144dca1b20a44ee91eb8c934Alan Viverette    }
42745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell}
428