// Path: episode_4/TVDemoApp/app/src/main/java/com/sgottard/tvdemoapp/Utils.java
// public class Utils {
// 
//     /*
//      * Making sure public utility methods remain static
//      */
//     private Utils() {
//     }
// 
//     /**
//      * Returns the screen/display size
//      */
//     public static Point getDisplaySize(Context context) {
//         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//         Display display = wm.getDefaultDisplay();
//         Point size = new Point();
//         display.getSize(size);
//         int width = size.x;
//         int height = size.y;
//         return size;
//     }
// 
//     /**
//      * Shows a (long) toast
//      */
//     public static void showToast(Context context, String msg) {
//         Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
//     }
// 
//     /**
//      * Shows a (long) toast.
//      */
//     public static void showToast(Context context, int resourceId) {
//         Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show();
//     }
// 
//     public static int convertDpToPixel(Context ctx, int dp) {
//         float density = ctx.getResources().getDisplayMetrics().density;
//         return Math.round((float) dp * density);
//     }
// 
// 
//     public static long getDuration(String videoUrl) {
//         MediaMetadataRetriever mmr = new MediaMetadataRetriever();
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//             mmr.setDataSource(videoUrl, new HashMap<String, String>());
//         } else {
//             mmr.setDataSource(videoUrl);
//         }
//         return Long.parseLong(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
//     }
// }
// 
// Path: episode_4/TVDemoApp/app/src/main/java/com/sgottard/tvdemoapp/presenter/GridItemPresenter.java
// public class GridItemPresenter extends Presenter {
//     private static int GRID_ITEM_WIDTH = 200;
//     private static int GRID_ITEM_HEIGHT = 200;
// 
//     private Fragment mainFragment;
// 
//     public GridItemPresenter(Fragment fragment) {
//         this.mainFragment = fragment;
//     }
// 
//     @Override
//     public ViewHolder onCreateViewHolder(ViewGroup parent) {
//         TextView view = new TextView(parent.getContext());
//         view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
//         view.setFocusable(true);
//         view.setFocusableInTouchMode(true);
//         view.setBackgroundColor(mainFragment.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) {
//     }
// }


import android.content.Intent;
import android.os.Bundle;
import android.support.v17.leanback.app.RowsFragment;
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.OnItemViewClickedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.sgottard.tvdemoapp.Utils;
import com.sgottard.tvdemoapp.presenter.GridItemPresenter;
import com.sgottard.tvdemoapp.tvleanback.R;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loadRows();
        setCustomPadding();
        setOnItemViewClickedListener(new OnItemViewClickedListener() {
            @Override
            public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
                if (((String) item).indexOf(getString(R.string.grid_view)) >= 0) {
                    Intent intent = new Intent(getActivity(), VerticalGridActivity.class);
                    getActivity().startActivity(intent);
                } else if (((String) item).indexOf(getString(R.string.error_fragment)) >= 0) {
                    Intent intent = new Intent(getActivity(), BrowseErrorActivity.class);
                    getActivity().startActivity(intent);
                } else if (((String) item)
                        .indexOf(getString(R.string.guidedstep_first_title)) >= 0) {
                    Intent intent = new Intent(getActivity(), GuidedStepActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), SettingsActivity.class);
                    getActivity().startActivity(intent);
                }
            }
        });
    }

    private void setCustomPadding() {
// Path: episode_4/TVDemoApp/app/src/main/java/com/sgottard/tvdemoapp/Utils.java
// public class Utils {
// 
//     /*
//      * Making sure public utility methods remain static
//      */
//     private Utils() {
//     }
// 
//     /**
//      * Returns the screen/display size
//      */
//     public static Point getDisplaySize(Context context) {
//         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//         Display display = wm.getDefaultDisplay();
//         Point size = new Point();
//         display.getSize(size);
//         int width = size.x;
//         int height = size.y;
//         return size;
//     }
// 
//     /**
//      * Shows a (long) toast
//      */
//     public static void showToast(Context context, String msg) {
//         Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
//     }
// 
//     /**
//      * Shows a (long) toast.
//      */
//     public static void showToast(Context context, int resourceId) {
//         Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show();
//     }
// 
//     public static int convertDpToPixel(Context ctx, int dp) {
//         float density = ctx.getResources().getDisplayMetrics().density;
//         return Math.round((float) dp * density);
//     }
// 
// 
//     public static long getDuration(String videoUrl) {
//         MediaMetadataRetriever mmr = new MediaMetadataRetriever();
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//             mmr.setDataSource(videoUrl, new HashMap<String, String>());
//         } else {
//             mmr.setDataSource(videoUrl);
//         }
//         return Long.parseLong(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
//     }
// }
// 
// Path: episode_4/TVDemoApp/app/src/main/java/com/sgottard/tvdemoapp/presenter/GridItemPresenter.java
// public class GridItemPresenter extends Presenter {
//     private static int GRID_ITEM_WIDTH = 200;
//     private static int GRID_ITEM_HEIGHT = 200;
// 
//     private Fragment mainFragment;
// 
//     public GridItemPresenter(Fragment fragment) {
//         this.mainFragment = fragment;
//     }
// 
//     @Override
//     public ViewHolder onCreateViewHolder(ViewGroup parent) {
//         TextView view = new TextView(parent.getContext());
//         view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
//         view.setFocusable(true);
//         view.setFocusableInTouchMode(true);
//         view.setBackgroundColor(mainFragment.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) {
//     }
// }


// Path: episode_4/TVDemoApp/app/src/main/java/com/sgottard/tvdemoapp/ui/MoreSamplesFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v17.leanback.app.RowsFragment;
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.OnItemViewClickedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.sgottard.tvdemoapp.Utils;
import com.sgottard.tvdemoapp.presenter.GridItemPresenter;
import com.sgottard.tvdemoapp.tvleanback.R;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loadRows();
        setCustomPadding();
        setOnItemViewClickedListener(new OnItemViewClickedListener() {
            @Override
            public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
                if (((String) item).indexOf(getString(R.string.grid_view)) >= 0) {
                    Intent intent = new Intent(getActivity(), VerticalGridActivity.class);
                    getActivity().startActivity(intent);
                } else if (((String) item).indexOf(getString(R.string.error_fragment)) >= 0) {
                    Intent intent = new Intent(getActivity(), BrowseErrorActivity.class);
                    getActivity().startActivity(intent);
                } else if (((String) item)
                        .indexOf(getString(R.string.guidedstep_first_title)) >= 0) {
                    Intent intent = new Intent(getActivity(), GuidedStepActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), SettingsActivity.class);
                    getActivity().startActivity(intent);
                }
            }
        });
    }

    private void setCustomPadding() {
        getView().setPadding(Utils.convertDpToPixel(getActivity(), -24), Utils.convertDpToPixel(getActivity(), 128), Utils.convertDpToPixel(getActivity(), 48), 0);