1 // CHECKSTYLE:OFF Generated code
2 /* This file is auto-generated from BrowseTestSupportFragment.java.  DO NOT MODIFY. */
3 
4 /*
5  * Copyright (C) 2015 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 package androidx.leanback.app;
18 
19 import static androidx.leanback.app.BrowseFragmentTestActivity.EXTRA_HEADERS_STATE;
20 import static androidx.leanback.app.BrowseFragmentTestActivity.EXTRA_LOAD_DATA_DELAY;
21 import static androidx.leanback.app.BrowseFragmentTestActivity.EXTRA_NUM_ROWS;
22 import static androidx.leanback.app.BrowseFragmentTestActivity.EXTRA_REPEAT_PER_ROW;
23 import static androidx.leanback.app.BrowseFragmentTestActivity.EXTRA_SET_ADAPTER_AFTER_DATA_LOAD;
24 import static androidx.leanback.app.BrowseFragmentTestActivity.EXTRA_TEST_ENTRANCE_TRANSITION;
25 
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.util.Log;
29 import android.view.View;
30 
31 import androidx.leanback.widget.ArrayObjectAdapter;
32 import androidx.leanback.widget.HeaderItem;
33 import androidx.leanback.widget.ListRow;
34 import androidx.leanback.widget.ListRowPresenter;
35 import androidx.leanback.widget.OnItemViewClickedListener;
36 import androidx.leanback.widget.OnItemViewSelectedListener;
37 import androidx.leanback.widget.Presenter;
38 import androidx.leanback.widget.Row;
39 import androidx.leanback.widget.RowPresenter;
40 import androidx.leanback.widget.VerticalGridView;
41 
42 public class BrowseTestFragment extends BrowseFragment {
43     private static final String TAG = "BrowseTestFragment";
44 
45     final static int DEFAULT_NUM_ROWS = 100;
46     final static int DEFAULT_REPEAT_PER_ROW = 20;
47     final static long DEFAULT_LOAD_DATA_DELAY = 2000;
48     final static boolean DEFAULT_TEST_ENTRANCE_TRANSITION = true;
49     final static boolean DEFAULT_SET_ADAPTER_AFTER_DATA_LOAD = false;
50 
51     private ArrayObjectAdapter mRowsAdapter;
52 
53     // For good performance, it's important to use a single instance of
54     // a card presenter for all rows using that presenter.
55     final static StringPresenter sCardPresenter = new StringPresenter();
56 
57     int NUM_ROWS;
58     int REPEAT_PER_ROW;
59     boolean mEntranceTransitionStarted;
60     boolean mEntranceTransitionEnded;
61 
62     @Override
onCreate(Bundle savedInstanceState)63     public void onCreate(Bundle savedInstanceState) {
64         Log.i(TAG, "onCreate");
65         super.onCreate(savedInstanceState);
66 
67         Bundle arguments = getArguments();
68         NUM_ROWS = arguments.getInt(EXTRA_NUM_ROWS, BrowseTestFragment.DEFAULT_NUM_ROWS);
69         REPEAT_PER_ROW = arguments.getInt(EXTRA_REPEAT_PER_ROW,
70                 DEFAULT_REPEAT_PER_ROW);
71         long LOAD_DATA_DELAY = arguments.getLong(EXTRA_LOAD_DATA_DELAY,
72                 DEFAULT_LOAD_DATA_DELAY);
73         boolean TEST_ENTRANCE_TRANSITION = arguments.getBoolean(
74                 EXTRA_TEST_ENTRANCE_TRANSITION,
75                 DEFAULT_TEST_ENTRANCE_TRANSITION);
76         final boolean SET_ADAPTER_AFTER_DATA_LOAD = arguments.getBoolean(
77                 EXTRA_SET_ADAPTER_AFTER_DATA_LOAD,
78                 DEFAULT_SET_ADAPTER_AFTER_DATA_LOAD);
79 
80         if (!SET_ADAPTER_AFTER_DATA_LOAD) {
81             setupRows();
82         }
83 
84         setTitle("BrowseTestFragment");
85         setHeadersState(arguments.getInt(EXTRA_HEADERS_STATE, HEADERS_ENABLED));
86 
87         setOnSearchClickedListener(new View.OnClickListener() {
88             @Override
89             public void onClick(View view) {
90                 Log.i(TAG, "onSearchClicked");
91             }
92         });
93 
94         setOnItemViewClickedListener(new ItemViewClickedListener());
95         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
96             @Override
97             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
98                     RowPresenter.ViewHolder rowViewHolder, Row row) {
99                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
100             }
101         });
102         if (TEST_ENTRANCE_TRANSITION) {
103             // don't run entrance transition if fragment is restored.
104             if (savedInstanceState == null) {
105                 prepareEntranceTransition();
106             }
107         }
108         // simulates in a real world use case  data being loaded two seconds later
109         new Handler().postDelayed(new Runnable() {
110             @Override
111             public void run() {
112                 if (getActivity() == null || getActivity().isDestroyed()) {
113                     return;
114                 }
115                 if (SET_ADAPTER_AFTER_DATA_LOAD) {
116                     setupRows();
117                 }
118                 loadData();
119                 startEntranceTransition();
120             }
121         }, LOAD_DATA_DELAY);
122     }
123 
setupRows()124     private void setupRows() {
125         ListRowPresenter lrp = new ListRowPresenter();
126 
127         mRowsAdapter = new ArrayObjectAdapter(lrp);
128 
129         setAdapter(mRowsAdapter);
130     }
131 
132     @Override
onEntranceTransitionStart()133     protected void onEntranceTransitionStart() {
134         super.onEntranceTransitionStart();
135         mEntranceTransitionStarted = true;
136     }
137 
138     @Override
onEntranceTransitionEnd()139     protected void onEntranceTransitionEnd() {
140         super.onEntranceTransitionEnd();
141         mEntranceTransitionEnded = true;
142     }
143 
loadData()144     private void loadData() {
145         for (int i = 0; i < NUM_ROWS; ++i) {
146             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter);
147             int index = 0;
148             for (int j = 0; j < REPEAT_PER_ROW; ++j) {
149                 listRowAdapter.add("Hello world-" + (index++));
150                 listRowAdapter.add("This is a test-" + (index++));
151                 listRowAdapter.add("Android TV-" + (index++));
152                 listRowAdapter.add("Leanback-" + (index++));
153                 listRowAdapter.add("Hello world-" + (index++));
154                 listRowAdapter.add("Android TV-" + (index++));
155                 listRowAdapter.add("Leanback-" + (index++));
156                 listRowAdapter.add("GuidedStepFragment-" + (index++));
157             }
158             HeaderItem header = new HeaderItem(i, "Row " + i);
159             mRowsAdapter.add(new ListRow(header, listRowAdapter));
160         }
161     }
162 
163     private final class ItemViewClickedListener implements OnItemViewClickedListener {
164         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)165         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
166                 RowPresenter.ViewHolder rowViewHolder, Row row) {
167             Log.i(TAG, "onItemClicked: " + item + " row " + row);
168         }
169     }
170 
getGridView()171     public VerticalGridView getGridView() {
172         return getRowsFragment().getVerticalGridView();
173     }
174 }
175