Android Open Source - BuildingForAndroidTV T V Video Details Fragment






From Project

Back to project page BuildingForAndroidTV.

License

The source code is released under:

MIT License

If you think the Android project BuildingForAndroidTV listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sgottard.tvdemoapp;
/*from  ww  w.j  a  v a2s. com*/
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v17.leanback.app.BackgroundManager;
import android.support.v17.leanback.app.DetailsFragment;
import android.support.v17.leanback.widget.Action;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.ClassPresenterSelector;
import android.support.v17.leanback.widget.DetailsOverviewRow;
import android.support.v17.leanback.widget.DetailsOverviewRowPresenter;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.OnActionClickedListener;
import android.support.v17.leanback.widget.OnItemClickedListener;
import android.support.v17.leanback.widget.Row;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class TVVideoDetailsFragment extends DetailsFragment {
    private static final String TAG = "TVVideoDetailsFragment";

    private static final int ACTION_WATCH_TRAILER = 1;
    private static final int ACTION_RENT = 2;
    private static final int ACTION_BUY = 3;

    private static final int DETAIL_THUMB_WIDTH = 274;
    private static final int DETAIL_THUMB_HEIGHT = 274;

    private static final int NUM_COLS = 10;

    private static final String MOVIE = "Movie";

    private Movie selectedMovie;

    private Drawable mDefaultBackground;
    private Target mBackgroundTarget;
    private DisplayMetrics mMetrics;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate DetailsFragment");
        super.onCreate(savedInstanceState);

        BackgroundManager backgroundManager = BackgroundManager.getInstance(getActivity());
        backgroundManager.attach(getActivity().getWindow());
        mBackgroundTarget = new PicassoBackgroundManagerTarget(backgroundManager);

        mDefaultBackground = getResources().getDrawable(R.drawable.default_background);

        mMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);

        selectedMovie = (Movie) getActivity().getIntent().getSerializableExtra(MOVIE);
        new DetailRowBuilderTask().execute(selectedMovie);

        setOnItemClickedListener(getDefaultItemClickedListener());
        updateBackground(selectedMovie.getBackgroundImageURI());

    }

    private class DetailRowBuilderTask extends AsyncTask<Movie, Integer, DetailsOverviewRow> {
        @Override
        protected DetailsOverviewRow doInBackground(Movie... movies) {
            selectedMovie = movies[0];

            DetailsOverviewRow row = new DetailsOverviewRow(selectedMovie);
            try {
                Bitmap poster = Picasso.with(getActivity())
                        .load(selectedMovie.getCardImageUrl())
                        .resize(dpToPx(DETAIL_THUMB_WIDTH, getActivity().getApplicationContext()),
                                dpToPx(DETAIL_THUMB_HEIGHT, getActivity().getApplicationContext()))
                        .centerCrop()
                        .get();
                row.setImageBitmap(getActivity(), poster);
            } catch (IOException e) {
            }

            row.addAction(new Action(ACTION_WATCH_TRAILER, getResources().getString(
                    R.string.watch_trailer_1), getResources().getString(R.string.watch_trailer_2)));
            row.addAction(new Action(ACTION_RENT, getResources().getString(R.string.rent_1),
                    getResources().getString(R.string.rent_2)));
            row.addAction(new Action(ACTION_BUY, getResources().getString(R.string.buy_1),
                    getResources().getString(R.string.buy_2)));
            return row;
        }

        @Override
        protected void onPostExecute(DetailsOverviewRow detailRow) {
            ClassPresenterSelector ps = new ClassPresenterSelector();
            DetailsOverviewRowPresenter dorPresenter =
                    new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter());
            // set detail background and style
            dorPresenter.setBackgroundColor(getResources().getColor(R.color.detail_background));
            dorPresenter.setStyleLarge(true);
            dorPresenter.setOnActionClickedListener(new OnActionClickedListener() {
                @Override
                public void onActionClicked(Action action) {
                    if (action.getId() == ACTION_WATCH_TRAILER) {
                        Intent intent = new Intent(getActivity(), PlayerActivity.class);
                        intent.putExtra(getResources().getString(R.string.movie), selectedMovie);
                        intent.putExtra(getResources().getString(R.string.should_start), true);
                        startActivity(intent);
                    }
                    else {
                        Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

            ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter);
            ps.addClassPresenter(ListRow.class,
                    new ListRowPresenter());

            ArrayObjectAdapter adapter = new ArrayObjectAdapter(ps);
            adapter.add(detailRow);

            String subcategories[] = {
                    getString(R.string.related_movies)
            };
            List<Movie> list = MovieList.list;
            Collections.shuffle(list);
            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
            for (int j = 0; j < NUM_COLS; j++) {
                listRowAdapter.add(list.get(j % 5));
            }

            HeaderItem header = new HeaderItem(0, subcategories[0], null);
            adapter.add(new ListRow(header, listRowAdapter));

            setAdapter(adapter);
        }

    }

    protected OnItemClickedListener getDefaultItemClickedListener() {
        return new OnItemClickedListener() {
            @Override
            public void onItemClicked(Object item, Row row) {
                if (item instanceof Movie) {
                    Movie movie = (Movie) item;
                    Intent intent = new Intent(getActivity(), TVDetailsActivity.class);
                    intent.putExtra(MOVIE, movie);
                    startActivity(intent);
                }
            }
        };
    }

    protected void updateBackground(URI uri) {
        Log.d(TAG, "uri" + uri);
        Log.d(TAG, "metrics" + mMetrics.toString());
        Picasso.with(getActivity())
                .load(uri.toString())
                .resize(mMetrics.widthPixels, mMetrics.heightPixels)
                .error(mDefaultBackground)
                .into(mBackgroundTarget);
    }

    public static int dpToPx(int dp, Context ctx) {
        float density = ctx.getResources().getDisplayMetrics().density;
        return Math.round((float) dp * density);
    }
}




Java Source Code List

com.sgottard.tvdemoapp.ApplicationTest.java
com.sgottard.tvdemoapp.ApplicationTest.java
com.sgottard.tvdemoapp.ApplicationTest.java
com.sgottard.tvdemoapp.CardPresenter.java
com.sgottard.tvdemoapp.CardPresenter.java
com.sgottard.tvdemoapp.CardPresenter.java
com.sgottard.tvdemoapp.CustomFrameLayout.java
com.sgottard.tvdemoapp.CustomHeadersFragment.java
com.sgottard.tvdemoapp.CustomHeadersFragment.java
com.sgottard.tvdemoapp.CustomRowsFragment.java
com.sgottard.tvdemoapp.CustomRowsFragment.java
com.sgottard.tvdemoapp.DetailsDescriptionPresenter.java
com.sgottard.tvdemoapp.DetailsDescriptionPresenter.java
com.sgottard.tvdemoapp.DetailsDescriptionPresenter.java
com.sgottard.tvdemoapp.MovieList.java
com.sgottard.tvdemoapp.MovieList.java
com.sgottard.tvdemoapp.MovieList.java
com.sgottard.tvdemoapp.Movie.java
com.sgottard.tvdemoapp.Movie.java
com.sgottard.tvdemoapp.Movie.java
com.sgottard.tvdemoapp.PicassoBackgroundManagerTarget.java
com.sgottard.tvdemoapp.PicassoBackgroundManagerTarget.java
com.sgottard.tvdemoapp.PicassoBackgroundManagerTarget.java
com.sgottard.tvdemoapp.PlayerActivity.java
com.sgottard.tvdemoapp.PlayerActivity.java
com.sgottard.tvdemoapp.PlayerActivity.java
com.sgottard.tvdemoapp.TVDemoActivity.java
com.sgottard.tvdemoapp.TVDemoActivity.java
com.sgottard.tvdemoapp.TVDemoActivity.java
com.sgottard.tvdemoapp.TVDemoFragment.java
com.sgottard.tvdemoapp.TVDemoFragment.java
com.sgottard.tvdemoapp.TVDemoFragment.java
com.sgottard.tvdemoapp.TVDetailsActivity.java
com.sgottard.tvdemoapp.TVDetailsActivity.java
com.sgottard.tvdemoapp.TVDetailsActivity.java
com.sgottard.tvdemoapp.TVGridActivity.java
com.sgottard.tvdemoapp.TVGridActivity.java
com.sgottard.tvdemoapp.TVGridActivity.java
com.sgottard.tvdemoapp.TVGridFragment.java
com.sgottard.tvdemoapp.TVGridFragment.java
com.sgottard.tvdemoapp.TVGridFragment.java
com.sgottard.tvdemoapp.TVSearchActivity.java
com.sgottard.tvdemoapp.TVSearchActivity.java
com.sgottard.tvdemoapp.TVSearchActivity.java
com.sgottard.tvdemoapp.TVSearchFragment.java
com.sgottard.tvdemoapp.TVSearchFragment.java
com.sgottard.tvdemoapp.TVSearchFragment.java
com.sgottard.tvdemoapp.TVVideoDetailsFragment.java
com.sgottard.tvdemoapp.TVVideoDetailsFragment.java
com.sgottard.tvdemoapp.TVVideoDetailsFragment.java
com.sgottard.tvdemoapp.Utils.java
com.sgottard.tvdemoapp.Utils.java
com.sgottard.tvdemoapp.Utils.java