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