1 /*
2  * Copyright (C) 2014 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.policy;
18 
19 import android.graphics.Canvas;
20 import android.graphics.Color;
21 import android.graphics.ColorFilter;
22 import android.graphics.Paint;
23 import android.graphics.PixelFormat;
24 import android.graphics.RadialGradient;
25 import android.graphics.Rect;
26 import android.graphics.Shader;
27 import android.graphics.drawable.Drawable;
28 import android.util.LayoutDirection;
29 import android.view.View;
30 
31 import com.android.systemui.R;
32 
33 /**
34  * Gradient background for the user switcher on Keyguard.
35  */
36 public class KeyguardUserSwitcherScrim extends Drawable
37         implements View.OnLayoutChangeListener {
38 
39     private static final float OUTER_EXTENT = 2.5f;
40     private static final float INNER_EXTENT = 0.75f;
41 
42     private int mDarkColor;
43     private int mTop;
44     private int mAlpha = 255;
45     private Paint mRadialGradientPaint = new Paint();
46     private int mLayoutWidth;
47 
KeyguardUserSwitcherScrim(View host)48     public KeyguardUserSwitcherScrim(View host) {
49         host.addOnLayoutChangeListener(this);
50         mDarkColor = host.getContext().getColor(
51                 R.color.keyguard_user_switcher_background_gradient_color);
52     }
53 
54     @Override
draw(Canvas canvas)55     public void draw(Canvas canvas) {
56         boolean isLtr = getLayoutDirection() == LayoutDirection.LTR;
57         Rect bounds = getBounds();
58         float width = bounds.width() * OUTER_EXTENT;
59         float height = (mTop + bounds.height()) * OUTER_EXTENT;
60         canvas.translate(0, -mTop);
61         canvas.scale(1, height / width);
62         canvas.drawRect(isLtr ? bounds.right - width : 0, 0,
63                 isLtr ? bounds.right : bounds.left + width, width, mRadialGradientPaint);
64     }
65 
66     @Override
setAlpha(int alpha)67     public void setAlpha(int alpha) {
68         mAlpha = alpha;
69         updatePaint();
70         invalidateSelf();
71     }
72 
73     @Override
getAlpha()74     public int getAlpha() {
75         return mAlpha;
76     }
77 
78     @Override
setColorFilter(ColorFilter colorFilter)79     public void setColorFilter(ColorFilter colorFilter) {
80     }
81 
82     @Override
getOpacity()83     public int getOpacity() {
84         return PixelFormat.TRANSLUCENT;
85     }
86 
87     @Override
onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)88     public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
89             int oldTop, int oldRight, int oldBottom) {
90         if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) {
91             mLayoutWidth = right - left;
92             mTop = top;
93             updatePaint();
94         }
95     }
96 
updatePaint()97     private void updatePaint() {
98         if (mLayoutWidth == 0) {
99             return;
100         }
101         float radius = mLayoutWidth * OUTER_EXTENT;
102         boolean isLtr = getLayoutDirection() == LayoutDirection.LTR;
103         mRadialGradientPaint.setShader(
104                 new RadialGradient(isLtr ? mLayoutWidth : 0, 0, radius,
105                         new int[] { Color.argb(
106                                         (int) (Color.alpha(mDarkColor) * mAlpha / 255f), 0, 0, 0),
107                                 Color.TRANSPARENT },
108                         new float[] { Math.max(0f, mLayoutWidth * INNER_EXTENT / radius), 1f },
109                         Shader.TileMode.CLAMP));
110     }
111 }
112