1/*
2 * Copyright (C) 2016 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.systemui.statusbar.notification;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.graphics.Outline;
22import android.util.AttributeSet;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.ViewOutlineProvider;
26import android.widget.LinearLayout;
27
28import com.android.systemui.R;
29import com.android.systemui.statusbar.AlphaOptimizedFrameLayout;
30
31/**
32 * A view used to cast a shadow of a certain size on another view
33 */
34public class FakeShadowView extends AlphaOptimizedFrameLayout {
35    public static final float SHADOW_SIBLING_TRESHOLD = 0.1f;
36    private final int mShadowMinHeight;
37
38    private View mFakeShadow;
39    private float mOutlineAlpha;
40
41    public FakeShadowView(Context context) {
42        this(context, null);
43    }
44
45    public FakeShadowView(Context context, @Nullable AttributeSet attrs) {
46        this(context, attrs, 0);
47    }
48
49    public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
50        this(context, attrs, defStyleAttr, 0);
51    }
52
53    public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
54            int defStyleRes) {
55        super(context, attrs, defStyleAttr, defStyleRes);
56        mFakeShadow = new View(context);
57        mFakeShadow.setVisibility(INVISIBLE);
58        mFakeShadow.setLayoutParams(new LinearLayout.LayoutParams(
59                ViewGroup.LayoutParams.MATCH_PARENT,
60                (int) (48 * getResources().getDisplayMetrics().density)));
61        mFakeShadow.setOutlineProvider(new ViewOutlineProvider() {
62            @Override
63            public void getOutline(View view, Outline outline) {
64                outline.setRect(0, 0, getWidth(), mFakeShadow.getHeight());
65                outline.setAlpha(mOutlineAlpha);
66            }
67        });
68        addView(mFakeShadow);
69        mShadowMinHeight = Math.max(1, context.getResources()
70                .getDimensionPixelSize(R.dimen.notification_divider_height));
71    }
72
73    public void setFakeShadowTranslationZ(float fakeShadowTranslationZ, float outlineAlpha,
74            int shadowYEnd, int outlineTranslation) {
75        if (fakeShadowTranslationZ == 0.0f) {
76            mFakeShadow.setVisibility(INVISIBLE);
77        } else {
78            mFakeShadow.setVisibility(VISIBLE);
79            fakeShadowTranslationZ = Math.max(mShadowMinHeight, fakeShadowTranslationZ);
80            mFakeShadow.setTranslationZ(fakeShadowTranslationZ);
81            mFakeShadow.setTranslationX(outlineTranslation);
82            mFakeShadow.setTranslationY(shadowYEnd - mFakeShadow.getHeight());
83            if (outlineAlpha != mOutlineAlpha) {
84                mOutlineAlpha = outlineAlpha;
85                mFakeShadow.invalidateOutline();
86            }
87        }
88    }
89}
90