Selim Cinek | 3322357 | 2016-02-19 19:32:22 -0800 | [diff] [blame] | 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.statusbar.AlphaOptimizedFrameLayout; |
| 29 | |
| 30 | /** |
| 31 | * A view used to cast a shadow of a certain size on another view |
| 32 | */ |
| 33 | public class FakeShadowView extends AlphaOptimizedFrameLayout { |
| 34 | public static final float SHADOW_SIBLING_TRESHOLD = 0.1f; |
| 35 | |
| 36 | private View mFakeShadow; |
| 37 | private float mOutlineAlpha; |
| 38 | |
| 39 | public FakeShadowView(Context context) { |
| 40 | this(context, null); |
| 41 | } |
| 42 | |
| 43 | public FakeShadowView(Context context, @Nullable AttributeSet attrs) { |
| 44 | this(context, attrs, 0); |
| 45 | } |
| 46 | |
| 47 | public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 48 | this(context, attrs, defStyleAttr, 0); |
| 49 | } |
| 50 | |
| 51 | public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| 52 | int defStyleRes) { |
| 53 | super(context, attrs, defStyleAttr, defStyleRes); |
| 54 | mFakeShadow = new View(context); |
| 55 | mFakeShadow.setVisibility(INVISIBLE); |
| 56 | mFakeShadow.setLayoutParams(new LinearLayout.LayoutParams( |
| 57 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 58 | (int) (48 * getResources().getDisplayMetrics().density))); |
| 59 | mFakeShadow.setOutlineProvider(new ViewOutlineProvider() { |
| 60 | @Override |
| 61 | public void getOutline(View view, Outline outline) { |
| 62 | outline.setRect(0, 0, getWidth(), mFakeShadow.getHeight()); |
| 63 | outline.setAlpha(mOutlineAlpha); |
| 64 | } |
| 65 | }); |
| 66 | addView(mFakeShadow); |
| 67 | } |
| 68 | |
| 69 | public void setFakeShadowTranslationZ(float fakeShadowTranslationZ, float outlineAlpha, |
| 70 | int shadowYEnd, int outlineTranslation) { |
| 71 | if (fakeShadowTranslationZ == 0.0f) { |
| 72 | mFakeShadow.setVisibility(INVISIBLE); |
| 73 | } else { |
| 74 | mFakeShadow.setVisibility(VISIBLE); |
| 75 | mFakeShadow.setTranslationZ(fakeShadowTranslationZ); |
| 76 | mFakeShadow.setTranslationX(outlineTranslation); |
| 77 | mFakeShadow.setTranslationY(shadowYEnd - mFakeShadow.getHeight()); |
| 78 | if (outlineAlpha != mOutlineAlpha) { |
| 79 | mOutlineAlpha = outlineAlpha; |
| 80 | mFakeShadow.invalidateOutline(); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |