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.stackdivider;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.LinearGradient;
24 import android.graphics.Paint;
25 import android.graphics.Shader;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.view.WindowManager;
29 
30 import com.android.systemui.R;
31 
32 /**
33  * Shadow for the minimized dock state on homescreen.
34  */
35 public class MinimizedDockShadow extends View {
36 
37     private final Paint mShadowPaint = new Paint();
38 
39     private int mDockSide = WindowManager.DOCKED_INVALID;
40 
MinimizedDockShadow(Context context, @Nullable AttributeSet attrs)41     public MinimizedDockShadow(Context context, @Nullable AttributeSet attrs) {
42         super(context, attrs);
43     }
44 
setDockSide(int dockSide)45     public void setDockSide(int dockSide) {
46         if (dockSide != mDockSide) {
47             mDockSide = dockSide;
48             updatePaint(getLeft(), getTop(), getRight(), getBottom());
49             invalidate();
50         }
51     }
52 
updatePaint(int left, int top, int right, int bottom)53     private void updatePaint(int left, int top, int right, int bottom) {
54         int startColor = mContext.getResources().getColor(
55                 R.color.minimize_dock_shadow_start, null);
56         int endColor = mContext.getResources().getColor(
57                 R.color.minimize_dock_shadow_end, null);
58         final int middleColor = Color.argb(
59                 (Color.alpha(startColor) + Color.alpha(endColor)) / 2, 0, 0, 0);
60         final int quarter = Color.argb(
61                 (int) (Color.alpha(startColor) * 0.25f + Color.alpha(endColor) * 0.75f),
62                 0, 0, 0);
63         if (mDockSide == WindowManager.DOCKED_TOP) {
64             mShadowPaint.setShader(new LinearGradient(
65                     0, 0, 0, bottom - top,
66                     new int[] { startColor, middleColor, quarter, endColor },
67                     new float[] { 0f, 0.35f, 0.6f, 1f }, Shader.TileMode.CLAMP));
68         } else if (mDockSide == WindowManager.DOCKED_LEFT) {
69             mShadowPaint.setShader(new LinearGradient(
70                     0, 0, right - left, 0,
71                     new int[] { startColor, middleColor, quarter, endColor },
72                     new float[] { 0f, 0.35f, 0.6f, 1f }, Shader.TileMode.CLAMP));
73         } else if (mDockSide == WindowManager.DOCKED_RIGHT) {
74             mShadowPaint.setShader(new LinearGradient(
75                     right - left, 0, 0, 0,
76                     new int[] { startColor, middleColor, quarter, endColor },
77                     new float[] { 0f, 0.35f, 0.6f, 1f }, Shader.TileMode.CLAMP));
78         }
79     }
80 
81     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)82     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
83         super.onLayout(changed, left, top, right, bottom);
84         if (changed) {
85             updatePaint(left, top, right, bottom);
86             invalidate();
87         }
88     }
89 
90     @Override
onDraw(Canvas canvas)91     protected void onDraw(Canvas canvas) {
92         canvas.drawRect(0, 0, getWidth(), getHeight(), mShadowPaint);
93     }
94 
95     @Override
hasOverlappingRendering()96     public boolean hasOverlappingRendering() {
97         return false;
98     }
99 }
100