Home | History | Annotate | Download | only in leanback
      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.support.v4.app.ActivityCompat;
     20 import android.support.v4.app.ActivityOptionsCompat;
     21 import android.support.v4.view.ViewCompat;
     22 import android.support.v17.leanback.widget.Action;
     23 import android.support.v17.leanback.widget.ArrayObjectAdapter;
     24 import android.support.v17.leanback.widget.ClassPresenterSelector;
     25 import android.support.v17.leanback.widget.DetailsOverviewRow;
     26 import android.support.v17.leanback.widget.DetailsOverviewRowPresenter;
     27 import android.support.v17.leanback.widget.HeaderItem;
     28 import android.support.v17.leanback.widget.ImageCardView;
     29 import android.support.v17.leanback.widget.ListRow;
     30 import android.support.v17.leanback.widget.ListRowPresenter;
     31 import android.support.v17.leanback.widget.OnActionClickedListener;
     32 import android.support.v17.leanback.widget.OnItemViewClickedListener;
     33 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
     34 import android.support.v17.leanback.widget.Presenter;
     35 import android.support.v17.leanback.widget.Row;
     36 import android.support.v17.leanback.widget.RowPresenter;
     37 import android.util.Log;
     38 import android.view.LayoutInflater;
     39 import android.view.View;
     40 import android.view.ViewGroup;
     41 import android.widget.Toast;
     42 
     43 import java.util.ArrayList;
     44 
     45 public class DetailsFragment extends android.support.v17.leanback.app.DetailsFragment {
     46     private static final String TAG = "leanback.DetailsFragment";
     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 
     53     @Override
     54     public void onCreate(Bundle savedInstanceState) {
     55         Log.i(TAG, "onCreate");
     56         super.onCreate(savedInstanceState);
     57 
     58         ClassPresenterSelector ps = new ClassPresenterSelector();
     59         DetailsOverviewRowPresenter dorPresenter =
     60                 new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter());
     61         dorPresenter.setOnActionClickedListener(new OnActionClickedListener() {
     62             public void onActionClicked(Action action) {
     63                 Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show();
     64             }
     65         });
     66 
     67         ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter);
     68         ps.addClassPresenter(ListRow.class, new ListRowPresenter());
     69 
     70         mRowsAdapter = new ArrayObjectAdapter(ps);
     71 
     72         PhotoItem item = (PhotoItem) (savedInstanceState != null ?
     73                 savedInstanceState.getParcelable(ITEM) : null);
     74         if (item != null) {
     75             setItem(item);
     76         }
     77         dorPresenter.setSharedElementEnterTransition(getActivity(),
     78                 DetailsActivity.SHARED_ELEMENT_NAME);
     79 
     80         setOnItemViewClickedListener(new OnItemViewClickedListener() {
     81             @Override
     82             public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
     83                     RowPresenter.ViewHolder rowViewHolder, Row row) {
     84                 Log.i(TAG, "onItemClicked: " + item + " row " + row);
     85                 if (item instanceof PhotoItem){
     86                     Intent intent = new Intent(getActivity(), DetailsActivity.class);
     87                     intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
     88 
     89                     Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
     90                             getActivity(),
     91                             ((ImageCardView)itemViewHolder.view).getMainImageView(),
     92                             DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
     93                     getActivity().startActivity(intent, bundle);
     94                 }
     95             }
     96         });
     97         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
     98             @Override
     99             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
    100                     RowPresenter.ViewHolder rowViewHolder, Row row) {
    101                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
    102             }
    103         });
    104     }
    105 
    106     @Override
    107     public void onSaveInstanceState(Bundle outState) {
    108         super.onSaveInstanceState(outState);
    109         outState.putParcelable(ITEM, mPhotoItem);
    110     }
    111 
    112     public void setItem(PhotoItem photoItem) {
    113         mPhotoItem = photoItem;
    114 
    115         mRowsAdapter.clear();
    116         Resources res = getActivity().getResources();
    117         DetailsOverviewRow dor = new DetailsOverviewRow("Details Overview");
    118         dor.setImageDrawable(res.getDrawable(photoItem.getImageResourceId()));
    119         dor.addAction(new Action(1, "Buy $9.99"));
    120         dor.addAction(new Action(2, "Rent", "$3.99", res.getDrawable(R.drawable.ic_action_a)));
    121         mRowsAdapter.add(dor);
    122 
    123         final CardPresenter cardPresenter = new CardPresenter();
    124         for (int i = 0; i < NUM_ROWS; ++i) {
    125             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
    126             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
    127             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
    128             listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3));
    129             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
    130             HeaderItem header = new HeaderItem(i, "Row " + i, null);
    131             mRowsAdapter.add(new ListRow(header, listRowAdapter));
    132         }
    133 
    134         setAdapter(mRowsAdapter);
    135     }
    136 
    137 }
    138