1/*
2 * Copyright (C) 2015 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 android.support.design.internal;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Canvas;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.support.annotation.NonNull;
25import android.support.design.R;
26import android.support.v4.view.ViewCompat;
27import android.support.v4.view.WindowInsetsCompat;
28import android.util.AttributeSet;
29import android.view.View;
30import android.widget.FrameLayout;
31
32/**
33 * @hide
34 */
35public class ScrimInsetsFrameLayout extends FrameLayout {
36
37    private Drawable mInsetForeground;
38
39    private Rect mInsets;
40
41    private Rect mTempRect = new Rect();
42
43    public ScrimInsetsFrameLayout(Context context) {
44        this(context, null);
45    }
46
47    public ScrimInsetsFrameLayout(Context context, AttributeSet attrs) {
48        this(context, attrs, 0);
49    }
50
51    public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
52        super(context, attrs, defStyleAttr);
53
54        final TypedArray a = context.obtainStyledAttributes(attrs,
55                R.styleable.ScrimInsetsFrameLayout, defStyleAttr,
56                R.style.Widget_Design_ScrimInsetsFrameLayout);
57        mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground);
58        a.recycle();
59        setWillNotDraw(true); // No need to draw until the insets are adjusted
60
61        ViewCompat.setOnApplyWindowInsetsListener(this,
62                new android.support.v4.view.OnApplyWindowInsetsListener() {
63                    @Override
64                    public WindowInsetsCompat onApplyWindowInsets(View v,
65                            WindowInsetsCompat insets) {
66                        if (null == mInsets) {
67                            mInsets = new Rect();
68                        }
69                        mInsets.set(insets.getSystemWindowInsetLeft(),
70                                insets.getSystemWindowInsetTop(),
71                                insets.getSystemWindowInsetRight(),
72                                insets.getSystemWindowInsetBottom());
73                        setWillNotDraw(mInsets.isEmpty() || mInsetForeground == null);
74                        ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this);
75                        return insets.consumeSystemWindowInsets();
76                    }
77                });
78    }
79
80    @Override
81    public void draw(@NonNull Canvas canvas) {
82        super.draw(canvas);
83
84        int width = getWidth();
85        int height = getHeight();
86        if (mInsets != null && mInsetForeground != null) {
87            int sc = canvas.save();
88            canvas.translate(getScrollX(), getScrollY());
89
90            // Top
91            mTempRect.set(0, 0, width, mInsets.top);
92            mInsetForeground.setBounds(mTempRect);
93            mInsetForeground.draw(canvas);
94
95            // Bottom
96            mTempRect.set(0, height - mInsets.bottom, width, height);
97            mInsetForeground.setBounds(mTempRect);
98            mInsetForeground.draw(canvas);
99
100            // Left
101            mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom);
102            mInsetForeground.setBounds(mTempRect);
103            mInsetForeground.draw(canvas);
104
105            // Right
106            mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom);
107            mInsetForeground.setBounds(mTempRect);
108            mInsetForeground.draw(canvas);
109
110            canvas.restoreToCount(sc);
111        }
112    }
113
114    @Override
115    protected void onAttachedToWindow() {
116        super.onAttachedToWindow();
117        if (mInsetForeground != null) {
118            mInsetForeground.setCallback(this);
119        }
120    }
121
122    @Override
123    protected void onDetachedFromWindow() {
124        super.onDetachedFromWindow();
125        if (mInsetForeground != null) {
126            mInsetForeground.setCallback(null);
127        }
128    }
129
130}
131