1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package android.support.v17.leanback.app;
15 
16 import android.os.Bundle;
17 import android.os.Handler;
18 import android.support.v17.leanback.widget.ArrayObjectAdapter;
19 import android.support.v17.leanback.widget.HeaderItem;
20 import android.support.v17.leanback.widget.ListRow;
21 import android.support.v17.leanback.widget.ListRowPresenter;
22 import android.support.v17.leanback.widget.OnItemViewClickedListener;
23 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
24 import android.support.v17.leanback.widget.Presenter;
25 import android.support.v17.leanback.widget.Row;
26 import android.support.v17.leanback.widget.RowPresenter;
27 import android.util.Log;
28 import android.view.View;
29 
30 /**
31  * @hide from javadoc
32  */
33 public class BrowseTestFragment extends BrowseFragment {
34     private static final String TAG = "BrowseTestFragment";
35 
36     final static int DEFAULT_NUM_ROWS = 100;
37     final static int DEFAULT_REPEAT_PER_ROW = 20;
38     final static long DEFAULT_LOAD_DATA_DELAY = 2000;
39     final static boolean DEFAULT_TEST_ENTRANCE_TRANSITION = true;
40 
41     static int NUM_ROWS = DEFAULT_NUM_ROWS;
42     static int REPEAT_PER_ROW = DEFAULT_REPEAT_PER_ROW;
43     static long LOAD_DATA_DELAY = DEFAULT_LOAD_DATA_DELAY;
44     static boolean TEST_ENTRANCE_TRANSITION = DEFAULT_TEST_ENTRANCE_TRANSITION;
45 
46     private ArrayObjectAdapter mRowsAdapter;
47 
48     // For good performance, it's important to use a single instance of
49     // a card presenter for all rows using that presenter.
50     final static StringPresenter sCardPresenter = new StringPresenter();
51 
52     @Override
onCreate(Bundle savedInstanceState)53     public void onCreate(Bundle savedInstanceState) {
54         Log.i(TAG, "onCreate");
55         super.onCreate(savedInstanceState);
56 
57         setTitle("BrowseTestFragment");
58         setHeadersState(HEADERS_ENABLED);
59 
60         setOnSearchClickedListener(new View.OnClickListener() {
61             @Override
62             public void onClick(View view) {
63                 Log.i(TAG, "onSearchClicked");
64             }
65         });
66 
67         setupRows();
68         setOnItemViewClickedListener(new ItemViewClickedListener());
69         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
70             @Override
71             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
72                     RowPresenter.ViewHolder rowViewHolder, Row row) {
73                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
74             }
75         });
76         if (TEST_ENTRANCE_TRANSITION) {
77             // don't run entrance transition if fragment is restored.
78             if (savedInstanceState == null) {
79                 prepareEntranceTransition();
80             }
81         }
82         // simulates in a real world use case  data being loaded two seconds later
83         new Handler().postDelayed(new Runnable() {
84             @Override
85             public void run() {
86                 loadData();
87                 startEntranceTransition();
88             }
89         }, LOAD_DATA_DELAY);
90     }
91 
setupRows()92     private void setupRows() {
93         ListRowPresenter lrp = new ListRowPresenter();
94 
95         mRowsAdapter = new ArrayObjectAdapter(lrp);
96 
97         setAdapter(mRowsAdapter);
98     }
99 
loadData()100     private void loadData() {
101         for (int i = 0; i < NUM_ROWS; ++i) {
102             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter);
103             for (int j = 0; j < REPEAT_PER_ROW; ++j) {
104                 listRowAdapter.add("Hello world");
105                 listRowAdapter.add("This is a test");
106                 listRowAdapter.add("Android TV");
107                 listRowAdapter.add("Leanback");
108                 listRowAdapter.add("Hello world");
109                 listRowAdapter.add("Android TV");
110                 listRowAdapter.add("Leanback");
111                 listRowAdapter.add("GuidedStepFragment");
112             }
113             HeaderItem header = new HeaderItem(i, "Row " + i);
114             mRowsAdapter.add(new ListRow(header, listRowAdapter));
115         }
116 
117     }
118 
119     private final class ItemViewClickedListener implements OnItemViewClickedListener {
120         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)121         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
122                 RowPresenter.ViewHolder rowViewHolder, Row row) {
123             Log.i(TAG, "onItemClicked: " + item + " row " + row);
124         }
125     }
126 }
127