• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.google.android.setupdesign.items;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.widget.Button;
23 import android.widget.LinearLayout;
24 import com.google.android.setupdesign.R;
25 import java.util.ArrayList;
26 
27 /**
28  * A list item with one or more buttons, declared as {@link
29  * com.google.android.setupdesign.items.ButtonItem}.
30  *
31  * <p>Example usage:
32  *
33  * <pre>{@code
34  * &lt;ButtonBarItem&gt;
35  *
36  *     &lt;ButtonItem
37  *         android:id="@+id/skip_button"
38  *         android:text="@string/skip_button_label /&gt;
39  *
40  *     &lt;ButtonItem
41  *         android:id="@+id/next_button"
42  *         android:text="@string/next_button_label
43  *         android:theme="@style/SudButtonItem.Colored" /&gt;
44  *
45  * &lt;/ButtonBarItem&gt;
46  * }</pre>
47  */
48 public class ButtonBarItem extends AbstractItem implements ItemInflater.ItemParent {
49 
50   private final ArrayList<ButtonItem> buttons = new ArrayList<>();
51   private boolean visible = true;
52 
ButtonBarItem()53   public ButtonBarItem() {
54     super();
55   }
56 
ButtonBarItem(Context context, AttributeSet attrs)57   public ButtonBarItem(Context context, AttributeSet attrs) {
58     super(context, attrs);
59   }
60 
61   @Override
getCount()62   public int getCount() {
63     return isVisible() ? 1 : 0;
64   }
65 
66   @Override
isEnabled()67   public boolean isEnabled() {
68     // The children buttons are enabled and clickable, but the item itself is not
69     return false;
70   }
71 
72   @Override
getLayoutResource()73   public int getLayoutResource() {
74     return R.layout.sud_items_button_bar;
75   }
76 
setVisible(boolean visible)77   public void setVisible(boolean visible) {
78     this.visible = visible;
79   }
80 
isVisible()81   public boolean isVisible() {
82     return visible;
83   }
84 
85   @Override
getViewId()86   public int getViewId() {
87     return getId();
88   }
89 
90   @Override
onBindView(View view)91   public void onBindView(View view) {
92     // Note: The efficiency could be improved by trying to recycle the buttons created by
93     // ButtonItem
94     final LinearLayout layout = (LinearLayout) view;
95     layout.removeAllViews();
96 
97     for (ButtonItem buttonItem : buttons) {
98       Button button = buttonItem.createButton(layout);
99       layout.addView(button);
100     }
101 
102     view.setId(getViewId());
103   }
104 
105   @Override
addChild(ItemHierarchy child)106   public void addChild(ItemHierarchy child) {
107     if (child instanceof ButtonItem) {
108       buttons.add((ButtonItem) child);
109     } else {
110       throw new UnsupportedOperationException("Cannot add non-button item to Button Bar");
111     }
112   }
113 
114   @Override
findItemById(int id)115   public ItemHierarchy findItemById(int id) {
116     if (getId() == id) {
117       return this;
118     }
119     for (ButtonItem button : buttons) {
120       final ItemHierarchy item = button.findItemById(id);
121       if (item != null) {
122         return item;
123       }
124     }
125     return null;
126   }
127 }
128