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 com.android.internal.util.ArrayUtils;
20import com.android.systemui.FontSizeUtils;
21import com.android.systemui.R;
22import com.android.systemui.statusbar.phone.UserAvatarView;
23
24import android.content.Context;
25import android.content.res.Configuration;
26import android.content.res.TypedArray;
27import android.graphics.Bitmap;
28import android.graphics.Typeface;
29import android.graphics.drawable.Drawable;
30import android.util.AttributeSet;
31import android.util.TypedValue;
32import android.view.LayoutInflater;
33import android.view.View;
34import android.view.ViewGroup;
35import android.widget.LinearLayout;
36import android.widget.TextView;
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
48    public UserDetailItemView(Context context) {
49        this(context, null);
50    }
51
52    public UserDetailItemView(Context context, AttributeSet attrs) {
53        this(context, attrs, 0);
54    }
55
56    public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr) {
57        this(context, attrs, defStyleAttr, 0);
58    }
59
60    public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr,
61            int defStyleRes) {
62        super(context, attrs, defStyleAttr, defStyleRes);
63        final TypedArray a = context.obtainStyledAttributes(
64                attrs, R.styleable.UserDetailItemView, defStyleAttr, defStyleRes);
65        final int N = a.getIndexCount();
66        for (int i = 0; i < N; i++) {
67            int attr = a.getIndex(i);
68            switch (attr) {
69                case R.styleable.UserDetailItemView_regularFontFamily:
70                    mRegularTypeface = Typeface.create(a.getString(attr), 0 /* style */);
71                    break;
72                case R.styleable.UserDetailItemView_activatedFontFamily:
73                    mActivatedTypeface = Typeface.create(a.getString(attr), 0 /* style */);
74                    break;
75            }
76        }
77        a.recycle();
78    }
79
80    public static UserDetailItemView convertOrInflate(Context context, View convertView,
81            ViewGroup root) {
82        if (!(convertView instanceof UserDetailItemView)) {
83            convertView = LayoutInflater.from(context).inflate(
84                    R.layout.qs_user_detail_item, root, false);
85        }
86        return (UserDetailItemView) convertView;
87    }
88
89    public void bind(String name, Bitmap picture) {
90        mName.setText(name);
91        mAvatar.setBitmap(picture);
92    }
93
94    public void bind(String name, Drawable picture) {
95        mName.setText(name);
96        mAvatar.setDrawable(picture);
97    }
98
99    @Override
100    protected void onFinishInflate() {
101        mAvatar = (UserAvatarView) findViewById(R.id.user_picture);
102        mName = (TextView) findViewById(R.id.user_name);
103        if (mRegularTypeface == null) {
104            mRegularTypeface = mName.getTypeface();
105        }
106        if (mActivatedTypeface == null) {
107            mActivatedTypeface = mName.getTypeface();
108        }
109        updateTypeface();
110    }
111
112    @Override
113    protected void onConfigurationChanged(Configuration newConfig) {
114        super.onConfigurationChanged(newConfig);
115        FontSizeUtils.updateFontSize(mName, R.dimen.qs_detail_item_secondary_text_size);
116    }
117
118    @Override
119    protected void drawableStateChanged() {
120        super.drawableStateChanged();
121        updateTypeface();
122    }
123
124    private void updateTypeface() {
125        boolean activated = ArrayUtils.contains(getDrawableState(), android.R.attr.state_activated);
126        mName.setTypeface(activated ? mActivatedTypeface : mRegularTypeface);
127    }
128
129    @Override
130    public boolean hasOverlappingRendering() {
131        return false;
132    }
133}
134