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