1 /* 2 * Copyright (C) 2014 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 com.example.android.leanback; 15 16 import android.app.Activity; 17 import android.content.Intent; 18 import android.content.res.Resources; 19 import android.os.Bundle; 20 import android.os.Handler; 21 import android.support.v4.app.ActivityOptionsCompat; 22 import android.support.v17.leanback.widget.Action; 23 import android.support.v17.leanback.widget.ArrayObjectAdapter; 24 import android.support.v17.leanback.widget.ClassPresenterSelector; 25 import android.support.v17.leanback.widget.DetailsOverviewRow; 26 import android.support.v17.leanback.widget.FullWidthDetailsOverviewRowPresenter; 27 import android.support.v17.leanback.widget.FullWidthDetailsOverviewSharedElementHelper; 28 import android.support.v17.leanback.widget.HeaderItem; 29 import android.support.v17.leanback.widget.ImageCardView; 30 import android.support.v17.leanback.widget.ListRow; 31 import android.support.v17.leanback.widget.ListRowPresenter; 32 import android.support.v17.leanback.widget.OnActionClickedListener; 33 import android.support.v17.leanback.widget.OnItemViewClickedListener; 34 import android.support.v17.leanback.widget.OnItemViewSelectedListener; 35 import android.support.v17.leanback.widget.Presenter; 36 import android.support.v17.leanback.widget.Row; 37 import android.support.v17.leanback.widget.RowPresenter; 38 import android.support.v17.leanback.widget.SparseArrayObjectAdapter; 39 import android.util.Log; 40 import android.view.View; 41 import android.widget.Toast; 42 43 public class NewDetailsFragment extends android.support.v17.leanback.app.DetailsFragment { 44 private static final String TAG = "leanback.DetailsFragment"; 45 private static final String ITEM = "item"; 46 47 private static final int NUM_ROWS = 3; 48 private ArrayObjectAdapter mRowsAdapter; 49 private PhotoItem mPhotoItem; 50 final CardPresenter cardPresenter = new CardPresenter(); 51 private BackgroundHelper mBackgroundHelper = new BackgroundHelper(); 52 53 private static final int ACTION_PLAY = 1; 54 private static final int ACTION_RENT = 2; 55 private static final int ACTION_BUY = 3; 56 57 private boolean TEST_OVERVIEW_ROW_ON_SECOND; 58 private boolean TEST_SHARED_ELEMENT_TRANSITION; 59 private boolean TEST_ENTRANCE_TRANSITION; 60 61 private static final long TIME_TO_LOAD_OVERVIEW_ROW_MS = 1000; 62 private static final long TIME_TO_LOAD_RELATED_ROWS_MS = 2000; 63 64 private Action mActionPlay; 65 private Action mActionRent; 66 private Action mActionBuy; 67 68 private FullWidthDetailsOverviewSharedElementHelper mHelper; 69 initializeTest()70 private void initializeTest() { 71 TEST_SHARED_ELEMENT_TRANSITION = null != getActivity().getWindow().getSharedElementEnterTransition(); 72 TEST_OVERVIEW_ROW_ON_SECOND = !TEST_SHARED_ELEMENT_TRANSITION; 73 TEST_ENTRANCE_TRANSITION = true; 74 } 75 76 @Override onCreate(Bundle savedInstanceState)77 public void onCreate(Bundle savedInstanceState) { 78 Log.i(TAG, "onCreate"); 79 super.onCreate(savedInstanceState); 80 initializeTest(); 81 82 setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title)); 83 setTitle("Leanback Sample App"); 84 setOnSearchClickedListener(new View.OnClickListener() { 85 @Override 86 public void onClick(View view) { 87 Intent intent = new Intent(getActivity(), SearchActivity.class); 88 startActivity(intent); 89 } 90 }); 91 92 mActionPlay = new Action(ACTION_PLAY, "Play"); 93 mActionRent = new Action(ACTION_RENT, "Rent", "$3.99", 94 getResources().getDrawable(R.drawable.ic_action_a)); 95 mActionBuy = new Action(ACTION_BUY, "Buy $9.99"); 96 97 ClassPresenterSelector ps = new ClassPresenterSelector(); 98 FullWidthDetailsOverviewRowPresenter dorPresenter = 99 new FullWidthDetailsOverviewRowPresenter(new DetailsDescriptionPresenter()); 100 dorPresenter.setOnActionClickedListener(new OnActionClickedListener() { 101 @Override 102 public void onActionClicked(Action action) { 103 Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show(); 104 int indexOfOverviewRow = TEST_OVERVIEW_ROW_ON_SECOND ? 1 : 0; 105 DetailsOverviewRow dor = (DetailsOverviewRow) mRowsAdapter.get(indexOfOverviewRow); 106 if (action.getId() == ACTION_BUY) { 107 // on the UI thread, we can modify actions adapter directly 108 SparseArrayObjectAdapter actions = (SparseArrayObjectAdapter) 109 dor.getActionsAdapter(); 110 actions.set(ACTION_PLAY, mActionPlay); 111 actions.clear(ACTION_RENT); 112 actions.clear(ACTION_BUY); 113 dor.setItem(mPhotoItem.getTitle() + "(Owned)"); 114 dor.setImageDrawable(getResources().getDrawable(R.drawable.details_img_16x9)); 115 } else if (action.getId() == ACTION_RENT) { 116 // on the UI thread, we can modify actions adapter directly 117 SparseArrayObjectAdapter actions = (SparseArrayObjectAdapter) 118 dor.getActionsAdapter(); 119 actions.set(ACTION_PLAY, mActionPlay); 120 actions.clear(ACTION_RENT); 121 dor.setItem(mPhotoItem.getTitle() + "(Rented)"); 122 } else if (action.getId() == ACTION_PLAY) { 123 Intent intent = new Intent(getActivity(), PlaybackOverlayActivity.class); 124 getActivity().startActivity(intent); 125 } 126 } 127 }); 128 if (TEST_OVERVIEW_ROW_ON_SECOND) { 129 dorPresenter.setInitialState(FullWidthDetailsOverviewRowPresenter.STATE_SMALL); 130 } 131 132 ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter); 133 ps.addClassPresenter(ListRow.class, new ListRowPresenter()); 134 135 mRowsAdapter = new ArrayObjectAdapter(ps); 136 137 PhotoItem item = (PhotoItem) (savedInstanceState != null ? 138 savedInstanceState.getParcelable(ITEM) : null); 139 if (item != null) { 140 setItem(item); 141 } 142 143 setOnItemViewClickedListener(new OnItemViewClickedListener() { 144 @Override 145 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 146 RowPresenter.ViewHolder rowViewHolder, Row row) { 147 Log.i(TAG, "onItemClicked: " + item + " row " + row); 148 if (item instanceof PhotoItem){ 149 Intent intent = new Intent(getActivity(), DetailsActivity.class); 150 intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item); 151 152 Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation( 153 getActivity(), 154 ((ImageCardView)itemViewHolder.view).getMainImageView(), 155 DetailsActivity.SHARED_ELEMENT_NAME).toBundle(); 156 getActivity().startActivity(intent, bundle); 157 } 158 } 159 }); 160 setOnItemViewSelectedListener(new OnItemViewSelectedListener() { 161 @Override 162 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 163 RowPresenter.ViewHolder rowViewHolder, Row row) { 164 Log.i(TAG, "onItemSelected: " + item + " row " + row); 165 } 166 }); 167 168 if (TEST_SHARED_ELEMENT_TRANSITION) { 169 mHelper = new FullWidthDetailsOverviewSharedElementHelper(); 170 mHelper.setSharedElementEnterTransition(getActivity(), 171 DetailsActivity.SHARED_ELEMENT_NAME); 172 dorPresenter.setListener(mHelper); 173 dorPresenter.setParticipatingEntranceTransition(false); 174 } else { 175 dorPresenter.setParticipatingEntranceTransition(true); 176 } 177 if (TEST_ENTRANCE_TRANSITION) { 178 // don't run entrance transition if Activity is restored. 179 if (savedInstanceState == null) { 180 prepareEntranceTransition(); 181 } 182 } 183 } 184 185 @Override onSaveInstanceState(Bundle outState)186 public void onSaveInstanceState(Bundle outState) { 187 super.onSaveInstanceState(outState); 188 outState.putParcelable(ITEM, mPhotoItem); 189 } 190 setItem(PhotoItem photoItem)191 public void setItem(PhotoItem photoItem) { 192 mPhotoItem = photoItem; 193 194 mRowsAdapter.clear(); 195 new Handler().postDelayed(new Runnable() { 196 public void run() { 197 if (TEST_OVERVIEW_ROW_ON_SECOND) { 198 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter); 199 listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1)); 200 listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2)); 201 listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3)); 202 listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4)); 203 HeaderItem header = new HeaderItem(0, "Search Result"); 204 mRowsAdapter.add(0, new ListRow(header, listRowAdapter)); 205 } 206 207 Resources res = getActivity().getResources(); 208 DetailsOverviewRow dor = new DetailsOverviewRow(mPhotoItem.getTitle()); 209 dor.setImageDrawable(res.getDrawable(mPhotoItem.getImageResourceId())); 210 SparseArrayObjectAdapter adapter = new SparseArrayObjectAdapter(); 211 adapter.set(ACTION_RENT, mActionRent); 212 adapter.set(ACTION_BUY, mActionBuy); 213 dor.setActionsAdapter(adapter); 214 int indexOfOverviewRow = TEST_OVERVIEW_ROW_ON_SECOND ? 1 : 0; 215 mRowsAdapter.add(indexOfOverviewRow, dor); 216 setSelectedPosition(0, true); 217 if (TEST_SHARED_ELEMENT_TRANSITION) { 218 if (mHelper != null && !mHelper.getAutoStartSharedElementTransition()) { 219 mHelper.startPostponedEnterTransition(); 220 } 221 } 222 } 223 }, TIME_TO_LOAD_OVERVIEW_ROW_MS); 224 225 new Handler().postDelayed(new Runnable() { 226 public void run() { 227 for (int i = 0; i < NUM_ROWS; ++i) { 228 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter); 229 listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1)); 230 listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2)); 231 listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3)); 232 listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4)); 233 HeaderItem header = new HeaderItem(i, "Row " + i); 234 mRowsAdapter.add(new ListRow(header, listRowAdapter)); 235 } 236 if (TEST_ENTRANCE_TRANSITION) { 237 startEntranceTransition(); 238 } 239 } 240 }, TIME_TO_LOAD_RELATED_ROWS_MS); 241 setAdapter(mRowsAdapter); 242 } 243 244 @Override onStart()245 public void onStart() { 246 super.onStart(); 247 if (mPhotoItem != null) { 248 mBackgroundHelper.setBackground( 249 getActivity(), mPhotoItem.getImageResourceId()); 250 } 251 } 252 253 } 254