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
1900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.content.Context;
20e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggiimport android.content.res.Configuration;
21ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roosimport android.content.res.TypedArray;
2200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.graphics.Bitmap;
23ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roosimport android.graphics.Typeface;
2400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.graphics.drawable.Drawable;
2500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.util.AttributeSet;
2600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.view.LayoutInflater;
2700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.view.View;
2800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.view.ViewGroup;
2900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.widget.LinearLayout;
3000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roosimport android.widget.TextView;
3100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
32c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport com.android.internal.util.ArrayUtils;
33aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Roskyimport com.android.settingslib.drawable.UserIconDrawable;
34c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport com.android.systemui.FontSizeUtils;
35c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport com.android.systemui.R;
36c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport com.android.systemui.statusbar.phone.UserAvatarView;
37c0d7058b14c24cd07912f5629c26b39b7b4673d5Winson
3800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos/**
3900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos * Displays one user in the {@link UserDetailView} view.
4000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos */
4100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roospublic class UserDetailItemView extends LinearLayout {
4200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
4300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    private UserAvatarView mAvatar;
4400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    private TextView mName;
45ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    private Typeface mRegularTypeface;
46ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    private Typeface mActivatedTypeface;
471c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka    private View mRestrictedPadlock;
4800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
4900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context) {
5000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        this(context, null);
5100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
5200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
5300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context, AttributeSet attrs) {
5400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        this(context, attrs, 0);
5500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
5600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
5700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr) {
5800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        this(context, attrs, defStyleAttr, 0);
5900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
6000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
6100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public UserDetailItemView(Context context, AttributeSet attrs, int defStyleAttr,
6200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos            int defStyleRes) {
6300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        super(context, attrs, defStyleAttr, defStyleRes);
641c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka
65ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        final TypedArray a = context.obtainStyledAttributes(
66ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                attrs, R.styleable.UserDetailItemView, defStyleAttr, defStyleRes);
67ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        final int N = a.getIndexCount();
68ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        for (int i = 0; i < N; i++) {
69ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            int attr = a.getIndex(i);
70ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            switch (attr) {
71ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                case R.styleable.UserDetailItemView_regularFontFamily:
72ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    mRegularTypeface = Typeface.create(a.getString(attr), 0 /* style */);
73ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    break;
74ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                case R.styleable.UserDetailItemView_activatedFontFamily:
75ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    mActivatedTypeface = Typeface.create(a.getString(attr), 0 /* style */);
76ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos                    break;
77ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            }
78ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        }
79ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        a.recycle();
8000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
8100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
8200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    public static UserDetailItemView convertOrInflate(Context context, View convertView,
8300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos            ViewGroup root) {
8400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        if (!(convertView instanceof UserDetailItemView)) {
8500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos            convertView = LayoutInflater.from(context).inflate(
8600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos                    R.layout.qs_user_detail_item, root, false);
8700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        }
8800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        return (UserDetailItemView) convertView;
8900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
9000a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
91aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky    public void bind(String name, Bitmap picture, int userId) {
9200a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mName.setText(name);
93aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky        mAvatar.setAvatarWithBadge(picture, userId);
9400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
9500a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
96aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky    public void bind(String name, Drawable picture, int userId) {
9700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mName.setText(name);
98aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky        mAvatar.setDrawableWithBadge(picture, userId);
99aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky    }
100aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky
101aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky    public void setAvatarEnabled(boolean enabled) {
102aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky        mAvatar.setEnabled(enabled);
10300a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
10400a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
1051c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka    public void setDisabledByAdmin(boolean disabled) {
1061c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka        mRestrictedPadlock.setVisibility(disabled ? View.VISIBLE : View.GONE);
1071c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka        mName.setEnabled(!disabled);
108aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky        mAvatar.setEnabled(!disabled);
1091c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka    }
1101c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka
11107140f7ffd56bf9d4946eba4e48b21a748125659Fyodor Kupolov    public void setEnabled(boolean enabled) {
11207140f7ffd56bf9d4946eba4e48b21a748125659Fyodor Kupolov        mName.setEnabled(enabled);
113aa7f51fe8c6e54485f0b5037c17777b4695d19e0Evan Rosky        mAvatar.setEnabled(enabled);
11407140f7ffd56bf9d4946eba4e48b21a748125659Fyodor Kupolov    }
11507140f7ffd56bf9d4946eba4e48b21a748125659Fyodor Kupolov
11600a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    @Override
11700a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    protected void onFinishInflate() {
11800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mAvatar = (UserAvatarView) findViewById(R.id.user_picture);
11900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos        mName = (TextView) findViewById(R.id.user_name);
120ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        if (mRegularTypeface == null) {
121ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            mRegularTypeface = mName.getTypeface();
122ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        }
123ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        if (mActivatedTypeface == null) {
124ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos            mActivatedTypeface = mName.getTypeface();
125ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        }
126ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        updateTypeface();
1271c7cda8bbfb848961536a4adbf2c81c5ffddae16Sudheer Shanka        mRestrictedPadlock = findViewById(R.id.restricted_padlock);
12800a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos    }
12900a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos
130ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    @Override
131e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggi    protected void onConfigurationChanged(Configuration newConfig) {
132e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggi        super.onConfigurationChanged(newConfig);
133e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggi        FontSizeUtils.updateFontSize(mName, R.dimen.qs_detail_item_secondary_text_size);
134e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggi    }
135e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggi
136e17c4b49a41ec9de5c1d7f229273fc4677ec24b1Jorim Jaggi    @Override
137ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    protected void drawableStateChanged() {
138ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        super.drawableStateChanged();
139ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        updateTypeface();
140ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    }
141ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos
142ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    private void updateTypeface() {
143ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        boolean activated = ArrayUtils.contains(getDrawableState(), android.R.attr.state_activated);
144ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos        mName.setTypeface(activated ? mActivatedTypeface : mRegularTypeface);
145ccdff62159b41ab130a8f90d30edb9b9542d8c72Adrian Roos    }
14698f8530af3bb8636b7b173443c90686c485205d6Jorim Jaggi
14798f8530af3bb8636b7b173443c90686c485205d6Jorim Jaggi    @Override
14898f8530af3bb8636b7b173443c90686c485205d6Jorim Jaggi    public boolean hasOverlappingRendering() {
14998f8530af3bb8636b7b173443c90686c485205d6Jorim Jaggi        return false;
15098f8530af3bb8636b7b173443c90686c485205d6Jorim Jaggi    }
15100a0b1f397557790cf9ab55fe06e72a96ebc5353Adrian Roos}
152