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