1 /* 2 * Copyright (C) 2007 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.focus; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.BaseAdapter; 24 import android.widget.ListView; 25 import android.util.InternalSelectionView; 26 27 /** 28 * A list of {@link InternalSelectionView}s paramatarized by the number of items, 29 * how many rows in each item, and how tall each item is. 30 */ 31 public class ListOfInternalSelectionViews extends Activity { 32 33 private ListView mListView; 34 35 36 // keys for initializing via Intent params 37 public static final String BUNDLE_PARAM_NUM_ITEMS = "com.google.test.numItems"; 38 public static final String BUNDLE_PARAM_NUM_ROWS_PER_ITEM = "com.google.test.numRowsPerItem"; 39 public static final String BUNDLE_PARAM_ITEM_SCREEN_HEIGHT_FACTOR = "com.google.test.itemScreenHeightFactor"; 40 41 private int mScreenHeight; 42 43 private int mNumItems = 5; 44 private int mNumRowsPerItem = 4; 45 private double mItemScreenSizeFactor = 5 / 4; 46 47 public ListView getListView() { 48 return mListView; 49 } 50 51 /** 52 * Each item is screen height * this factor tall. 53 */ 54 public double getItemScreenSizeFactor() { 55 return mItemScreenSizeFactor; 56 } 57 58 /** 59 * @return The number of rows per item. 60 */ 61 public int getNumRowsPerItem() { 62 return mNumRowsPerItem; 63 } 64 65 /** 66 * @return The number of items in the list. 67 */ 68 public int getNumItems() { 69 return mNumItems; 70 } 71 72 /** 73 * @param position The position 74 * @return The label (closest thing to a value) for the item at position 75 */ 76 public String getLabelForPosition(int position) { 77 return "position " + position; 78 } 79 80 /** 81 * Get the currently selected view. 82 */ 83 public InternalSelectionView getSelectedView() { 84 return (InternalSelectionView) getListView().getSelectedView(); 85 } 86 87 /** 88 * Get the screen height. 89 */ 90 public int getScreenHeight() { 91 return mScreenHeight; 92 } 93 94 /** 95 * Initialize a bundle suitable for sending as the params of the intent that 96 * launches this activity. 97 * @param numItems The number of items in the list. 98 * @param numRowsPerItem The number of rows per item. 99 * @param itemScreenHeightFactor see {@link #getScreenHeight()} 100 * @return the intialized bundle. 101 */ 102 public static Bundle getBundleFor(int numItems, int numRowsPerItem, double itemScreenHeightFactor) { 103 Bundle bundle = new Bundle(); 104 bundle.putInt(BUNDLE_PARAM_NUM_ITEMS, numItems); 105 bundle.putInt(BUNDLE_PARAM_NUM_ROWS_PER_ITEM, numRowsPerItem); 106 bundle.putDouble(BUNDLE_PARAM_ITEM_SCREEN_HEIGHT_FACTOR, itemScreenHeightFactor); 107 return bundle; 108 } 109 110 @Override 111 protected void onCreate(Bundle icicle) { 112 super.onCreate(icicle); 113 mScreenHeight = getWindowManager().getDefaultDisplay().getHeight(); 114 115 Bundle extras = getIntent().getExtras(); 116 if (extras != null) { 117 initFromBundle(extras); 118 } 119 120 mListView = new ListView(this); 121 mListView.setLayoutParams(new ViewGroup.LayoutParams( 122 ViewGroup.LayoutParams.MATCH_PARENT, 123 ViewGroup.LayoutParams.MATCH_PARENT)); 124 mListView.setDrawSelectorOnTop(false); 125 mListView.setAdapter(new MyAdapter()); 126 mListView.setItemsCanFocus(true); 127 setContentView(mListView); 128 } 129 130 private void initFromBundle(Bundle icicle) { 131 132 int numItems = icicle.getInt(BUNDLE_PARAM_NUM_ITEMS, -1); 133 if (numItems != -1) { 134 mNumItems = numItems; 135 } 136 int numRowsPerItem = icicle.getInt(BUNDLE_PARAM_NUM_ROWS_PER_ITEM, -1); 137 if (numRowsPerItem != -1) { 138 mNumRowsPerItem = numRowsPerItem; 139 } 140 double screenHeightFactor = icicle.getDouble(BUNDLE_PARAM_ITEM_SCREEN_HEIGHT_FACTOR, -1.0); 141 if (screenHeightFactor > 0) { 142 mItemScreenSizeFactor = screenHeightFactor; 143 } 144 } 145 146 private class MyAdapter extends BaseAdapter { 147 148 public int getCount() { 149 return mNumItems; 150 } 151 152 public Object getItem(int position) { 153 return getLabelForPosition(position); 154 } 155 156 public long getItemId(int position) { 157 return position; 158 } 159 160 public View getView(int position, View convertView, ViewGroup parent) { 161 InternalSelectionView item = 162 new InternalSelectionView( 163 parent.getContext(), 164 mNumRowsPerItem, 165 getLabelForPosition(position)); 166 item.setDesiredHeight((int) (mScreenHeight * mItemScreenSizeFactor)); 167 return item; 168 } 169 } 170 } 171