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.content.Context;
     21 import android.content.res.Resources;
     22 import android.graphics.Color;
     23 import android.graphics.drawable.Drawable;
     24 import android.graphics.drawable.LayerDrawable;
     25 import android.view.Gravity;
     26 
     27 import com.android.systemui.R;
     28 import com.android.systemui.statusbar.phone.ShadowKeyDrawable;
     29 
     30 /**
     31  * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light
     32  * navigation bar mode.
     33  */
     34 public class KeyButtonDrawable extends LayerDrawable {
     35 
     36     private final boolean mHasDarkDrawable;
     37 
     38     public static KeyButtonDrawable create(Context lightContext, Drawable lightDrawable,
     39             @Nullable Drawable darkDrawable, boolean hasShadow) {
     40         if (darkDrawable != null) {
     41             ShadowKeyDrawable light = new ShadowKeyDrawable(lightDrawable.mutate());
     42             ShadowKeyDrawable dark = new ShadowKeyDrawable(darkDrawable.mutate());
     43             if (hasShadow) {
     44                 // Only apply the shadow on the light drawable
     45                 Resources res = lightContext.getResources();
     46                 int offsetX = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_x);
     47                 int offsetY = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_y);
     48                 int radius = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_radius);
     49                 int color = lightContext.getColor(R.color.nav_key_button_shadow_color);
     50                 light.setShadowProperties(offsetX, offsetY, radius, color);
     51             }
     52             return new KeyButtonDrawable(new Drawable[] { light, dark });
     53         } else {
     54             return new KeyButtonDrawable(new Drawable[] {
     55                     new ShadowKeyDrawable(lightDrawable.mutate()) });
     56         }
     57     }
     58 
     59     protected KeyButtonDrawable(Drawable[] drawables) {
     60         super(drawables);
     61         for (int i = 0; i < drawables.length; i++) {
     62             setLayerGravity(i, Gravity.CENTER);
     63         }
     64         mutate();
     65         mHasDarkDrawable = drawables.length > 1;
     66         setDarkIntensity(0f);
     67     }
     68 
     69     public void setDarkIntensity(float intensity) {
     70         if (!mHasDarkDrawable) {
     71             return;
     72         }
     73         getDrawable(0).setAlpha((int) ((1 - intensity) * 255f));
     74         getDrawable(1).setAlpha((int) (intensity * 255f));
     75         invalidateSelf();
     76     }
     77 
     78     public void setRotation(float degrees) {
     79         if (getDrawable(0) instanceof ShadowKeyDrawable) {
     80             ((ShadowKeyDrawable) getDrawable(0)).setRotation(degrees);
     81         }
     82         if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) {
     83             ((ShadowKeyDrawable) getDrawable(1)).setRotation(degrees);
     84         }
     85     }
     86 }
     87