1 /* This file is auto-generated from DetailsFragment.java.  DO NOT MODIFY. */
2 
3 /*
4  * Copyright (C) 2014 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.example.android.leanback;
17 
18 import android.content.Intent;
19 import android.content.res.Resources;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.support.v4.app.ActivityOptionsCompat;
23 import android.support.v17.leanback.widget.Action;
24 import android.support.v17.leanback.widget.ArrayObjectAdapter;
25 import android.support.v17.leanback.widget.ClassPresenterSelector;
26 import android.support.v17.leanback.widget.DetailsOverviewRow;
27 import android.support.v17.leanback.widget.DetailsOverviewRowPresenter;
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 DetailsSupportFragment extends android.support.v17.leanback.app.DetailsSupportFragment {
44     private static final String TAG = "leanback.DetailsSupportFragment";
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 static final boolean TEST_SHARED_ELEMENT_TRANSITION = true;
58     private static final boolean TEST_ENTRANCE_TRANSITION = true;
59 
60     private static final long TIME_TO_LOAD_OVERVIEW_ROW_MS = 1000;
61     private static final long TIME_TO_LOAD_RELATED_ROWS_MS = 2000;
62 
63     private Action mActionPlay;
64     private Action mActionRent;
65     private Action mActionBuy;
66 
67     @Override
onCreate(Bundle savedInstanceState)68     public void onCreate(Bundle savedInstanceState) {
69         Log.i(TAG, "onCreate");
70         super.onCreate(savedInstanceState);
71 
72         setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
73         setTitle("Leanback Sample App");
74         setOnSearchClickedListener(new View.OnClickListener() {
75             @Override
76             public void onClick(View view) {
77                 Intent intent = new Intent(getActivity(), SearchSupportActivity.class);
78                 startActivity(intent);
79             }
80         });
81 
82         mActionPlay = new Action(ACTION_PLAY, "Play");
83         mActionRent = new Action(ACTION_RENT, "Rent", "$3.99",
84                 getResources().getDrawable(R.drawable.ic_action_a));
85         mActionBuy = new Action(ACTION_BUY, "Buy $9.99");
86 
87         ClassPresenterSelector ps = new ClassPresenterSelector();
88         DetailsOverviewRowPresenter dorPresenter =
89                 new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter());
90         dorPresenter.setOnActionClickedListener(new OnActionClickedListener() {
91             @Override
92             public void onActionClicked(Action action) {
93                 Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show();
94                 DetailsOverviewRow dor = (DetailsOverviewRow) mRowsAdapter.get(0);
95                 if (action.getId() == ACTION_BUY) {
96                     // on the UI thread, we can modify actions adapter directly
97                     SparseArrayObjectAdapter actions = (SparseArrayObjectAdapter)
98                             dor.getActionsAdapter();
99                     actions.set(ACTION_PLAY, mActionPlay);
100                     actions.clear(ACTION_RENT);
101                     actions.clear(ACTION_BUY);
102                     dor.setItem(mPhotoItem.getTitle() + "(Owned)");
103                     dor.setImageDrawable(getResources().getDrawable(R.drawable.details_img_16x9));
104                 } else if (action.getId() == ACTION_RENT) {
105                     // on the UI thread, we can modify actions adapter directly
106                     SparseArrayObjectAdapter actions = (SparseArrayObjectAdapter)
107                             dor.getActionsAdapter();
108                     actions.set(ACTION_PLAY, mActionPlay);
109                     actions.clear(ACTION_RENT);
110                     dor.setItem(mPhotoItem.getTitle() + "(Rented)");
111                 } else if (action.getId() == ACTION_PLAY) {
112                     Intent intent = new Intent(getActivity(), PlaybackOverlaySupportActivity.class);
113                     getActivity().startActivity(intent);
114                 }
115             }
116         });
117 
118         ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter);
119         ps.addClassPresenter(ListRow.class, new ListRowPresenter());
120 
121         mRowsAdapter = new ArrayObjectAdapter(ps);
122 
123         PhotoItem item = (PhotoItem) (savedInstanceState != null ?
124                 savedInstanceState.getParcelable(ITEM) : null);
125         if (item != null) {
126             setItem(item);
127         }
128 
129         setOnItemViewClickedListener(new OnItemViewClickedListener() {
130             @Override
131             public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
132                     RowPresenter.ViewHolder rowViewHolder, Row row) {
133                 Log.i(TAG, "onItemClicked: " + item + " row " + row);
134                 if (item instanceof PhotoItem){
135                     Intent intent = new Intent(getActivity(), DetailsSupportActivity.class);
136                     intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item);
137 
138                     Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
139                             getActivity(),
140                             ((ImageCardView)itemViewHolder.view).getMainImageView(),
141                             DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle();
142                     getActivity().startActivity(intent, bundle);
143                 }
144             }
145         });
146         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
147             @Override
148             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
149                     RowPresenter.ViewHolder rowViewHolder, Row row) {
150                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
151             }
152         });
153 
154         if (TEST_SHARED_ELEMENT_TRANSITION) {
155             dorPresenter.setSharedElementEnterTransition(getActivity(),
156                     DetailsSupportActivity.SHARED_ELEMENT_NAME);
157         }
158         if (TEST_ENTRANCE_TRANSITION) {
159             // don't run entrance transition if Activity is restored.
160             if (savedInstanceState == null) {
161                 prepareEntranceTransition();
162             }
163         }
164     }
165 
166     @Override
onSaveInstanceState(Bundle outState)167     public void onSaveInstanceState(Bundle outState) {
168         super.onSaveInstanceState(outState);
169         outState.putParcelable(ITEM, mPhotoItem);
170     }
171 
setItem(PhotoItem photoItem)172     public void setItem(PhotoItem photoItem) {
173         mPhotoItem = photoItem;
174 
175         mRowsAdapter.clear();
176         new Handler().postDelayed(new Runnable() {
177             public void run() {
178                 Resources res = getActivity().getResources();
179                 DetailsOverviewRow dor = new DetailsOverviewRow(mPhotoItem.getTitle());
180                 dor.setImageDrawable(res.getDrawable(mPhotoItem.getImageResourceId()));
181                 SparseArrayObjectAdapter adapter = new SparseArrayObjectAdapter();
182                 adapter.set(ACTION_RENT, mActionRent);
183                 adapter.set(ACTION_BUY, mActionBuy);
184                 dor.setActionsAdapter(adapter);
185                 mRowsAdapter.add(0, dor);
186                 setSelectedPosition(0, false);
187             }
188         }, TIME_TO_LOAD_OVERVIEW_ROW_MS);
189 
190         new Handler().postDelayed(new Runnable() {
191             public void run() {
192                 for (int i = 0; i < NUM_ROWS; ++i) {
193                     ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
194                     listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
195                     listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
196                     listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3));
197                     listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
198                     HeaderItem header = new HeaderItem(i, "Row " + i);
199                     mRowsAdapter.add(new ListRow(header, listRowAdapter));
200                 }
201                 if (TEST_ENTRANCE_TRANSITION) {
202                     startEntranceTransition();
203                 }
204             }
205         }, TIME_TO_LOAD_RELATED_ROWS_MS);
206         setAdapter(mRowsAdapter);
207     }
208 
209     @Override
onStart()210     public void onStart() {
211         super.onStart();
212         if (mPhotoItem != null) {
213             mBackgroundHelper.setBackground(
214                     getActivity(), mPhotoItem.getImageResourceId());
215         }
216     }
217 
218 }
219