1 // CHECKSTYLE:OFF Generated code 2 /* This file is auto-generated from BrowseFragment.java. DO NOT MODIFY. */ 3 4 /* 5 * Copyright (C) 2014 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 com.example.android.leanback; 18 19 import android.content.Intent; 20 import android.os.Bundle; 21 import android.os.Handler; 22 import android.util.Log; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import androidx.core.app.ActivityOptionsCompat; 28 import androidx.core.content.res.ResourcesCompat; 29 import androidx.fragment.app.Fragment; 30 import androidx.leanback.app.GuidedStepSupportFragment; 31 import androidx.leanback.app.RowsSupportFragment; 32 import androidx.leanback.widget.ArrayObjectAdapter; 33 import androidx.leanback.widget.DividerRow; 34 import androidx.leanback.widget.HeaderItem; 35 import androidx.leanback.widget.ImageCardView; 36 import androidx.leanback.widget.ListRow; 37 import androidx.leanback.widget.ListRowPresenter; 38 import androidx.leanback.widget.OnItemViewClickedListener; 39 import androidx.leanback.widget.OnItemViewSelectedListener; 40 import androidx.leanback.widget.PageRow; 41 import androidx.leanback.widget.Presenter; 42 import androidx.leanback.widget.Row; 43 import androidx.leanback.widget.RowPresenter; 44 import androidx.leanback.widget.SectionRow; 45 46 public class BrowseSupportFragment extends androidx.leanback.app.BrowseSupportFragment { 47 private static final String TAG = "leanback.BrowseSupportFragment"; 48 49 private static final boolean TEST_ENTRANCE_TRANSITION = true; 50 private static final int NUM_ROWS = 8; 51 private static final long HEADER_ID1 = 1001; 52 private static final long HEADER_ID2 = 1002; 53 private static final long HEADER_ID3 = 1003; 54 55 private ArrayObjectAdapter mRowsAdapter; 56 private BackgroundHelper mBackgroundHelper; 57 58 // For good performance, it's important to use a single instance of 59 // a card presenter for all rows using that presenter. 60 final CardPresenter mCardPresenter = new CardPresenter(); 61 final CardPresenter mCardPresenter2 = new CardPresenter(R.style.MyImageCardViewTheme); 62 BrowseSupportFragment()63 public BrowseSupportFragment() { 64 getMainFragmentRegistry().registerFragment(PageRow.class, new PageRowFragmentFactory()); 65 } 66 67 @Override onCreate(Bundle savedInstanceState)68 public void onCreate(Bundle savedInstanceState) { 69 Log.i(TAG, "onCreate"); 70 super.onCreate(savedInstanceState); 71 72 mBackgroundHelper = new BackgroundHelper(getActivity()); 73 mBackgroundHelper.attachToWindow(); 74 75 setBadgeDrawable(ResourcesCompat.getDrawable(getActivity().getResources(), 76 R.drawable.ic_title, getActivity().getTheme())); 77 setTitle("Leanback Sample App"); 78 setHeadersState(HEADERS_ENABLED); 79 setOnSearchClickedListener(new View.OnClickListener() { 80 @Override 81 public void onClick(View view) { 82 Intent intent = new Intent(getActivity(), SearchSupportActivity.class); 83 startActivity(intent); 84 } 85 }); 86 87 setOnItemViewClickedListener(new ItemViewClickedListener()); 88 setOnItemViewSelectedListener(new OnItemViewSelectedListener() { 89 @Override 90 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 91 RowPresenter.ViewHolder rowViewHolder, Row row) { 92 Log.i(TAG, "onItemSelected: " + item + " row " + row); 93 94 updateBackgroundToSelection(); 95 } 96 }); 97 setBrowseTransitionListener(new BrowseTransitionListener() { 98 @Override 99 public void onHeadersTransitionStop(boolean withHeaders) { 100 updateBackgroundToSelection(); 101 } 102 }); 103 if (TEST_ENTRANCE_TRANSITION) { 104 // don't run entrance transition if fragment is restored. 105 if (savedInstanceState == null) { 106 prepareEntranceTransition(); 107 } 108 } 109 110 // simulates in a real world use case data being loaded two seconds later 111 new Handler().postDelayed(new Runnable() { 112 @Override 113 public void run() { 114 setupRows(); 115 loadData(); 116 startEntranceTransition(); 117 } 118 }, 2000); 119 } 120 121 @Override onStart()122 public void onStart() { 123 super.onStart(); 124 updateBackgroundToSelection(); 125 } 126 updateBackgroundToSelection()127 void updateBackgroundToSelection() { 128 if (!isShowingHeaders()) { 129 RowPresenter.ViewHolder rowViewHolder = getSelectedRowViewHolder(); 130 Object item = rowViewHolder == null ? null : rowViewHolder.getSelectedItem(); 131 if (item != null) { 132 mBackgroundHelper.setBackground(((PhotoItem) item).getImageResourceId()); 133 } else { 134 mBackgroundHelper.clearDrawable(); 135 } 136 } else { 137 mBackgroundHelper.clearDrawable(); 138 } 139 } 140 141 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)142 public View onCreateView( 143 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 144 return super.onCreateView(inflater, container, savedInstanceState); 145 } 146 setupRows()147 private void setupRows() { 148 ListRowPresenter listRowPresenter = new ListRowPresenter(); 149 listRowPresenter.setNumRows(1); 150 mRowsAdapter = new ArrayObjectAdapter(listRowPresenter); 151 } 152 loadData()153 private void loadData() { 154 int i = 0; 155 156 mRowsAdapter.add(new PageRow(new HeaderItem(HEADER_ID1, "Page Row 0"))); 157 mRowsAdapter.add(new DividerRow()); 158 159 mRowsAdapter.add(new SectionRow(new HeaderItem("section 0"))); 160 for (; i < NUM_ROWS; ++i) { 161 HeaderItem headerItem = new HeaderItem(i, "Row " + i); 162 headerItem.setDescription("Description for Row "+i); 163 mRowsAdapter.add(new ListRow(headerItem, createListRowAdapter(i))); 164 } 165 166 mRowsAdapter.add(new DividerRow()); 167 mRowsAdapter.add(new PageRow(new HeaderItem(HEADER_ID2, "Page Row 1"))); 168 169 mRowsAdapter.add(new PageRow(new HeaderItem(HEADER_ID3, "Page Row 2"))); 170 setAdapter(mRowsAdapter); 171 } 172 createListRowAdapter(int i)173 private ArrayObjectAdapter createListRowAdapter(int i) { 174 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter((i & 1) == 0 ? 175 mCardPresenter : mCardPresenter2); 176 listRowAdapter.add(new PhotoItem( 177 "Hello world", 178 R.drawable.gallery_photo_1)); 179 listRowAdapter.add(new PhotoItem( 180 "This is a test", 181 "Only a test", 182 R.drawable.gallery_photo_2)); 183 listRowAdapter.add(new PhotoItem( 184 "Android TV", 185 "by Google", 186 R.drawable.gallery_photo_3)); 187 listRowAdapter.add(new PhotoItem( 188 "Leanback", 189 R.drawable.gallery_photo_4)); 190 listRowAdapter.add(new PhotoItem( 191 "GuidedStep (Slide left/right)", 192 R.drawable.gallery_photo_5)); 193 listRowAdapter.add(new PhotoItem( 194 "GuidedStep (Slide bottom up)", 195 "Open GuidedStepSupportFragment", 196 R.drawable.gallery_photo_6)); 197 listRowAdapter.add(new PhotoItem( 198 "Android TV", 199 "open RowsSupportActivity", 200 R.drawable.gallery_photo_7)); 201 listRowAdapter.add(new PhotoItem( 202 "Leanback", 203 "open BrowseSupportActivity", 204 R.drawable.gallery_photo_8)); 205 listRowAdapter.add(new PhotoItem( 206 "Hello world", 207 R.drawable.gallery_photo_1)); 208 listRowAdapter.add(new PhotoItem( 209 "This is a test", 210 "Only a test", 211 R.drawable.gallery_photo_2)); 212 listRowAdapter.add(new PhotoItem( 213 "Android TV", 214 "by Google", 215 R.drawable.gallery_photo_3)); 216 listRowAdapter.add(new PhotoItem( 217 "Leanback", 218 R.drawable.gallery_photo_4)); 219 return listRowAdapter; 220 } 221 222 private final class ItemViewClickedListener implements OnItemViewClickedListener { 223 @Override onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)224 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 225 RowPresenter.ViewHolder rowViewHolder, Row row) { 226 227 Intent intent; 228 Bundle bundle; 229 if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_6) { 230 GuidedStepSupportFragment.add(getFragmentManager(), 231 new GuidedStepSupportHalfScreenActivity.FirstStepFragment(), 232 R.id.lb_guidedstep_host); 233 return; 234 } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_5) { 235 GuidedStepSupportFragment.add(getFragmentManager(), 236 new GuidedStepSupportActivity.FirstStepFragment(), R.id.lb_guidedstep_host); 237 return; 238 } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) { 239 intent = new Intent(getActivity(), BrowseSupportActivity.class); 240 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) 241 .toBundle(); 242 } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) { 243 intent = new Intent(getActivity(), RowsSupportActivity.class); 244 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) 245 .toBundle(); 246 } else { 247 intent = new Intent(getActivity(), DetailsSupportActivity.class); 248 intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item); 249 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation( 250 getActivity(), 251 ((ImageCardView) itemViewHolder.view).getMainImageView(), 252 DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle(); 253 } 254 getActivity().startActivity(intent, bundle); 255 } 256 } 257 258 public static class PageRowFragmentFactory extends FragmentFactory { 259 260 @Override createFragment(Object rowObj)261 public Fragment createFragment(Object rowObj) { 262 Row row = (Row) rowObj; 263 if (row.getHeaderItem().getId() == HEADER_ID1) { 264 return new SampleRowsSupportFragment(); 265 } else if (row.getHeaderItem().getId() == HEADER_ID2) { 266 return new SampleRowsSupportFragment(); 267 } else if (row.getHeaderItem().getId() == HEADER_ID3) { 268 return new SampleFragment(); 269 } 270 271 return null; 272 } 273 } 274 275 public static class SampleRowsSupportFragment extends RowsSupportFragment { 276 final CardPresenter mCardPresenter = new CardPresenter(); 277 final CardPresenter mCardPresenter2 = new CardPresenter(R.style.MyImageCardViewTheme); 278 loadFragmentData()279 void loadFragmentData() { 280 ArrayObjectAdapter adapter = new ArrayObjectAdapter(new ListRowPresenter()); 281 for (int i = 0; i < 4; i++) { 282 ListRow row = new ListRow(new HeaderItem("Row " + i), createListRowAdapter(i)); 283 adapter.add(row); 284 } 285 if (getMainFragmentAdapter() != null) { 286 getMainFragmentAdapter().getFragmentHost() 287 .notifyDataReady(getMainFragmentAdapter()); 288 } 289 setAdapter(adapter); 290 } 291 SampleRowsSupportFragment()292 public SampleRowsSupportFragment() { 293 // simulates late data loading: 294 new Handler().postDelayed(new Runnable() { 295 @Override 296 public void run() { 297 loadFragmentData(); 298 } 299 }, 500); 300 301 setOnItemViewClickedListener(new OnItemViewClickedListener() { 302 @Override 303 public void onItemClicked( 304 Presenter.ViewHolder itemViewHolder, 305 Object item, 306 RowPresenter.ViewHolder rowViewHolder, 307 Row row) { 308 Intent intent; 309 Bundle bundle; 310 if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_6) { 311 GuidedStepSupportFragment.add(getActivity().getSupportFragmentManager(), 312 new GuidedStepSupportHalfScreenActivity.FirstStepFragment(), 313 R.id.lb_guidedstep_host); 314 return; 315 } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_5) { 316 GuidedStepSupportFragment.add(getActivity().getSupportFragmentManager(), 317 new GuidedStepSupportActivity.FirstStepFragment(), R.id.lb_guidedstep_host); 318 return; 319 } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) { 320 intent = new Intent(getActivity(), BrowseSupportActivity.class); 321 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) 322 .toBundle(); 323 } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) { 324 intent = new Intent(getActivity(), RowsSupportActivity.class); 325 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) 326 .toBundle(); 327 } else { 328 intent = new Intent(getActivity(), DetailsSupportActivity.class); 329 intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item); 330 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation( 331 getActivity(), 332 ((ImageCardView) itemViewHolder.view).getMainImageView(), 333 DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle(); 334 } 335 getActivity().startActivity(intent, bundle); 336 } 337 }); 338 } 339 createListRowAdapter(int i)340 private ArrayObjectAdapter createListRowAdapter(int i) { 341 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter((i & 1) == 0 ? 342 mCardPresenter : mCardPresenter2); 343 listRowAdapter.add(new PhotoItem( 344 "Hello world", 345 R.drawable.gallery_photo_1)); 346 listRowAdapter.add(new PhotoItem( 347 "This is a test", 348 "Only a test", 349 R.drawable.gallery_photo_2)); 350 listRowAdapter.add(new PhotoItem( 351 "Android TV", 352 "by Google", 353 R.drawable.gallery_photo_3)); 354 listRowAdapter.add(new PhotoItem( 355 "Leanback", 356 R.drawable.gallery_photo_4)); 357 listRowAdapter.add(new PhotoItem( 358 "GuidedStep (Slide left/right)", 359 R.drawable.gallery_photo_5)); 360 listRowAdapter.add(new PhotoItem( 361 "GuidedStep (Slide bottom up)", 362 "Open GuidedStepSupportFragment", 363 R.drawable.gallery_photo_6)); 364 listRowAdapter.add(new PhotoItem( 365 "Android TV", 366 "open RowsSupportActivity", 367 R.drawable.gallery_photo_7)); 368 listRowAdapter.add(new PhotoItem( 369 "Leanback", 370 "open BrowseSupportActivity", 371 R.drawable.gallery_photo_8)); 372 return listRowAdapter; 373 } 374 } 375 376 public static class PageFragmentAdapterImpl extends MainFragmentAdapter<SampleFragment> { 377 PageFragmentAdapterImpl(SampleFragment fragment)378 public PageFragmentAdapterImpl(SampleFragment fragment) { 379 super(fragment); 380 setScalingEnabled(true); 381 } 382 383 @Override setEntranceTransitionState(boolean state)384 public void setEntranceTransitionState(boolean state) { 385 getFragment().setEntranceTransitionState(state); 386 } 387 } 388 389 public static class SampleFragment extends Fragment implements MainFragmentAdapterProvider { 390 391 final PageFragmentAdapterImpl mMainFragmentAdapter = new PageFragmentAdapterImpl(this); 392 setEntranceTransitionState(boolean state)393 public void setEntranceTransitionState(boolean state) { 394 final View view = getView(); 395 int visibility = state ? View.VISIBLE : View.INVISIBLE; 396 view.findViewById(R.id.tv1).setVisibility(visibility); 397 view.findViewById(R.id.tv2).setVisibility(visibility); 398 view.findViewById(R.id.tv3).setVisibility(visibility); 399 } 400 401 @Override onCreateView( final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)402 public View onCreateView( 403 final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 404 View view = inflater.inflate(R.layout.page_fragment, container, false); 405 view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() { 406 @Override 407 public void onClick(View view) { 408 Intent intent = new Intent(inflater.getContext(), GuidedStepSupportActivity.class); 409 startActivity(intent); 410 } 411 }); 412 413 return view; 414 } 415 416 @Override onViewCreated(View view, Bundle savedInstanceState)417 public void onViewCreated(View view, Bundle savedInstanceState) { 418 // static layout has view and data ready immediately 419 mMainFragmentAdapter.getFragmentHost().notifyViewCreated(mMainFragmentAdapter); 420 mMainFragmentAdapter.getFragmentHost().notifyDataReady(mMainFragmentAdapter); 421 } 422 423 @Override getMainFragmentAdapter()424 public MainFragmentAdapter getMainFragmentAdapter() { 425 return mMainFragmentAdapter; 426 } 427 } 428 } 429