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