blob: a5214288a7c352687ca3b3d9a484329a7f04fc29 [file] [log] [blame]
Mick Lin97850c02014-09-11 16:36:08 +08001/*
2* Copyright (C) 2011-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.internal.widget;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.text.TextUtils;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28
29import com.android.internal.R;
30import com.android.internal.widget.AccountViewAdapter.AccountElements;
31
32
33/**
34 * An LinearLayout view, to show Accounts elements.
35 */
36public class AccountItemView extends LinearLayout {
37
38 private ImageView mAccountIcon;
39 private TextView mAccountName;
40 private TextView mAccountNumber;
41
42 /**
43 * Constructor.
44 */
45 public AccountItemView(Context context) {
46 this(context, null);
47 }
48
49 /**
50 * Constructor.
51 */
52 public AccountItemView(Context context, AttributeSet attrs) {
53 super(context, attrs);
54 LayoutInflater inflator = (LayoutInflater)
55 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
56 View view = inflator.inflate(R.layout.simple_account_item, null);
57 addView(view);
58 initViewItem(view);
59 }
60
61 private void initViewItem(View view) {
62 mAccountIcon = (ImageView)view.findViewById(android.R.id.icon);
63 mAccountName = (TextView)view.findViewById(android.R.id.title);
64 mAccountNumber = (TextView)view.findViewById(android.R.id.summary);
65 }
66
67 public void setViewItem(AccountElements element) {
68 Drawable drawable = element.getDrawable();
69 if (drawable != null) {
70 setAccountIcon(drawable);
71 } else {
72 setAccountIcon(element.getIcon());
73 }
74 setAccountName(element.getName());
75 setAccountNumber(element.getNumber());
76 }
77
78 public void setAccountIcon(int resId) {
79 mAccountIcon.setImageResource(resId);
80 }
81
82 public void setAccountIcon(Drawable drawable) {
83 mAccountIcon.setBackgroundDrawable(drawable);
84 }
85
86 public void setAccountName(String name) {
87 setText(mAccountName, name);
88 }
89
90 public void setAccountNumber(String number) {
91 setText(mAccountNumber, number);
92 }
93
94 private void setText(TextView view, String text) {
95 if (TextUtils.isEmpty(text)) {
96 view.setVisibility(View.GONE);
97 } else {
98 view.setText(text);
99 view.setVisibility(View.VISIBLE);
100 }
101 }
102}