package com.example.android.leanback;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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.ObjectAdapter;
import android.support.v17.leanback.widget.OnItemClickedListener;
import android.support.v17.leanback.widget.Row;
import android.text.TextUtils;
import android.util.Log;

public class SearchFragment extends android.support.v17.leanback.app.SearchFragment
    implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider {
    private static final String TAG = "leanback.SearchFragment";
    private static final int NUM_ROWS = 3;
    private static final int SEARCH_DELAY_MS = 1000;

    private ArrayObjectAdapter mRowsAdapter;
    private Handler mHandler = new Handler();
    private String mQuery;

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

        mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());

        setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
        setTitle("Leanback Sample App");
        setSearchResultProvider(this);
        setOnItemClickedListener(new ItemClickedListener());
    }

    @Override
    public ObjectAdapter getResultsAdapter() {
        return mRowsAdapter;
    }

    @Override
    public boolean onQueryTextChange(String newQuery) {
        Log.i(TAG, String.format("Search Query Text Change %s", newQuery));
        mRowsAdapter.clear();
        loadQuery(newQuery);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.i(TAG, String.format("Search Query Text Submit %s", query));
        mRowsAdapter.clear();
        loadQuery(query);
        return true;
    }

    private void loadQuery(String query) {
        mQuery = query;
        mHandler.removeCallbacks(mDelayedLoad);
        if (!TextUtils.isEmpty(query) && !query.equals("nil")) {
            mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS);
        }
    }

    private void loadRows() {
        for (int i = 0; i < NUM_ROWS; ++i) {
            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter());
            listRowAdapter.add("Hello world");
            listRowAdapter.add("This is a test");
            HeaderItem header = new HeaderItem(i, mQuery + " results row " + i, null);
            mRowsAdapter.add(new ListRow(header, listRowAdapter));
        }
    }

    private Runnable mDelayedLoad = new Runnable() {
        @Override
        public void run() {
            loadRows();
        }
    };
    private final class ItemClickedListener implements OnItemClickedListener {
        public void onItemClicked(Object item, Row row) {
            // TODO: use a fragment transaction instead of launching a new
            // activity
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            startActivity(intent);
        }
    }
}

@@ -112,24 +112,77 @@
 os.Handler;%0A
+import android.support.v4.app.ActivityOptionsCompat;%0A
 import andro
@@ -287,16 +287,74 @@
 erItem;%0A
+import android.support.v17.leanback.widget.ImageCardView;%0A
 import a
@@ -561,24 +561,28 @@
 idget.OnItem
+View
 ClickedListe
@@ -629,19 +629,130 @@
 .widget.
-Row
+Presenter;%0Aimport android.support.v17.leanback.widget.Row;%0Aimport android.support.v17.leanback.widget.RowPresenter
 ;%0Aimport
@@ -1620,24 +1620,28 @@
    setOnItem
+View
 ClickedListe
@@ -1648,24 +1648,28 @@
 ner(new Item
+View
 ClickedListe
@@ -2649,22 +2649,20 @@
 ter(new 
-String
+Card
 Presente
@@ -2702,21 +2702,64 @@
 add(
-%22Hello world%22
+new PhotoItem(%22Hello world%22, R.drawable.gallery_photo_1)
 );%0A 
@@ -2788,16 +2788,30 @@
 ter.add(
+new PhotoItem(
 %22This is
@@ -2818,16 +2818,45 @@
  a test%22
+, R.drawable.gallery_photo_2)
 );%0A     
@@ -3161,16 +3161,17 @@
 %0A    %7D;%0A
+%0A
     priv
@@ -3186,24 +3186,28 @@
 l class Item
+View
 ClickedListe
@@ -3223,24 +3223,28 @@
 ments OnItem
+View
 ClickedListe
@@ -3245,24 +3245,42 @@
 dListener %7B%0A
+        @Override%0A
         publ
@@ -3305,206 +3305,536 @@
 ked(
-Object item, Row row) %7B%0A            // TODO: use a fragment transaction instead of launching a new%0A            // activity%0A            Intent intent = new Intent(getActivity(), DetailsActivity.class
+Presenter.ViewHolder itemViewHolder, Object item,%0A                RowPresenter.ViewHolder rowViewHolder, Row row) %7B%0A            Intent intent = new Intent(getActivity(), DetailsActivity.class);%0A            intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);%0A%0A            Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(%0A                    getActivity(),%0A                    ((ImageCardView)itemViewHolder.view).getMainImageView(),%0A                    DetailsActivity.SHARED_ELEMENT_NAME).toBundle(
 );%0A 
@@ -3844,16 +3844,30 @@
         
+getActivity().
 startAct
@@ -3878,16 +3878,24 @@
 y(intent
+, bundle
 );%0A     

Fix crash of launch Detail from Search