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 java.util.ArrayList; 20 import java.util.HashMap; 21 import java.util.HashSet; 22 import java.util.List; 23 import java.util.Map; 24 import java.util.Set; 25 26 import android.app.Activity; 27 import android.graphics.Rect; 28 import android.os.Bundle; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.view.Window; 32 import android.widget.AdapterView; 33 import android.widget.BaseAdapter; 34 import android.widget.EditText; 35 import android.widget.LinearLayout; 36 import android.widget.ListView; 37 import android.widget.TextView; 38 39 /** 40 * Utility base class for creating various List scenarios. Configurable by the number 41 * of items, how tall each item should be (in relation to the screen height), and 42 * what item should start with selection. 43 */ 44 public abstract class ListScenario extends Activity { 45 46 private ListView mListView; 47 private TextView mHeaderTextView; 48 49 private int mNumItems; 50 protected boolean mItemsFocusable; 51 52 private int mStartingSelectionPosition; 53 private double mItemScreenSizeFactor; 54 private Map<Integer, Double> mOverrideItemScreenSizeFactors = new HashMap<>(); 55 56 private int mScreenHeight; 57 58 // whether to include a text view above the list 59 private boolean mIncludeHeader; 60 61 // separators 62 private Set<Integer> mUnselectableItems = new HashSet<Integer>(); 63 64 private boolean mStackFromBottom; 65 66 private int mClickedPosition = -1; 67 68 private int mLongClickedPosition = -1; 69 70 private int mConvertMisses = 0; 71 72 private int mHeaderViewCount; 73 private boolean mHeadersFocusable; 74 75 private int mFooterViewCount; 76 private LinearLayout mLinearLayout; 77 getListView()78 public ListView getListView() { 79 return mListView; 80 } 81 82 /** 83 * Return whether the item at position is selectable (i.e is a separator). 84 * (external users can access this info using the adapter) 85 */ isItemAtPositionSelectable(int position)86 private boolean isItemAtPositionSelectable(int position) { 87 return !mUnselectableItems.contains(position); 88 } 89 90 /** 91 * Better way to pass in optional params than a honkin' paramater list :) 92 */ 93 public static class Params { 94 private int mNumItems = 4; 95 private boolean mItemsFocusable = false; 96 private int mStartingSelectionPosition = 0; 97 private double mItemScreenSizeFactor = 1 / 5; 98 private Double mFadingEdgeScreenSizeFactor = null; 99 100 private Map<Integer, Double> mOverrideItemScreenSizeFactors = new HashMap<>(); 101 102 // separators 103 private List<Integer> mUnselectableItems = new ArrayList<Integer>(8); 104 // whether to include a text view above the list 105 private boolean mIncludeHeader = false; 106 private boolean mStackFromBottom = false; 107 public boolean mMustFillScreen = true; 108 private int mHeaderViewCount; 109 private boolean mHeaderFocusable = false; 110 private int mFooterViewCount; 111 112 private boolean mConnectAdapter = true; 113 114 /** 115 * Set the number of items in the list. 116 */ setNumItems(int numItems)117 public Params setNumItems(int numItems) { 118 mNumItems = numItems; 119 return this; 120 } 121 122 /** 123 * Set whether the items are focusable. 124 */ setItemsFocusable(boolean itemsFocusable)125 public Params setItemsFocusable(boolean itemsFocusable) { 126 mItemsFocusable = itemsFocusable; 127 return this; 128 } 129 130 /** 131 * Set the position that starts selected. 132 * 133 * @param startingSelectionPosition The selected position within the adapter's data set. 134 * Pass -1 if you do not want to force a selection. 135 * @return 136 */ setStartingSelectionPosition(int startingSelectionPosition)137 public Params setStartingSelectionPosition(int startingSelectionPosition) { 138 mStartingSelectionPosition = startingSelectionPosition; 139 return this; 140 } 141 142 /** 143 * Set the factor that determines how tall each item is in relation to the 144 * screen height. 145 */ setItemScreenSizeFactor(double itemScreenSizeFactor)146 public Params setItemScreenSizeFactor(double itemScreenSizeFactor) { 147 mItemScreenSizeFactor = itemScreenSizeFactor; 148 return this; 149 } 150 151 /** 152 * Override the item screen size factor for a particular item. Useful for 153 * creating lists with non-uniform item height. 154 * @param position The position in the list. 155 * @param itemScreenSizeFactor The screen size factor to use for the height. 156 */ setPositionScreenSizeFactorOverride( int position, double itemScreenSizeFactor)157 public Params setPositionScreenSizeFactorOverride( 158 int position, double itemScreenSizeFactor) { 159 mOverrideItemScreenSizeFactors.put(position, itemScreenSizeFactor); 160 return this; 161 } 162 163 /** 164 * Set a position as unselectable (a.k.a a separator) 165 * @param position 166 * @return 167 */ setPositionUnselectable(int position)168 public Params setPositionUnselectable(int position) { 169 mUnselectableItems.add(position); 170 return this; 171 } 172 173 /** 174 * Set positions as unselectable (a.k.a a separator) 175 */ setPositionsUnselectable(int ...positions)176 public Params setPositionsUnselectable(int ...positions) { 177 for (int pos : positions) { 178 setPositionUnselectable(pos); 179 } 180 return this; 181 } 182 183 /** 184 * Include a header text view above the list. 185 * @param includeHeader 186 * @return 187 */ includeHeaderAboveList(boolean includeHeader)188 public Params includeHeaderAboveList(boolean includeHeader) { 189 mIncludeHeader = includeHeader; 190 return this; 191 } 192 193 /** 194 * Sets the stacking direction 195 * @param stackFromBottom 196 * @return 197 */ setStackFromBottom(boolean stackFromBottom)198 public Params setStackFromBottom(boolean stackFromBottom) { 199 mStackFromBottom = stackFromBottom; 200 return this; 201 } 202 203 /** 204 * Sets whether the sum of the height of the list items must be at least the 205 * height of the list view. 206 */ setMustFillScreen(boolean fillScreen)207 public Params setMustFillScreen(boolean fillScreen) { 208 mMustFillScreen = fillScreen; 209 return this; 210 } 211 212 /** 213 * Set the factor for the fading edge length. 214 */ setFadingEdgeScreenSizeFactor(double fadingEdgeScreenSizeFactor)215 public Params setFadingEdgeScreenSizeFactor(double fadingEdgeScreenSizeFactor) { 216 mFadingEdgeScreenSizeFactor = fadingEdgeScreenSizeFactor; 217 return this; 218 } 219 220 /** 221 * Set the number of header views to appear within the list 222 */ setHeaderViewCount(int headerViewCount)223 public Params setHeaderViewCount(int headerViewCount) { 224 mHeaderViewCount = headerViewCount; 225 return this; 226 } 227 228 /** 229 * Set whether the headers should be focusable. 230 * @param headerFocusable Whether the headers should be focusable (i.e 231 * created as edit texts rather than text views). 232 */ setHeaderFocusable(boolean headerFocusable)233 public Params setHeaderFocusable(boolean headerFocusable) { 234 mHeaderFocusable = headerFocusable; 235 return this; 236 } 237 238 /** 239 * Set the number of footer views to appear within the list 240 */ setFooterViewCount(int footerViewCount)241 public Params setFooterViewCount(int footerViewCount) { 242 mFooterViewCount = footerViewCount; 243 return this; 244 } 245 246 /** 247 * Sets whether the {@link ListScenario} will automatically set the 248 * adapter on the list view. If this is false, the client MUST set it 249 * manually (this is useful when adding headers to the list view, which 250 * must be done before the adapter is set). 251 */ setConnectAdapter(boolean connectAdapter)252 public Params setConnectAdapter(boolean connectAdapter) { 253 mConnectAdapter = connectAdapter; 254 return this; 255 } 256 } 257 258 /** 259 * How each scenario customizes its behavior. 260 * @param params 261 */ init(Params params)262 protected abstract void init(Params params); 263 264 /** 265 * Override this if you want to know when something has been selected (perhaps 266 * more importantly, that {@link android.widget.AdapterView.OnItemSelectedListener} has 267 * been triggered). 268 */ positionSelected(int positon)269 protected void positionSelected(int positon) { 270 } 271 272 /** 273 * Override this if you want to know that nothing is selected. 274 */ nothingSelected()275 protected void nothingSelected() { 276 } 277 278 /** 279 * Override this if you want to know when something has been clicked (perhaps 280 * more importantly, that {@link android.widget.AdapterView.OnItemClickListener} has 281 * been triggered). 282 */ positionClicked(int position)283 protected void positionClicked(int position) { 284 setClickedPosition(position); 285 } 286 287 @Override onCreate(Bundle icicle)288 protected void onCreate(Bundle icicle) { 289 super.onCreate(icicle); 290 291 // for test stability, turn off title bar 292 requestWindowFeature(Window.FEATURE_NO_TITLE); 293 294 mScreenHeight = getWindowManager().getDefaultDisplay().getHeight(); 295 296 final Params params = createParams(); 297 init(params); 298 299 readAndValidateParams(params); 300 301 mListView = createListView(); 302 mListView.setLayoutParams(new ViewGroup.LayoutParams( 303 ViewGroup.LayoutParams.MATCH_PARENT, 304 ViewGroup.LayoutParams.MATCH_PARENT)); 305 mListView.setDrawSelectorOnTop(false); 306 307 for (int i=0; i<mHeaderViewCount; i++) { 308 TextView header = mHeadersFocusable ? 309 new EditText(this) : 310 new TextView(this); 311 header.setText("Header: " + i); 312 mListView.addHeaderView(header); 313 } 314 315 for (int i=0; i<mFooterViewCount; i++) { 316 TextView header = new TextView(this); 317 header.setText("Footer: " + i); 318 mListView.addFooterView(header); 319 } 320 321 if (params.mConnectAdapter) { 322 setAdapter(mListView); 323 } 324 325 mListView.setItemsCanFocus(mItemsFocusable); 326 if (mStartingSelectionPosition >= 0) { 327 mListView.setSelection(mStartingSelectionPosition); 328 } 329 mListView.setPadding(0, 0, 0, 0); 330 mListView.setStackFromBottom(mStackFromBottom); 331 mListView.setDivider(null); 332 333 mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 334 public void onItemSelected(AdapterView parent, View v, int position, long id) { 335 positionSelected(position); 336 } 337 338 public void onNothingSelected(AdapterView parent) { 339 nothingSelected(); 340 } 341 }); 342 343 if (shouldRegisterItemClickListener()) { 344 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 345 public void onItemClick(AdapterView parent, View v, int position, long id) { 346 positionClicked(position); 347 } 348 }); 349 } 350 351 // set the fading edge length porportionally to the screen 352 // height for test stability 353 if (params.mFadingEdgeScreenSizeFactor != null) { 354 mListView.setFadingEdgeLength((int) (params.mFadingEdgeScreenSizeFactor * mScreenHeight)); 355 } else { 356 mListView.setFadingEdgeLength((int) ((64.0 / 480) * mScreenHeight)); 357 } 358 359 if (mIncludeHeader) { 360 mLinearLayout = new LinearLayout(this); 361 362 mHeaderTextView = new TextView(this); 363 mHeaderTextView.setText("hi"); 364 mHeaderTextView.setLayoutParams(new LinearLayout.LayoutParams( 365 ViewGroup.LayoutParams.MATCH_PARENT, 366 ViewGroup.LayoutParams.WRAP_CONTENT)); 367 mLinearLayout.addView(mHeaderTextView); 368 369 mLinearLayout.setOrientation(LinearLayout.VERTICAL); 370 mLinearLayout.setLayoutParams(new ViewGroup.LayoutParams( 371 ViewGroup.LayoutParams.MATCH_PARENT, 372 ViewGroup.LayoutParams.MATCH_PARENT)); 373 mListView.setLayoutParams((new LinearLayout.LayoutParams( 374 ViewGroup.LayoutParams.MATCH_PARENT, 375 0, 376 1f))); 377 378 mLinearLayout.addView(mListView); 379 setContentView(mLinearLayout); 380 } else { 381 mLinearLayout = new LinearLayout(this); 382 mLinearLayout.setOrientation(LinearLayout.VERTICAL); 383 mLinearLayout.setLayoutParams(new ViewGroup.LayoutParams( 384 ViewGroup.LayoutParams.MATCH_PARENT, 385 ViewGroup.LayoutParams.MATCH_PARENT)); 386 mListView.setLayoutParams((new LinearLayout.LayoutParams( 387 ViewGroup.LayoutParams.MATCH_PARENT, 388 0, 389 1f))); 390 mLinearLayout.addView(mListView); 391 setContentView(mLinearLayout); 392 } 393 } 394 395 /** 396 * Override to return false if you don't want the activity to register a default item click 397 * listener that redirects clicks to {@link #positionClicked(int)}. 398 */ shouldRegisterItemClickListener()399 protected boolean shouldRegisterItemClickListener() { 400 return true; 401 } 402 403 /** 404 * @return The newly created ListView widget. 405 */ createListView()406 protected ListView createListView() { 407 return new ListView(this); 408 } 409 410 /** 411 * @return The newly created Params object. 412 */ createParams()413 protected Params createParams() { 414 return new Params(); 415 } 416 417 /** 418 * Sets an adapter on a ListView. 419 * 420 * @param listView The ListView to set the adapter on. 421 */ setAdapter(ListView listView)422 protected void setAdapter(ListView listView) { 423 listView.setAdapter(new MyAdapter()); 424 } 425 426 /** 427 * Read in and validate all of the params passed in by the scenario. 428 * @param params 429 */ readAndValidateParams(Params params)430 protected void readAndValidateParams(Params params) { 431 if (params.mMustFillScreen ) { 432 double totalFactor = 0.0; 433 for (int i = 0; i < params.mNumItems; i++) { 434 if (params.mOverrideItemScreenSizeFactors.containsKey(i)) { 435 totalFactor += params.mOverrideItemScreenSizeFactors.get(i); 436 } else { 437 totalFactor += params.mItemScreenSizeFactor; 438 } 439 } 440 if (totalFactor < 1.0) { 441 throw new IllegalArgumentException("list items must combine to be at least " + 442 "the height of the screen. this is not the case with " + params.mNumItems 443 + " items and " + params.mItemScreenSizeFactor + " screen factor and " + 444 "screen height of " + mScreenHeight); 445 } 446 } 447 448 mNumItems = params.mNumItems; 449 mItemsFocusable = params.mItemsFocusable; 450 mStartingSelectionPosition = params.mStartingSelectionPosition; 451 mItemScreenSizeFactor = params.mItemScreenSizeFactor; 452 453 mOverrideItemScreenSizeFactors.putAll(params.mOverrideItemScreenSizeFactors); 454 455 mUnselectableItems.addAll(params.mUnselectableItems); 456 mIncludeHeader = params.mIncludeHeader; 457 mStackFromBottom = params.mStackFromBottom; 458 mHeaderViewCount = params.mHeaderViewCount; 459 mHeadersFocusable = params.mHeaderFocusable; 460 mFooterViewCount = params.mFooterViewCount; 461 } 462 getValueAtPosition(int position)463 public final String getValueAtPosition(int position) { 464 return isItemAtPositionSelectable(position) 465 ? 466 "position " + position: 467 "------- " + position; 468 } 469 470 /** 471 * @return The height that will be set for a particular position. 472 */ getHeightForPosition(int position)473 public int getHeightForPosition(int position) { 474 int desiredHeight = (int) (mScreenHeight * mItemScreenSizeFactor); 475 if (mOverrideItemScreenSizeFactors.containsKey(position)) { 476 desiredHeight = (int) (mScreenHeight * mOverrideItemScreenSizeFactors.get(position)); 477 } 478 return desiredHeight; 479 } 480 481 /** 482 * @return The contents of the header above the list. 483 * @throws IllegalArgumentException if there is no header. 484 */ getHeaderValue()485 public final String getHeaderValue() { 486 if (!mIncludeHeader) { 487 throw new IllegalArgumentException("no header above list"); 488 } 489 return mHeaderTextView.getText().toString(); 490 } 491 492 /** 493 * @param value What to put in the header text view 494 * @throws IllegalArgumentException if there is no header. 495 */ setHeaderValue(String value)496 protected final void setHeaderValue(String value) { 497 if (!mIncludeHeader) { 498 throw new IllegalArgumentException("no header above list"); 499 } 500 mHeaderTextView.setText(value); 501 } 502 503 /** 504 * Create a view for a list item. Override this to create a custom view beyond 505 * the simple focusable / unfocusable text view. 506 * @param position The position. 507 * @param parent The parent 508 * @param desiredHeight The height the view should be to respect the desired item 509 * to screen height ratio. 510 * @return a view for the list. 511 */ createView(int position, ViewGroup parent, int desiredHeight)512 protected View createView(int position, ViewGroup parent, int desiredHeight) { 513 return ListItemFactory.text(position, parent.getContext(), getValueAtPosition(position), 514 desiredHeight); 515 } 516 517 /** 518 * Convert a non-null view. 519 */ convertView(int position, View convertView, ViewGroup parent)520 public View convertView(int position, View convertView, ViewGroup parent) { 521 return ListItemFactory.convertText(convertView, getValueAtPosition(position), position); 522 } 523 setClickedPosition(int clickedPosition)524 public void setClickedPosition(int clickedPosition) { 525 mClickedPosition = clickedPosition; 526 } 527 getClickedPosition()528 public int getClickedPosition() { 529 return mClickedPosition; 530 } 531 setLongClickedPosition(int longClickedPosition)532 public void setLongClickedPosition(int longClickedPosition) { 533 mLongClickedPosition = longClickedPosition; 534 } 535 getLongClickedPosition()536 public int getLongClickedPosition() { 537 return mLongClickedPosition; 538 } 539 540 /** 541 * Have a child of the list view call {@link View#requestRectangleOnScreen(android.graphics.Rect)}. 542 * @param childIndex The index into the viewgroup children (i.e the children that are 543 * currently visible). 544 * @param rect The rectangle, in the child's coordinates. 545 */ requestRectangleOnScreen(int childIndex, final Rect rect)546 public void requestRectangleOnScreen(int childIndex, final Rect rect) { 547 final View child = getListView().getChildAt(childIndex); 548 549 child.post(new Runnable() { 550 public void run() { 551 child.requestRectangleOnScreen(rect); 552 } 553 }); 554 } 555 556 /** 557 * Return an item type for the specified position in the adapter. Override if your 558 * adapter creates more than one type. 559 */ getItemViewType(int position)560 public int getItemViewType(int position) { 561 return 0; 562 } 563 564 /** 565 * Return an the number of types created by the adapter. Override if your 566 * adapter creates more than one type. 567 */ getViewTypeCount()568 public int getViewTypeCount() { 569 return 1; 570 } 571 572 /** 573 * @return The number of times convertView failed 574 */ getConvertMisses()575 public int getConvertMisses() { 576 return mConvertMisses; 577 } 578 579 private class MyAdapter extends BaseAdapter { 580 getCount()581 public int getCount() { 582 return mNumItems; 583 } 584 getItem(int position)585 public Object getItem(int position) { 586 return getValueAtPosition(position); 587 } 588 getItemId(int position)589 public long getItemId(int position) { 590 return position; 591 } 592 593 @Override areAllItemsEnabled()594 public boolean areAllItemsEnabled() { 595 return mUnselectableItems.isEmpty(); 596 } 597 598 @Override isEnabled(int position)599 public boolean isEnabled(int position) { 600 return isItemAtPositionSelectable(position); 601 } 602 getView(int position, View convertView, ViewGroup parent)603 public View getView(int position, View convertView, ViewGroup parent) { 604 View result = null; 605 if (position >= mNumItems || position < 0) { 606 throw new IllegalStateException("position out of range for adapter!"); 607 } 608 609 if (convertView != null) { 610 result = convertView(position, convertView, parent); 611 if (result == null) { 612 mConvertMisses++; 613 } 614 } 615 616 if (result == null) { 617 int desiredHeight = getHeightForPosition(position); 618 result = createView(position, parent, desiredHeight); 619 } 620 return result; 621 } 622 623 @Override getItemViewType(int position)624 public int getItemViewType(int position) { 625 return ListScenario.this.getItemViewType(position); 626 } 627 628 @Override getViewTypeCount()629 public int getViewTypeCount() { 630 return ListScenario.this.getViewTypeCount(); 631 } 632 633 } 634 } 635