UserDetailItemView.java revision ccdff62159b41ab130a8f90d30edb9b9542d8c72
100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos/*
200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * Copyright (C) 2014 The Android Open Source Project
300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos *
400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License");
500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * you may not use this file except in compliance with the License.
600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * You may obtain a copy of the License at
700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos *
800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos *      http://www.apache.org/licenses/LICENSE-2.0
900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos *
1000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * Unless required by applicable law or agreed to in writing, software
1100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS,
1200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * See the License for the specific language governing permissions and
1400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * limitations under the License
1500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos */
1600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
1700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roospackage com.android.systemui.qs.tiles;
1800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
19ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roosimport com.android.internal.util.ArrayUtils;
2000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport com.android.systemui.R;
2100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport com.android.systemui.statusbar.phone.UserAvatarView;
2200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
2300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.content.Context;
24ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roosimport android.content.res.TypedArray;
2500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.graphics.Bitmap;
26ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roosimport android.graphics.Typeface;
2700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.graphics.drawable.Drawable;
2800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.util.AttributeSet;
2900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.view.LayoutInflater;
3000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.view.View;
3100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.view.ViewGroup;
3200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.widget.LinearLayout;
3300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.widget.TextView;
3400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
3500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos/**
3600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * Displays one user in the {@link UserDetailView} view.
3700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos */
3800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roospublic class UserDetailItemView extends LinearLayout {
3900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
4000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    private UserAvatarView mAvatar;
4100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    private TextView mName;
42ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    private Typeface mRegularTypeface;
43ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    private Typeface mActivatedTypeface;
4400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
4500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context) {
4600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        this(context, null);
4700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
4800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
4900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context, AttributeSet attrs) {
5000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        this(context, attrs, 0);
5100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
5200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
5300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr) {
5400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        this(context, attrs, defStyleAttr, 0);
5500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
5600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
5700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr,
5800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos            int defStyleRes) {
5900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        super(context, attrs, defStyleAttr, defStyleRes);
60ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        final TypedArray a = context.obtainStyledAttributes(
61ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                attrs, R.styleable.UserDetailItemView, defStyleAttr, defStyleRes);
62ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        final int N = a.getIndexCount();
63ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        for (int i = 0; i < N; i++) {
64ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            int attr = a.getIndex(i);
65ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            switch (attr) {
66ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                case R.styleable.UserDetailItemView_regularFontFamily:
67ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    mRegularTypeface = Typeface.create(a.getString(attr), 0 /* style */);
68ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    break;
69ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                case R.styleable.UserDetailItemView_activatedFontFamily:
70ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    mActivatedTypeface = Typeface.create(a.getString(attr), 0 /* style */);
71ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    break;
72ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            }
73ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        }
74ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        a.recycle();
7500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
7600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
7700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public static UserDetailItemView convertOrInflate(Context context, View convertView,
7800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos            ViewGroup root) {
7900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        if (!(convertView instanceof UserDetailItemView)) {
8000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos            convertView = LayoutInflater.from(context).inflate(
8100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos                    R.layout.qs_user_detail_item, root, false);
8200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        }
8300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        return (UserDetailItemView) convertView;
8400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
8500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
8600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public void bind(String name, Bitmap picture) {
8700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mName.setText(name);
8800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mAvatar.setBitmap(picture);
8900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
9000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
9100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public void bind(String name, Drawable picture) {
9200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mName.setText(name);
9300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mAvatar.setDrawable(picture);
9400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
9500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
9600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    @Override
9700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    protected void onFinishInflate() {
9800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mAvatar = (UserAvatarView) findViewById(R.id.user_picture);
9900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mName = (TextView) findViewById(R.id.user_name);
100ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        if (mRegularTypeface == null) {
101ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            mRegularTypeface = mName.getTypeface();
102ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        }
103ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        if (mActivatedTypeface == null) {
104ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            mActivatedTypeface = mName.getTypeface();
105ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        }
106ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        updateTypeface();
10700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
10800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
109ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    @Override
110ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    protected void drawableStateChanged() {
111ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        super.drawableStateChanged();
112ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        updateTypeface();
113ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    }
114ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos
115ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    private void updateTypeface() {
116ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        boolean activated = ArrayUtils.contains(getDrawableState(), android.R.attr.state_activated);
117ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        mName.setTypeface(activated ? mActivatedTypeface : mRegularTypeface);
118ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    }
11900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos}
120