1 /* This file is auto-generated from BrowseFragment.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.support.v4.app.Fragment;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.support.v17.leanback.app.GuidedStepSupportFragment;
23 import android.support.v17.leanback.app.RowsSupportFragment;
24 import android.support.v17.leanback.widget.ArrayObjectAdapter;
25 import android.support.v17.leanback.widget.DividerRow;
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.OnItemViewClickedListener;
31 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
32 import android.support.v17.leanback.widget.PageRow;
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.SectionRow;
37 import android.support.v4.app.ActivityOptionsCompat;
38 import android.util.Log;
39 import android.view.LayoutInflater;
40 import android.view.View;
41 import android.view.ViewGroup;
42 
43 public class BrowseSupportFragment extends android.support.v17.leanback.app.BrowseSupportFragment {
44     private static final String TAG = "leanback.BrowseSupportFragment";
45 
46     private static final boolean TEST_ENTRANCE_TRANSITION = true;
47     private static final int NUM_ROWS = 8;
48     private static final long HEADER_ID1 = 1001;
49     private static final long HEADER_ID2 = 1002;
50     private static final long HEADER_ID3 = 1003;
51 
52     private ArrayObjectAdapter mRowsAdapter;
53     private BackgroundHelper mBackgroundHelper = new BackgroundHelper();
54 
55     // For good performance, it's important to use a single instance of
56     // a card presenter for all rows using that presenter.
57     final CardPresenter mCardPresenter = new CardPresenter();
58     final CardPresenter mCardPresenter2 = new CardPresenter(R.style.MyImageCardViewTheme);
59 
BrowseSupportFragment()60     public BrowseSupportFragment() {
61         getMainFragmentRegistry().registerFragment(PageRow.class, new PageRowFragmentFactory());
62     }
63 
64     @Override
onCreate(Bundle savedInstanceState)65     public void onCreate(Bundle savedInstanceState) {
66         Log.i(TAG, "onCreate");
67         super.onCreate(savedInstanceState);
68 
69         setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
70         setTitle("Leanback Sample App");
71         setHeadersState(HEADERS_ENABLED);
72         setupRows();
73         setOnSearchClickedListener(new View.OnClickListener() {
74             @Override
75             public void onClick(View view) {
76                 Intent intent = new Intent(getActivity(), SearchSupportActivity.class);
77                 startActivity(intent);
78             }
79         });
80 
81         setOnItemViewClickedListener(new ItemViewClickedListener());
82         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
83             @Override
84             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
85                     RowPresenter.ViewHolder rowViewHolder, Row row) {
86                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
87 
88                 if (isShowingHeaders()) {
89                     mBackgroundHelper.setBackground(getActivity(), null);
90                 } else if (item instanceof PhotoItem) {
91                     mBackgroundHelper.setBackground(
92                             getActivity(), ((PhotoItem) item).getImageResourceId());
93                 }
94             }
95         });
96         if (TEST_ENTRANCE_TRANSITION) {
97             // don't run entrance transition if fragment is restored.
98             if (savedInstanceState == null) {
99                 prepareEntranceTransition();
100             }
101         }
102 
103         // simulates in a real world use case  data being loaded two seconds later
104         new Handler().postDelayed(new Runnable() {
105             public void run() {
106                 loadData();
107                 startEntranceTransition();
108             }
109         }, 2000);
110     }
111 
112     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)113     public View onCreateView(
114             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
115         return super.onCreateView(inflater, container, savedInstanceState);
116     }
117 
setupRows()118     private void setupRows() {
119         ListRowPresenter listRowPresenter = new ListRowPresenter();
120         listRowPresenter.setNumRows(2);
121         mRowsAdapter = new ArrayObjectAdapter(listRowPresenter);
122         setAdapter(mRowsAdapter);
123     }
124 
loadData()125     private void loadData() {
126         int i = 0;
127 
128         mRowsAdapter.add(new PageRow(new HeaderItem(HEADER_ID1, "Page Row 0")));
129         mRowsAdapter.add(new DividerRow());
130 
131         mRowsAdapter.add(new SectionRow(new HeaderItem("section 0")));
132         for (; i < NUM_ROWS; ++i) {
133             mRowsAdapter.add(new ListRow(new HeaderItem(i, "Row " + i), createListRowAdapter(i)));
134         }
135 
136         mRowsAdapter.add(new DividerRow());
137         mRowsAdapter.add(new PageRow(new HeaderItem(HEADER_ID2, "Page Row 1")));
138 
139         mRowsAdapter.add(new PageRow(new HeaderItem(HEADER_ID3, "Page Row 2")));
140     }
141 
createListRowAdapter(int i)142     private ArrayObjectAdapter createListRowAdapter(int i) {
143         ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter((i & 1) == 0 ?
144                 mCardPresenter : mCardPresenter2);
145         listRowAdapter.add(new PhotoItem(
146                 "Hello world",
147                 R.drawable.gallery_photo_1));
148         listRowAdapter.add(new PhotoItem(
149                 "This is a test",
150                 "Only a test",
151                 R.drawable.gallery_photo_2));
152         listRowAdapter.add(new PhotoItem(
153                 "Android TV",
154                 "by Google",
155                 R.drawable.gallery_photo_3));
156         listRowAdapter.add(new PhotoItem(
157                 "Leanback",
158                 R.drawable.gallery_photo_4));
159         listRowAdapter.add(new PhotoItem(
160                 "GuidedStep (Slide left/right)",
161                 R.drawable.gallery_photo_5));
162         listRowAdapter.add(new PhotoItem(
163                 "GuidedStep (Slide bottom up)",
164                 "Open GuidedStepSupportFragment",
165                 R.drawable.gallery_photo_6));
166         listRowAdapter.add(new PhotoItem(
167                 "Android TV",
168                 "open RowsSupportActivity",
169                 R.drawable.gallery_photo_7));
170         listRowAdapter.add(new PhotoItem(
171                 "Leanback",
172                 "open BrowseSupportActivity",
173                 R.drawable.gallery_photo_8));
174         listRowAdapter.add(new PhotoItem(
175                 "Hello world",
176                 R.drawable.gallery_photo_1));
177         listRowAdapter.add(new PhotoItem(
178                 "This is a test",
179                 "Only a test",
180                 R.drawable.gallery_photo_2));
181         listRowAdapter.add(new PhotoItem(
182                 "Android TV",
183                 "by Google",
184                 R.drawable.gallery_photo_3));
185         listRowAdapter.add(new PhotoItem(
186                 "Leanback",
187                 R.drawable.gallery_photo_4));
188         return listRowAdapter;
189     }
190 
191     private final class ItemViewClickedListener implements OnItemViewClickedListener {
192         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)193         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
194                 RowPresenter.ViewHolder rowViewHolder, Row row) {
195 
196             Intent intent;
197             Bundle bundle;
198             if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_6) {
199                 GuidedStepSupportFragment.add(getFragmentManager(),
200                         new GuidedStepSupportHalfScreenActivity.FirstStepFragment(),
201                         R.id.lb_guidedstep_host);
202                 return;
203             } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_5) {
204                 GuidedStepSupportFragment.add(getFragmentManager(),
205                         new GuidedStepSupportActivity.FirstStepFragment(), R.id.lb_guidedstep_host);
206                 return;
207             } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) {
208                 intent = new Intent(getActivity(), BrowseSupportActivity.class);
209                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
210                         .toBundle();
211             } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) {
212                 intent = new Intent(getActivity(), RowsSupportActivity.class);
213                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
214                         .toBundle();
215             } else {
216                 intent = new Intent(getActivity(), DetailsSupportActivity.class);
217                 intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item);
218                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
219                         getActivity(),
220                         ((ImageCardView) itemViewHolder.view).getMainImageView(),
221                         DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle();
222             }
223             getActivity().startActivity(intent, bundle);
224         }
225     }
226 
227     public static class PageRowFragmentFactory extends FragmentFactory {
228 
229         @Override
createFragment(Object rowObj)230         public Fragment createFragment(Object rowObj) {
231             Row row = (Row) rowObj;
232             if (row.getHeaderItem().getId() == HEADER_ID1) {
233                 return new SampleRowsSupportFragment();
234             } else if (row.getHeaderItem().getId() == HEADER_ID2) {
235                 return new SampleRowsSupportFragment();
236             } else if (row.getHeaderItem().getId() == HEADER_ID3) {
237                 return new SampleFragment();
238             }
239 
240             return null;
241         }
242     }
243 
244     public static class SampleRowsSupportFragment extends RowsSupportFragment {
245         final CardPresenter mCardPresenter = new CardPresenter();
246         final CardPresenter mCardPresenter2 = new CardPresenter(R.style.MyImageCardViewTheme);
247 
loadFragmentData()248         void loadFragmentData() {
249             ArrayObjectAdapter adapter = (ArrayObjectAdapter) getAdapter();
250             for (int i = 0; i < 4; i++) {
251                 ListRow row = new ListRow(new HeaderItem("Row " + i), createListRowAdapter(i));
252                 adapter.add(row);
253             }
254             if (getMainFragmentAdapter() != null) {
255                 getMainFragmentAdapter().getFragmentHost()
256                         .notifyDataReady(getMainFragmentAdapter());
257             }
258         }
259 
SampleRowsSupportFragment()260         public SampleRowsSupportFragment() {
261             ArrayObjectAdapter adapter = new ArrayObjectAdapter(new ListRowPresenter());
262             setAdapter(adapter);
263             // simulates late data loading:
264             new Handler().postDelayed(new Runnable() {
265                 public void run() {
266                     loadFragmentData();
267                 }
268             }, 500);
269 
270             setOnItemViewClickedListener(new OnItemViewClickedListener() {
271                 @Override
272                 public void onItemClicked(
273                         Presenter.ViewHolder itemViewHolder,
274                         Object item,
275                         RowPresenter.ViewHolder rowViewHolder,
276                         Row row) {
277                     Intent intent;
278                     Bundle bundle;
279                     if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_6) {
280                         GuidedStepSupportFragment.add(getActivity().getSupportFragmentManager(),
281                                 new GuidedStepSupportHalfScreenActivity.FirstStepFragment(),
282                                 R.id.lb_guidedstep_host);
283                         return;
284                     } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_5) {
285                         GuidedStepSupportFragment.add(getActivity().getSupportFragmentManager(),
286                                 new GuidedStepSupportActivity.FirstStepFragment(), R.id.lb_guidedstep_host);
287                         return;
288                     } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) {
289                         intent = new Intent(getActivity(), BrowseSupportActivity.class);
290                         bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
291                                 .toBundle();
292                     } else if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) {
293                         intent = new Intent(getActivity(), RowsSupportActivity.class);
294                         bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
295                                 .toBundle();
296                     } else {
297                         intent = new Intent(getActivity(), DetailsSupportActivity.class);
298                         intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item);
299                         bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
300                                 getActivity(),
301                                 ((ImageCardView) itemViewHolder.view).getMainImageView(),
302                                 DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle();
303                     }
304                     getActivity().startActivity(intent, bundle);
305                 }
306             });
307         }
308 
createListRowAdapter(int i)309         private ArrayObjectAdapter createListRowAdapter(int i) {
310             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter((i & 1) == 0 ?
311                     mCardPresenter : mCardPresenter2);
312             listRowAdapter.add(new PhotoItem(
313                     "Hello world",
314                     R.drawable.gallery_photo_1));
315             listRowAdapter.add(new PhotoItem(
316                     "This is a test",
317                     "Only a test",
318                     R.drawable.gallery_photo_2));
319             listRowAdapter.add(new PhotoItem(
320                     "Android TV",
321                     "by Google",
322                     R.drawable.gallery_photo_3));
323             listRowAdapter.add(new PhotoItem(
324                     "Leanback",
325                     R.drawable.gallery_photo_4));
326             listRowAdapter.add(new PhotoItem(
327                     "GuidedStep (Slide left/right)",
328                     R.drawable.gallery_photo_5));
329             listRowAdapter.add(new PhotoItem(
330                     "GuidedStep (Slide bottom up)",
331                     "Open GuidedStepSupportFragment",
332                     R.drawable.gallery_photo_6));
333             listRowAdapter.add(new PhotoItem(
334                     "Android TV",
335                     "open RowsSupportActivity",
336                     R.drawable.gallery_photo_7));
337             listRowAdapter.add(new PhotoItem(
338                     "Leanback",
339                     "open BrowseSupportActivity",
340                     R.drawable.gallery_photo_8));
341             return listRowAdapter;
342         }
343     }
344 
345     public static class PageFragmentAdapterImpl extends MainFragmentAdapter<SampleFragment> {
346 
PageFragmentAdapterImpl(SampleFragment fragment)347         public PageFragmentAdapterImpl(SampleFragment fragment) {
348             super(fragment);
349             setScalingEnabled(true);
350         }
351 
352         @Override
setEntranceTransitionState(boolean state)353         public void setEntranceTransitionState(boolean state) {
354             getFragment().setEntranceTransitionState(state);
355         }
356     }
357 
358     public static class SampleFragment extends Fragment implements MainFragmentAdapterProvider {
359 
360         final PageFragmentAdapterImpl mMainFragmentAdapter = new PageFragmentAdapterImpl(this);
361 
setEntranceTransitionState(boolean state)362         public void setEntranceTransitionState(boolean state) {
363             final View view = getView();
364             int visibility = state ? View.VISIBLE : View.INVISIBLE;
365             view.findViewById(R.id.tv1).setVisibility(visibility);
366             view.findViewById(R.id.tv2).setVisibility(visibility);
367             view.findViewById(R.id.tv3).setVisibility(visibility);
368         }
369 
370         @Override
onCreateView( final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)371         public View onCreateView(
372                 final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
373             View view = inflater.inflate(R.layout.page_fragment, container, false);
374             view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
375                 @Override
376                 public void onClick(View view) {
377                     Intent intent = new Intent(inflater.getContext(), GuidedStepSupportActivity.class);
378                     startActivity(intent);
379                 }
380             });
381 
382             return view;
383         }
384 
385         @Override
onViewCreated(View view, Bundle savedInstanceState)386         public void onViewCreated(View view, Bundle savedInstanceState) {
387             // static layout has view and data ready immediately
388             mMainFragmentAdapter.getFragmentHost().notifyViewCreated(mMainFragmentAdapter);
389             mMainFragmentAdapter.getFragmentHost().notifyDataReady(mMainFragmentAdapter);
390         }
391 
392         @Override
getMainFragmentAdapter()393         public MainFragmentAdapter getMainFragmentAdapter() {
394             return mMainFragmentAdapter;
395         }
396     }
397 }
398