1 /*
2  * Copyright (C) 2008 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 android.widget.cts.util;
18 
19 import android.content.Context;
20 import android.view.Gravity;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AbsListView;
24 import android.widget.Button;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 
28 /**
29  * Reusable methods for creating more complex list items.
30  */
31 public class ListItemFactory {
32 
33     /**
34      * Create a view with a button at the top and bottom, with filler in between.
35      * The filler is sized to take up any space left over within desiredHeight.
36      *
37      * @param position      The position within the list.
38      * @param context       The context.
39      * @param desiredHeight The desired height of the entire view.
40      * @return The created view.
41      */
twoButtonsSeparatedByFiller(int position, Context context, int desiredHeight)42     public static View twoButtonsSeparatedByFiller(int position, Context context, int desiredHeight) {
43         if (desiredHeight < 90) {
44             throw new IllegalArgumentException("need at least 90 pixels of height " +
45                     "to create the two buttons and leave 10 pixels for the filler");
46         }
47 
48         final LinearLayout ll = new LinearLayout(context);
49         ll.setOrientation(LinearLayout.VERTICAL);
50 
51         final LinearLayout.LayoutParams buttonLp =
52                 new LinearLayout.LayoutParams(
53                         ViewGroup.LayoutParams.MATCH_PARENT,
54                         50);
55 
56         final Button topButton = new Button(context);
57         topButton.setLayoutParams(
58                 buttonLp);
59         topButton.setText("top (position " + position + ")");
60         ll.addView(topButton);
61 
62         final TextView middleFiller = new TextView(context);
63         middleFiller.setLayoutParams(new LinearLayout.LayoutParams(
64                 ViewGroup.LayoutParams.MATCH_PARENT,
65                 desiredHeight - 100));
66         middleFiller.setText("filler");
67         ll.addView(middleFiller);
68 
69         final Button bottomButton = new Button(context);
70         bottomButton.setLayoutParams(buttonLp);
71         bottomButton.setText("bottom (position " + position + ")");
72         ll.addView(bottomButton);
73         ll.setTag("twoButtons");
74         return ll;
75     }
76 
77     public enum Slot {
78         Left,
79         Middle,
80         Right
81     }
82 
83     /**
84      * Create a horizontal linear layout divided into thirds (with some margins
85      * separating the thirds), filled with buttons into some slots.
86      * @param context The context.
87      * @param desiredHeight The height of the LL.
88      * @param slots Which slots to fill with buttons.
89      * @return The linear layout.
90      */
horizontalButtonSlots(Context context, int desiredHeight, Slot... slots)91     public static View horizontalButtonSlots(Context context, int desiredHeight, Slot... slots) {
92 
93         final LinearLayout ll = new LinearLayout(context);
94         ll.setOrientation(LinearLayout.HORIZONTAL);
95 
96         final LinearLayout.LayoutParams lp
97                 = new LinearLayout.LayoutParams(0, desiredHeight);
98         lp.setMargins(10, 0, 10, 0);
99         lp.weight = 0.33f;
100 
101         boolean left = false;
102         boolean middle = false;
103         boolean right = false;
104         for (Slot slot : slots) {
105             switch (slot) {
106                 case Left:
107                     left = true;
108                     break;
109                 case Middle:
110                     middle = true;
111                     break;
112                 case Right:
113                     right = true;
114                     break;
115             }
116         }
117 
118         if (left) {
119             final Button button = new Button(context);
120             button.setText("left");
121             ll.addView(button, lp);
122         } else {
123            ll.addView(new View(context), lp);
124         }
125 
126         if (middle) {
127             final Button button = new Button(context);
128             button.setText("center");
129             ll.addView(button, lp);
130         } else {
131            ll.addView(new View(context), lp);
132         }
133 
134         if (right) {
135             final Button button = new Button(context);
136             button.setText("right");
137             ll.addView(button, lp);
138         } else {
139            ll.addView(new View(context), lp);
140         }
141 
142         return ll;
143     }
144 
145     /**
146      * Create a button ready to be a list item.
147      *
148      * @param position      The position within the list.
149      * @param context       The context.
150      * @param text          The text of the button
151      * @param desiredHeight The desired height of the button
152      * @return The created view.
153      */
button(int position, Context context, String text, int desiredHeight)154     public static View button(int position, Context context, String text, int desiredHeight) {
155         TextView result = new Button(context);
156         result.setHeight(desiredHeight);
157         result.setText(text);
158         final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
159                 ViewGroup.LayoutParams.MATCH_PARENT,
160                 ViewGroup.LayoutParams.WRAP_CONTENT);
161         result.setLayoutParams(lp);
162         result.setId(position);
163         result.setTag("button");
164         return result;
165     }
166 
167     /**
168      * Convert an existing button view to display the data at a new position.
169      *
170      * @param convertView Non-null Button created by {@link #button}
171      * @param text The text of the button
172      * @param position The position withion the list
173      * @return The converted view
174      */
convertButton(View convertView, String text, int position)175     public static View convertButton(View convertView, String text, int position) {
176         if (((String) convertView.getTag()).equals("button")) {
177             ((Button) convertView).setText(text);
178             convertView.setId(position);
179             return convertView;
180         } else {
181             return null;
182         }
183     }
184 
185     /**
186      * Create a text view ready to be a list item.
187      *
188      * @param position      The position within the list.
189      * @param context       The context.
190      * @param text          The text to display
191      * @param desiredHeight The desired height of the text view
192      * @return The created view.
193      */
text(int position, Context context, String text, int desiredHeight)194     public static View text(int position, Context context, String text, int desiredHeight) {
195         TextView result = new TextView(context);
196         result.setHeight(desiredHeight);
197         result.setText(text);
198         final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
199                 ViewGroup.LayoutParams.MATCH_PARENT,
200                 ViewGroup.LayoutParams.WRAP_CONTENT);
201         result.setLayoutParams(lp);
202         result.setId(position);
203         result.setTag("text");
204         return result;
205     }
206 
207     /**
208      * Convert an existing text view to display the data at a new position.
209      *
210      * @param convertView Non-null TextView created by {@link #text}
211      * @param text The text to display
212      * @param position The position withion the list
213      * @return The converted view
214      */
convertText(View convertView, String text, int position)215     public static View convertText(View convertView, String text, int position) {
216         if(convertView.getTag() != null && ((String) convertView.getTag()).equals("text")) {
217             ((TextView) convertView).setText(text);
218             convertView.setId(position);
219             return convertView;
220 
221         } else {
222             return null;
223         }
224     }
225 
226     /**
227      * Create a text view ready to be a list item.
228      *
229      * @param position      The position within the list.
230      * @param context       The context.
231      * @param text          The text of the button
232      * @param desiredHeight The desired height of the button
233      * @return The created view.
234      */
doubleText(int position, Context context, String text, int desiredHeight)235     public static View doubleText(int position, Context context, String text, int desiredHeight) {
236         final LinearLayout ll = new LinearLayout(context);
237         ll.setOrientation(LinearLayout.HORIZONTAL);
238 
239         final AbsListView.LayoutParams lp =
240                 new AbsListView.LayoutParams(
241                         ViewGroup.LayoutParams.MATCH_PARENT,
242                         desiredHeight);
243         ll.setLayoutParams(lp);
244         ll.setId(position);
245 
246         TextView t1 = new TextView(context);
247         t1.setHeight(desiredHeight);
248         t1.setText(text);
249         t1.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
250         final ViewGroup.LayoutParams lp1 = new LinearLayout.LayoutParams(
251                 0,
252                 ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
253         ll.addView(t1, lp1);
254 
255         TextView t2 = new TextView(context);
256         t2.setHeight(desiredHeight);
257         t2.setText(text);
258         t2.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
259         final ViewGroup.LayoutParams lp2 = new LinearLayout.LayoutParams(
260                 0,
261                 ViewGroup.LayoutParams.WRAP_CONTENT,
262                 1.0f);
263 
264         ll.addView(t2, lp2);
265         ll.setTag("double");
266         return ll;
267     }
268 
269     /**
270      * Convert an existing button view to display the data at a new position.
271      *
272      * @param convertView Non-null view created by {@link #doubleText}
273      * @param text The text of the button
274      * @param position The position withion the list
275      * @return The converted view
276      */
convertDoubleText(View convertView, String text, int position)277     public static View convertDoubleText(View convertView, String text, int position) {
278         if (((String) convertView.getTag()).equals("double")) {
279             TextView t1 = (TextView) ((LinearLayout) convertView).getChildAt(0);
280             TextView t2 = (TextView) ((LinearLayout) convertView).getChildAt(1);
281             t1.setText(text);
282             t2.setText(text);
283             convertView.setId(position);
284             return convertView;
285         } else {
286             return null;
287         }
288     }
289 }
290