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.qs.tiles;
18 
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.content.res.TypedArray;
22 import android.graphics.Bitmap;
23 import android.graphics.drawable.Drawable;
24 import android.util.AttributeSet;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.LinearLayout;
29 import android.widget.TextView;
30 
31 import com.android.internal.util.ArrayUtils;
32 import com.android.systemui.FontSizeUtils;
33 import com.android.systemui.R;
34 import com.android.systemui.statusbar.phone.UserAvatarView;
35 
36 /**
37  * Displays one user in the {@link UserDetailView} view.
38  */
39 public class UserDetailItemView extends LinearLayout {
40 
41     protected static int layoutResId = R.layout.qs_user_detail_item;
42 
43     private UserAvatarView mAvatar;
44     private TextView mName;
45     private int mActivatedStyle;
46     private int mRegularStyle;
47     private View mRestrictedPadlock;
48 
UserDetailItemView(Context context)49     public UserDetailItemView(Context context) {
50         this(context, null);
51     }
52 
UserDetailItemView(Context context, AttributeSet attrs)53     public UserDetailItemView(Context context, AttributeSet attrs) {
54         this(context, attrs, 0);
55     }
56 
UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr)57     public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr) {
58         this(context, attrs, defStyleAttr, 0);
59     }
60 
UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)61     public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr,
62             int defStyleRes) {
63         super(context, attrs, defStyleAttr, defStyleRes);
64 
65         final TypedArray a = context.obtainStyledAttributes(
66                 attrs, R.styleable.UserDetailItemView, defStyleAttr, defStyleRes);
67         final int N = a.getIndexCount();
68         for (int i = 0; i < N; i++) {
69             int attr = a.getIndex(i);
70             if (attr == R.styleable.UserDetailItemView_regularTextAppearance) {
71                 mRegularStyle = a.getResourceId(attr, 0);
72             } else if (attr == R.styleable.UserDetailItemView_activatedTextAppearance) {
73                 mActivatedStyle = a.getResourceId(attr, 0);
74             }
75         }
76         a.recycle();
77     }
78 
convertOrInflate(Context context, View convertView, ViewGroup root)79     public static UserDetailItemView convertOrInflate(Context context, View convertView,
80             ViewGroup root) {
81         if (!(convertView instanceof UserDetailItemView)) {
82             convertView = LayoutInflater.from(context).inflate(
83                     layoutResId, root, false);
84         }
85         return (UserDetailItemView) convertView;
86     }
87 
bind(String name, Bitmap picture, int userId)88     public void bind(String name, Bitmap picture, int userId) {
89         mName.setText(name);
90         mAvatar.setAvatarWithBadge(picture, userId);
91     }
92 
bind(String name, Drawable picture, int userId)93     public void bind(String name, Drawable picture, int userId) {
94         mName.setText(name);
95         mAvatar.setDrawableWithBadge(picture, userId);
96     }
97 
setDisabledByAdmin(boolean disabled)98     public void setDisabledByAdmin(boolean disabled) {
99         if (mRestrictedPadlock != null) {
100             mRestrictedPadlock.setVisibility(disabled ? View.VISIBLE : View.GONE);
101         }
102         setEnabled(!disabled);
103     }
104 
setEnabled(boolean enabled)105     public void setEnabled(boolean enabled) {
106         super.setEnabled(enabled);
107         mName.setEnabled(enabled);
108         mAvatar.setEnabled(enabled);
109     }
110 
111     @Override
onFinishInflate()112     protected void onFinishInflate() {
113         mAvatar = findViewById(R.id.user_picture);
114         mName = findViewById(R.id.user_name);
115 
116         if (mRegularStyle == 0) {
117             mRegularStyle = mName.getExplicitStyle();
118         }
119 
120         if (mActivatedStyle == 0) {
121             mActivatedStyle = mName.getExplicitStyle();
122         }
123 
124         updateTextStyle();
125         mRestrictedPadlock = findViewById(R.id.restricted_padlock);
126     }
127 
128     @Override
onConfigurationChanged(Configuration newConfig)129     protected void onConfigurationChanged(Configuration newConfig) {
130         super.onConfigurationChanged(newConfig);
131         FontSizeUtils.updateFontSize(mName, getFontSizeDimen());
132     }
133 
134     @Override
drawableStateChanged()135     protected void drawableStateChanged() {
136         super.drawableStateChanged();
137         updateTextStyle();
138     }
139 
updateTextStyle()140     private void updateTextStyle() {
141         boolean activated = ArrayUtils.contains(getDrawableState(), android.R.attr.state_activated);
142         mName.setTextAppearance(activated ? mActivatedStyle : mRegularStyle);
143     }
144 
145     @Override
hasOverlappingRendering()146     public boolean hasOverlappingRendering() {
147         return false;
148     }
149 
getFontSizeDimen()150     protected int getFontSizeDimen() {
151         return R.dimen.qs_detail_item_secondary_text_size;
152     }
153 }
154