/*
 * Copyright (C) 2015 The Android Open Source Project
 * Copyright (C) 2016 Yaroslav Mytkalyk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.doctoror.fuckoffmusicplayer.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/**
 * Like {@link android.support.design.internal.ScrimInsetsFrameLayout}, but does not affect
 * fitsSystemWindows
 */
public class NoOffsetScrimInsetsFrameLayout extends FrameLayout {

    private final Rect mTempRect = new Rect();

    private Drawable mInsetForeground;

    private Rect mInsets;

    public NoOffsetScrimInsetsFrameLayout(Context context) {
        this(context, null);
    }

    public NoOffsetScrimInsetsFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NoOffsetScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(true); // No need to draw until the insets are adjusted

        ViewCompat.setOnApplyWindowInsetsListener(this,
                (v, insets) -> {
                    if (null == mInsets) {
                        mInsets = new Rect();
                    }
                    mInsets.set(insets.getSystemWindowInsetLeft(),
                            insets.getSystemWindowInsetTop(),
                            insets.getSystemWindowInsetRight(),
                            insets.getSystemWindowInsetBottom());
                    setWillNotDraw(!insets.hasSystemWindowInsets() || mInsetForeground == null);
                    ViewCompat.postInvalidateOnAnimation(NoOffsetScrimInsetsFrameLayout.this);
                    InsetsHolder.getInstance().onInsetsChanged(mInsets);
                    return insets;
                });
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        super.draw(canvas);

        int width = getWidth();
        int height = getHeight();
        if (mInsets != null && mInsetForeground != null) {
            int sc = canvas.save();
            canvas.translate(getScrollX(), getScrollY());

            // Top
            mTempRect.set(0, 0, width, mInsets.top);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            // Bottom
            mTempRect.set(0, height - mInsets.bottom, width, height);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            // Left
            mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            // Right
            mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            canvas.restoreToCount(sc);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mInsetForeground != null) {
            mInsetForeground.setCallback(this);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mInsetForeground != null) {
            mInsetForeground.setCallback(null);
        }
    }
}

@@ -762,130 +762,12 @@
 ics.
-Canvas;%0Aimport android.graphics.Rect;%0Aimport android.graphics.drawable.Drawable;%0Aimport android.support.annotation.NonNull
+Rect
 ;%0Aim
@@ -1069,96 +1069,8 @@
  %7B%0A%0A
-    private final Rect mTempRect = new Rect();%0A%0A    private Drawable mInsetForeground;%0A%0A
     
@@ -2063,36 +2063,8 @@
 ts()
- %7C%7C mInsetForeground == null
 );%0A 
@@ -2265,16 +2265,16 @@
 insets;%0A
+
         
@@ -2294,1533 +2294,7 @@
    %7D
-%0A%0A    @Override%0A    public void draw(@NonNull Canvas canvas) %7B%0A        super.draw(canvas);%0A%0A        int width = getWidth();%0A        int height = getHeight();%0A        if (mInsets != null && mInsetForeground != null) %7B%0A            int sc = canvas.save();%0A            canvas.translate(getScrollX(), getScrollY());%0A%0A            // Top%0A            mTempRect.set(0, 0, width, mInsets.top);%0A            mInsetForeground.setBounds(mTempRect);%0A            mInsetForeground.draw(canvas);%0A%0A            // Bottom%0A            mTempRect.set(0, height - mInsets.bottom, width, height);%0A            mInsetForeground.setBounds(mTempRect);%0A            mInsetForeground.draw(canvas);%0A%0A            // Left%0A            mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom);%0A            mInsetForeground.setBounds(mTempRect);%0A            mInsetForeground.draw(canvas);%0A%0A            // Right%0A            mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom);%0A            mInsetForeground.setBounds(mTempRect);%0A            mInsetForeground.draw(canvas);%0A%0A            canvas.restoreToCount(sc);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void onAttachedToWindow() %7B%0A        super.onAttachedToWindow();%0A        if (mInsetForeground != null) %7B%0A            mInsetForeground.setCallback(this);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void onDetachedFromWindow() %7B%0A        super.onDetachedFromWindow();%0A        if (mInsetForeground != null) %7B%0A            mInsetForeground.setCallback(null);%0A        %7D%0A    %7D
 %0A%7D%0A

Remove insetForeground parameter.