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