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