1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.widget;
18
19import android.annotation.NonNull;
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.graphics.Canvas;
23import android.graphics.ColorFilter;
24import android.graphics.Outline;
25import android.graphics.PixelFormat;
26import android.graphics.RectF;
27import android.graphics.drawable.Drawable;
28import android.util.AttributeSet;
29import android.view.ActionMode;
30import android.view.MotionEvent;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.FrameLayout;
34
35import java.util.List;
36
37/**
38 * This class acts as a container for the action bar view and action mode context views.
39 * It applies special styles as needed to help handle animated transitions between them.
40 * @hide
41 */
42public class ActionBarContainer extends FrameLayout {
43    private boolean mIsTransitioning;
44    private View mTabContainer;
45    private View mActionBarView;
46    private View mActionContextView;
47
48    private Drawable mBackground;
49    private Drawable mStackedBackground;
50    private Drawable mSplitBackground;
51    private boolean mIsSplit;
52    private boolean mIsStacked;
53    private int mHeight;
54
55    public ActionBarContainer(Context context) {
56        this(context, null);
57    }
58
59    public ActionBarContainer(Context context, AttributeSet attrs) {
60        super(context, attrs);
61
62        // Set a transparent background so that we project appropriately.
63        setBackground(new ActionBarBackgroundDrawable());
64
65        TypedArray a = context.obtainStyledAttributes(attrs,
66                com.android.internal.R.styleable.ActionBar);
67        mBackground = a.getDrawable(com.android.internal.R.styleable.ActionBar_background);
68        mStackedBackground = a.getDrawable(
69                com.android.internal.R.styleable.ActionBar_backgroundStacked);
70        mHeight = a.getDimensionPixelSize(com.android.internal.R.styleable.ActionBar_height, -1);
71
72        if (getId() == com.android.internal.R.id.split_action_bar) {
73            mIsSplit = true;
74            mSplitBackground = a.getDrawable(
75                    com.android.internal.R.styleable.ActionBar_backgroundSplit);
76        }
77        a.recycle();
78
79        setWillNotDraw(mIsSplit ? mSplitBackground == null :
80                mBackground == null && mStackedBackground == null);
81    }
82
83    @Override
84    public void onFinishInflate() {
85        super.onFinishInflate();
86        mActionBarView = findViewById(com.android.internal.R.id.action_bar);
87        mActionContextView = findViewById(com.android.internal.R.id.action_context_bar);
88    }
89
90    public void setPrimaryBackground(Drawable bg) {
91        if (mBackground != null) {
92            mBackground.setCallback(null);
93            unscheduleDrawable(mBackground);
94        }
95        mBackground = bg;
96        if (bg != null) {
97            bg.setCallback(this);
98            if (mActionBarView != null) {
99                mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(),
100                        mActionBarView.getRight(), mActionBarView.getBottom());
101            }
102        }
103        setWillNotDraw(mIsSplit ? mSplitBackground == null :
104                mBackground == null && mStackedBackground == null);
105        invalidate();
106    }
107
108    public void setStackedBackground(Drawable bg) {
109        if (mStackedBackground != null) {
110            mStackedBackground.setCallback(null);
111            unscheduleDrawable(mStackedBackground);
112        }
113        mStackedBackground = bg;
114        if (bg != null) {
115            bg.setCallback(this);
116            if ((mIsStacked && mStackedBackground != null)) {
117                mStackedBackground.setBounds(mTabContainer.getLeft(), mTabContainer.getTop(),
118                        mTabContainer.getRight(), mTabContainer.getBottom());
119            }
120        }
121        setWillNotDraw(mIsSplit ? mSplitBackground == null :
122                mBackground == null && mStackedBackground == null);
123        invalidate();
124    }
125
126    public void setSplitBackground(Drawable bg) {
127        if (mSplitBackground != null) {
128            mSplitBackground.setCallback(null);
129            unscheduleDrawable(mSplitBackground);
130        }
131        mSplitBackground = bg;
132        if (bg != null) {
133            bg.setCallback(this);
134            if (mIsSplit && mSplitBackground != null) {
135                mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
136            }
137        }
138        setWillNotDraw(mIsSplit ? mSplitBackground == null :
139                mBackground == null && mStackedBackground == null);
140        invalidate();
141    }
142
143    @Override
144    public void setVisibility(int visibility) {
145        super.setVisibility(visibility);
146        final boolean isVisible = visibility == VISIBLE;
147        if (mBackground != null) mBackground.setVisible(isVisible, false);
148        if (mStackedBackground != null) mStackedBackground.setVisible(isVisible, false);
149        if (mSplitBackground != null) mSplitBackground.setVisible(isVisible, false);
150    }
151
152    @Override
153    protected boolean verifyDrawable(Drawable who) {
154        return (who == mBackground && !mIsSplit) || (who == mStackedBackground && mIsStacked) ||
155                (who == mSplitBackground && mIsSplit) || super.verifyDrawable(who);
156    }
157
158    @Override
159    protected void drawableStateChanged() {
160        super.drawableStateChanged();
161        if (mBackground != null && mBackground.isStateful()) {
162            mBackground.setState(getDrawableState());
163        }
164        if (mStackedBackground != null && mStackedBackground.isStateful()) {
165            mStackedBackground.setState(getDrawableState());
166        }
167        if (mSplitBackground != null && mSplitBackground.isStateful()) {
168            mSplitBackground.setState(getDrawableState());
169        }
170    }
171
172    @Override
173    public void jumpDrawablesToCurrentState() {
174        super.jumpDrawablesToCurrentState();
175        if (mBackground != null) {
176            mBackground.jumpToCurrentState();
177        }
178        if (mStackedBackground != null) {
179            mStackedBackground.jumpToCurrentState();
180        }
181        if (mSplitBackground != null) {
182            mSplitBackground.jumpToCurrentState();
183        }
184    }
185
186    /**
187     * @hide
188     */
189    @Override
190    public void onResolveDrawables(int layoutDirection) {
191        super.onResolveDrawables(layoutDirection);
192        if (mBackground != null) {
193            mBackground.setLayoutDirection(layoutDirection);
194        }
195        if (mStackedBackground != null) {
196            mStackedBackground.setLayoutDirection(layoutDirection);
197        }
198        if (mSplitBackground != null) {
199            mSplitBackground.setLayoutDirection(layoutDirection);
200        }
201    }
202
203    /**
204     * Set the action bar into a "transitioning" state. While transitioning
205     * the bar will block focus and touch from all of its descendants. This
206     * prevents the user from interacting with the bar while it is animating
207     * in or out.
208     *
209     * @param isTransitioning true if the bar is currently transitioning, false otherwise.
210     */
211    public void setTransitioning(boolean isTransitioning) {
212        mIsTransitioning = isTransitioning;
213        setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS
214                : FOCUS_AFTER_DESCENDANTS);
215    }
216
217    @Override
218    public boolean onInterceptTouchEvent(MotionEvent ev) {
219        return mIsTransitioning || super.onInterceptTouchEvent(ev);
220    }
221
222    @Override
223    public boolean onTouchEvent(MotionEvent ev) {
224        super.onTouchEvent(ev);
225
226        // An action bar always eats touch events.
227        return true;
228    }
229
230    @Override
231    public boolean onHoverEvent(MotionEvent ev) {
232        super.onHoverEvent(ev);
233
234        // An action bar always eats hover events.
235        return true;
236    }
237
238    public void setTabContainer(ScrollingTabContainerView tabView) {
239        if (mTabContainer != null) {
240            removeView(mTabContainer);
241        }
242        mTabContainer = tabView;
243        if (tabView != null) {
244            addView(tabView);
245            final ViewGroup.LayoutParams lp = tabView.getLayoutParams();
246            lp.width = LayoutParams.MATCH_PARENT;
247            lp.height = LayoutParams.WRAP_CONTENT;
248            tabView.setAllowCollapse(false);
249        }
250    }
251
252    public View getTabContainer() {
253        return mTabContainer;
254    }
255
256    @Override
257    public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) {
258        // No starting an action mode for an action bar child! (Where would it go?)
259        return null;
260    }
261
262    private static boolean isCollapsed(View view) {
263        return view == null || view.getVisibility() == GONE || view.getMeasuredHeight() == 0;
264    }
265
266    private int getMeasuredHeightWithMargins(View view) {
267        final LayoutParams lp = (LayoutParams) view.getLayoutParams();
268        return view.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
269    }
270
271    @Override
272    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
273        if (mActionBarView == null &&
274                MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST && mHeight >= 0) {
275            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
276                    Math.min(mHeight, MeasureSpec.getSize(heightMeasureSpec)), MeasureSpec.AT_MOST);
277        }
278        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
279
280        if (mActionBarView == null) return;
281
282        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
283            int nonTabMaxHeight = 0;
284            final int childCount = getChildCount();
285            for (int i = 0; i < childCount; i++) {
286                final View child = getChildAt(i);
287                if (child == mTabContainer) {
288                    continue;
289                }
290                nonTabMaxHeight = Math.max(nonTabMaxHeight, isCollapsed(child) ? 0 :
291                        getMeasuredHeightWithMargins(child));
292            }
293            final int mode = MeasureSpec.getMode(heightMeasureSpec);
294            final int maxHeight = mode == MeasureSpec.AT_MOST ?
295                    MeasureSpec.getSize(heightMeasureSpec) : Integer.MAX_VALUE;
296            setMeasuredDimension(getMeasuredWidth(),
297                    Math.min(nonTabMaxHeight + getMeasuredHeightWithMargins(mTabContainer),
298                            maxHeight));
299        }
300    }
301
302    @Override
303    public void onLayout(boolean changed, int l, int t, int r, int b) {
304        super.onLayout(changed, l, t, r, b);
305
306        final View tabContainer = mTabContainer;
307        final boolean hasTabs = tabContainer != null && tabContainer.getVisibility() != GONE;
308
309        if (tabContainer != null && tabContainer.getVisibility() != GONE) {
310            final int containerHeight = getMeasuredHeight();
311            final LayoutParams lp = (LayoutParams) tabContainer.getLayoutParams();
312            final int tabHeight = tabContainer.getMeasuredHeight();
313            tabContainer.layout(l, containerHeight - tabHeight - lp.bottomMargin, r,
314                    containerHeight - lp.bottomMargin);
315        }
316
317        boolean needsInvalidate = false;
318        if (mIsSplit) {
319            if (mSplitBackground != null) {
320                mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
321                needsInvalidate = true;
322            }
323        } else {
324            if (mBackground != null) {
325                if (mActionBarView.getVisibility() == View.VISIBLE) {
326                    mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(),
327                            mActionBarView.getRight(), mActionBarView.getBottom());
328                } else if (mActionContextView != null &&
329                        mActionContextView.getVisibility() == View.VISIBLE) {
330                    mBackground.setBounds(mActionContextView.getLeft(), mActionContextView.getTop(),
331                            mActionContextView.getRight(), mActionContextView.getBottom());
332                } else {
333                    mBackground.setBounds(0, 0, 0, 0);
334                }
335                needsInvalidate = true;
336            }
337            mIsStacked = hasTabs;
338            if (hasTabs && mStackedBackground != null) {
339                mStackedBackground.setBounds(tabContainer.getLeft(), tabContainer.getTop(),
340                        tabContainer.getRight(), tabContainer.getBottom());
341                needsInvalidate = true;
342            }
343        }
344
345        if (needsInvalidate) {
346            invalidate();
347        }
348    }
349
350    /**
351     * Dummy drawable so that we don't break background display lists and
352     * projection surfaces.
353     */
354    private class ActionBarBackgroundDrawable extends Drawable {
355        @Override
356        public void draw(Canvas canvas) {
357            if (mIsSplit) {
358                if (mSplitBackground != null) {
359                    mSplitBackground.draw(canvas);
360                }
361            } else {
362                if (mBackground != null) {
363                    mBackground.draw(canvas);
364                }
365                if (mStackedBackground != null && mIsStacked) {
366                    mStackedBackground.draw(canvas);
367                }
368            }
369        }
370
371        @Override
372        public void getOutline(@NonNull Outline outline) {
373            if (mIsSplit) {
374                if (mSplitBackground != null) {
375                    mSplitBackground.getOutline(outline);
376                }
377            } else {
378                // ignore the stacked background for shadow casting
379                if (mBackground != null) {
380                    mBackground.getOutline(outline);
381                }
382            }
383        }
384
385        @Override
386        public void setAlpha(int alpha) {
387        }
388
389        @Override
390        public void setColorFilter(ColorFilter cf) {
391        }
392
393        @Override
394        public int getOpacity() {
395            if (mIsSplit) {
396                if (mSplitBackground != null
397                        && mSplitBackground.getOpacity() == PixelFormat.OPAQUE) {
398                    return PixelFormat.OPAQUE;
399                }
400            } else {
401                if (mIsStacked && (mStackedBackground == null
402                        || mStackedBackground.getOpacity() != PixelFormat.OPAQUE)) {
403                    return PixelFormat.UNKNOWN;
404                }
405                if (!isCollapsed(mActionBarView) && mBackground != null
406                        && mBackground.getOpacity() == PixelFormat.OPAQUE) {
407                    return PixelFormat.OPAQUE;
408                }
409            }
410
411            return PixelFormat.UNKNOWN;
412        }
413    }
414}
415