1 // CHECKSTYLE:OFF Generated code
2 /* This file is auto-generated from SearchFragment.java.  DO NOT MODIFY. */
3 
4 package com.example.android.leanback;
5 
6 import static com.example.android.leanback.CardPresenter.CONTENT;
7 import static com.example.android.leanback.CardPresenter.IMAGE;
8 import static com.example.android.leanback.CardPresenter.TITLE;
9 
10 import android.content.Context;
11 import android.content.Intent;
12 import android.os.Bundle;
13 import android.os.Handler;
14 import android.text.TextUtils;
15 import android.util.Log;
16 
17 import androidx.annotation.Nullable;
18 import androidx.core.app.ActivityOptionsCompat;
19 import androidx.core.content.res.ResourcesCompat;
20 import androidx.leanback.widget.ArrayObjectAdapter;
21 import androidx.leanback.widget.DiffCallback;
22 import androidx.leanback.widget.HeaderItem;
23 import androidx.leanback.widget.ImageCardView;
24 import androidx.leanback.widget.ListRow;
25 import androidx.leanback.widget.ListRowPresenter;
26 import androidx.leanback.widget.ObjectAdapter;
27 import androidx.leanback.widget.OnItemViewClickedListener;
28 import androidx.leanback.widget.Presenter;
29 import androidx.leanback.widget.Row;
30 import androidx.leanback.widget.RowPresenter;
31 
32 import java.util.ArrayList;
33 
34 public class SearchSupportFragment extends androidx.leanback.app.SearchSupportFragment
35         implements androidx.leanback.app.SearchSupportFragment.SearchResultProvider {
36     private static final String TAG = "leanback.SearchSupportFragment";
37     private static final int NUM_ROWS = 3;
38     private static final int SEARCH_DELAY_MS = 1000;
39 
40     private ArrayObjectAdapter mRowsAdapter;
41     private Handler mHandler = new Handler();
42     private String mQuery;
43 
44     // Flag to represent if data set one is presented in the fragment
45     private boolean mIsDataSetOnePresented;
46 
47     // Adapter for first row
48     private ArrayObjectAdapter mFirstRowAdapter;
49 
50     // The diff callback which defines the standard to judge if two items are the same or if
51     // two items have the same content.
52     private DiffCallback<PhotoItem> mDiffCallback = new DiffCallback<PhotoItem>() {
53 
54         // when two photo items have the same id, they are the same from adapter's
55         // perspective
56         @Override
57         public boolean areItemsTheSame(PhotoItem oldItem, PhotoItem newItem) {
58             return oldItem.getId() == newItem.getId();
59         }
60 
61         // when two photo items is equal to each other (based on the equal method defined in
62         // PhotoItem), they have the same content.
63         @Override
64         public boolean areContentsTheSame(PhotoItem oldItem, PhotoItem newItem) {
65             return oldItem.equals(newItem);
66         }
67 
68         @Nullable
69         @Override
70         public Object getChangePayload(PhotoItem oldItem, PhotoItem newItem) {
71             Bundle diff = new Bundle();
72             if (oldItem.getImageResourceId()
73                     != newItem.getImageResourceId()) {
74                 diff.putLong(IMAGE, newItem.getImageResourceId());
75             }
76 
77             if (oldItem.getTitle() != null && newItem.getTitle() != null
78                     && !oldItem.getTitle().equals(newItem.getTitle())) {
79                 diff.putString(TITLE, newItem.getTitle());
80             }
81 
82             if (oldItem.getContent() != null && newItem.getContent() != null
83                     && !oldItem.getContent().equals(newItem.getContent())) {
84                 diff.putString(CONTENT, newItem.getContent());
85             }
86             return diff;
87         }
88     };
89 
90     @Override
onCreate(Bundle savedInstanceState)91     public void onCreate(Bundle savedInstanceState) {
92         super.onCreate(savedInstanceState);
93 
94         mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
95 
96         final Context context = getActivity();
97         setBadgeDrawable(ResourcesCompat.getDrawable(context.getResources(),
98                 R.drawable.ic_title, context.getTheme()));
99         setTitle("Leanback Sample App");
100         setSearchResultProvider(this);
101         setOnItemViewClickedListener(new ItemViewClickedListener());
102     }
103 
104     @Override
getResultsAdapter()105     public ObjectAdapter getResultsAdapter() {
106         return mRowsAdapter;
107     }
108 
109     @Override
onQueryTextChange(String newQuery)110     public boolean onQueryTextChange(String newQuery) {
111         Log.i(TAG, String.format("Search Query Text Change %s", newQuery));
112         mRowsAdapter.clear();
113         loadQuery(newQuery);
114         return true;
115     }
116 
117     @Override
onQueryTextSubmit(String query)118     public boolean onQueryTextSubmit(String query) {
119         Log.i(TAG, String.format("Search Query Text Submit %s", query));
120         mRowsAdapter.clear();
121         loadQuery(query);
122         return true;
123     }
124 
loadQuery(String query)125     private void loadQuery(String query) {
126         mQuery = query;
127         mHandler.removeCallbacks(mDelayedLoad);
128         if (!TextUtils.isEmpty(query) && !query.equals("nil")) {
129             mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS);
130         }
131     }
132 
loadRows()133     private void loadRows() {
134         HeaderItem header = new HeaderItem(0, mQuery + " results row " + 0);
135 
136         // Every time when the query event is fired, we will update the fake search result in the
137         // first row based on the flag mIsDataSetOnePresented flag.
138         // Also the first row adapter will only be created once so the animation will be triggered
139         // when the items in the adapter changed.
140         if (!mIsDataSetOnePresented) {
141             if (mFirstRowAdapter == null) {
142                 mFirstRowAdapter = createFirstListRowAdapter();
143             } else {
144                 mFirstRowAdapter.setItems(createDataSetOneDebug(), mDiffCallback);
145             }
146             mIsDataSetOnePresented = true;
147         } else {
148             mFirstRowAdapter.setItems(createDataSetTwoDebug(), mDiffCallback);
149             mIsDataSetOnePresented = false;
150         }
151         mRowsAdapter.add(new ListRow(header, mFirstRowAdapter));
152         for (int i = 1; i < NUM_ROWS + 1; ++i) {
153             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
154             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
155             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
156             header = new HeaderItem(i, mQuery + " results row " + i);
157             mRowsAdapter.add(new ListRow(header, listRowAdapter));
158         }
159     }
160 
161     private Runnable mDelayedLoad = new Runnable() {
162         @Override
163         public void run() {
164             loadRows();
165         }
166     };
167 
168     private final class ItemViewClickedListener implements OnItemViewClickedListener {
169         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)170         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
171                 RowPresenter.ViewHolder rowViewHolder, Row row) {
172             Intent intent = new Intent(getActivity(), DetailsSupportActivity.class);
173             intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item);
174 
175             Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
176                     getActivity(),
177                     ((ImageCardView) itemViewHolder.view).getMainImageView(),
178                     DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle();
179             getActivity().startActivity(intent, bundle);
180         }
181     }
182 
183 
createFirstListRowAdapter()184     private ArrayObjectAdapter createFirstListRowAdapter() {
185         ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
186         listRowAdapter.setItems(createDataSetOneDebug(), mDiffCallback);
187         mIsDataSetOnePresented = true;
188         return listRowAdapter;
189     }
190 
191     /**
192      * Create a data set (data set one) for the last row of this browse fragment. It will be
193      * changed by another set of data when user click one of the photo items in the list.
194      * Different with other rows in the browsing fragment, the photo item in last row all have been
195      * allocated with a unique id. And the id will be used to jduge if two photo items are the same
196      * or not.
197      *
198      * @return List of photoItem
199      */
createDataSetOne()200     private ArrayList<PhotoItem> createDataSetOne() {
201         ArrayList<PhotoItem> photoItems = new ArrayList<>();
202         photoItems.add(new PhotoItem(
203                 "Hello world",
204                 R.drawable.gallery_photo_1,
205                 1));
206         photoItems.add(new PhotoItem(
207                 "This is a test",
208                 "Only a test",
209                 R.drawable.gallery_photo_2,
210                 2));
211         photoItems.add(new PhotoItem(
212                 "Android TV",
213                 "by Google",
214                 R.drawable.gallery_photo_3,
215                 3));
216         photoItems.add(new PhotoItem(
217                 "Leanback",
218                 R.drawable.gallery_photo_4,
219                 4));
220         photoItems.add(new PhotoItem(
221                 "GuidedStep (Slide left/right)",
222                 R.drawable.gallery_photo_5,
223                 5));
224         photoItems.add(new PhotoItem(
225                 "GuidedStep (Slide bottom up)",
226                 "Open GuidedStepFragment",
227                 R.drawable.gallery_photo_6,
228                 6));
229         photoItems.add(new PhotoItem(
230                 "Android TV",
231                 "open RowsActivity",
232                 R.drawable.gallery_photo_7,
233                 7));
234         photoItems.add(new PhotoItem(
235                 "Leanback",
236                 "open BrowseActivity",
237                 R.drawable.gallery_photo_8,
238                 8));
239         photoItems.add(new PhotoItem(
240                 "Hello world",
241                 R.drawable.gallery_photo_1,
242                 1));
243         photoItems.add(new PhotoItem(
244                 "This is a test",
245                 "Only a test",
246                 R.drawable.gallery_photo_2,
247                 2));
248         photoItems.add(new PhotoItem(
249                 "Android TV",
250                 "by Google",
251                 R.drawable.gallery_photo_3,
252                 3));
253         photoItems.add(new PhotoItem(
254                 "Leanback",
255                 R.drawable.gallery_photo_4,
256                 4));
257         return photoItems;
258     }
259 
260     /**
261      * Create a new data set (data set one) for the last row of this browse fragment. It will be
262      * changed by another set of data when user click one of the photo items in the list.
263      * Different with other rows in the browsing fragment, the photo item in last row all have been
264      * allocated with a unique id. And the id will be used to jduge if two photo items are the same
265      * or not.
266      *
267      * @return List of photoItem
268      */
createDataSetTwo()269     private ArrayList<PhotoItem> createDataSetTwo() {
270         ArrayList<PhotoItem> photoItems = new ArrayList<>();
271         photoItems.add(new PhotoItem(
272                 "This is a test",
273                 "Only a test",
274                 R.drawable.gallery_photo_2,
275                 2));
276         photoItems.add(new PhotoItem(
277                 "Hello world",
278                 R.drawable.gallery_photo_1,
279                 1));
280         photoItems.add(new PhotoItem(
281                 "Leanback",
282                 R.drawable.gallery_photo_4,
283                 4));
284         photoItems.add(new PhotoItem(
285                 "Android TV",
286                 "by Google",
287                 R.drawable.gallery_photo_3,
288                 3));
289         photoItems.add(new PhotoItem(
290                 "change title",
291                 R.drawable.gallery_photo_5,
292                 5));
293         photoItems.add(new PhotoItem(
294                 "GuidedStep (Slide bottom up)",
295                 "change comment",
296                 R.drawable.gallery_photo_6,
297                 6));
298         photoItems.add(new PhotoItem(
299                 "Android TV",
300                 R.drawable.gallery_photo_7,
301                 7));
302         photoItems.add(new PhotoItem(
303                 "Leanback",
304                 "open BrowseActivity",
305                 R.drawable.gallery_photo_7,
306                 8));
307         photoItems.add(new PhotoItem(
308                 "Hello world",
309                 R.drawable.gallery_photo_1,
310                 10));
311         photoItems.add(new PhotoItem(
312                 "This is a test",
313                 "Only a test",
314                 R.drawable.gallery_photo_2,
315                 20));
316         photoItems.add(new PhotoItem(
317                 "Android TV",
318                 "by Google",
319                 R.drawable.gallery_photo_3,
320                 30));
321         photoItems.add(new PhotoItem(
322                 "Leanback",
323                 R.drawable.gallery_photo_4,
324                 40));
325         return photoItems;
326     }
327 
328 
createDataSetOneDebug()329     private ArrayList<PhotoItem> createDataSetOneDebug() {
330         ArrayList<PhotoItem> photoItems = new ArrayList<>();
331         photoItems.add(new PhotoItem(
332                 "Hello world",
333                 R.drawable.gallery_photo_1,
334                 1));
335         return photoItems;
336     }
337 
338     /**
339      * Create a new data set (data set one) for the last row of this browse fragment. It will be
340      * changed by another set of data when user click one of the photo items in the list.
341      * Different with other rows in the browsing fragment, the photo item in last row all have been
342      * allocated with a unique id. And the id will be used to jduge if two photo items are the same
343      * or not.
344      *
345      * @return List of photoItem
346      */
createDataSetTwoDebug()347     private ArrayList<PhotoItem> createDataSetTwoDebug() {
348         ArrayList<PhotoItem> photoItems = new ArrayList<>();
349         photoItems.add(new PhotoItem(
350                 "Hello world Hello world",
351                 R.drawable.gallery_photo_1,
352                 1));
353         return photoItems;
354     }
355 }
356