1a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout/*
2a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * Copyright (C) 2014 The Android Open Source Project
3a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout *
4a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * in compliance with the License. You may obtain a copy of the License at
6a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout *
7a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * http://www.apache.org/licenses/LICENSE-2.0
8a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout *
9a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * Unless required by applicable law or agreed to in writing, software distributed under the License
10a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * or implied. See the License for the specific language governing permissions and limitations under
12a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout * the License.
13a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout */
14a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutpackage com.example.android.leanback;
15a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
16b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.content.Context;
1705a2ef073edfc6226e014d93c70e0774887de700Jerome Poichetimport android.content.Intent;
18a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutimport android.os.Bundle;
19da2c70f656f02c377b796694ca27d93e2b698733Dake Guimport android.os.Handler;
20a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutimport android.support.v17.leanback.widget.ArrayObjectAdapter;
21b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.support.v17.leanback.widget.OnItemViewClickedListener;
22b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.support.v17.leanback.widget.OnItemViewSelectedListener;
23a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutimport android.support.v17.leanback.widget.Presenter;
24b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.support.v17.leanback.widget.Row;
25ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stoutimport android.support.v17.leanback.widget.RowPresenter;
26a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutimport android.support.v17.leanback.widget.VerticalGridPresenter;
27b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.support.v4.content.res.ResourcesCompat;
28a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutimport android.util.Log;
29a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutimport android.view.View;
30a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
31a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stoutpublic class VerticalGridFragment extends android.support.v17.leanback.app.VerticalGridFragment {
32a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    private static final String TAG = "leanback.VerticalGridFragment";
33a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
34a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    private static final int NUM_COLUMNS = 3;
35a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    private static final int NUM_ITEMS = 50;
36a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    private static final int HEIGHT = 200;
37da2c70f656f02c377b796694ca27d93e2b698733Dake Gu    private static final boolean TEST_ENTRANCE_TRANSITION = true;
38a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
39ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu    private static class Adapter extends ArrayObjectAdapter {
40ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu        public Adapter(StringPresenter presenter) {
41ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu            super(presenter);
42ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu        }
43ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu        public void callNotifyChanged() {
44ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu            super.notifyChanged();
45ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu        }
46ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu    }
47ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu    private Adapter mAdapter;
48a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
49a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    @Override
50a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    public void onCreate(Bundle savedInstanceState) {
51a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        Log.i(TAG, "onCreate");
52a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        super.onCreate(savedInstanceState);
53a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
54b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu        final Context context = getActivity();
55b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu        setBadgeDrawable(ResourcesCompat.getDrawable(context.getResources(),
56b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu                R.drawable.ic_title, context.getTheme()));
572085f1d8c47e93c8bf94d137533fd126d32a48b4Craig Stout        setTitle("Leanback Vertical Grid Demo");
58a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
59a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        setupFragment();
60da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        if (TEST_ENTRANCE_TRANSITION) {
61da2c70f656f02c377b796694ca27d93e2b698733Dake Gu            // don't run entrance transition if fragment is restored.
62da2c70f656f02c377b796694ca27d93e2b698733Dake Gu            if (savedInstanceState == null) {
63da2c70f656f02c377b796694ca27d93e2b698733Dake Gu                prepareEntranceTransition();
64da2c70f656f02c377b796694ca27d93e2b698733Dake Gu            }
65da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        }
66da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        // simulates in a real world use case  data being loaded two seconds later
67da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        new Handler().postDelayed(new Runnable() {
68e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas            @Override
69da2c70f656f02c377b796694ca27d93e2b698733Dake Gu            public void run() {
70da2c70f656f02c377b796694ca27d93e2b698733Dake Gu                loadData();
71da2c70f656f02c377b796694ca27d93e2b698733Dake Gu                startEntranceTransition();
72da2c70f656f02c377b796694ca27d93e2b698733Dake Gu            }
73da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        }, 2000);
74da2c70f656f02c377b796694ca27d93e2b698733Dake Gu    }
75da2c70f656f02c377b796694ca27d93e2b698733Dake Gu
76da2c70f656f02c377b796694ca27d93e2b698733Dake Gu    private void loadData() {
77da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        for (int i = 0; i < NUM_ITEMS; i++) {
78da2c70f656f02c377b796694ca27d93e2b698733Dake Gu            mAdapter.add(Integer.toString(i));
79da2c70f656f02c377b796694ca27d93e2b698733Dake Gu        }
80a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    }
81a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
82a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    private void setupFragment() {
83a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
84a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        gridPresenter.setNumberOfColumns(NUM_COLUMNS);
85a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        setGridPresenter(gridPresenter);
86a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
87ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu        mAdapter = new Adapter(new StringPresenter());
88a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        setAdapter(mAdapter);
89a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
90ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout        setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
91a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout            @Override
92ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout            public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
93ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout                    RowPresenter.ViewHolder rowViewHolder, Row row) {
94ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout                Log.i(TAG, "onItemSelected: " + item + " row " + row);
95a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout            }
96a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        });
97a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout
98ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout        setOnItemViewClickedListener(new OnItemViewClickedListener() {
99a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout            @Override
100ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout            public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
101ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout                    RowPresenter.ViewHolder rowViewHolder, Row row) {
102ffd20ebba154edf2374a53d4bb1c682b6737ab77Craig Stout                Log.i(TAG, "onItemClicked: " + item + " row " + row);
103ccea98979aa38d1061ae38a31271040a698c9d87Dake Gu                mAdapter.callNotifyChanged();
104a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout            }
105a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout        });
10605a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet        setOnSearchClickedListener(new View.OnClickListener() {
10705a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet            @Override
10805a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet            public void onClick(View view) {
10905a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet                Intent intent = new Intent(getActivity(), SearchActivity.class);
11005a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet                startActivity(intent);
11105a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet            }
11205a2ef073edfc6226e014d93c70e0774887de700Jerome Poichet        });
113a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout    }
114a9e0eb45436fc7748192976223d44a94e04b7a6bCraig Stout}
115