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.app.Activity; 20 import android.graphics.Rect; 21 import android.os.Bundle; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.view.Window; 25 import android.widget.AdapterView; 26 import android.widget.BaseAdapter; 27 import android.widget.EditText; 28 import android.widget.LinearLayout; 29 import android.widget.ListView; 30 import android.widget.TextView; 31 32 import java.util.ArrayList; 33 import java.util.HashMap; 34 import java.util.HashSet; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.Set; 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' parameter 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 position)269 protected void positionSelected(int position) { 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 proportionally 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 mLinearLayout.setFitsSystemWindows(true); 380 setContentView(mLinearLayout); 381 } else { 382 mLinearLayout = new LinearLayout(this); 383 mLinearLayout.setOrientation(LinearLayout.VERTICAL); 384 mLinearLayout.setLayoutParams(new ViewGroup.LayoutParams( 385 ViewGroup.LayoutParams.MATCH_PARENT, 386 ViewGroup.LayoutParams.MATCH_PARENT)); 387 mListView.setLayoutParams((new LinearLayout.LayoutParams( 388 ViewGroup.LayoutParams.MATCH_PARENT, 389 0, 390 1f))); 391 mLinearLayout.addView(mListView); 392 mLinearLayout.setFitsSystemWindows(true); 393 setContentView(mLinearLayout); 394 } 395 mLinearLayout.restoreDefaultFocus(); 396 } 397 398 /** 399 * Override to return false if you don't want the activity to register a default item click 400 * listener that redirects clicks to {@link #positionClicked(int)}. 401 */ shouldRegisterItemClickListener()402 protected boolean shouldRegisterItemClickListener() { 403 return true; 404 } 405 406 /** 407 * @return The newly created ListView widget. 408 */ createListView()409 protected ListView createListView() { 410 return new ListView(this); 411 } 412 413 /** 414 * @return The newly created Params object. 415 */ createParams()416 protected Params createParams() { 417 return new Params(); 418 } 419 420 /** 421 * Sets an adapter on a ListView. 422 * 423 * @param listView The ListView to set the adapter on. 424 */ setAdapter(ListView listView)425 protected void setAdapter(ListView listView) { 426 listView.setAdapter(new MyAdapter()); 427 } 428 429 /** 430 * Read in and validate all of the params passed in by the scenario. 431 * @param params 432 */ readAndValidateParams(Params params)433 protected void readAndValidateParams(Params params) { 434 if (params.mMustFillScreen ) { 435 double totalFactor = 0.0; 436 for (int i = 0; i < params.mNumItems; i++) { 437 if (params.mOverrideItemScreenSizeFactors.containsKey(i)) { 438 totalFactor += params.mOverrideItemScreenSizeFactors.get(i); 439 } else { 440 totalFactor += params.mItemScreenSizeFactor; 441 } 442 } 443 if (totalFactor < 1.0) { 444 throw new IllegalArgumentException("list items must combine to be at least " + 445 "the height of the screen. this is not the case with " + params.mNumItems 446 + " items and " + params.mItemScreenSizeFactor + " screen factor and " + 447 "screen height of " + mScreenHeight); 448 } 449 } 450 451 mNumItems = params.mNumItems; 452 mItemsFocusable = params.mItemsFocusable; 453 mStartingSelectionPosition = params.mStartingSelectionPosition; 454 mItemScreenSizeFactor = params.mItemScreenSizeFactor; 455 456 mOverrideItemScreenSizeFactors.putAll(params.mOverrideItemScreenSizeFactors); 457 458 mUnselectableItems.addAll(params.mUnselectableItems); 459 mIncludeHeader = params.mIncludeHeader; 460 mStackFromBottom = params.mStackFromBottom; 461 mHeaderViewCount = params.mHeaderViewCount; 462 mHeadersFocusable = params.mHeaderFocusable; 463 mFooterViewCount = params.mFooterViewCount; 464 } 465 getValueAtPosition(int position)466 public final String getValueAtPosition(int position) { 467 return isItemAtPositionSelectable(position) 468 ? 469 "position " + position: 470 "------- " + position; 471 } 472 473 /** 474 * @return The height that will be set for a particular position. 475 */ getHeightForPosition(int position)476 public int getHeightForPosition(int position) { 477 int desiredHeight = (int) (mScreenHeight * mItemScreenSizeFactor); 478 if (mOverrideItemScreenSizeFactors.containsKey(position)) { 479 desiredHeight = (int) (mScreenHeight * mOverrideItemScreenSizeFactors.get(position)); 480 } 481 return desiredHeight; 482 } 483 484 /** 485 * @return The contents of the header above the list. 486 * @throws IllegalArgumentException if there is no header. 487 */ getHeaderValue()488 public final String getHeaderValue() { 489 if (!mIncludeHeader) { 490 throw new IllegalArgumentException("no header above list"); 491 } 492 return mHeaderTextView.getText().toString(); 493 } 494 495 /** 496 * @param value What to put in the header text view 497 * @throws IllegalArgumentException if there is no header. 498 */ setHeaderValue(String value)499 protected final void setHeaderValue(String value) { 500 if (!mIncludeHeader) { 501 throw new IllegalArgumentException("no header above list"); 502 } 503 mHeaderTextView.setText(value); 504 } 505 506 /** 507 * Create a view for a list item. Override this to create a custom view beyond 508 * the simple focusable / unfocusable text view. 509 * @param position The position. 510 * @param parent The parent 511 * @param desiredHeight The height the view should be to respect the desired item 512 * to screen height ratio. 513 * @return a view for the list. 514 */ createView(int position, ViewGroup parent, int desiredHeight)515 protected View createView(int position, ViewGroup parent, int desiredHeight) { 516 return ListItemFactory.text(position, parent.getContext(), getValueAtPosition(position), 517 desiredHeight); 518 } 519 520 /** 521 * Convert a non-null view. 522 */ convertView(int position, View convertView, ViewGroup parent)523 public View convertView(int position, View convertView, ViewGroup parent) { 524 return ListItemFactory.convertText(convertView, getValueAtPosition(position), position); 525 } 526 setClickedPosition(int clickedPosition)527 public void setClickedPosition(int clickedPosition) { 528 mClickedPosition = clickedPosition; 529 } 530 getClickedPosition()531 public int getClickedPosition() { 532 return mClickedPosition; 533 } 534 setLongClickedPosition(int longClickedPosition)535 public void setLongClickedPosition(int longClickedPosition) { 536 mLongClickedPosition = longClickedPosition; 537 } 538 getLongClickedPosition()539 public int getLongClickedPosition() { 540 return mLongClickedPosition; 541 } 542 543 /** 544 * Have a child of the list view call {@link View#requestRectangleOnScreen(android.graphics.Rect)}. 545 * @param childIndex The index into the viewgroup children (i.e the children that are 546 * currently visible). 547 * @param rect The rectangle, in the child's coordinates. 548 */ requestRectangleOnScreen(int childIndex, final Rect rect)549 public void requestRectangleOnScreen(int childIndex, final Rect rect) { 550 final View child = getListView().getChildAt(childIndex); 551 552 child.post(new Runnable() { 553 public void run() { 554 child.requestRectangleOnScreen(rect); 555 } 556 }); 557 } 558 559 /** 560 * Return an item type for the specified position in the adapter. Override if your 561 * adapter creates more than one type. 562 */ getItemViewType(int position)563 public int getItemViewType(int position) { 564 return 0; 565 } 566 567 /** 568 * Return the number of types created by the adapter. Override if your 569 * adapter creates more than one type. 570 */ getViewTypeCount()571 public int getViewTypeCount() { 572 return 1; 573 } 574 575 /** 576 * @return The number of times convertView failed 577 */ getConvertMisses()578 public int getConvertMisses() { 579 return mConvertMisses; 580 } 581 582 private class MyAdapter extends BaseAdapter { 583 getCount()584 public int getCount() { 585 return mNumItems; 586 } 587 getItem(int position)588 public Object getItem(int position) { 589 return getValueAtPosition(position); 590 } 591 getItemId(int position)592 public long getItemId(int position) { 593 return position; 594 } 595 596 @Override areAllItemsEnabled()597 public boolean areAllItemsEnabled() { 598 return mUnselectableItems.isEmpty(); 599 } 600 601 @Override isEnabled(int position)602 public boolean isEnabled(int position) { 603 return isItemAtPositionSelectable(position); 604 } 605 getView(int position, View convertView, ViewGroup parent)606 public View getView(int position, View convertView, ViewGroup parent) { 607 View result = null; 608 if (position >= mNumItems || position < 0) { 609 throw new IllegalStateException("position out of range for adapter!"); 610 } 611 612 if (convertView != null) { 613 result = convertView(position, convertView, parent); 614 if (result == null) { 615 mConvertMisses++; 616 } 617 } 618 619 if (result == null) { 620 int desiredHeight = getHeightForPosition(position); 621 result = createView(position, parent, desiredHeight); 622 } 623 return result; 624 } 625 626 @Override getItemViewType(int position)627 public int getItemViewType(int position) { 628 return ListScenario.this.getItemViewType(position); 629 } 630 631 @Override getViewTypeCount()632 public int getViewTypeCount() { 633 return ListScenario.this.getViewTypeCount(); 634 } 635 636 } 637 } 638