1 package com.example.android.leanback;
2 
3 import android.content.Intent;
4 import android.os.Bundle;
5 import android.os.Handler;
6 import android.support.v4.app.ActivityOptionsCompat;
7 import android.support.v17.leanback.widget.ArrayObjectAdapter;
8 import android.support.v17.leanback.widget.HeaderItem;
9 import android.support.v17.leanback.widget.ImageCardView;
10 import android.support.v17.leanback.widget.ListRow;
11 import android.support.v17.leanback.widget.ListRowPresenter;
12 import android.support.v17.leanback.widget.ObjectAdapter;
13 import android.support.v17.leanback.widget.OnItemViewClickedListener;
14 import android.support.v17.leanback.widget.Presenter;
15 import android.support.v17.leanback.widget.Row;
16 import android.support.v17.leanback.widget.RowPresenter;
17 import android.text.TextUtils;
18 import android.util.Log;
19 
20 public class SearchFragment extends android.support.v17.leanback.app.SearchFragment
21     implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider {
22     private static final String TAG = "leanback.SearchFragment";
23     private static final int NUM_ROWS = 3;
24     private static final int SEARCH_DELAY_MS = 1000;
25 
26     private ArrayObjectAdapter mRowsAdapter;
27     private Handler mHandler = new Handler();
28     private String mQuery;
29 
30     @Override
onCreate(Bundle savedInstanceState)31     public void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33 
34         mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
35 
36         setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
37         setTitle("Leanback Sample App");
38         setSearchResultProvider(this);
39         setOnItemViewClickedListener(new ItemViewClickedListener());
40     }
41 
42     @Override
getResultsAdapter()43     public ObjectAdapter getResultsAdapter() {
44         return mRowsAdapter;
45     }
46 
47     @Override
onQueryTextChange(String newQuery)48     public boolean onQueryTextChange(String newQuery) {
49         Log.i(TAG, String.format("Search Query Text Change %s", newQuery));
50         mRowsAdapter.clear();
51         loadQuery(newQuery);
52         return true;
53     }
54 
55     @Override
onQueryTextSubmit(String query)56     public boolean onQueryTextSubmit(String query) {
57         Log.i(TAG, String.format("Search Query Text Submit %s", query));
58         mRowsAdapter.clear();
59         loadQuery(query);
60         return true;
61     }
62 
loadQuery(String query)63     private void loadQuery(String query) {
64         mQuery = query;
65         mHandler.removeCallbacks(mDelayedLoad);
66         if (!TextUtils.isEmpty(query) && !query.equals("nil")) {
67             mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS);
68         }
69     }
70 
loadRows()71     private void loadRows() {
72         for (int i = 0; i < NUM_ROWS; ++i) {
73             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
74             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
75             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
76             HeaderItem header = new HeaderItem(i, mQuery + " results row " + i);
77             mRowsAdapter.add(new ListRow(header, listRowAdapter));
78         }
79     }
80 
81     private Runnable mDelayedLoad = new Runnable() {
82         @Override
83         public void run() {
84             loadRows();
85         }
86     };
87 
88     private final class ItemViewClickedListener implements OnItemViewClickedListener {
89         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)90         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
91                 RowPresenter.ViewHolder rowViewHolder, Row row) {
92             Intent intent = new Intent(getActivity(), DetailsActivity.class);
93             intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
94 
95             Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
96                     getActivity(),
97                     ((ImageCardView)itemViewHolder.view).getMainImageView(),
98                     DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
99             getActivity().startActivity(intent, bundle);
100         }
101     }
102 }
103