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