1 /*
2  * Copyright (C) 2020 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.content.Context;
20 import android.graphics.Color;
21 import android.util.AttributeSet;
22 import android.util.Log;
23 import android.view.View;
24 
25 import androidx.core.graphics.ColorUtils;
26 
27 import com.android.app.animation.Interpolators;
28 import com.android.keyguard.KeyguardConstants;
29 import com.android.systemui.R;
30 import com.android.systemui.qs.tiles.UserDetailItemView;
31 
32 /**
33  * Displays a user on the keyguard user switcher.
34  */
35 public class KeyguardUserDetailItemView extends UserDetailItemView {
36 
37     private static final String TAG = "KeyguardUserDetailItemView";
38     private static final boolean DEBUG = KeyguardConstants.DEBUG;
39 
40     private static final int ANIMATION_DURATION_FADE_NAME = 240;
41 
42     private float mDarkAmount;
43     private int mTextColor;
44 
KeyguardUserDetailItemView(Context context)45     public KeyguardUserDetailItemView(Context context) {
46         this(context, null);
47     }
48 
KeyguardUserDetailItemView(Context context, AttributeSet attrs)49     public KeyguardUserDetailItemView(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52 
KeyguardUserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr)53     public KeyguardUserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr) {
54         this(context, attrs, defStyleAttr, 0);
55     }
56 
KeyguardUserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)57     public KeyguardUserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr,
58             int defStyleRes) {
59         super(context, attrs, defStyleAttr, defStyleRes);
60     }
61 
62     @Override
getFontSizeDimen()63     protected int getFontSizeDimen() {
64         return R.dimen.kg_user_switcher_text_size;
65     }
66 
67     @Override
onFinishInflate()68     protected void onFinishInflate() {
69         super.onFinishInflate();
70         mTextColor = mName.getCurrentTextColor();
71         updateDark();
72     }
73 
74     /**
75      * Update visibility of this view.
76      *
77      * @param showItem If true, this item is visible on the screen to the user. Generally this
78      *                 means that the item would be clickable. If false, item visibility will be
79      *                 set to GONE and hidden entirely.
80      * @param showTextName Whether or not the name should be shown next to the icon. If false,
81      *                     only the icon is shown.
82      * @param animate Whether the transition should be animated. Note, this only applies to
83      *                animating the text name. The item itself will not animate (i.e. fade in/out).
84      *                Instead, we delegate that to the parent view.
85      */
updateVisibilities(boolean showItem, boolean showTextName, boolean animate)86     void updateVisibilities(boolean showItem, boolean showTextName, boolean animate) {
87         if (DEBUG) {
88             Log.d(TAG, String.format("updateVisibilities itemIsShown=%b nameIsShown=%b animate=%b",
89                     showItem, showTextName, animate));
90         }
91 
92         getBackground().setAlpha((showItem && showTextName) ? 255 : 0);
93 
94         if (showItem) {
95             if (showTextName) {
96                 mName.setVisibility(View.VISIBLE);
97                 if (animate) {
98                     mName.setAlpha(0f);
99                     mName.animate()
100                             .alpha(1f)
101                             .setDuration(ANIMATION_DURATION_FADE_NAME)
102                             .setInterpolator(Interpolators.ALPHA_IN);
103                 } else {
104                     mName.setAlpha(1f);
105                 }
106             } else {
107                 if (animate) {
108                     mName.setVisibility(View.VISIBLE);
109                     mName.setAlpha(1f);
110                     mName.animate()
111                             .alpha(0f)
112                             .setDuration(ANIMATION_DURATION_FADE_NAME)
113                             .setInterpolator(Interpolators.ALPHA_OUT)
114                             .withEndAction(() -> {
115                                 mName.setVisibility(View.GONE);
116                                 mName.setAlpha(1f);
117                             });
118                 } else {
119                     mName.setVisibility(View.GONE);
120                     mName.setAlpha(1f);
121                 }
122             }
123             setVisibility(View.VISIBLE);
124             setAlpha(1f);
125         } else {
126             // If item isn't shown, don't animate. The parent class will animate the view instead
127             setVisibility(View.GONE);
128             setAlpha(1f);
129             mName.setVisibility(showTextName ? View.VISIBLE : View.GONE);
130             mName.setAlpha(1f);
131         }
132     }
133 
134     /**
135      * Set the amount (ratio) that the device has transitioned to doze.
136      *
137      * @param darkAmount Amount of transition to doze: 1f for doze and 0f for awake.
138      */
setDarkAmount(float darkAmount)139     public void setDarkAmount(float darkAmount) {
140         if (mDarkAmount == darkAmount) {
141             return;
142         }
143         mDarkAmount = darkAmount;
144         updateDark();
145     }
146 
updateDark()147     private void updateDark() {
148         final int blendedTextColor = ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
149         mName.setTextColor(blendedTextColor);
150     }
151 }
152