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