• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.Log;
37 import android.widget.Toast;
38 
39 public class DetailsFragment extends android.support.v17.leanback.app.DetailsFragment {
40     private static final String TAG = "leanback.DetailsFragment";
41     private static final String ITEM = "item";
42 
43     private static final int NUM_ROWS = 3;
44     private ArrayObjectAdapter mRowsAdapter;
45     private PhotoItem mPhotoItem;
46     final CardPresenter cardPresenter = new CardPresenter();
47 
48     private static final int ACTION_BUY = 1;
49     private static final int ACTION_RENT = 2;
50     private static final int ACTION_PLAY = 3;
51 
52     private static final boolean TEST_SHARED_ELEMENT_TRANSITION = true;
53     private static final boolean TEST_ENTRANCE_TRANSITION = true;
54 
55     private static final long TIME_TO_LOAD_OVERVIEW_ROW_MS = 1000;
56     private static final long TIME_TO_LOAD_RELATED_ROWS_MS = 2000;
57 
58     @Override
onCreate(Bundle savedInstanceState)59     public void onCreate(Bundle savedInstanceState) {
60         Log.i(TAG, "onCreate");
61         super.onCreate(savedInstanceState);
62 
63         ClassPresenterSelector ps = new ClassPresenterSelector();
64         DetailsOverviewRowPresenter dorPresenter =
65                 new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter());
66         dorPresenter.setOnActionClickedListener(new OnActionClickedListener() {
67             @Override
68             public void onActionClicked(Action action) {
69                 Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show();
70                 if (action.getId() == ACTION_BUY) {
71                     DetailsOverviewRow dor = new DetailsOverviewRow(mPhotoItem.getTitle() + "(Owned)");
72                     dor.setImageDrawable(getResources().getDrawable(mPhotoItem.getImageResourceId()));
73                     dor.addAction(new Action(ACTION_PLAY, "Play"));
74                     mRowsAdapter.replace(0, dor);
75                 } else if (action.getId() == ACTION_RENT) {
76                     DetailsOverviewRow dor = new DetailsOverviewRow(mPhotoItem.getTitle() + "(Rented)");
77                     dor.setImageDrawable(getResources().getDrawable(mPhotoItem.getImageResourceId()));
78                     dor.addAction(new Action(ACTION_PLAY, "Play"));
79                     dor.addAction(new Action(ACTION_BUY, "Buy $9.99"));
80                     mRowsAdapter.replace(0, dor);
81                 } else if (action.getId() == ACTION_PLAY) {
82                     Intent intent = new Intent(getActivity(), PlaybackOverlayActivity.class);
83                     getActivity().startActivity(intent);
84                 }
85             }
86         });
87 
88         ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter);
89         ps.addClassPresenter(ListRow.class, new ListRowPresenter());
90 
91         mRowsAdapter = new ArrayObjectAdapter(ps);
92 
93         PhotoItem item = (PhotoItem) (savedInstanceState != null ?
94                 savedInstanceState.getParcelable(ITEM) : null);
95         if (item != null) {
96             setItem(item);
97         }
98 
99         setOnItemViewClickedListener(new OnItemViewClickedListener() {
100             @Override
101             public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
102                     RowPresenter.ViewHolder rowViewHolder, Row row) {
103                 Log.i(TAG, "onItemClicked: " + item + " row " + row);
104                 if (item instanceof PhotoItem){
105                     Intent intent = new Intent(getActivity(), DetailsActivity.class);
106                     intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
107 
108                     Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
109                             getActivity(),
110                             ((ImageCardView)itemViewHolder.view).getMainImageView(),
111                             DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
112                     getActivity().startActivity(intent, bundle);
113                 }
114             }
115         });
116         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
117             @Override
118             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
119                     RowPresenter.ViewHolder rowViewHolder, Row row) {
120                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
121             }
122         });
123 
124         if (TEST_SHARED_ELEMENT_TRANSITION) {
125             dorPresenter.setSharedElementEnterTransition(getActivity(),
126                     DetailsActivity.SHARED_ELEMENT_NAME);
127         }
128         if (TEST_ENTRANCE_TRANSITION) {
129             // don't run entrance transition if Activity is restored.
130             if (savedInstanceState == null) {
131                 prepareEntranceTransition();
132             }
133         }
134     }
135 
136     @Override
onSaveInstanceState(Bundle outState)137     public void onSaveInstanceState(Bundle outState) {
138         super.onSaveInstanceState(outState);
139         outState.putParcelable(ITEM, mPhotoItem);
140     }
141 
setItem(PhotoItem photoItem)142     public void setItem(PhotoItem photoItem) {
143         mPhotoItem = photoItem;
144 
145         mRowsAdapter.clear();
146         new Handler().postDelayed(new Runnable() {
147             public void run() {
148                 Resources res = getActivity().getResources();
149                 DetailsOverviewRow dor = new DetailsOverviewRow(mPhotoItem.getTitle());
150                 dor.setImageDrawable(res.getDrawable(mPhotoItem.getImageResourceId()));
151                 dor.addAction(new Action(ACTION_BUY, "Buy $9.99"));
152                 dor.addAction(new Action(ACTION_RENT, "Rent", "$3.99", res.getDrawable(R.drawable.ic_action_a)));
153                 mRowsAdapter.add(0, dor);
154                 setSelectedPosition(0, false);
155             }
156         }, TIME_TO_LOAD_OVERVIEW_ROW_MS);
157 
158         new Handler().postDelayed(new Runnable() {
159             public void run() {
160                 for (int i = 0; i < NUM_ROWS; ++i) {
161                     ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
162                     listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
163                     listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
164                     listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3));
165                     listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
166                     HeaderItem header = new HeaderItem(i, "Row " + i);
167                     mRowsAdapter.add(new ListRow(header, listRowAdapter));
168                 }
169                 if (TEST_ENTRANCE_TRANSITION) {
170                     startEntranceTransition();
171                 }
172             }
173         }, TIME_TO_LOAD_RELATED_ROWS_MS);
174         setAdapter(mRowsAdapter);
175     }
176 
177 }
178