Android Open Source - BuildingForAndroidTV T V Demo 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   w  ww .  ja  v a2s. c om*/
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v17.leanback.app.BackgroundManager;
import android.support.v17.leanback.app.BrowseFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
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.OnItemClickedListener;
import android.support.v17.leanback.widget.OnItemSelectedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

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

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class TVDemoFragment extends BrowseFragment {
    private static final String TAG = "TVDemoFragment";

    private static final int NUM_ROWS = 6;
    private static final int NUM_COLS = 15;

    private ArrayObjectAdapter mRowsAdapter;
    private Drawable mDefaultBackground;
    private Target mBackgroundTarget;
    private DisplayMetrics mMetrics;
    private Timer mBackgroundTimer;
    private final Handler mHandler = new Handler();
    private URI mBackgroundURI;
    Movie mMovie;
    CardPresenter mCardPresenter;

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

        prepareBackgroundManager();

        setupUIElements();

        loadRows();

        setupEventListeners();
    }

    private void loadRows() {
        List<Movie> list = MovieList.setupMovies();

        mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
        mCardPresenter = new CardPresenter();

        int i;
        for (i = 0; i < NUM_ROWS; i++) {
            if (i != 0) {
                Collections.shuffle(list);
            }
            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(mCardPresenter);
            for (int j = 0; j < NUM_COLS; j++) {
                listRowAdapter.add(list.get(j % 5));
            }
            HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i], null);
            mRowsAdapter.add(new ListRow(header, listRowAdapter));
        }

        HeaderItem gridHeader = new HeaderItem(i, "PREFERENCES", null);

        GridItemPresenter mGridPresenter = new GridItemPresenter();
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        gridRowAdapter.add(getResources().getString(R.string.grid_view));
        gridRowAdapter.add(getResources().getString(R.string.send_feeback));
        gridRowAdapter.add(getResources().getString(R.string.personal_settings));
        mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));

        setAdapter(mRowsAdapter);

    }

    private void prepareBackgroundManager() {

        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);
    }

    private void setupUIElements() {
        // setBadgeDrawable(getActivity().getResources().getDrawable(
        // R.drawable.videos_by_google_banner));
        setTitle(getString(R.string.browse_title)); // Badge, when set, takes precedent
                                                    // over title
        setHeadersState(HEADERS_ENABLED);
        setHeadersTransitionOnBackEnabled(true);

        // set fastLane (or headers) background color
        setBrandColor(getResources().getColor(R.color.fastlane_background));
        // set search icon color
        setSearchAffordanceColor(getResources().getColor(R.color.search_opaque));
    }

    private void setupEventListeners() {
        setOnItemSelectedListener(getDefaultItemSelectedListener());
        setOnItemClickedListener(getDefaultItemClickedListener());
        setOnSearchClickedListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), TVSearchActivity.class);
        startActivity(intent);
            }
        });
    }

    protected OnItemSelectedListener getDefaultItemSelectedListener() {
        return new OnItemSelectedListener() {
            @Override
            public void onItemSelected(Object item, Row row) {
                if (item instanceof Movie) {
                    mBackgroundURI = ((Movie) item).getBackgroundImageURI();
                    startBackgroundTimer();
                }
            }
        };
    }

    protected OnItemClickedListener getDefaultItemClickedListener() {
        return new OnItemClickedListener() {
            @Override
            public void onItemClicked(Object item, Row row) {
                if (item instanceof Movie) {
                    Movie movie = (Movie) item;
                    Log.d(TAG, "Item: " + item.toString());
                    Intent intent = new Intent(getActivity(), TVDetailsActivity.class);
                    intent.putExtra(getString(R.string.movie), movie);
                    startActivity(intent);
                }
                else if (item instanceof String) {
          if (((String) item).contains("Grid")) {
            Intent intent = new Intent(getActivity(), TVGridActivity.class);
            startActivity(intent);
          } else {
            Toast.makeText(getActivity(), (String) item, Toast.LENGTH_SHORT).show();
          }
                }
            }
        };
    }

    protected void setDefaultBackground(Drawable background) {
        mDefaultBackground = background;
    }

    protected void setDefaultBackground(int resourceId) {
        mDefaultBackground = getResources().getDrawable(resourceId);
    }

    protected void updateBackground(URI uri) {
        Picasso.with(getActivity())
                .load(uri.toString())
                .resize(mMetrics.widthPixels, mMetrics.heightPixels)
                .centerCrop()
                .error(mDefaultBackground)
                .into(mBackgroundTarget);
    }

    protected void updateBackground(Drawable drawable) {
        BackgroundManager.getInstance(getActivity()).setDrawable(drawable);
    }

    protected void clearBackground() {
        BackgroundManager.getInstance(getActivity()).setDrawable(mDefaultBackground);
    }

    private void startBackgroundTimer() {
        if (null != mBackgroundTimer) {
            mBackgroundTimer.cancel();
        }
        mBackgroundTimer = new Timer();
        mBackgroundTimer.schedule(new UpdateBackgroundTask(), 300);
    }

    private class UpdateBackgroundTask extends TimerTask {

        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mBackgroundURI != null) {
                        updateBackground(mBackgroundURI);
                    }
                }
            });

        }
    }

    private class GridItemPresenter extends Presenter {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent) {
            TextView view = new TextView(parent.getContext());
            view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.setBackgroundColor(getResources().getColor(R.color.default_background));
            view.setTextColor(Color.WHITE);
            view.setGravity(Gravity.CENTER);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, Object item) {
            ((TextView) viewHolder.view).setText((String) item);
        }

        @Override
        public void onUnbindViewHolder(ViewHolder viewHolder) {
        }
    }

}




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