content: /**
   * This file was modified by Amazon:
   * Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *     http://aws.amazon.com/apache2.0/
   *
   * or in the "license" file accompanying this file. This file is distributed
   * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
   * express or implied. See the License for the specific language governing
   * permissions and limitations under the License.
   */
  /*
   * Copyright (C) 2014 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License
   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing permissions and limitations under
   * the License.
   */
  package com.amazon.android.tv.tenfoot.ui.fragments;
  
  import com.amazon.android.contentbrowser.ContentBrowser;
  import com.amazon.android.model.content.Content;
  import com.amazon.android.model.content.ContentContainer;
  import com.amazon.android.tv.tenfoot.presenter.CardPresenter;
  import com.amazon.android.tv.tenfoot.presenter.CustomListRowPresenter;
  
  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.OnItemViewClickedListener;
  import android.support.v17.leanback.widget.Presenter;
  import android.support.v17.leanback.widget.Row;
  import android.support.v17.leanback.widget.RowHeaderPresenter;
  import android.support.v17.leanback.widget.RowPresenter;
  import android.util.Log;
  
  /**
   * A fragment to display recommended content.
   */
  public class RecommendedContentFragment extends RowsFragment {
  
      private static final String TAG = RecommendedContentFragment.class.getSimpleName();
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
  
          super.onActivityCreated(savedInstanceState);
  
          RowHeaderPresenter rowHeaderPresenter = new RowHeaderPresenter();
          CustomListRowPresenter customListRowPresenter = new CustomListRowPresenter();
          customListRowPresenter.setHeaderPresenter(rowHeaderPresenter);
          ArrayObjectAdapter rowsAdapter = new ArrayObjectAdapter(customListRowPresenter);
  
          loadRootContentContainer(rowsAdapter);
  
          setAdapter(rowsAdapter);
  
          setOnItemViewClickedListener(new ItemViewClickedListener());
      }
  
      private void loadRootContentContainer(ArrayObjectAdapter rowsAdapter) {
  
          ContentContainer rootContentContainer = ContentBrowser.getInstance(getActivity())
                                                                .getRootContentContainer();
  
          CardPresenter cardPresenter = new CardPresenter();
  
          for (ContentContainer contentContainer : rootContentContainer.getContentContainers()) {
  
              HeaderItem header = new HeaderItem(0, contentContainer.getName());
              ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
  
              for (ContentContainer innerContentContainer : contentContainer.getContentContainers()) {
                  listRowAdapter.add(innerContentContainer);
              }
  
              for (Content content : contentContainer.getContents()) {
                  listRowAdapter.add(content);
              }
  
              rowsAdapter.add(new ListRow(header, listRowAdapter));
          }
      }
  
      private final class ItemViewClickedListener implements OnItemViewClickedListener {
  
          @Override
          public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                                    RowPresenter.ViewHolder rowViewHolder, Row row) {
  
              if (item instanceof Content) {
                  Content content = (Content) item;
                  Log.d(TAG, "Content with title " + content.getTitle() + " was clicked");
  
                  ContentBrowser.getInstance(getActivity())
                                .setLastSelectedContent(content)
                                .switchToScreen(ContentBrowser.CONTENT_DETAILS_SCREEN, content);
  
              }
          }
      }
  
  }
  