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.util.InternalSelectionView;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.BaseAdapter;
25 import android.widget.ListView;
26 import android.window.WindowMetricsHelper;
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  */
32 public 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 
getListView()48     public ListView getListView() {
49         return mListView;
50     }
51 
52     /**
53      * Each item is screen height * this factor tall.
54      */
getItemScreenSizeFactor()55     public double getItemScreenSizeFactor() {
56         return mItemScreenSizeFactor;
57     }
58 
59     /**
60      * @return The number of rows per item.
61      */
getNumRowsPerItem()62     public int getNumRowsPerItem() {
63         return mNumRowsPerItem;
64     }
65 
66     /**
67      * @return The number of items in the list.
68      */
getNumItems()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      */
getLabelForPosition(int position)77     public String getLabelForPosition(int position) {
78         return "position " + position;
79     }
80 
81     /**
82      * Get the currently selected view.
83      */
getSelectedView()84     public InternalSelectionView getSelectedView() {
85         return (InternalSelectionView) getListView().getSelectedView();
86     }
87 
88     /**
89      * Get the screen height.
90      */
getScreenHeight()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      */
getBundleFor(int numItems, int numRowsPerItem, double itemScreenHeightFactor)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
onCreate(Bundle icicle)112     protected void onCreate(Bundle icicle) {
113         super.onCreate(icicle);
114 
115         mScreenHeight = WindowMetricsHelper.getBoundsExcludingNavigationBarAndCutout(
116                 getWindowManager().getCurrentWindowMetrics()).height();
117 
118         Bundle extras = getIntent().getExtras();
119         if (extras != null) {
120             initFromBundle(extras);
121         }
122 
123         mListView = new ListView(this);
124         mListView.setLayoutParams(new ViewGroup.LayoutParams(
125                 ViewGroup.LayoutParams.MATCH_PARENT,
126                 ViewGroup.LayoutParams.MATCH_PARENT));
127         mListView.setDrawSelectorOnTop(false);
128         mListView.setAdapter(new MyAdapter());
129         mListView.setItemsCanFocus(true);
130         setContentView(mListView);
131     }
132 
initFromBundle(Bundle icicle)133     private void initFromBundle(Bundle icicle) {
134 
135         int numItems = icicle.getInt(BUNDLE_PARAM_NUM_ITEMS, -1);
136         if (numItems != -1) {
137             mNumItems = numItems;
138         }
139         int numRowsPerItem = icicle.getInt(BUNDLE_PARAM_NUM_ROWS_PER_ITEM, -1);
140         if (numRowsPerItem != -1) {
141             mNumRowsPerItem = numRowsPerItem;
142         }
143         double screenHeightFactor = icicle.getDouble(BUNDLE_PARAM_ITEM_SCREEN_HEIGHT_FACTOR, -1.0);
144         if (screenHeightFactor > 0) {
145             mItemScreenSizeFactor = screenHeightFactor;
146         }
147     }
148 
149     private class MyAdapter extends BaseAdapter {
150 
getCount()151         public int getCount() {
152             return mNumItems;
153         }
154 
getItem(int position)155         public Object getItem(int position) {
156             return getLabelForPosition(position);
157         }
158 
getItemId(int position)159         public long getItemId(int position) {
160             return position;
161         }
162 
getView(int position, View convertView, ViewGroup parent)163         public View getView(int position, View convertView, ViewGroup parent) {
164             InternalSelectionView item =
165                     new InternalSelectionView(
166                             parent.getContext(),
167                             mNumRowsPerItem,
168                             getLabelForPosition(position));
169             item.setDesiredHeight((int) (mScreenHeight * mItemScreenSizeFactor));
170             return item;
171         }
172     }
173 }
174