1 // CHECKSTYLE:OFF Generated code 2 /* This file is auto-generated from NewDetailsFragment.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.Context; 20 import android.content.Intent; 21 import android.graphics.Bitmap; 22 import android.graphics.drawable.Drawable; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.util.Log; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 31 import androidx.core.app.ActivityOptionsCompat; 32 import androidx.core.content.res.ResourcesCompat; 33 import androidx.leanback.app.DetailsSupportFragmentBackgroundController; 34 import androidx.leanback.media.MediaPlayerAdapter; 35 import androidx.leanback.media.MediaPlayerGlue; 36 import androidx.leanback.media.PlaybackGlue; 37 import androidx.leanback.media.PlaybackTransportControlGlue; 38 import androidx.leanback.widget.Action; 39 import androidx.leanback.widget.ArrayObjectAdapter; 40 import androidx.leanback.widget.ClassPresenterSelector; 41 import androidx.leanback.widget.DetailsOverviewRow; 42 import androidx.leanback.widget.FullWidthDetailsOverviewRowPresenter; 43 import androidx.leanback.widget.FullWidthDetailsOverviewSharedElementHelper; 44 import androidx.leanback.widget.HeaderItem; 45 import androidx.leanback.widget.ImageCardView; 46 import androidx.leanback.widget.ListRow; 47 import androidx.leanback.widget.ListRowPresenter; 48 import androidx.leanback.widget.OnActionClickedListener; 49 import androidx.leanback.widget.OnItemViewClickedListener; 50 import androidx.leanback.widget.OnItemViewSelectedListener; 51 import androidx.leanback.widget.Presenter; 52 import androidx.leanback.widget.Row; 53 import androidx.leanback.widget.RowPresenter; 54 import androidx.leanback.widget.SparseArrayObjectAdapter; 55 56 public class NewDetailsSupportFragment extends androidx.leanback.app.DetailsSupportFragment { 57 58 private static final String TAG = "leanback.DetailsSupportFragment"; 59 private static final String ITEM = "item"; 60 61 private static final int NUM_ROWS = 3; 62 private ArrayObjectAdapter mRowsAdapter; 63 private PhotoItem mPhotoItem; 64 final CardPresenter cardPresenter = new CardPresenter(); 65 66 private static final int ACTION_PLAY = 1; 67 private static final int ACTION_RENT = 2; 68 private static final int ACTION_BUY = 3; 69 70 private boolean TEST_SHARED_ELEMENT_TRANSITION = true; 71 private boolean TEST_BACKGROUND_PLAYER; 72 73 private static final long TIME_TO_LOAD_OVERVIEW_ROW_MS = 1000; 74 private static final long TIME_TO_LOAD_RELATED_ROWS_MS = 2000; 75 76 private Action mActionPlay; 77 private Action mActionRent; 78 private Action mActionBuy; 79 80 private FullWidthDetailsOverviewSharedElementHelper mHelper; 81 private BackgroundHelper mBackgroundHelper; // used to download bitmap async. 82 private final DetailsSupportFragmentBackgroundController mDetailsBackground = 83 new DetailsSupportFragmentBackgroundController(this); 84 setupTrailerVideo()85 void setupTrailerVideo() { 86 MediaPlayerGlue mediaPlayerGlue = new MediaPlayerGlue(getActivity()); 87 mDetailsBackground.setupVideoPlayback(mediaPlayerGlue); 88 mediaPlayerGlue.setMode(MediaPlayerGlue.REPEAT_ONE); 89 mediaPlayerGlue.setArtist("A Googler"); 90 mediaPlayerGlue.setTitle("Diving with Sharks Trailer"); 91 mediaPlayerGlue.setMediaSource(Uri.parse("android.resource://com.example.android.leanback/" 92 + "raw/browse")); 93 } 94 setupMainVideo()95 void setupMainVideo() { 96 Context context = getActivity(); 97 MediaPlayerAdapter adapter = new MediaPlayerAdapter(context); 98 PlaybackTransportControlGlue<MediaPlayerAdapter> mediaPlayerGlue = 99 new PlaybackTransportControlGlue(context, adapter); 100 mDetailsBackground.setupVideoPlayback(mediaPlayerGlue); 101 mediaPlayerGlue.setSubtitle("A Googler"); 102 mediaPlayerGlue.setTitle("Diving with Sharks"); 103 mediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse( 104 "https://storage.googleapis.com/android-tv/Sample videos/April Fool's " 105 + "2013/Explore Treasure Mode with Google Maps.mp4")); 106 mediaPlayerGlue.addPlayerCallback(new PlaybackGlue.PlayerCallback() { 107 @Override 108 public void onPreparedStateChanged(PlaybackGlue glue) { 109 super.onPreparedStateChanged(glue); 110 PlaybackTransportControlGlue controlGlue = (PlaybackTransportControlGlue) glue; 111 controlGlue.setSeekProvider(new PlaybackSeekDiskDataProvider( 112 controlGlue.getDuration(), 1000, 113 "/sdcard/seek/frame_%04d.jpg")); 114 } 115 }); 116 } 117 118 @Override onCreate(Bundle savedInstanceState)119 public void onCreate(Bundle savedInstanceState) { 120 Log.i(TAG, "onCreate"); 121 super.onCreate(savedInstanceState); 122 123 mBackgroundHelper = new BackgroundHelper(getActivity()); 124 mDetailsBackground.enableParallax(); 125 if (TEST_BACKGROUND_PLAYER) { 126 if (MovieData.sStatus == MovieData.STATUS_INIT) { 127 // not own/rented, play trailer 128 setupTrailerVideo(); 129 } else { 130 // bought or rented, play the main content 131 setupMainVideo(); 132 // hide details main ui 133 mDetailsBackground.switchToVideo(); 134 } 135 } 136 137 final Context context = getActivity(); 138 setBadgeDrawable(ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_title, 139 context.getTheme())); 140 setTitle("Leanback Sample App"); 141 if (!TEST_BACKGROUND_PLAYER) { 142 setOnSearchClickedListener(new View.OnClickListener() { 143 @Override 144 public void onClick(View view) { 145 Intent intent = new Intent(getActivity(), SearchSupportActivity.class); 146 startActivity(intent); 147 } 148 }); 149 } 150 151 mActionPlay = new Action(ACTION_PLAY, "Play"); 152 mActionRent = new Action(ACTION_RENT, "Rent", "$3.99", ResourcesCompat.getDrawable( 153 context.getResources(), R.drawable.ic_action_a, context.getTheme())); 154 mActionBuy = new Action(ACTION_BUY, "Buy $9.99"); 155 156 ClassPresenterSelector ps = new ClassPresenterSelector(); 157 FullWidthDetailsOverviewRowPresenter dorPresenter = 158 new FullWidthDetailsOverviewRowPresenter(new DetailsDescriptionPresenter()); 159 dorPresenter.setOnActionClickedListener(new OnActionClickedListener() { 160 @Override 161 public void onActionClicked(Action action) { 162 final Context context = getActivity(); 163 DetailsOverviewRow dor = (DetailsOverviewRow) mRowsAdapter.get(0); 164 if (action.getId() == ACTION_BUY) { 165 // on the UI thread, we can modify actions adapter directly 166 SparseArrayObjectAdapter actions = (SparseArrayObjectAdapter) 167 dor.getActionsAdapter(); 168 actions.set(ACTION_PLAY, mActionPlay); 169 actions.clear(ACTION_RENT); 170 actions.clear(ACTION_BUY); 171 boolean previousRented = MovieData.sStatus == MovieData.STATUS_RENTED; 172 MovieData.sStatus = MovieData.STATUS_OWN; 173 dor.setItem(getDisplayTitle(mPhotoItem.getTitle())); 174 dor.setImageDrawable(ResourcesCompat.getDrawable(context.getResources(), 175 R.drawable.details_img_16x9, context.getTheme())); 176 if (TEST_BACKGROUND_PLAYER) { 177 if (!previousRented) { 178 setupMainVideo(); 179 mDetailsBackground.switchToVideo(); 180 } 181 } else { 182 Intent intent = new Intent(context, PlaybackActivity.class); 183 getActivity().startActivity(intent); 184 } 185 } else if (action.getId() == ACTION_RENT) { 186 // on the UI thread, we can modify actions adapter directly 187 SparseArrayObjectAdapter actions = (SparseArrayObjectAdapter) 188 dor.getActionsAdapter(); 189 actions.set(ACTION_PLAY, mActionPlay); 190 actions.clear(ACTION_RENT); 191 MovieData.sStatus = MovieData.STATUS_RENTED; 192 dor.setItem(getDisplayTitle(mPhotoItem.getTitle())); 193 if (TEST_BACKGROUND_PLAYER) { 194 setupMainVideo(); 195 mDetailsBackground.switchToVideo(); 196 } else { 197 Intent intent = new Intent(context, PlaybackActivity.class); 198 getActivity().startActivity(intent); 199 } 200 } else if (action.getId() == ACTION_PLAY) { 201 if (TEST_BACKGROUND_PLAYER) { 202 mDetailsBackground.switchToVideo(); 203 } else { 204 Intent intent = new Intent(context, PlaybackActivity.class); 205 getActivity().startActivity(intent); 206 } 207 } 208 } 209 }); 210 211 ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter); 212 ps.addClassPresenter(ListRow.class, new ListRowPresenter()); 213 214 mRowsAdapter = new ArrayObjectAdapter(ps); 215 updateAdapter(); 216 217 setOnItemViewClickedListener(new OnItemViewClickedListener() { 218 @Override 219 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 220 RowPresenter.ViewHolder rowViewHolder, Row row) { 221 Log.i(TAG, "onItemClicked: " + item + " row " + row); 222 if (item instanceof PhotoItem) { 223 Intent intent = new Intent(getActivity(), DetailsSupportActivity.class); 224 intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item); 225 226 Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation( 227 getActivity(), 228 ((ImageCardView) itemViewHolder.view).getMainImageView(), 229 DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle(); 230 getActivity().startActivity(intent, bundle); 231 } 232 } 233 }); 234 setOnItemViewSelectedListener(new OnItemViewSelectedListener() { 235 @Override 236 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 237 RowPresenter.ViewHolder rowViewHolder, Row row) { 238 Log.i(TAG, "onItemSelected: " + item + " row " + row); 239 } 240 }); 241 242 if (TEST_SHARED_ELEMENT_TRANSITION) { 243 mHelper = new FullWidthDetailsOverviewSharedElementHelper(); 244 mHelper.setSharedElementEnterTransition(getActivity(), 245 DetailsSupportActivity.SHARED_ELEMENT_NAME); 246 dorPresenter.setListener(mHelper); 247 dorPresenter.setParticipatingEntranceTransition(false); 248 } else { 249 dorPresenter.setParticipatingEntranceTransition(true); 250 } 251 } 252 253 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)254 public View onCreateView(LayoutInflater inflater, ViewGroup container, 255 Bundle savedInstanceState) { 256 View view = super.onCreateView(inflater, container, savedInstanceState); 257 return view; 258 } 259 setBackgroundVideo(boolean backgroundVideo)260 public void setBackgroundVideo(boolean backgroundVideo) { 261 TEST_BACKGROUND_PLAYER = backgroundVideo; 262 } 263 setItem(PhotoItem photoItem)264 public void setItem(PhotoItem photoItem) { 265 mPhotoItem = photoItem; 266 updateAdapter(); 267 } 268 getDisplayTitle(String title)269 static String getDisplayTitle(String title) { 270 switch (MovieData.sStatus) { 271 case MovieData.STATUS_OWN: 272 title = title + "(Owned)"; 273 break; 274 case MovieData.STATUS_RENTED: 275 title = title + "(Rented)"; 276 break; 277 case MovieData.STATUS_INIT: 278 default: 279 } 280 return title; 281 } 282 updateAdapter()283 void updateAdapter() { 284 if (mRowsAdapter == null) { 285 return; 286 } 287 mRowsAdapter.clear(); 288 new Handler().postDelayed(new Runnable() { 289 @Override 290 public void run() { 291 final Context context = getActivity(); 292 if (context == null) { 293 return; 294 } 295 296 DetailsOverviewRow dor = new DetailsOverviewRow( 297 getDisplayTitle(mPhotoItem.getTitle())); 298 dor.setImageDrawable(ResourcesCompat.getDrawable(context.getResources(), 299 mPhotoItem.getImageResourceId(), context.getTheme())); 300 SparseArrayObjectAdapter adapter = new SparseArrayObjectAdapter(); 301 switch (MovieData.sStatus) { 302 case MovieData.STATUS_INIT: 303 adapter.set(ACTION_RENT, mActionRent); 304 adapter.set(ACTION_BUY, mActionBuy); 305 break; 306 case MovieData.STATUS_OWN: 307 adapter.set(ACTION_PLAY, mActionPlay); 308 break; 309 case MovieData.STATUS_RENTED: 310 adapter.set(ACTION_PLAY, mActionPlay); 311 adapter.set(ACTION_BUY, mActionBuy); 312 break; 313 } 314 // one line text with icon 315 Drawable d = ResourcesCompat.getDrawable(context.getResources(), 316 R.drawable.ic_action_a, context.getTheme()); 317 adapter.set(202, new Action(202, "Top", null, d)); 318 dor.setActionsAdapter(adapter); 319 mRowsAdapter.add(0, dor); 320 setSelectedPosition(0, true); 321 if (TEST_SHARED_ELEMENT_TRANSITION) { 322 if (mHelper != null && !mHelper.getAutoStartSharedElementTransition()) { 323 mHelper.startPostponedEnterTransition(); 324 } 325 } 326 } 327 }, TIME_TO_LOAD_OVERVIEW_ROW_MS); 328 329 new Handler().postDelayed(new Runnable() { 330 @Override 331 public void run() { 332 if (getActivity() == null) { 333 return; 334 } 335 for (int i = 0; i < NUM_ROWS; ++i) { 336 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter); 337 listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1)); 338 listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2)); 339 listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3)); 340 listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4)); 341 HeaderItem header = new HeaderItem(i, "Row " + i); 342 mRowsAdapter.add(new ListRow(header, listRowAdapter)); 343 } 344 } 345 }, TIME_TO_LOAD_RELATED_ROWS_MS); 346 setAdapter(mRowsAdapter); 347 } 348 349 @Override onStart()350 public void onStart() { 351 super.onStart(); 352 353 // Restore background drawable in onStart(): 354 mBackgroundHelper.loadBitmap(R.drawable.spiderman, 355 new BackgroundHelper.BitmapLoadCallback() { 356 @Override 357 public void onBitmapLoaded(Bitmap bitmap) { 358 mDetailsBackground.setCoverBitmap(bitmap); 359 } 360 }); 361 } 362 363 @Override onStop()364 public void onStop() { 365 mDetailsBackground.setCoverBitmap(null); 366 super.onStop(); 367 } 368 } 369