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 17package com.android.systemui.statusbar.policy; 18 19import android.content.Context; 20import android.graphics.Canvas; 21import android.graphics.Color; 22import android.graphics.ColorFilter; 23import android.graphics.Paint; 24import android.graphics.PixelFormat; 25import android.graphics.RadialGradient; 26import android.graphics.Rect; 27import android.graphics.Shader; 28import android.graphics.drawable.Drawable; 29import android.util.LayoutDirection; 30import android.view.View; 31 32import com.android.systemui.R; 33 34/** 35 * Gradient background for the user switcher on Keyguard. 36 */ 37public class KeyguardUserSwitcherScrim extends Drawable 38 implements View.OnLayoutChangeListener { 39 40 private static final float OUTER_EXTENT = 2.5f; 41 private static final float INNER_EXTENT = 0.75f; 42 43 private int mDarkColor; 44 private int mTop; 45 private int mAlpha = 255; 46 private Paint mRadialGradientPaint = new Paint(); 47 private int mLayoutWidth; 48 49 public KeyguardUserSwitcherScrim(Context context) { 50 mDarkColor = context.getColor( 51 R.color.keyguard_user_switcher_background_gradient_color); 52 } 53 54 @Override 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 67 public void setAlpha(int alpha) { 68 mAlpha = alpha; 69 updatePaint(); 70 invalidateSelf(); 71 } 72 73 @Override 74 public int getAlpha() { 75 return mAlpha; 76 } 77 78 @Override 79 public void setColorFilter(ColorFilter colorFilter) { 80 } 81 82 @Override 83 public int getOpacity() { 84 return PixelFormat.TRANSLUCENT; 85 } 86 87 @Override 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 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