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 androidx.leanback.app; 15 16 import static androidx.leanback.app.BrowseSupportFragmentTestActivity.EXTRA_HEADERS_STATE; 17 import static androidx.leanback.app.BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY; 18 import static androidx.leanback.app.BrowseSupportFragmentTestActivity.EXTRA_NUM_ROWS; 19 import static androidx.leanback.app.BrowseSupportFragmentTestActivity.EXTRA_REPEAT_PER_ROW; 20 import static androidx.leanback.app.BrowseSupportFragmentTestActivity 21 .EXTRA_SET_ADAPTER_AFTER_DATA_LOAD; 22 import static androidx.leanback.app.BrowseSupportFragmentTestActivity 23 .EXTRA_TEST_ENTRANCE_TRANSITION; 24 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.util.Log; 28 import android.view.View; 29 30 import androidx.leanback.widget.ArrayObjectAdapter; 31 import androidx.leanback.widget.HeaderItem; 32 import androidx.leanback.widget.ListRow; 33 import androidx.leanback.widget.ListRowPresenter; 34 import androidx.leanback.widget.OnItemViewClickedListener; 35 import androidx.leanback.widget.OnItemViewSelectedListener; 36 import androidx.leanback.widget.Presenter; 37 import androidx.leanback.widget.Row; 38 import androidx.leanback.widget.RowPresenter; 39 import androidx.leanback.widget.VerticalGridView; 40 41 public class BrowseTestSupportFragment extends BrowseSupportFragment { 42 private static final String TAG = "BrowseTestSupportFragment"; 43 44 final static int DEFAULT_NUM_ROWS = 100; 45 final static int DEFAULT_REPEAT_PER_ROW = 20; 46 final static long DEFAULT_LOAD_DATA_DELAY = 2000; 47 final static boolean DEFAULT_TEST_ENTRANCE_TRANSITION = true; 48 final static boolean DEFAULT_SET_ADAPTER_AFTER_DATA_LOAD = false; 49 50 private ArrayObjectAdapter mRowsAdapter; 51 52 // For good performance, it's important to use a single instance of 53 // a card presenter for all rows using that presenter. 54 final static StringPresenter sCardPresenter = new StringPresenter(); 55 56 int NUM_ROWS; 57 int REPEAT_PER_ROW; 58 boolean mEntranceTransitionStarted; 59 boolean mEntranceTransitionEnded; 60 61 @Override onCreate(Bundle savedInstanceState)62 public void onCreate(Bundle savedInstanceState) { 63 Log.i(TAG, "onCreate"); 64 super.onCreate(savedInstanceState); 65 66 Bundle arguments = getArguments(); 67 NUM_ROWS = arguments.getInt(EXTRA_NUM_ROWS, BrowseTestSupportFragment.DEFAULT_NUM_ROWS); 68 REPEAT_PER_ROW = arguments.getInt(EXTRA_REPEAT_PER_ROW, 69 DEFAULT_REPEAT_PER_ROW); 70 long LOAD_DATA_DELAY = arguments.getLong(EXTRA_LOAD_DATA_DELAY, 71 DEFAULT_LOAD_DATA_DELAY); 72 boolean TEST_ENTRANCE_TRANSITION = arguments.getBoolean( 73 EXTRA_TEST_ENTRANCE_TRANSITION, 74 DEFAULT_TEST_ENTRANCE_TRANSITION); 75 final boolean SET_ADAPTER_AFTER_DATA_LOAD = arguments.getBoolean( 76 EXTRA_SET_ADAPTER_AFTER_DATA_LOAD, 77 DEFAULT_SET_ADAPTER_AFTER_DATA_LOAD); 78 79 if (!SET_ADAPTER_AFTER_DATA_LOAD) { 80 setupRows(); 81 } 82 83 setTitle("BrowseTestSupportFragment"); 84 setHeadersState(arguments.getInt(EXTRA_HEADERS_STATE, HEADERS_ENABLED)); 85 86 setOnSearchClickedListener(new View.OnClickListener() { 87 @Override 88 public void onClick(View view) { 89 Log.i(TAG, "onSearchClicked"); 90 } 91 }); 92 93 setOnItemViewClickedListener(new ItemViewClickedListener()); 94 setOnItemViewSelectedListener(new OnItemViewSelectedListener() { 95 @Override 96 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 97 RowPresenter.ViewHolder rowViewHolder, Row row) { 98 Log.i(TAG, "onItemSelected: " + item + " row " + row); 99 } 100 }); 101 if (TEST_ENTRANCE_TRANSITION) { 102 // don't run entrance transition if fragment is restored. 103 if (savedInstanceState == null) { 104 prepareEntranceTransition(); 105 } 106 } 107 // simulates in a real world use case data being loaded two seconds later 108 new Handler().postDelayed(new Runnable() { 109 @Override 110 public void run() { 111 if (getActivity() == null || getActivity().isDestroyed()) { 112 return; 113 } 114 if (SET_ADAPTER_AFTER_DATA_LOAD) { 115 setupRows(); 116 } 117 loadData(); 118 startEntranceTransition(); 119 } 120 }, LOAD_DATA_DELAY); 121 } 122 setupRows()123 private void setupRows() { 124 ListRowPresenter lrp = new ListRowPresenter(); 125 126 mRowsAdapter = new ArrayObjectAdapter(lrp); 127 128 setAdapter(mRowsAdapter); 129 } 130 131 @Override onEntranceTransitionStart()132 protected void onEntranceTransitionStart() { 133 super.onEntranceTransitionStart(); 134 mEntranceTransitionStarted = true; 135 } 136 137 @Override onEntranceTransitionEnd()138 protected void onEntranceTransitionEnd() { 139 super.onEntranceTransitionEnd(); 140 mEntranceTransitionEnded = true; 141 } 142 loadData()143 private void loadData() { 144 for (int i = 0; i < NUM_ROWS; ++i) { 145 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter); 146 int index = 0; 147 for (int j = 0; j < REPEAT_PER_ROW; ++j) { 148 listRowAdapter.add("Hello world-" + (index++)); 149 listRowAdapter.add("This is a test-" + (index++)); 150 listRowAdapter.add("Android TV-" + (index++)); 151 listRowAdapter.add("Leanback-" + (index++)); 152 listRowAdapter.add("Hello world-" + (index++)); 153 listRowAdapter.add("Android TV-" + (index++)); 154 listRowAdapter.add("Leanback-" + (index++)); 155 listRowAdapter.add("GuidedStepSupportFragment-" + (index++)); 156 } 157 HeaderItem header = new HeaderItem(i, "Row " + i); 158 mRowsAdapter.add(new ListRow(header, listRowAdapter)); 159 } 160 } 161 162 private final class ItemViewClickedListener implements OnItemViewClickedListener { 163 @Override onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)164 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 165 RowPresenter.ViewHolder rowViewHolder, Row row) { 166 Log.i(TAG, "onItemClicked: " + item + " row " + row); 167 } 168 } 169 getGridView()170 public VerticalGridView getGridView() { 171 return getRowsSupportFragment().getVerticalGridView(); 172 } 173 } 174