Home | History | Annotate | Download | only in policy
      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.policy;
     18 
     19 import android.annotation.Nullable;
     20 import android.graphics.drawable.Drawable;
     21 import android.graphics.drawable.LayerDrawable;
     22 import android.view.Gravity;
     23 
     24 /**
     25  * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light
     26  * navigation bar mode.
     27  */
     28 public class KeyButtonDrawable extends LayerDrawable {
     29 
     30     private final boolean mHasDarkDrawable;
     31 
     32     public static KeyButtonDrawable create(Drawable lightDrawable,
     33             @Nullable Drawable darkDrawable) {
     34         if (darkDrawable != null) {
     35             return new KeyButtonDrawable(
     36                     new Drawable[] { lightDrawable.mutate(), darkDrawable.mutate() });
     37         } else {
     38             return new KeyButtonDrawable(new Drawable[] { lightDrawable.mutate() });
     39         }
     40     }
     41 
     42     private KeyButtonDrawable(Drawable[] drawables) {
     43         super(drawables);
     44         for (int i = 0; i < drawables.length; i++) {
     45             setLayerGravity(i, Gravity.CENTER);
     46         }
     47         mutate();
     48         mHasDarkDrawable = drawables.length > 1;
     49         setDarkIntensity(0f);
     50     }
     51 
     52     public void setDarkIntensity(float intensity) {
     53         if (!mHasDarkDrawable) {
     54             return;
     55         }
     56         getDrawable(0).setAlpha((int) ((1 - intensity) * 255f));
     57         getDrawable(1).setAlpha((int) (intensity * 255f));
     58         invalidateSelf();
     59     }
     60 }
     61