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 
17 package com.android.systemui.statusbar.notification;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.Outline;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.ViewOutlineProvider;
26 import android.widget.LinearLayout;
27 
28 import com.android.systemui.R;
29 import com.android.systemui.statusbar.AlphaOptimizedFrameLayout;
30 
31 /**
32  * A view used to cast a shadow of a certain size on another view
33  */
34 public 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 
FakeShadowView(Context context)41     public FakeShadowView(Context context) {
42         this(context, null);
43     }
44 
FakeShadowView(Context context, @Nullable AttributeSet attrs)45     public FakeShadowView(Context context, @Nullable AttributeSet attrs) {
46         this(context, attrs, 0);
47     }
48 
FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)49     public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
50         this(context, attrs, defStyleAttr, 0);
51     }
52 
FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)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 
setFakeShadowTranslationZ(float fakeShadowTranslationZ, float outlineAlpha, int shadowYEnd, int outlineTranslation)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