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