[go: nahoru, domu]

1/*
2 * Copyright (C) 2015 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.setupwizardlib.items;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.ImageView;
25import android.widget.TextView;
26
27import com.android.setupwizardlib.R;
28
29/**
30 * Definition of an item in SetupWizardItemsLayout. An item is usually defined in XML and inflated
31 * using {@link ItemInflater}.
32 */
33public class Item extends AbstractItem {
34
35    private boolean mEnabled = true;
36    private Drawable mIcon;
37    private int mLayoutRes;
38    private CharSequence mSummary;
39    private CharSequence mTitle;
40    private boolean mVisible = true;
41
42    public Item() {
43        super();
44        mLayoutRes = getDefaultLayoutResource();
45    }
46
47    public Item(Context context, AttributeSet attrs) {
48        super(context, attrs);
49        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwItem);
50        mEnabled = a.getBoolean(R.styleable.SuwItem_android_enabled, true);
51        mIcon = a.getDrawable(R.styleable.SuwItem_android_icon);
52        mTitle = a.getText(R.styleable.SuwItem_android_title);
53        mSummary = a.getText(R.styleable.SuwItem_android_summary);
54        mLayoutRes = a.getResourceId(R.styleable.SuwItem_android_layout,
55                getDefaultLayoutResource());
56        mVisible = a.getBoolean(R.styleable.SuwItem_android_visible, true);
57        a.recycle();
58    }
59
60    protected int getDefaultLayoutResource() {
61        return R.layout.suw_items_default;
62    }
63
64    public void setEnabled(boolean enabled) {
65        mEnabled = enabled;
66    }
67
68    @Override
69    public int getCount() {
70        return isVisible() ? 1 : 0;
71    }
72
73    @Override
74    public boolean isEnabled() {
75        return mEnabled;
76    }
77
78    public void setIcon(Drawable icon) {
79        mIcon = icon;
80    }
81
82    public Drawable getIcon() {
83        return mIcon;
84    }
85
86    public void setLayoutResource(int layoutResource) {
87        mLayoutRes = layoutResource;
88    }
89
90    @Override
91    public int getLayoutResource() {
92        return mLayoutRes;
93    }
94
95    public void setSummary(CharSequence summary) {
96        mSummary = summary;
97    }
98
99    public CharSequence getSummary() {
100        return mSummary;
101    }
102
103    public void setTitle(CharSequence title) {
104        mTitle = title;
105    }
106
107    public CharSequence getTitle() {
108        return mTitle;
109    }
110
111    public void setVisible(boolean visible) {
112        mVisible = visible;
113    }
114
115    public boolean isVisible() {
116        return mVisible;
117    }
118
119    public int getViewId() {
120        return getId();
121    }
122
123    @Override
124    public void onBindView(View view) {
125        TextView label = (TextView) view.findViewById(R.id.suw_items_title);
126        label.setText(getTitle());
127
128        TextView summaryView = (TextView) view.findViewById(R.id.suw_items_summary);
129        CharSequence summary = getSummary();
130        if (summary != null && summary.length() > 0) {
131            summaryView.setText(summary);
132            summaryView.setVisibility(View.VISIBLE);
133        } else {
134            summaryView.setVisibility(View.GONE);
135        }
136
137        final View iconContainer = view.findViewById(R.id.suw_items_icon_container);
138        final Drawable icon = getIcon();
139        if (icon != null) {
140            final ImageView iconView = (ImageView) view.findViewById(R.id.suw_items_icon);
141            // Set the image drawable to null before setting the state and level to avoid affecting
142            // any recycled drawable in the ImageView
143            iconView.setImageDrawable(null);
144            iconView.setImageState(icon.getState(), false /* merge */);
145            iconView.setImageLevel(icon.getLevel());
146            iconView.setImageDrawable(icon);
147            iconContainer.setVisibility(View.VISIBLE);
148        } else {
149            iconContainer.setVisibility(View.GONE);
150        }
151
152        view.setId(getViewId());
153    }
154}
155