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


import android.app.Fragment;
import android.os.Bundle;
import android.support.v17.leanback.app.HeadersFragment;
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.OnChildSelectedListener;
import android.support.v17.leanback.widget.VerticalGridView;
import android.view.View;
import android.view.ViewGroup;
import com.sgottard.tvdemoapp.Utils;
import com.sgottard.tvdemoapp.tvleanback.R;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
		VerticalGridView gridView = ((MainActivity) getActivity()).getVerticalGridView(this);
		gridView.setOnChildSelectedListener(new OnChildSelectedListener() {
			@Override
			public void onChildSelected(ViewGroup viewGroup, View view, int i, long l) {
				Object obj = ((ListRow) getAdapter().get(i)).getAdapter().get(0);
				getFragmentManager().beginTransaction().replace(R.id.rows_container, (Fragment) obj).commit();
				((MainActivity) getActivity()).updateCurrentFragment((Fragment) obj);
			}
		});

	}

	private void setHeaderAdapter() {
		adapter = new ArrayObjectAdapter();

		LinkedHashMap<Integer, Fragment> fragments = ((MainActivity) getActivity()).getFragments();

		int id = 0;
		for (int i = 0; i < fragments.size(); i++) {
			HeaderItem header = new HeaderItem(id, "Category " + i);
			ArrayObjectAdapter innerAdapter = new ArrayObjectAdapter();
			innerAdapter.add(fragments.get(i));
			adapter.add(id, new ListRow(header, innerAdapter));
			id++;
		}

		setAdapter(adapter);
	}

	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/ui/CustomHeadersFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.support.v17.leanback.app.HeadersFragment;
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.OnChildSelectedListener;
import android.support.v17.leanback.widget.VerticalGridView;
import android.view.View;
import android.view.ViewGroup;
import com.sgottard.tvdemoapp.Utils;
import com.sgottard.tvdemoapp.tvleanback.R;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;

		VerticalGridView gridView = ((MainActivity) getActivity()).getVerticalGridView(this);
		gridView.setOnChildSelectedListener(new OnChildSelectedListener() {
			@Override
			public void onChildSelected(ViewGroup viewGroup, View view, int i, long l) {
				Object obj = ((ListRow) getAdapter().get(i)).getAdapter().get(0);
				getFragmentManager().beginTransaction().replace(R.id.rows_container, (Fragment) obj).commit();
				((MainActivity) getActivity()).updateCurrentFragment((Fragment) obj);
			}
		});

	}

	private void setHeaderAdapter() {
		adapter = new ArrayObjectAdapter();

		LinkedHashMap<Integer, Fragment> fragments = ((MainActivity) getActivity()).getFragments();

		int id = 0;
		for (int i = 0; i < fragments.size(); i++) {
			HeaderItem header = new HeaderItem(id, "Category " + i);
			ArrayObjectAdapter innerAdapter = new ArrayObjectAdapter();
			innerAdapter.add(fragments.get(i));
			adapter.add(id, new ListRow(header, innerAdapter));
			id++;
		}

		setAdapter(adapter);
	}

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