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